Skip to content
This repository has been archived by the owner on Aug 5, 2019. It is now read-only.

Commit

Permalink
Server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbardian committed Mar 22, 2018
1 parent 3aefff1 commit 9b5b2b9
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 162 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
collectCoverage: true,
coverageDirectory: './coverage',
coveragePathIgnorePatterns: ['dist'],
coveragePathIgnorePatterns: ['dist', 'node_modules'],
testPathIgnorePatterns: ['dist'],
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"pretest": "npm run clean",
"test": "jest --no-cache --runInBand --forceExit",
"test": "jest --forceExit",
"test:watch": "jest --watch",
"lint": "eslint . ",
"build": "npm run build:server && npm run build:client",
Expand Down
30 changes: 24 additions & 6 deletions src/server/models/CreditCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,30 @@ mongoose.Promise = bluebird;

// Create a schema
const CreditCardSchema = new mongoose.Schema({
userId: String,
name: String,
limit: Number,
balance: Number,
interest_rate: Number,
updated_at: { type: Date, default: Date.now },
userId: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
limit: {
type: Number,
require: true,
},
balance: {
type: Number,
require: true,
},
interest_rate: {
type: Number,
require: true,
},
updated_at: {
type: Date,
default: Date.now,
},
});

module.exports = mongoose.model('CreditCard', CreditCardSchema);
142 changes: 56 additions & 86 deletions src/server/routesLogic/creditCards.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,38 @@
import logger from 'console';

const User = require('../models/User');
const CreditCard = require('../models/CreditCard');

