2016-12-23 11:50:11 +00:00
|
|
|
"use strict";
|
|
|
|
|
2018-06-15 20:31:06 +00:00
|
|
|
const log = require("../src/log");
|
2022-05-01 19:12:39 +00:00
|
|
|
const Config = require("../src/config");
|
2016-12-23 11:50:11 +00:00
|
|
|
const expect = require("chai").expect;
|
2020-01-18 23:14:52 +00:00
|
|
|
const stub = require("sinon").stub;
|
2019-04-15 16:19:50 +00:00
|
|
|
const got = require("got");
|
2016-12-23 11:50:11 +00:00
|
|
|
const io = require("socket.io-client");
|
2020-02-09 12:21:45 +00:00
|
|
|
const util = require("./util");
|
2020-01-18 23:14:52 +00:00
|
|
|
const changelog = require("../src/plugins/changelog");
|
2016-12-23 11:50:11 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("Server", function () {
|
2019-11-27 18:25:29 +00:00
|
|
|
// Increase timeout due to unpredictable I/O on CI services
|
2020-02-09 12:21:45 +00:00
|
|
|
this.timeout(util.isRunningOnCI() ? 25000 : 5000);
|
2018-02-28 20:42:21 +00:00
|
|
|
|
2017-10-06 09:53:08 +00:00
|
|
|
let server;
|
2017-12-09 23:56:05 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
before(function () {
|
2020-01-18 23:14:52 +00:00
|
|
|
stub(log, "info");
|
|
|
|
stub(changelog, "checkForUpdates");
|
2017-10-06 09:53:08 +00:00
|
|
|
|
|
|
|
server = require("../src/server")();
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
after(function (done) {
|
2017-10-06 09:53:08 +00:00
|
|
|
server.close(done);
|
2020-01-18 23:14:52 +00:00
|
|
|
log.info.restore();
|
|
|
|
changelog.checkForUpdates.restore();
|
2017-10-06 09:53:08 +00:00
|
|
|
});
|
2016-12-23 11:50:11 +00:00
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
const webURL = `http://${Config.values.host}:${Config.values.port}/`;
|
2016-12-23 11:50:11 +00:00
|
|
|
|
|
|
|
describe("Express", () => {
|
2019-04-15 16:19:50 +00:00
|
|
|
it("should run a web server on " + webURL, async () => {
|
|
|
|
const response = await got(webURL);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
expect(response.body).to.include("<title>The Lounge</title>");
|
|
|
|
expect(response.body).to.include("js/bundle.js");
|
2016-12-23 11:50:11 +00:00
|
|
|
});
|
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
it("should serve static content correctly", async () => {
|
|
|
|
const response = await got(webURL + "thelounge.webmanifest");
|
|
|
|
const body = JSON.parse(response.body);
|
2016-12-23 11:50:11 +00:00
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
expect(body.name).to.equal("The Lounge");
|
|
|
|
expect(response.headers["content-type"]).to.equal("application/manifest+json");
|
2016-12-23 11:50:11 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("WebSockets", function () {
|
2017-11-27 23:47:19 +00:00
|
|
|
this.slow(300);
|
|
|
|
|
2016-12-23 11:50:11 +00:00
|
|
|
let client;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = io(webURL, {
|
|
|
|
path: "/socket.io/",
|
|
|
|
autoConnect: false,
|
|
|
|
reconnection: false,
|
|
|
|
timeout: 1000,
|
2019-07-17 09:33:59 +00:00
|
|
|
transports: ["websocket"],
|
2016-12-23 11:50:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Server emits events faster than the test can bind them
|
|
|
|
setTimeout(() => {
|
|
|
|
client.open();
|
|
|
|
}, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
client.close();
|
|
|
|
});
|
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
it("should emit authorized message", (done) => {
|
2019-11-05 19:29:51 +00:00
|
|
|
client.on("auth:success", done);
|
2016-12-23 11:50:11 +00:00
|
|
|
});
|
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
it("should create network", (done) => {
|
2016-12-23 11:50:11 +00:00
|
|
|
client.on("init", () => {
|
2018-03-15 08:37:32 +00:00
|
|
|
client.emit("network:new", {
|
2016-12-23 11:50:11 +00:00
|
|
|
username: "test-user",
|
|
|
|
realname: "The Lounge Test",
|
|
|
|
nick: "test-user",
|
|
|
|
join: "#thelounge, #spam",
|
|
|
|
name: "Test Network",
|
2022-05-01 19:12:39 +00:00
|
|
|
host: Config.values.host,
|
2016-12-23 11:50:11 +00:00
|
|
|
port: 6667,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
client.on("network", (data) => {
|
2016-12-23 11:50:11 +00:00
|
|
|
expect(data.networks).to.be.an("array");
|
|
|
|
expect(data.networks).to.have.lengthOf(1);
|
2019-11-02 18:45:00 +00:00
|
|
|
expect(data.networks[0].nick).to.equal("test-user");
|
|
|
|
expect(data.networks[0].name).to.equal("Test Network");
|
2016-12-23 11:50:11 +00:00
|
|
|
expect(data.networks[0].channels).to.have.lengthOf(3);
|
|
|
|
expect(data.networks[0].channels[0].name).to.equal("Test Network");
|
|
|
|
expect(data.networks[0].channels[1].name).to.equal("#thelounge");
|
2019-11-02 18:45:00 +00:00
|
|
|
expect(data.networks[0].channels[2].name).to.equal("#spam");
|
2016-12-23 11:50:11 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-12 20:03:59 +00:00
|
|
|
it("should emit configuration message", (done) => {
|
|
|
|
client.on("configuration", (data) => {
|
|
|
|
// Private key defined in vapid.json is "01020304050607080910111213141516" for this public key.
|
|
|
|
expect(data.applicationServerKey).to.equal(
|
|
|
|
"BM0eTDpvDnH7ewlHuXWcPTE1NjlJ06XWIS1cQeBTZmsg4EDx5sOpY7kdX1pniTo8RakL3UdfFuIbC8_zog_BWIM"
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(data.public).to.equal(true);
|
|
|
|
expect(data.defaultTheme).to.equal("default");
|
|
|
|
expect(data.themes).to.be.an("array");
|
|
|
|
expect(data.lockNetwork).to.equal(false);
|
|
|
|
expect(data.useHexIp).to.equal(false);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit push subscription state message", (done) => {
|
|
|
|
client.on("push:issubscribed", (data) => {
|
|
|
|
expect(data).to.be.false;
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
it("should emit init message", (done) => {
|
|
|
|
client.on("init", (data) => {
|
2016-12-23 11:50:11 +00:00
|
|
|
expect(data.active).to.equal(-1);
|
|
|
|
expect(data.networks).to.be.an("array");
|
|
|
|
expect(data.networks).to.be.empty;
|
|
|
|
expect(data.token).to.be.null;
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|