mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-03-05 19:51:08 +01:00
108 lines
2.4 KiB
JavaScript
108 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
var should = require('should'),
|
|
request = require('supertest'),
|
|
path = require('path'),
|
|
mongoose = require('mongoose'),
|
|
User = mongoose.model('User'),
|
|
express = require(path.resolve('./config/lib/express'));
|
|
|
|
/**
|
|
* Globals
|
|
*/
|
|
var app, agent, credentials, user, admin;
|
|
|
|
/**
|
|
* User routes tests
|
|
*/
|
|
describe('User CRUD tests', function () {
|
|
before(function (done) {
|
|
// Get application
|
|
app = express.init(mongoose);
|
|
agent = request.agent(app);
|
|
|
|
done();
|
|
});
|
|
|
|
beforeEach(function (done) {
|
|
// Create user credentials
|
|
credentials = {
|
|
username: 'username',
|
|
password: 'password'
|
|
};
|
|
|
|
// Create a new user
|
|
user = new User({
|
|
firstName: 'Full',
|
|
lastName: 'Name',
|
|
displayName: 'Full Name',
|
|
email: 'test@test.com',
|
|
username: credentials.username,
|
|
password: credentials.password,
|
|
provider: 'local'
|
|
});
|
|
|
|
// Save a user to the test db and create new article
|
|
user.save(function () {
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('should not be able to retrieve a list of users if not admin', function (done) {
|
|
agent.post('/api/auth/signin')
|
|
.send(credentials)
|
|
.expect(200)
|
|
.end(function (signinErr, signinRes) {
|
|
// Handle signin error
|
|
if (signinErr) {
|
|
return done(signinErr);
|
|
}
|
|
|
|
// Save a new article
|
|
agent.get('/api/users')
|
|
.expect(403)
|
|
.end(function (usersGetErr, usersGetRes) {
|
|
if (usersGetErr) {
|
|
return done(usersGetErr);
|
|
}
|
|
|
|
return done();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should be able to retrieve a list of users if admin', function (done) {
|
|
user.roles = ['user', 'admin'];
|
|
|
|
user.save(function () {
|
|
agent.post('/api/auth/signin')
|
|
.send(credentials)
|
|
.expect(200)
|
|
.end(function (signinErr, signinRes) {
|
|
// Handle signin error
|
|
if (signinErr) {
|
|
return done(signinErr);
|
|
}
|
|
|
|
// Save a new article
|
|
agent.get('/api/users')
|
|
.expect(200)
|
|
.end(function (usersGetErr, usersGetRes) {
|
|
if (usersGetErr) {
|
|
return done(usersGetErr);
|
|
}
|
|
|
|
usersGetRes.body.should.be.instanceof(Array).and.have.lengthOf(1);
|
|
|
|
// Call the assertion callback
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
afterEach(function (done) {
|
|
User.remove().exec(done);
|
|
});
|
|
});
|