You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
2.7 KiB
JavaScript
135 lines
2.7 KiB
JavaScript
8 years ago
|
'use strict';
|
||
|
/*global require, before*/
|
||
|
|
||
|
var assert = require('assert');
|
||
|
var nconf = require('nconf');
|
||
|
var request = require('request');
|
||
|
|
||
|
var db = require('./mocks/databasemock');
|
||
8 years ago
|
var user = require('../src/user');
|
||
8 years ago
|
|
||
|
describe('authentication', function () {
|
||
|
var jar = request.jar();
|
||
|
|
||
8 years ago
|
before(function (done) {
|
||
8 years ago
|
user.create({username: 'regular', password: 'regularpwd', email: '[email protected]' }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
8 years ago
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should register and login a user', function (done) {
|
||
|
request({
|
||
|
url: nconf.get('url') + '/api/config',
|
||
|
json: true,
|
||
|
jar: jar
|
||
|
}, function (err, response, body) {
|
||
|
assert.ifError(err);
|
||
|
|
||
|
request.post(nconf.get('url') + '/register', {
|
||
|
form: {
|
||
|
email: '[email protected]',
|
||
|
username: 'admin',
|
||
|
password: 'adminpwd',
|
||
|
},
|
||
|
json: true,
|
||
|
jar: jar,
|
||
|
headers: {
|
||
|
'x-csrf-token': body.csrf_token
|
||
|
}
|
||
|
}, function (err, response, body) {
|
||
|
assert.ifError(err);
|
||
|
assert(body);
|
||
|
|
||
|
request({
|
||
|
url: nconf.get('url') + '/api/me',
|
||
|
json: true,
|
||
|
jar: jar
|
||
|
}, function (err, response, body) {
|
||
|
assert.ifError(err);
|
||
|
assert(body);
|
||
|
assert.equal(body.username, 'admin');
|
||
|
assert.equal(body.email, '[email protected]');
|
||
8 years ago
|
done();
|
||
8 years ago
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should logout a user', function (done) {
|
||
|
request({
|
||
|
url: nconf.get('url') + '/api/config',
|
||
|
json: true,
|
||
|
jar: jar
|
||
|
}, function (err, response, body) {
|
||
|
assert.ifError(err);
|
||
|
|
||
|
request.post(nconf.get('url') + '/logout', {
|
||
|
form: {},
|
||
|
json: true,
|
||
|
jar: jar,
|
||
|
headers: {
|
||
|
'x-csrf-token': body.csrf_token
|
||
|
}
|
||
|
}, function (err, response, body) {
|
||
|
assert.ifError(err);
|
||
|
|
||
|
request({
|
||
|
url: nconf.get('url') + '/api/me',
|
||
|
json: true,
|
||
|
jar: jar
|
||
|
}, function (err, response, body) {
|
||
|
assert.ifError(err);
|
||
|
assert.equal(body, 'not-authorized');
|
||
|
done()
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should login a user', function (done) {
|
||
|
var jar = request.jar();
|
||
|
request({
|
||
|
url: nconf.get('url') + '/api/config',
|
||
|
json: true,
|
||
|
jar: jar
|
||
|
}, function (err, response, body) {
|
||
|
assert.ifError(err);
|
||
|
|
||
|
request.post(nconf.get('url') + '/login', {
|
||
|
form: {
|
||
8 years ago
|
username: 'regular',
|
||
|
password: 'regularpwd',
|
||
8 years ago
|
},
|
||
|
json: true,
|
||
|
jar: jar,
|
||
|
headers: {
|
||
|
'x-csrf-token': body.csrf_token
|
||
|
}
|
||
|
}, function (err, response, body) {
|
||
|
assert.ifError(err);
|
||
|
assert(body);
|
||
|
|
||
|
request({
|
||
|
url: nconf.get('url') + '/api/me',
|
||
|
json: true,
|
||
|
jar: jar
|
||
|
}, function (err, response, body) {
|
||
|
assert.ifError(err);
|
||
|
assert(body);
|
||
8 years ago
|
assert.equal(body.username, 'regular');
|
||
|
assert.equal(body.email, '[email protected]');
|
||
8 years ago
|
done()
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
|
||
|
after(function (done) {
|
||
8 years ago
|
db.emptydb(done);
|
||
8 years ago
|
});
|
||
|
|
||
|
});
|
||
|
|