Skip to content

Commit

Permalink
Finished testing api
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercbk committed Jan 9, 2018
1 parent 2b39509 commit 6d235ce
Show file tree
Hide file tree
Showing 10 changed files with 730 additions and 27 deletions.
7 changes: 6 additions & 1 deletion app/api/budget-transaction/budget-transaction-api.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const _ = require('lodash');
const apiOptions = require('../../lib/endpoint/api-options');
const RestError = require('../../lib/error');
const { handleTransactionError, validateBudgetDependencies } = require('../../lib/budget');
const {
handleTransactionError,
validateBudgetDependencies,
validateCompanyDepartment
} = require('../../lib/budget');

class BudgetTransactionAPI {
constructor(options) {
apiOptions.apply(this, [options]);
}

async query(budgetTransactionQuery) {
await validateCompanyDepartment(this.db, budgetTransactionQuery);
if (budgetTransactionQuery.id) {
// prettier screws up here
// eslint-disable-next-line max-len
Expand Down
34 changes: 25 additions & 9 deletions app/api/expense/expense-api.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const apiOptions = require('../../lib/endpoint/api-options');
const RestError = require('../../lib/error');
const { handleTransactionError, validateBudgetDependencies } = require('../../lib/budget');
const {
handleTransactionError,
validateBudgetDependencies,
validateCompanyDepartment
} = require('../../lib/budget');

class ExpenseAPI {
constructor(options) {
apiOptions.apply(this, [options]);
}

async query(expenseQuery) {
await validateCompanyDepartment(this.db, expenseQuery);
if (expenseQuery.id) {
// prettier screws up here
// eslint-disable-next-line max-len
Expand All @@ -24,13 +29,22 @@ class ExpenseAPI {
return expense;
}
const query = {
attributes: ['id', 'amount', 'concept', 'date', 'createdAt', 'updatedAt'],
attributes: [
'id',
'amount',
'concept',
'date',
'department',
'user',
'createdAt',
'updatedAt'
],
include: [
{
model: this.db.Department,
required: true,
where: {
department: expenseQuery.department
id: expenseQuery.department
}
}
],
Expand All @@ -40,7 +54,7 @@ class ExpenseAPI {
query.include[0].where.company = expenseQuery.company;
}
if (expenseQuery.from) {
query.where.$or = [
query.where.$and = [
{
date: {
$gte: expenseQuery.from
Expand All @@ -56,10 +70,10 @@ class ExpenseAPI {
}
}
];
if (query.where.$or) {
query.where.$or = query.where.$or.concat(condition);
if (query.where.$and) {
query.where.$and = query.where.$and.concat(condition);
} else {
query.where.$or = condition;
query.where.$and = condition;
}
}
const expenses = await this.db.Expense.findAll(query);
Expand All @@ -72,7 +86,8 @@ class ExpenseAPI {
const { departmentBudget } = await this._validateDependencies(prospect);
try {
transaction = await this.db.sequelize.transaction({ autocommit: false });
expense = await this.db.Expense.create(prospect, { transaction });
const expenseTemplate = Object.assign({}, prospect, { user: this.user.id });
expense = await this.db.Expense.create(expenseTemplate, { transaction });
this._applyExpense(departmentBudget, expense);
await departmentBudget.save({ transaction });
await transaction.commit();
Expand Down Expand Up @@ -103,7 +118,8 @@ class ExpenseAPI {

async remove(toDelete) {
let transaction;
let expense;
const expense = await this.query({ id: toDelete.id });
toDelete.date = expense.date;
const { originalExpense, departmentBudget } = await this._validateDependencies(toDelete);
try {
transaction = await this.db.sequelize.transaction({ autocommit: false });
Expand Down
19 changes: 14 additions & 5 deletions app/lib/budget/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const RestError = require('../error');

const validateBudgetDependencies = async function (db, prospect) {
const validateCompanyDepartment = async function (db, prospect) {
let company;
let department;
if (prospect.company) {
company = await db.Company.findById(prospect.company);
if (!company) {
throw new RestError(404, { message: `Company ${prospect.company} does not exist` });
}
}
const department = await db.Department.findById(prospect.department);
if (!department || (company && department.company !== company.id)) {
throw new RestError(404, { message: `Department ${prospect.department} does not exist` });
if (prospect.department) {
department = await db.Department.findById(prospect.department);
if (!department || (company && department.company !== company.id)) {
throw new RestError(404, { message: `Department ${prospect.department} does not exist` });
}
}
return { company, department };
};

const validateBudgetDependencies = async function (db, prospect) {
const { company, department } = await validateCompanyDepartment(db, prospect);
const departmentBudget = await db.Budget.findOne({
where: {
department: prospect.department,
Expand Down Expand Up @@ -55,5 +63,6 @@ const handleTransactionError = async function (err, transaction, errMessage, log

module.exports = {
handleTransactionError,
validateBudgetDependencies
validateBudgetDependencies,
validateCompanyDepartment
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ module.exports = {
type: Sequelize.DATE,
allowNull: false
},
user_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
references: {
model: 'users',
key: 'id',
onDelete: 'CASCADE'
}
},
department_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
Expand Down
7 changes: 7 additions & 0 deletions app/lib/db/models/expense.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ module.exports = function (sequelize, DataTypes) {
);

Expense.associate = (models) => {
Expense.belongsTo(models.User, {
foreignKey: {
allowNull: false,
name: 'user',
field: 'user_id'
}
});
Expense.belongsTo(models.Department, {
foreignKey: {
allowNull: false,
Expand Down
2 changes: 1 addition & 1 deletion test/api/auth/auth-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('AuthAPI', () => {

it('should throw a 401 if no user matches', async () => {
const credentials = {
email: 'unexisting@email.com',
email: 'inexisting@email.com',
password: 'wrongPassword'
};
await _failureLoginTest(credentials);
Expand Down
62 changes: 53 additions & 9 deletions test/api/budget-transaction/budget-transaction-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,50 @@ describe('BudgetTransactionAPI', () => {
expect(errThrown.message).to.eql('Budget transaction does not exist');
});

it('should throw a 404 if department does not exist', async () => {
const btAPI = createBudgetTransactionAPI();
let errThrown;
const badId = -1;
try {
await btAPI.query({ department: badId });
} catch (err) {
errThrown = err;
}
expect(errThrown).to.exist;
expect(errThrown.code).to.eql(404);
expect(errThrown.message).to.eql(`Department ${badId} does not exist`);
});

it('should throw a 404 if company does not exist', async () => {
const btAPI = createBudgetTransactionAPI();
let errThrown;
const badId = -1;
try {
await btAPI.query({ department: department.id, company: badId });
} catch (err) {
errThrown = err;
}
expect(errThrown).to.exist;
expect(errThrown.code).to.eql(404);
expect(errThrown.message).to.eql(`Company ${badId} does not exist`);
});

it('should throw a 404 if department does not belong to the company', async () => {
const btAPI = createBudgetTransactionAPI();
let errThrown;
const newCompany = await db.Company.create({
name: 'C2'
});
try {
await btAPI.query({ department: department.id, company: newCompany.id });
} catch (err) {
errThrown = err;
}
expect(errThrown).to.exist;
expect(errThrown.code).to.eql(404);
expect(errThrown.message).to.eql(`Department ${department.id} does not exist`);
});

it('should return a budget transaction by id', async () => {
const btAPI = createBudgetTransactionAPI();
const newBt = await db.BudgetTransaction.create({
Expand Down Expand Up @@ -196,7 +240,7 @@ describe('BudgetTransactionAPI', () => {
});
});

it('should throw 404 when creating budget transaction with an unexisting department', async () => {
it('should throw 404 when creating budget transaction with an inexisting department', async () => {
const btAPI = createBudgetTransactionAPI();
let errThrown;
const badId = -1;
Expand All @@ -215,7 +259,7 @@ describe('BudgetTransactionAPI', () => {
expect(errThrown.message).to.eql(`Department ${badId} does not exist`);
});

it('should throw 404 when creating budget transaction with an unexisting company', async () => {
it('should throw 404 when creating budget transaction with an inexisting company', async () => {
const btAPI = createBudgetTransactionAPI();
let errThrown;
const badId = -1;
Expand Down Expand Up @@ -311,7 +355,7 @@ describe('BudgetTransactionAPI', () => {
expect(dbBudget.expenses).to.eql(0);
});

it('should throw 404 when editing budget transaction with an unexisting department', async () => {
it('should throw 404 when editing budget transaction with an inexisting department', async () => {
const btAPI = createBudgetTransactionAPI();
let errThrown;
const badId = -1;
Expand All @@ -338,7 +382,7 @@ describe('BudgetTransactionAPI', () => {
expect(errThrown.message).to.eql(`Department ${badId} does not exist`);
});

it('should throw 404 when editing budget transaction with an unexisting company', async () => {
it('should throw 404 when editing budget transaction with an inexisting company', async () => {
const btAPI = createBudgetTransactionAPI();
let errThrown;
const badId = -1;
Expand Down Expand Up @@ -366,7 +410,7 @@ describe('BudgetTransactionAPI', () => {
expect(errThrown.message).to.eql(`Company ${badId} does not exist`);
});

it('should throw 404 when editing budget transaction with an unexisting company', async () => {
it('should throw 404 when editing budget transaction with an inexisting company', async () => {
const btAPI = createBudgetTransactionAPI();
let errThrown;
const newCompany = await db.Company.create({
Expand Down Expand Up @@ -396,7 +440,7 @@ describe('BudgetTransactionAPI', () => {
expect(errThrown.message).to.eql(`Department ${department.id} does not exist`);
});

it('should throw 404 when editing budget transaction with an unexisting budget transaction', async () => {
it('should throw 404 when editing budget transaction with an inexisting budget transaction', async () => {
const btAPI = createBudgetTransactionAPI();
let errThrown;
const badId = -1;
Expand Down Expand Up @@ -478,7 +522,7 @@ describe('BudgetTransactionAPI', () => {
expect(b.allocatedAmount).to.eql(0);
});

it('should throw 404 when removing budget transaction with an unexisting department', async () => {
it('should throw 404 when removing budget transaction with an inexisting department', async () => {
let errThrown;
const btAPI = createBudgetTransactionAPI();
const badId = -1;
Expand All @@ -498,7 +542,7 @@ describe('BudgetTransactionAPI', () => {
expect(errThrown.message).to.eql(`Department ${badId} does not exist`);
});

it('should throw 404 when removing budget transaction with an unexisting company', async () => {
it('should throw 404 when removing budget transaction with an inexisting company', async () => {
let errThrown;
const btAPI = createBudgetTransactionAPI();
const badId = -1;
Expand Down Expand Up @@ -540,7 +584,7 @@ describe('BudgetTransactionAPI', () => {
expect(errThrown.message).to.eql(`Department ${department.id} does not exist`);
});

it('should throw 404 when removing budget transaction with an unexisting budget transaction', async () => {
it('should throw 404 when removing budget transaction with an inexisting budget transaction', async () => {
let errThrown;
const btAPI = createBudgetTransactionAPI();
const badId = -1;
Expand Down
1 change: 1 addition & 0 deletions test/api/department/department-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ describe('DepartmentAPI', () => {
});
const expense = await db.Expense.create({
department,
user: user.id,
amount: 50,
concept: 'test concept',
date: moment.utc().startOf('day')
Expand Down

0 comments on commit 6d235ce

Please sign in to comment.