Permalink
Browse files

Work on improved tests.

  • Loading branch information...
1 parent cd7a2f3 commit 2608cb847321027537563db03f0d0e0c7d0ec9f7 @jerolimov jerolimov committed Jul 14, 2014
@@ -0,0 +1,10 @@
+'use strict';
+
+var mongoose = require('mongoose');
+
+/**
+ * List of subjects
+ */
+exports.list = function(req, res) {
+
+};
@@ -1,25 +1,11 @@
-// Run tests in production environment
-process.env.NODE_ENV = 'production';
-var request = require('supertest'),
- server = require('../../server');
-
-request = request(server);
+var server = require('../tests/server');
describe('API authentification', function() {
- describe('GET /api/users/me', function() {
- it('should return 401 when trying to fetch user data without authentification', function(done) {
- request
- .get('/api/users/me')
- .expect(401)
- .end(done);
- });
- });
-
describe('POST /api/login', function() {
it('should return 400 when trying to login without data', function(done) {
- request
+ server
.post('/api/login')
.expect(400)
.end(done);
@@ -36,7 +22,7 @@ describe('API authentification', function() {
password: 'password'
};
- request
+ server
.post('/api/login')
.send(credentials)
.expect(401)
@@ -0,0 +1,77 @@
+'use strict';
+
+var should = require('should');
+var async = require('async');
+var server = require('../server');
+var db = require('../../../config/db.js')
+var User = require('../../models/User.js');
+
+describe('Subject controller', function() {
+
+ beforeEach(function(done) {
+
+ async.series([
+ function(callback) {
+ User.count(function(err, count) {
+ console.log(err + ' - ' + count);
+ callback();
+ });
+ },
+ function(callback) {
+ new User({
+ username: 'Username',
+ firstname: 'Vorname',
+ lastname: 'Nachname',
+ email: 'asd@asd.de'
+ }).save(function(err, user) {
+ console.log('Saved: ' + err + ' - ' + user);
+ callback();
+ });
+ },
+ function(callback) {
+ User.count(function(err, count) {
+ console.log('Count: ' + err + ' - ' + count);
+ callback();
+ });
+ },
+ function(callback) {
+ User.remove(function(err, users) {
+ console.log('Removed: ' + err + ' - ' + users);
+ callback();
+ });
+ },
+ function(callback) {
+ User.count(function(err, count) {
+ console.log('Count: ' + err + ' - ' + count);
+ callback();
+ });
+ }
+ ], done);
+ });
+
+ it('should return all resources', function(done) {
+ User.find(function(err, users) {
+ console.log(err);
+ console.log(users);
+ done();
+ });
+ });
+
+ it('should return all resources', function(done) {
+
+ var user = {
+ name: 'tj'
+ , pets: ['tobi', 'loki', 'jane', 'bandit']
+ };
+
+ user.should.have.property('name', 'tj');
+ user.should.have.property('pets').with.lengthOf(4);
+
+ server
+ .get('/api/subjects')
+ .expect(200)
+ .end(done);
+
+ });
+
+});
@@ -0,0 +1,13 @@
+'use strict';
+
+var server = require('../server');
+
+describe('API authentification', function() {
+ it('should return 401 when trying to fetch user data without authentification', function(done) {
+ server
+ .get('/api/users/me')
+ .expect(401)
+ .end(done);
+ });
+
+});
View
@@ -0,0 +1,7 @@
+// Change environment
+process.env.NODE_ENV = 'test';
+
+var request = require('supertest'),
+ server = require('../../server');
+
+module.exports = request(server);
View
@@ -2,7 +2,13 @@
var mongoose = require('mongoose');
-module.exports.url = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/levelpad';
+var env = process.env.NODE_ENV;
+
+if (env === 'test') {
+ module.exports.url = 'mongodb://localhost/levelpadtest';
+} else {
+ module.exports.url = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/levelpad';
+}
// Connect to our database
mongoose.connect(module.exports.url);
View
@@ -18,12 +18,19 @@ module.exports = function(app) {
// Basic request processing:
app.use(require('cookie-parser')(process.env.COOKIE_SECRET || 'H2YlmVI=srH5DCw4xKA(IA4YZ|Gr4gutt|Lh0WD:'));
- app.use(session({
- secret: process.env.SESSION_SECRET || '&Rd65y($lbBh}=)N{U}uBL&3BXitK$G2h@C8mpew',
- store: new MongoStore({ url: require('./db').url }),
- resave: false,
- saveUninitialized: false
- }));
+
+ // Session
+ if (env === 'test') {
+ // TODO: Disable session in tests, or?
+ } else {
+ app.use(session({
+ secret: process.env.SESSION_SECRET || '&Rd65y($lbBh}=)N{U}uBL&3BXitK$G2h@C8mpew',
+ store: new MongoStore({ url: require('./db').url }),
+ resave: false,
+ saveUninitialized: false
+ }));
+ }
+
app.use(bodyParser.json());
app.use(require('connect-timeout')(10 * 1000));
View
@@ -7,7 +7,7 @@
"postinstall": "bower install",
"run": "node server.js",
"run-auto": "nodemon --delay 2.5 --ignore public server.js",
- "test": "mocha --reporter spec app/tests"
+ "test": "mocha --recursive --reporter spec app/tests"
},
"repository": {
"type": "git",
@@ -42,8 +42,7 @@
"morgan": "~1.1.1",
"passport": "~0.2.0",
"passport-local": "~1.0.0",
- "path-to-regexp": "~0.2.1",
- "socket.io": "1.0.6"
+ "path-to-regexp": "~0.2.1"
},
"devDependencies": {
"bower": "*",
View
@@ -12,7 +12,11 @@ var port = process.env.PORT || 5000;
var express = require('express'),
app = express();
-console.info('Will start server on port %d in %s mode...', port, app.get('env'));
+if (process.env.NODE_ENV !== 'test') {
+ console.info('Will start server on port %d in %s mode...', port, app.get('env'));
+} else {
+ console.info('Provide test server for unit tests...');
+}
// Express settings
require('./config/express')(app);
@@ -29,27 +33,13 @@ require('./config/auth')(app);
// Routing
app.use(require('./config/routes'));
-// @todo
-// Socket.io
var server = require('http').createServer(app);
-var io = require('socket.io').listen(server);
-io.sockets.on('connection', function (socket) {
- console.log('new connection...');
-
- socket.broadcast.emit('chat_message', {
- type: 'message',
- text: 'hallo!'
- });
-
- socket.on('chat_message', function (data) {
- console.log(data);
- });
-});
-
// Start server
-server.listen(port, function() {
- console.info('Server started! Waiting for requests...');
-});
+if (process.env.NODE_ENV !== 'test') {
+ server.listen(port, function() {
+ console.info('Server started! Waiting for requests...');
+ });
+}
module.exports = server;

0 comments on commit 2608cb8

Please sign in to comment.