mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-03-06 12:11:02 +01:00
279 lines
7.6 KiB
JavaScript
279 lines
7.6 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, _user, admin;
|
|
|
|
/**
|
|
* User routes tests
|
|
*/
|
|
describe('User CRUD tests', function () {
|
|
this.timeout(10000);
|
|
|
|
before(function (done) {
|
|
// Get application
|
|
app = express.init(mongoose);
|
|
agent = request.agent(app);
|
|
|
|
done();
|
|
});
|
|
|
|
beforeEach(function (done) {
|
|
// Create user credentials
|
|
credentials = {
|
|
username: 'username',
|
|
password: 'M3@n.jsI$Aw3$0m3'
|
|
};
|
|
|
|
// Create a new user
|
|
_user = {
|
|
firstName: 'Full',
|
|
lastName: 'Name',
|
|
displayName: 'Full Name',
|
|
email: 'test@test.com',
|
|
username: credentials.username,
|
|
password: credentials.password,
|
|
provider: 'local'
|
|
};
|
|
|
|
user = new User(_user);
|
|
|
|
// Save a user to the test db and create new article
|
|
user.save(function (err) {
|
|
should.not.exist(err);
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('should be able to register a new user', function (done) {
|
|
|
|
_user.username = 'register_new_user';
|
|
_user.email = 'register_new_user_@test.com';
|
|
|
|
agent.post('/api/auth/signup')
|
|
.send(_user)
|
|
.expect(200)
|
|
.end(function (signupErr, signupRes) {
|
|
// Handle signpu error
|
|
if (signupErr) {
|
|
return done(signupErr);
|
|
}
|
|
|
|
signupRes.body.username.should.equal(_user.username);
|
|
signupRes.body.email.should.equal(_user.email);
|
|
// Assert a proper profile image has been set, even if by default
|
|
signupRes.body.profileImageURL.should.not.be.empty();
|
|
// Assert we have just the default 'user' role
|
|
signupRes.body.roles.should.be.instanceof(Array).and.have.lengthOf(1);
|
|
signupRes.body.roles.indexOf('user').should.equal(0);
|
|
return done();
|
|
});
|
|
});
|
|
|
|
it('should be able to login successfully and logout successfully', function (done) {
|
|
agent.post('/api/auth/signin')
|
|
.send(credentials)
|
|
.expect(200)
|
|
.end(function (signinErr, signinRes) {
|
|
// Handle signin error
|
|
if (signinErr) {
|
|
return done(signinErr);
|
|
}
|
|
|
|
// Logout
|
|
agent.get('/api/auth/signout')
|
|
.expect(302)
|
|
.end(function (signoutErr, signoutRes) {
|
|
if (signoutErr) {
|
|
return done(signoutErr);
|
|
}
|
|
|
|
signoutRes.redirect.should.equal(true);
|
|
|
|
// NodeJS v4 changed the status code representation so we must check
|
|
// before asserting, to be comptabile with all node versions.
|
|
if (process.version.indexOf('v4') === 0) {
|
|
signoutRes.text.should.equal('Found. Redirecting to /');
|
|
} else {
|
|
signoutRes.text.should.equal('Moved Temporarily. Redirecting to /');
|
|
}
|
|
|
|
return 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);
|
|
}
|
|
|
|
// Request list of users
|
|
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 (err) {
|
|
should.not.exist(err);
|
|
agent.post('/api/auth/signin')
|
|
.send(credentials)
|
|
.expect(200)
|
|
.end(function (signinErr, signinRes) {
|
|
// Handle signin error
|
|
if (signinErr) {
|
|
return done(signinErr);
|
|
}
|
|
|
|
// Request list of users
|
|
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
|
|
return done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should be able to get a single user details if admin', function (done) {
|
|
user.roles = ['user', 'admin'];
|
|
|
|
user.save(function (err) {
|
|
should.not.exist(err);
|
|
agent.post('/api/auth/signin')
|
|
.send(credentials)
|
|
.expect(200)
|
|
.end(function (signinErr, signinRes) {
|
|
// Handle signin error
|
|
if (signinErr) {
|
|
return done(signinErr);
|
|
}
|
|
|
|
// Get single user information from the database
|
|
agent.get('/api/users/' + user._id)
|
|
.expect(200)
|
|
.end(function (userInfoErr, userInfoRes) {
|
|
if (userInfoErr) {
|
|
return done(userInfoErr);
|
|
}
|
|
|
|
userInfoRes.body.should.be.instanceof(Object);
|
|
userInfoRes.body._id.should.be.equal(String(user._id));
|
|
|
|
// Call the assertion callback
|
|
return done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should be able to update a single user details if admin', function (done) {
|
|
user.roles = ['user', 'admin'];
|
|
|
|
user.save(function (err) {
|
|
should.not.exist(err);
|
|
agent.post('/api/auth/signin')
|
|
.send(credentials)
|
|
.expect(200)
|
|
.end(function (signinErr, signinRes) {
|
|
// Handle signin error
|
|
if (signinErr) {
|
|
return done(signinErr);
|
|
}
|
|
|
|
// Get single user information from the database
|
|
|
|
var userUpdate = {
|
|
firstName: 'admin_update_first',
|
|
lastName: 'admin_update_last',
|
|
roles: ['admin']
|
|
};
|
|
|
|
agent.put('/api/users/' + user._id)
|
|
.send(userUpdate)
|
|
.expect(200)
|
|
.end(function (userInfoErr, userInfoRes) {
|
|
if (userInfoErr) {
|
|
return done(userInfoErr);
|
|
}
|
|
|
|
userInfoRes.body.should.be.instanceof(Object);
|
|
userInfoRes.body.firstName.should.be.equal('admin_update_first');
|
|
userInfoRes.body.lastName.should.be.equal('admin_update_last');
|
|
userInfoRes.body.roles.should.be.instanceof(Array).and.have.lengthOf(1);
|
|
userInfoRes.body._id.should.be.equal(String(user._id));
|
|
|
|
// Call the assertion callback
|
|
return done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should be able to delete a single user if admin', function (done) {
|
|
user.roles = ['user', 'admin'];
|
|
|
|
user.save(function (err) {
|
|
should.not.exist(err);
|
|
agent.post('/api/auth/signin')
|
|
.send(credentials)
|
|
.expect(200)
|
|
.end(function (signinErr, signinRes) {
|
|
// Handle signin error
|
|
if (signinErr) {
|
|
return done(signinErr);
|
|
}
|
|
|
|
agent.delete('/api/users/' + user._id)
|
|
//.send(userUpdate)
|
|
.expect(200)
|
|
.end(function (userInfoErr, userInfoRes) {
|
|
if (userInfoErr) {
|
|
return done(userInfoErr);
|
|
}
|
|
|
|
userInfoRes.body.should.be.instanceof(Object);
|
|
userInfoRes.body._id.should.be.equal(String(user._id));
|
|
|
|
// Call the assertion callback
|
|
return done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
afterEach(function (done) {
|
|
User.remove().exec(done);
|
|
});
|
|
});
|