export const getAllCreditCards = (req, res, next) => {
export const getAllCreditCards = (req, res) => {
// TODO: Update to use query to sort (ex: 'sort=balance', 'sort=interest_rate'.
User.findById(req.session && req.session.userId).exec((error, user) => {
if (error) {
return next(error);
}
if (user === null) {
return res.redirect('/login');
User.findById(req.session && req.session.userId, userError => {
if (userError) {
return res
.status(400)
.json({ error: true, message: 'Error fetching data' });
}
let response = {};
CreditCard.find({ userId: req.session.userId }, (err, data) => {
if (err) {
response = { error: true, message: 'Error fetching data' };
} else {
response = { error: false, message: data };
CreditCard.find({ userId: req.session.userId }, (cardError, data) => {
if (cardError) {
return res
.status(400)
.json({ error: true, message: 'Error fetching data' });
}
res.json(response);
return res.json({ error: false, message: data });
}).sort([['balance', 'descending']]);
});
};

export const getCreditCardById = (req, res) => {
logger.info('request = ', req.params);
let response = {};
CreditCard.findById(req.params.id, (err, data) => {
if (err) {
response = { error: true, message: 'Error fetching data' };
} else {
response = { error: false, message: data };
return res
.status(400)
.json({ error: true, message: 'Error fetching data' });
}
res.json(response);
return res.json({ error: false, message: data });
});
};

export const addCreditCard = (req, res) => {
let response = {};
const creditCard = new CreditCard();
if (
!(
req.session.userId &&
req.body.user &&
req.body.name &&
req.body.limit &&
req.body.balance &&
req.body.interest_rate
)
) {
response = {
error: true,
message: 'Error adding data, all fields required',
};
return res.json(response);
}
creditCard.userId = req.session.userId;
creditCard.user = req.body.user;
creditCard.name = req.body.name;
Expand All @@ -64,69 +41,62 @@ export const addCreditCard = (req, res) => {
creditCard.interest_rate = req.body.interest_rate;
creditCard.save((err, data) => {
if (err) {
response = { error: true, message: 'Error adding data' };
} else {
response = {
error: false,
message: 'Data added',
data,
};
return res
.status(400)
.json({ error: true, message: 'Error adding data' });
}
res.json(response);
return res.json({
error: false,
message: 'Data added',
data,
});
});
};

export const deleteCreditCard = (req, res) => {
let response = {};
CreditCard.findById(req.params.id, err => {
if (err) {
return res.json({ error: true, message: 'Error fetching data' });
CreditCard.findById(req.params.id, findError => {
if (findError) {
return res
.status(400)
.json({ error: true, message: 'Error fetching data' });
}
CreditCard.remove({ _id: req.params.id }, () => {
if (err) {
response = { error: true, message: 'Error deleting data' };
} else {
response = {
error: false,
message: `Data associated with ${req.params.id} is deleted`,
};
CreditCard.remove({ _id: req.params.id }, removeError => {
if (removeError) {
return res
.status(400)
.json({ error: true, message: 'Error deleting data' });
}
res.json(response);
return res.json({
error: false,
message: `Data associated with ${req.params.id} is deleted`,
});
});
});
};

export const putOrUpdate = (req, res) => {
let response = {};
CreditCard.findById(req.params.id, (err, initialData) => {
const data = initialData;
data.userId = req.session.userId;
if (err) {
response = { error: true, message: 'Error fetching data' };
} else {
if (req.body.name !== undefined) {
data.name = req.body.name;
}
if (req.body.limit !== undefined) {
data.limit = req.body.limit;
}
if (req.body.balance !== undefined) {
data.balance = req.body.balance;
}
if (req.body.interest_rate !== undefined) {
data.interest_rate = req.body.interest_rate;
return res
.status(400)
.json({ error: true, message: 'Error fetching data' });
}
data.userId = req.session.userId;
data.name = req.body.name;
data.limit = req.body.limit;
data.balance = req.body.balance;
data.interest_rate = req.body.interest_rate;
data.save(saveError => {
if (saveError) {
return res
.status(400)
.json({ error: true, message: 'Error updating data' });
}
data.save(() => {
if (err) {
response = { error: true, message: 'Error updating data' };
} else {
response = {
error: false,
message: `Data is updated for ${req.body.name}`,
};
}
res.json(response);
return res.json({
error: false,
message: `Data is updated for ${req.body.name}`,
});
}
});
});
};
129 changes: 126 additions & 3 deletions src/server/routesLogic/creditCards.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import session from 'supertest-session';
import mockingoose from 'mockingoose';
import { server } from '../server';
import {
VALID_USERID,
CREDIT_CARD,
INVALID_CREDIT_CARD,
LOGIN_SUCCESS_MOCK_USER,
Expand All @@ -23,6 +24,54 @@ describe('Test /creditcards API routes', () => {
.expect(200);
});

describe('Test getCreditCards', () => {
it('getCreditCards success, return 200 status, and error = false', async () => {
mockingoose.User.toReturn(VALID_USERID, 'findOne');
mockingoose.CreditCard.toReturn(CREDIT_CARD, 'find');
const response = await serverSession
.get('/api/creditcards')
.set('Accept', 'text/html, application/json');
expect(response.statusCode).toBe(200);
expect(response.error).toEqual(false);
});
it('getCreditCards failure, find User error, return 400 status, and error = true', async () => {
mockingoose.User.toReturn(new Error('Error'), 'findOne');
const response = await serverSession
.get('/api/creditcards')
.set('Accept', 'text/html application/json');
expect(response.statusCode).toBe(400);
expect(response.body.error).toEqual(true);
});
it('getCreditCards failure, find CreditCard, return 400 status, and error = true', async () => {
mockingoose.User.toReturn(VALID_USERID, 'findOne');
mockingoose.CreditCard.toReturn(new Error('Error'), 'find');
const response = await serverSession
.get('/api/creditcards')
.set('Accept', 'text/html application/json');
expect(response.statusCode).toBe(400);
expect(response.body.error).toEqual(true);
});
});

describe('Test getCreditCardById', () => {
it('getCreditCardById success, return 200 status, and error = false', async () => {
mockingoose.CreditCard.toReturn(CREDIT_CARD, 'findOne');
const response = await serverSession
.get('/api/creditcards/8675309')
.set('Accept', 'text/html application/json');
expect(response.statusCode).toBe(200);
expect(response.body.error).toEqual(false);
});
it('getCreditCardById failure, return 400 status, and error = true', async () => {
mockingoose.CreditCard.toReturn(new Error('Error'), 'findOne');
const response = await serverSession
.get('/api/creditcards/8675309')
.set('Accept', 'text/html application/json');
expect(response.statusCode).toBe(400);
expect(response.body.error).toEqual(true);
});
});

describe('Test addCreditCard', () => {
it('addCreditCard success, return 200 status, and error = false', async () => {
mockingoose.CreditCard.toReturn(CREDIT_CARD, 'save');
Expand All @@ -32,16 +81,90 @@ describe('Test /creditcards API routes', () => {
.send(CREDIT_CARD);
expect(response.body.data.limit).toEqual(10000);
});
it('addCreditCard failure, error = true', async () => {
it('addCreditCard failure, return 400 status, error = true', async () => {
mockingoose.CreditCard.toReturn(INVALID_CREDIT_CARD, 'save');
const response = await serverSession
.post('/api/creditcards')
.set('Accept', 'text/html, application/json')
.send(INVALID_CREDIT_CARD);
expect(response.statusCode).toBe(400);
expect(response.body.error).toBe(true);
expect(response.body.message).toEqual(
'Error adding data, all fields required',
expect(response.body.message).toEqual('Error adding data');
});
});

describe('Test deleteCreditCard', () => {
it('deleteCreditCard success, return 200 status, and error = false', async () => {
mockingoose.CreditCard.toReturn(VALID_USERID, 'findOne').toReturn(
VALID_USERID,
'remove',
);
const response = await serverSession
.delete('/api/creditcards/8675309')
.set('Accept', 'text/html application/json');
expect(response.statusCode).toBe(200);
expect(response.body.error).toEqual(false);
});
it('deleteCreditCard failure, invalid userId, return 400 status, and error = true', async () => {
mockingoose.CreditCard.toReturn(new Error('Error'), 'findOne').toReturn(
VALID_USERID,
'remove',
);
const response = await serverSession
.delete('/api/creditcards/8675309')
.set('Accept', 'text/html application/json');
expect(response.statusCode).toBe(400);
expect(response.body.error).toEqual(true);
});
it('deleteCreditCard failure, invalid credit card, return 400 status, and error = true', async () => {
mockingoose.CreditCard.toReturn(VALID_USERID, 'findOne').toReturn(
new Error('Error'),
'remove',
);
const response = await serverSession
.delete('/api/creditcards/8675309')
.set('Accept', 'text/html application/json');
expect(response.statusCode).toBe(400);
expect(response.body.error).toEqual(true);
});
});

describe('Test putOrUpdate', () => {
it('putOrUpdate success, return 200 status, and error = false', async () => {
mockingoose.CreditCard.toReturn(CREDIT_CARD, 'findOne').toReturn(
CREDIT_CARD,
'save',
);
const response = await serverSession
.put('/api/creditcards/8675309')
.set('Accept', 'text/html application/json')
.send(CREDIT_CARD);
expect(response.statusCode).toBe(200);
expect(response.body.error).toEqual(false);
});
it('putOrUpdate failure, find error, return 400 status, and error = true', async () => {
mockingoose.CreditCard.toReturn(new Error('Error'), 'findOne').toReturn(
CREDIT_CARD,
'save',
);
const response = await serverSession
.put('/api/creditcards/8675309')
.set('Accept', 'text/html application/json')
.send(CREDIT_CARD);
expect(response.statusCode).toBe(400);
expect(response.body.error).toEqual(true);
});
it('putOrUpdate failure, update error, return 400 status, and error = true', async () => {
mockingoose.CreditCard.toReturn(CREDIT_CARD, 'findOne').toReturn(
new Error('Error'),
'save',
);
const response = await serverSession
.put('/api/creditcards/8675309')
.set('Accept', 'text/html application/json')
.send(CREDIT_CARD);
expect(response.statusCode).toBe(400);
expect(response.body.error).toEqual(true);
});
});
});
Loading

0 comments on commit 9b5b2b9

Please sign in to comment.