/* jslint node:true */ /* global it:false */ /* global describe:false */ /* global before:false */ /* global after:false */ 'use strict'; const constants = require('../constants.js'), database = require('../database.js'), expect = require('expect.js'), safe = require('safetydance'), server = require('../server.js'), superagent = require('superagent'), util = require('util'); const SERVER_URL = 'http://localhost:' + constants.PORT; describe('Server', function () { describe('startup', function () { it('succeeds', function (done) { server.start(function (error) { expect(error).to.not.be.ok(); done(); }); }); it('is reachable', async function () { const response = await superagent.get(SERVER_URL + '/api/v1/cloudron/status'); expect(response.status).to.equal(200); }); it('should fail because already running', function (done) { expect(server.start).to.throwException(function () { done(); }); }); after(function (done) { server.stop(function () { done(); }); }); }); describe('runtime', function () { before(function (done) { server.start(done); }); after(async function () { await database._clear(); await util.promisify(server.stop)(); }); it('random bad superagents', async function () { const response = await superagent.get(SERVER_URL + '/random').ok(() => true); expect(response.status).to.equal(404); }); it('version', async function () { const response = await superagent.get(SERVER_URL + '/api/v1/cloudron/status'); expect(response.status).to.equal(200); expect(response.body.version).to.contain('-test'); }); it('status route is GET', async function () { const response = await superagent.post(SERVER_URL + '/api/v1/cloudron/status').ok(() => true); expect(response.status).to.equal(404); const response2 = await superagent.get(SERVER_URL + '/api/v1/cloudron/status'); expect(response2.statusCode).to.equal(200); }); }); describe('config', function () { before(function (done) { server.start(done); }); after(function (done) { server.stop(function () { done(); }); }); it('config fails due missing token', async function () { const response = await superagent.get(SERVER_URL + '/api/v1/config').ok(() => true); expect(response.statusCode).to.equal(401); }); it('config fails due wrong token', async function () { const response = await superagent.get(SERVER_URL + '/api/v1/config').query({ access_token: 'somewrongtoken' }).ok(() => true); expect(response.status).to.equal(401); }); }); describe('shutdown', function () { before(function (done) { server.start(done); }); it('succeeds', function (done) { server.stop(function () { done(); }); }); it('is not reachable anymore', async function () { const [error] = await safe(superagent.get(SERVER_URL + '/api/v1/cloudron/status').ok(() => true)); expect(error).to.not.be(null); }); }); describe('cors', function () { before(function (done) { server.start(function (error) { done(error); }); }); it('responds to OPTIONS', async function () { const response = await superagent('OPTIONS', SERVER_URL + '/api/v1/cloudron/status') .set('Access-Control-Request-Method', 'GET') .set('Access-Control-Request-Headers', 'accept, origin, x-superagented-with') .set('Origin', 'http://localhost'); expect(response.headers['access-control-allow-methods']).to.be('GET, PUT, DELETE, POST, OPTIONS'); expect(response.headers['access-control-allow-credentials']).to.be('false'); expect(response.headers['access-control-allow-headers']).to.be('accept, origin, x-superagented-with'); // mirrored from superagent expect(response.headers['access-control-allow-origin']).to.be('http://localhost'); // mirrors from superagent }); it('does not crash for malformed origin', async function () { const response = await superagent('OPTIONS', SERVER_URL + '/api/v1/cloudron/status') .set('Origin', 'foobar') .ok(() => true); expect(response.status).to.be(405); }); after(function (done) { server.stop(function () { done(); }); }); }); });