Files
cloudron-box/src/routes/test/developer-test.js

222 lines
8.3 KiB
JavaScript
Raw Normal View History

'use strict';
/* jslint node:true */
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
var async = require('async'),
config = require('../../config.js'),
database = require('../../database.js'),
expect = require('expect.js'),
superagent = require('superagent'),
2018-01-18 13:41:10 -08:00
server = require('../../server.js');
var SERVER_URL = 'http://localhost:' + config.get('port');
2016-04-13 16:50:20 -07:00
var USERNAME = 'superadmin', PASSWORD = 'Foobar?1337', EMAIL ='silly@me.com';
function setup(done) {
2017-11-27 15:30:55 -08:00
config._reset();
config.setFqdn('example-developer-test.com');
async.series([
server.start.bind(server),
database._clear
], done);
}
function cleanup(done) {
database._clear(function (error) {
expect(error).to.not.be.ok();
server.stop(done);
});
}
describe('Developer API', function () {
describe('login', function () {
before(function (done) {
async.series([
setup,
function (callback) {
superagent.post(SERVER_URL + '/api/v1/cloudron/activate')
2018-01-18 13:41:10 -08:00
.query({ setupToken: 'somesetuptoken' })
.send({ username: USERNAME, password: PASSWORD, email: EMAIL })
.end(function (error, result) {
expect(result).to.be.ok();
callback();
});
},
], done);
});
after(cleanup);
it('fails without body', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('fails without username', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ password: PASSWORD })
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('fails without password', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ username: USERNAME })
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('fails with empty username', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ username: '', password: PASSWORD })
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('fails with empty password', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ username: USERNAME, password: '' })
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('fails with unknown username', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ username: USERNAME + USERNAME, password: PASSWORD })
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
2016-04-13 12:39:50 +02:00
});
it('fails with unknown email', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ username: USERNAME + EMAIL, password: PASSWORD })
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('fails with wrong password', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ username: USERNAME, password: PASSWORD.toUpperCase() })
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
});
});
it('with username succeeds', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ username: USERNAME, password: PASSWORD })
.end(function (error, result) {
expect(result.statusCode).to.equal(200);
expect(new Date(result.body.expiresAt).toString()).to.not.be('Invalid Date');
expect(result.body.token).to.be.a('string');
done();
});
});
2016-04-13 12:39:50 +02:00
it('with uppercase username succeeds', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ username: USERNAME.toUpperCase(), password: PASSWORD })
.end(function (error, result) {
expect(result.statusCode).to.equal(200);
expect(new Date(result.body.expiresAt).toString()).to.not.be('Invalid Date');
expect(result.body.token).to.be.a('string');
done();
});
2016-04-13 12:39:50 +02:00
});
it('with email succeeds', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ username: EMAIL, password: PASSWORD })
.end(function (error, result) {
expect(result.statusCode).to.equal(200);
expect(new Date(result.body.expiresAt).toString()).to.not.be('Invalid Date');
expect(result.body.token).to.be.a('string');
done();
});
});
2016-04-13 12:39:50 +02:00
it('with uppercase email succeeds', function (done) {
superagent.post(SERVER_URL + '/api/v1/developer/login')
2018-01-18 13:41:10 -08:00
.send({ username: EMAIL.toUpperCase(), password: PASSWORD })
.end(function (error, result) {
expect(result.statusCode).to.equal(200);
expect(new Date(result.body.expiresAt).toString()).to.not.be('Invalid Date');
expect(result.body.token).to.be.a('string');
done();
});
2016-04-13 12:39:50 +02:00
});
});
2016-06-03 11:11:11 +02:00
describe('sdk tokens are valid without password checks', function () {
var token_normal, token_sdk;
before(function (done) {
async.series([
setup,
function (callback) {
superagent.post(SERVER_URL + '/api/v1/cloudron/activate')
2018-01-18 13:41:10 -08:00
.query({ setupToken: 'somesetuptoken' })
.send({ username: USERNAME, password: PASSWORD, email: EMAIL })
.end(function (error, result) {
expect(result).to.be.ok();
2016-06-03 11:11:11 +02:00
2018-01-18 13:41:10 -08:00
token_normal = result.body.token;
2016-06-03 11:11:11 +02:00
2018-01-18 13:41:10 -08:00
superagent.post(SERVER_URL + '/api/v1/developer/login')
.send({ username: USERNAME, password: PASSWORD })
.end(function (error, result) {
expect(result.statusCode).to.equal(200);
expect(new Date(result.body.expiresAt).toString()).to.not.be('Invalid Date');
expect(result.body.token).to.be.a('string');
2016-06-03 11:11:11 +02:00
2018-01-18 13:41:10 -08:00
token_sdk = result.body.token;
2016-06-03 11:11:11 +02:00
2018-01-18 13:41:10 -08:00
callback();
});
2016-06-03 11:11:11 +02:00
});
},
], done);
});
after(cleanup);
it('fails with non sdk token', function (done) {
2018-04-26 19:57:44 -07:00
superagent.post(SERVER_URL + '/api/v1/user/profile/password').query({ access_token: token_normal }).send({ newPassword: 'Some?$123' }).end(function (error, result) {
2016-06-03 11:11:11 +02:00
expect(result.statusCode).to.equal(400);
done();
});
});
it('succeeds', function (done) {
2018-04-26 19:57:44 -07:00
superagent.post(SERVER_URL + '/api/v1/user/profile/password').query({ access_token: token_sdk }).send({ newPassword: 'Some?$123' }).end(function (error, result) {
2016-06-03 11:11:11 +02:00
expect(result.statusCode).to.equal(204);
done();
});
});
});
});