Skip to content

Commit

Permalink
ch(test) Add frontend controller test units
Browse files Browse the repository at this point in the history
  - Mock http request for angular
  - write unit test cases for controller methods
  - modified controller methods to return promise
  [Finishes #158457036]
  • Loading branch information
Ben Onah committed Jul 12, 2018
1 parent 3700f23 commit eacc774
Show file tree
Hide file tree
Showing 36 changed files with 280 additions and 789 deletions.
3 changes: 0 additions & 3 deletions .env 2

This file was deleted.

2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MONGODB_URI=<Test database url>
MONGOHQ_URL_TEST=<Test database url>
MONGODB_URI=<Development database url>
SECRET_KEY=<Secret key for JWT>
GMAIL_PASSWORD=<password is pinned at slack channel>
22 changes: 13 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"root": true,
"root": true,
"extends": "airbnb-base",
"env": {
"node": true,
Expand Down Expand Up @@ -31,19 +31,23 @@
"requireReturnDescription": true
}],
"require-jsdoc": ["error", {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
}
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
}
}]
},
"globals": {
"document": true,
"window": true,
"localStorage": true,
"window": true,
"localStorage": true,
"FormData": true,
"angular": true,
"$": true,
"require": true,
"describe": true,
"before": true,
"it": true,
"$": true
}
}
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"scripts": {
},
"env": {
"MONGOHQ_URL": {
"MONGODB_URI": {
"required": true
},
"GMAIL_PASSWORD": {
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ const signin = (req, res) => {
* @param {function} next - next function for passing the request to next handler
* @description Controller for handling requests to '/api/auth/login', returns token in response as JSON.
* @param {object} passport - passport with all the startegies registered
* @description Controller for handling requests to '/api/auth/login',
* @description Controller for handling requests to '/api/auth/login',
* returns token in response as JSON.
* @param {object} passport - passport with all the startegies registered
* @description Controller for handling requests to '/api/auth/login',
* @description Controller for handling requests to '/api/auth/login',
* returns token in response as JSON.
* Uses Tokenizer helper method to handle generation of token
*/
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/sendInvitationEmail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint no-useless-escape: 0 */
/* eslint no-useless-escape: 0, no-console: 0 */
import nodemailer from 'nodemailer';
import IsUrl from 'is-url';

Expand Down
2 changes: 1 addition & 1 deletion app/middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import jwt from 'jsonwebtoken';
const auth = (req, res, next) => {
let token = req.headers.authorization || req.headers['x-access-token'];
token = token.replace('Bearer ', '');

if (!token) {
return res.status(401).json({ message: 'Unauthorized Access' });
}
Expand Down
1 change: 1 addition & 0 deletions backend-test/game/game.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-undef: 0 */
import should from 'should';
import io from 'socket.io-client';
import config from '../../config/config';
Expand Down
1 change: 1 addition & 0 deletions backend-test/helpers/sendEmail.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-undef: 0 */
import { expect } from 'chai';
import sendInvitationEmail from '../../app/helpers/sendInvitationEmail';

Expand Down
1 change: 1 addition & 0 deletions backend-test/helpers/tokenizer.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-undef: 0 */
import { expect } from 'chai';
import jwt from 'jsonwebtoken';
import { Tokenizer, decodeToken } from '../../app/helpers/tokenizer';
Expand Down
2 changes: 1 addition & 1 deletion backend-test/integration/auth.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'babel-polyfill';
/* eslint no-undef: 0 */
import request from 'supertest';
import faker from 'faker';
import { expect } from 'chai';
Expand Down
10 changes: 5 additions & 5 deletions backend-test/integration/findUsers.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'babel-polyfill';
/* eslint no-undef: 0 */
import request from 'supertest';
import { expect } from 'chai';
import mongoose from 'mongoose';
Expand Down Expand Up @@ -44,9 +44,9 @@ describe('User endpoints', () => {
Promise.resolve(User.remove({}));
});

it('GET /users/findUsers/:searchKey should return statusCode 200 with 2 users', (done) => {
it('GET /api/users/findUsers/:searchKey should return statusCode 200 with 2 users', (done) => {
request(app)
.get('/users/findUsers/ben')
.get('/api/users/findUsers/ben')
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
if (err) return done(err);
Expand All @@ -56,9 +56,9 @@ describe('User endpoints', () => {
});
});

it('GET /users/findUsers/:searchKey should return statusCode 200 with no user', (done) => {
it('GET /api/users/findUsers/:searchKey should return statusCode 200 with no user', (done) => {
request(app)
.get('/users/findUsers/nothing')
.get('/api/users/findUsers/nothing')
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
if (err) return done(err);
Expand Down
14 changes: 7 additions & 7 deletions backend-test/integration/inviteUsers.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'babel-polyfill';
/* eslint no-undef: 0 */
import request from 'supertest';
import { expect } from 'chai';
import app from '../../server';
Expand All @@ -13,7 +13,7 @@ const user = {
const token = Tokenizer(user);

describe('User endpoints', () => {
it('POST /users/invite should return statusCode 200 with a user object', (done) => {
it('POST /api/users/invite should return statusCode 200 with a user object', (done) => {
const payload = {
user: {
name: 'Ben Onah',
Expand All @@ -23,7 +23,7 @@ describe('User endpoints', () => {
link: 'http://localhost:3333/app'
};
request(app)
.post('/users/invite')
.post('/api/users/invite')
.set('Authorization', `Bearer ${token}`)
.send(payload)
.end((err, res) => {
Expand All @@ -34,7 +34,7 @@ describe('User endpoints', () => {
});
});

it('POST /users/invite should return statusCode 400 with error message if wrong email is used', (done) => {
it('POST /api/users/invite should return statusCode 400 with error message if wrong email is used', (done) => {
const payload = {
user: {
name: 'Ben Onah',
Expand All @@ -44,7 +44,7 @@ describe('User endpoints', () => {
link: 'http://localhost:3333/app'
};
request(app)
.post('/users/invite')
.post('/api/users/invite')
.set('Authorization', `Bearer ${token}`)
.send(payload)
.end((err, res) => {
Expand All @@ -55,7 +55,7 @@ describe('User endpoints', () => {
});
});

it('POST /users/invite should return statusCode 400 with error message if wrong link is used', (done) => {
it('POST /api/users/invite should return statusCode 400 with error message if wrong link is used', (done) => {
const payload = {
user: {
name: 'Ben Onah',
Expand All @@ -65,7 +65,7 @@ describe('User endpoints', () => {
link: 'htt/localhost:3333/app'
};
request(app)
.post('/users/invite')
.post('/api/users/invite')
.set('Authorization', `Bearer ${token}`)
.send(payload)
.end((err, res) => {
Expand Down
4 changes: 1 addition & 3 deletions backend-test/user/model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
* Module dependencies.
*/
/* eslint no-undef: 0 */
const should = require('should');
const mongoose = require('mongoose');

Expand Down
8 changes: 4 additions & 4 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export default (router, passport, app) => {
const api = Router();
api
.post('/auth/login', users.handleLogin)
.post('/auth/signup', users.handleSignUp);
.post('/auth/signup', users.handleSignUp)
.get('/users/findUsers/:searchKey', auth, users.findUsers)
.get('/users/findUsers/', auth, users.findUsers)
.post('/users/invite', auth, users.invite);

// Setting up the game api
api
Expand All @@ -26,9 +29,6 @@ export default (router, passport, app) => {
// Setting up the users api
router.post('/users', users.create);
router.post('/users/avatars', users.avatars);
router.get('/users/findUsers/:searchKey', middleware.auth, users.findUsers);
router.get('/users/findUsers/', middleware.auth, users.findUsers);
router.post('/users/invite', middleware.auth, users.invite);

// Donation Routes
router.post('/donations', users.addDonation);
Expand Down
Loading

0 comments on commit eacc774

Please sign in to comment.