1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-14 00:46:38 -05:00

Start unit tests

This commit is contained in:
Jack 2017-10-12 17:27:03 -04:00
parent 25d28720dd
commit 3191aad0c1
Signed by: foltik
GPG Key ID: 303F88F996E95541
2 changed files with 138 additions and 1 deletions

View File

@ -34,7 +34,7 @@
"author": "Jack Foltz",
"license": "WTFPL",
"scripts": {
"test": "echo No tests implemented."
"test": "mocha"
},
"repository": {
"type": "git",

137
test/user.js Normal file
View File

@ -0,0 +1,137 @@
process.env.NODE_ENV = 'test';
var async = require('async');
var mongoose = require('mongoose');
var User = require('../app/models/User.js');
var Invite = require('../app/models/Invite.js');
var chai = require('chai');
var http = require('chai-http');
var should = chai.should();
var app = require('../server');
var server = app.server;
var db = app.db;
chai.use(http);
before(function (done) {
async.parallel([
function (cb) {
db.once('open', cb);
},
function (cb) {
User.remove({}, function (err) {
cb(err);
});
},
function (cb) {
Invite.remove({}, function (err) {
cb(err);
});
},
function (cb) {
var inv = new Invite();
inv.code = 'TestCode1';
inv.scope = ['test.perm'];
inv.save(function (err) {
cb(err);
});
},
function (cb) {
var inv = new Invite();
inv.code = 'TestCode2';
inv.scope = ['test.perm', 'file.upload'];
inv.save(function (err) {
cb(err);
});
}
], function (err) {
if (err) console.log(err);
done();
});
});
describe('Users', function () {
describe('/POST register', function () {
it('SHOULD register valid user, valid invite', function (done) {
var user = {
username: 'TestUser',
password: 'TestPassword',
invite: 'TestCode1'
};
chai.request(server)
.post('/api/auth/register')
.send(user)
.end(function (err, res) {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('token');
done();
});
});
it('SHOULD NOT register invalid user, valid invite', function (done) {
var user = {
username: 'TestUser',
password: 'TestPassword',
invite: 'TestCode2'
};
chai.request(server)
.post('/api/auth/register')
.send(user)
.end(function (err, res) {
res.should.have.status(401);
res.body.should.be.a('object');
res.body.should.have.property('message').eql('Username in use.');
done();
});
});
it('SHOULD NOT register valid user, nonexistant invite', function(done) {
var user = {
username: 'TestUser2',
password: 'TestPassword',
invite: 'bogus'
};
chai.request(server)
.post('/api/auth/register')
.send(user)
.end(function(err, res) {
res.should.have.a.status(401);
res.body.should.be.a('object');
res.body.should.have.property('message').eql('Invalid invite code.');
done();
});
});
it('SHOULD NOT register valid user, used invite', function(done) {
var user = {
username: 'TestUser2',
password: 'TestPassword',
invite: 'TestCode1'
};
chai.request(server)
.post('/api/auth/register')
.send(user)
.end(function(err, res) {
res.should.have.a.status(401);
res.body.should.be.a('object');
res.body.should.have.property('message').eql('Invalid invite code.');
done();
});
});
//TODO: Make sure expired invites don't work
});
});
after(function () {
server.close(function () {
process.exit();
});
});