2016-06-01 16:31:47 -07:00
|
|
|
/* jslint node:true */
|
|
|
|
|
/* global it:false */
|
|
|
|
|
/* global describe:false */
|
|
|
|
|
/* global before:false */
|
|
|
|
|
/* global after:false */
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async'),
|
|
|
|
|
config = require('../../config.js'),
|
|
|
|
|
database = require('../../database.js'),
|
|
|
|
|
expect = require('expect.js'),
|
|
|
|
|
net = require('net'),
|
|
|
|
|
nock = require('nock'),
|
|
|
|
|
superagent = require('superagent'),
|
|
|
|
|
server = require('../../server.js');
|
|
|
|
|
|
|
|
|
|
var SERVER_URL = 'http://localhost:' + config.get('port');
|
|
|
|
|
|
|
|
|
|
var USERNAME = 'superadmin', PASSWORD = 'Foobar?1337', EMAIL ='silly@me.com';
|
|
|
|
|
var token = null;
|
|
|
|
|
|
|
|
|
|
var server;
|
|
|
|
|
function setup(done) {
|
2017-11-27 15:30:55 -08:00
|
|
|
config._reset();
|
|
|
|
|
config.setFqdn('example-server-test.com');
|
2017-11-21 01:56:24 +01:00
|
|
|
config.set('provider', 'caas');
|
2016-06-01 16:31:47 -07:00
|
|
|
config.setVersion('1.2.3');
|
|
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
|
server.start.bind(server),
|
|
|
|
|
|
|
|
|
|
database._clear,
|
|
|
|
|
|
|
|
|
|
function createAdmin(callback) {
|
|
|
|
|
var scope1 = nock(config.apiServerOrigin()).get('/api/v1/boxes/' + config.fqdn() + '/setup/verify?setupToken=somesetuptoken').reply(200, {});
|
|
|
|
|
var scope2 = nock(config.apiServerOrigin()).post('/api/v1/boxes/' + config.fqdn() + '/setup/done?setupToken=somesetuptoken').reply(201, {});
|
|
|
|
|
|
|
|
|
|
superagent.post(SERVER_URL + '/api/v1/cloudron/activate')
|
|
|
|
|
.query({ setupToken: 'somesetuptoken' })
|
|
|
|
|
.send({ username: USERNAME, password: PASSWORD, email: EMAIL })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result).to.be.ok();
|
|
|
|
|
expect(result.statusCode).to.eql(201);
|
|
|
|
|
expect(scope1.isDone()).to.be.ok();
|
|
|
|
|
expect(scope2.isDone()).to.be.ok();
|
|
|
|
|
|
|
|
|
|
// stash token for further use
|
|
|
|
|
token = result.body.token;
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
], done);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cleanup(done) {
|
|
|
|
|
database._clear(function (error) {
|
|
|
|
|
expect(!error).to.be.ok();
|
|
|
|
|
|
|
|
|
|
server.stop(done);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('REST API', function () {
|
|
|
|
|
before(setup);
|
|
|
|
|
after(cleanup);
|
|
|
|
|
|
|
|
|
|
it('does not crash with invalid JSON', function (done) {
|
|
|
|
|
superagent.post(SERVER_URL + '/api/v1/users')
|
|
|
|
|
.query({ access_token: token })
|
|
|
|
|
.set('content-type', 'application/json')
|
2017-11-21 01:56:24 +01:00
|
|
|
.send('some invalid non-strict json')
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(400);
|
2017-11-27 13:59:56 -08:00
|
|
|
expect(result.body.message).to.be('Failed to parse body');
|
2017-11-21 01:56:24 +01:00
|
|
|
|
|
|
|
|
done();
|
2016-06-01 16:31:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('does not crash with invalid string', function (done) {
|
|
|
|
|
superagent.post(SERVER_URL + '/api/v1/users')
|
|
|
|
|
.query({ access_token: token })
|
|
|
|
|
.set('content-type', 'application/x-www-form-urlencoded')
|
2017-11-21 01:56:24 +01:00
|
|
|
.send('some string')
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(400);
|
|
|
|
|
|
|
|
|
|
done();
|
2016-06-01 16:31:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|