2016-05-02 14:54:25 -07:00
|
|
|
/* jslint node:true */
|
|
|
|
|
/* global it:false */
|
|
|
|
|
/* global describe:false */
|
|
|
|
|
/* global before:false */
|
|
|
|
|
/* global after:false */
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2020-02-11 17:32:58 -08:00
|
|
|
var async = require('async'),
|
2019-07-25 15:43:51 -07:00
|
|
|
constants = require('../../constants.js'),
|
2016-05-02 14:54:25 -07:00
|
|
|
database = require('../../database.js'),
|
2019-01-23 17:11:57 +01:00
|
|
|
eventlogdb = require('../../eventlogdb.js'),
|
2016-05-02 14:54:25 -07:00
|
|
|
expect = require('expect.js'),
|
2019-02-15 14:40:15 -08:00
|
|
|
hat = require('../../hat.js'),
|
2016-05-02 14:54:25 -07:00
|
|
|
superagent = require('superagent'),
|
|
|
|
|
server = require('../../server.js'),
|
|
|
|
|
tokendb = require('../../tokendb.js');
|
|
|
|
|
|
2019-07-25 15:43:51 -07:00
|
|
|
var SERVER_URL = 'http://localhost:' + constants.PORT;
|
2016-05-02 14:54:25 -07:00
|
|
|
|
|
|
|
|
var USERNAME = 'superadmin', PASSWORD = 'Foobar?1337', EMAIL ='silly@me.com';
|
|
|
|
|
var token = null;
|
|
|
|
|
|
|
|
|
|
var USER_1_ID = null, token_1;
|
|
|
|
|
|
2019-01-23 17:11:57 +01:00
|
|
|
var EVENT_0 = {
|
|
|
|
|
id: 'event_0',
|
|
|
|
|
action: 'foobaraction',
|
|
|
|
|
source: {
|
|
|
|
|
ip: '127.0.0.1'
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
something: 'is there'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-02 14:54:25 -07:00
|
|
|
function setup(done) {
|
|
|
|
|
async.series([
|
|
|
|
|
server.start.bind(server),
|
|
|
|
|
|
|
|
|
|
database._clear,
|
|
|
|
|
|
|
|
|
|
function createAdmin(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();
|
|
|
|
|
expect(result.statusCode).to.eql(201);
|
2016-05-02 14:54:25 -07:00
|
|
|
|
2018-01-18 13:41:10 -08:00
|
|
|
// stash token for further use
|
|
|
|
|
token = result.body.token;
|
2016-05-02 14:54:25 -07:00
|
|
|
|
2018-01-18 13:41:10 -08:00
|
|
|
callback();
|
|
|
|
|
});
|
2016-05-02 14:54:25 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
function (callback) {
|
|
|
|
|
superagent.post(SERVER_URL + '/api/v1/users')
|
2018-01-18 13:41:10 -08:00
|
|
|
.query({ access_token: token })
|
2020-02-21 12:17:06 -08:00
|
|
|
.send({ username: 'nonadmin', email: 'notadmin@server.test' })
|
2018-01-18 13:41:10 -08:00
|
|
|
.end(function (err, res) {
|
|
|
|
|
expect(res.statusCode).to.equal(201);
|
2016-05-02 14:54:25 -07:00
|
|
|
|
2018-01-18 13:41:10 -08:00
|
|
|
USER_1_ID = res.body.id;
|
2016-05-02 14:54:25 -07:00
|
|
|
|
2018-01-18 13:41:10 -08:00
|
|
|
callback(null);
|
|
|
|
|
});
|
2016-05-02 14:54:25 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
function (callback) {
|
2019-02-15 14:40:15 -08:00
|
|
|
token_1 = hat(8 * 32);
|
2016-05-02 14:54:25 -07:00
|
|
|
|
|
|
|
|
// HACK to get a token for second user (passwords are generated and the user should have gotten a password setup link...)
|
2020-02-11 17:32:58 -08:00
|
|
|
tokendb.add({ id: 'tid-0', accessToken: token_1, identifier: USER_1_ID, clientId: 'test-client-id', expires: Date.now() + 100000, scope: 'unused', name: '' }, callback);
|
2019-01-23 17:11:57 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
function (callback) {
|
|
|
|
|
eventlogdb.add(EVENT_0.id, EVENT_0.action, EVENT_0.source, EVENT_0.data, callback);
|
2016-05-02 14:54:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
], done);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cleanup(done) {
|
|
|
|
|
database._clear(function (error) {
|
|
|
|
|
expect(!error).to.be.ok();
|
|
|
|
|
|
|
|
|
|
server.stop(done);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('Eventlog API', function () {
|
|
|
|
|
before(setup);
|
|
|
|
|
after(cleanup);
|
|
|
|
|
|
|
|
|
|
describe('get', function () {
|
2019-01-23 17:11:57 +01:00
|
|
|
it('fails due to wrong token', function (done) {
|
|
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog/' + EVENT_0.id)
|
|
|
|
|
.query({ access_token: token.toUpperCase() })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(401);
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('fails for non-admin', function (done) {
|
|
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog/' + EVENT_0.id)
|
|
|
|
|
.query({ access_token: token_1 })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(403);
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('fails if not exists', function (done) {
|
|
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog/doesnotexist')
|
|
|
|
|
.query({ access_token: token })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(404);
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('succeeds for admin', function (done) {
|
|
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog/' + EVENT_0.id)
|
|
|
|
|
.query({ access_token: token })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(200);
|
|
|
|
|
expect(result.body.event).to.be.an('object');
|
|
|
|
|
expect(result.body.event.creationTime).to.be.a('string');
|
|
|
|
|
|
|
|
|
|
delete result.body.event.creationTime;
|
|
|
|
|
expect(result.body.event).to.eql(EVENT_0);
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('list', function () {
|
2016-05-02 14:54:25 -07:00
|
|
|
it('fails due to wrong token', function (done) {
|
2017-04-18 14:53:08 -07:00
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog')
|
2018-01-18 13:41:10 -08:00
|
|
|
.query({ access_token: token.toUpperCase() })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(401);
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-05-02 14:54:25 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('fails for non-admin', function (done) {
|
2017-04-18 14:53:08 -07:00
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog')
|
2018-01-18 13:41:10 -08:00
|
|
|
.query({ access_token: token_1, page: 1, per_page: 10 })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(403);
|
2016-05-02 14:54:25 -07:00
|
|
|
|
2018-01-18 13:41:10 -08:00
|
|
|
done();
|
|
|
|
|
});
|
2016-05-02 14:54:25 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('succeeds for admin', function (done) {
|
2017-04-18 14:53:08 -07:00
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog')
|
2018-01-18 13:41:10 -08:00
|
|
|
.query({ access_token: token, page: 1, per_page: 10 })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(200);
|
|
|
|
|
expect(result.body.eventlogs.length >= 2).to.be.ok(); // activate, user.add
|
2016-05-02 14:54:25 -07:00
|
|
|
|
2018-01-18 13:41:10 -08:00
|
|
|
done();
|
|
|
|
|
});
|
2016-05-02 14:54:25 -07:00
|
|
|
});
|
2016-05-06 16:49:17 +02:00
|
|
|
|
2018-03-05 11:14:36 +01:00
|
|
|
it('succeeds with deprecated action', function (done) {
|
2017-04-18 14:53:08 -07:00
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog')
|
2018-01-18 13:41:10 -08:00
|
|
|
.query({ access_token: token, page: 1, per_page: 10, action: 'cloudron.activate' })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(200);
|
|
|
|
|
expect(result.body.eventlogs.length).to.equal(1);
|
2016-05-06 16:49:17 +02:00
|
|
|
|
2018-01-18 13:41:10 -08:00
|
|
|
done();
|
|
|
|
|
});
|
2016-05-06 16:49:17 +02:00
|
|
|
});
|
|
|
|
|
|
2018-03-05 11:14:36 +01:00
|
|
|
it('succeeds with actions', function (done) {
|
|
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog')
|
|
|
|
|
.query({ access_token: token, page: 1, per_page: 10, actions: 'cloudron.activate, user.add' })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(200);
|
|
|
|
|
expect(result.body.eventlogs.length).to.equal(3);
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-05-06 16:49:17 +02:00
|
|
|
it('succeeds with search', function (done) {
|
2017-04-18 14:53:08 -07:00
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog')
|
2018-01-18 13:41:10 -08:00
|
|
|
.query({ access_token: token, page: 1, per_page: 10, search: EMAIL })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(200);
|
2020-02-21 12:17:06 -08:00
|
|
|
expect(result.body.eventlogs.length).to.equal(1);
|
2016-05-06 16:49:17 +02:00
|
|
|
|
2018-01-18 13:41:10 -08:00
|
|
|
done();
|
|
|
|
|
});
|
2016-05-06 16:49:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('succeeds with search', function (done) {
|
2017-04-18 14:53:08 -07:00
|
|
|
superagent.get(SERVER_URL + '/api/v1/cloudron/eventlog')
|
2018-03-05 11:14:36 +01:00
|
|
|
.query({ access_token: token, page: 1, per_page: 10, search: EMAIL, actions: 'cloudron.activate' })
|
2018-01-18 13:41:10 -08:00
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result.statusCode).to.equal(200);
|
|
|
|
|
expect(result.body.eventlogs.length).to.equal(0);
|
2016-05-06 16:49:17 +02:00
|
|
|
|
2018-01-18 13:41:10 -08:00
|
|
|
done();
|
|
|
|
|
});
|
2016-05-06 16:49:17 +02:00
|
|
|
});
|
2016-05-02 14:54:25 -07:00
|
|
|
});
|
|
|
|
|
});
|