thelounge/test/server.js

122 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-12-23 12:50:11 +01:00
"use strict";
2018-06-15 22:31:06 +02:00
const log = require("../src/log");
2016-12-23 12:50:11 +01:00
const Helper = require("../src/helper");
const expect = require("chai").expect;
2019-04-15 18:19:50 +02:00
const got = require("got");
2016-12-23 12:50:11 +01:00
const io = require("socket.io-client");
2017-11-28 08:34:04 +01:00
describe("Server", function() {
2018-02-28 21:42:21 +01:00
// Travis is having issues with slow workers and thus tests timeout
this.timeout(process.env.CI ? 25000 : 5000);
2017-10-06 11:53:08 +02:00
let server;
let originalLogInfo;
before(function() {
originalLogInfo = log.info;
log.info = () => {};
2017-10-06 11:53:08 +02:00
server = require("../src/server")();
});
after(function(done) {
2017-10-06 11:53:08 +02:00
server.close(done);
log.info = originalLogInfo;
2017-10-06 11:53:08 +02:00
});
2016-12-23 12:50:11 +01:00
const webURL = `http://${Helper.config.host}:${Helper.config.port}/`;
describe("Express", () => {
2019-04-15 18:19:50 +02: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 12:50:11 +01:00
});
2019-04-15 18:19:50 +02:00
it("should serve static content correctly", async () => {
const response = await got(webURL + "thelounge.webmanifest");
const body = JSON.parse(response.body);
2016-12-23 12:50:11 +01:00
2019-04-15 18:19:50 +02: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 12:50:11 +01:00
});
});
describe("WebSockets", function() {
this.slow(300);
2016-12-23 12:50:11 +01:00
let client;
beforeEach(() => {
client = io(webURL, {
path: "/socket.io/",
autoConnect: false,
reconnection: false,
timeout: 1000,
2019-07-17 11:33:59 +02:00
transports: ["websocket"],
2016-12-23 12:50:11 +01:00
});
// Server emits events faster than the test can bind them
setTimeout(() => {
client.open();
}, 1);
});
afterEach(() => {
client.close();
});
it("should emit authorized message", (done) => {
2016-12-23 12:50:11 +01:00
client.on("authorized", done);
});
it("should create network", (done) => {
2016-12-23 12:50:11 +01:00
client.on("init", () => {
client.emit("network:new", {
2016-12-23 12:50:11 +01:00
username: "test-user",
realname: "The Lounge Test",
nick: "test-user",
join: "#thelounge, #spam",
name: "Test Network",
host: Helper.config.host,
port: 6667,
});
});
client.on("network", (data) => {
2016-12-23 12:50:11 +01:00
expect(data.networks).to.be.an("array");
expect(data.networks).to.have.lengthOf(1);
2019-11-02 19:45:00 +01:00
expect(data.networks[0].nick).to.equal("test-user");
expect(data.networks[0].name).to.equal("Test Network");
2016-12-23 12:50:11 +01: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 19:45:00 +01:00
expect(data.networks[0].channels[2].name).to.equal("#spam");
2016-12-23 12:50:11 +01:00
done();
});
});
it("should emit init message", (done) => {
client.on("init", (data) => {
2016-12-23 12:50:11 +01: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;
expect(data.pushSubscription).to.be.undefined;
// Private key defined in vapid.json is "01020304050607080910111213141516" for this public key.
2019-07-17 11:33:59 +02:00
expect(data.applicationServerKey).to.equal(
"BM0eTDpvDnH7ewlHuXWcPTE1NjlJ06XWIS1cQeBTZmsg4EDx5sOpY7kdX1pniTo8RakL3UdfFuIbC8_zog_BWIM"
);
2016-12-23 12:50:11 +01:00
done();
});
});
});
});