Skip to content

Commit

Permalink
add test for remove function
Browse files Browse the repository at this point in the history
  • Loading branch information
haipham23 committed Sep 30, 2017
1 parent aa0e678 commit 2a5d29e
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"require": true,
"module": true,
"after": true,
"before": true
"before": true,
"global": true
}
}
147 changes: 119 additions & 28 deletions src/controllers/productController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,34 +152,25 @@ describe('Product Controller', () => {
let INVALID_ACCOUNT_4;
let INVALID_ACCOUNT_5;

before((done) => {
productServices
.get('testUser')
.then((products) => {
productId =
products
.filter((p) => p.productName === 'valid product')[0]
.productId;

expect(productId).to.be.a.string;

VALID_PRODUCT = {
productName: 'updated product',
productId: productId,
productDesc: 'This is the test product update',
price: '200',
discount: '5',
currency: 'USD'
};

INVALID_ACCOUNT_1 = _.omit(VALID_PRODUCT, 'productName');
INVALID_ACCOUNT_2 = _.omit(VALID_PRODUCT, 'productDesc');
INVALID_ACCOUNT_3 = _.omit(VALID_PRODUCT, 'price');
INVALID_ACCOUNT_4 = _.omit(VALID_PRODUCT, 'discount');
INVALID_ACCOUNT_5 = _.omit(VALID_PRODUCT, 'currency');

done();
});
before(() => {
productId = global.TEST_PRODUCT.productId;

expect(productId).to.be.a.string;

VALID_PRODUCT = {
productName: 'updated product',
productId: productId,
productDesc: 'This is the test product update',
price: '200',
discount: '5',
currency: 'USD'
};

INVALID_ACCOUNT_1 = _.omit(VALID_PRODUCT, 'productName');
INVALID_ACCOUNT_2 = _.omit(VALID_PRODUCT, 'productDesc');
INVALID_ACCOUNT_3 = _.omit(VALID_PRODUCT, 'price');
INVALID_ACCOUNT_4 = _.omit(VALID_PRODUCT, 'discount');
INVALID_ACCOUNT_5 = _.omit(VALID_PRODUCT, 'currency');
});


Expand Down Expand Up @@ -289,4 +280,104 @@ describe('Product Controller', () => {
});
});
});


describe('remove() function', () => {
let productId;
let VALID_PRODUCT;
let INVALID_PRODUCT_1;
let INVALID_PRODUCT_2;

before(() => {
productId = global.TEST_PRODUCT.productId;

expect(productId).to.be.a.string;

VALID_PRODUCT = {
productName: 'updated product',
productId: productId,
productDesc: 'This is the test product update',
price: '200',
discount: '5',
currency: 'USD'
};

INVALID_PRODUCT_1 = _.omit(VALID_PRODUCT, 'productId');
INVALID_PRODUCT_2 = Object.assign(
{},
VALID_PRODUCT,
{ productId: '123' }
);
});


it('should fail to update - missing apiKey', (done) => {
request(app)
.delete(PRODUCT_URL)
.send(VALID_PRODUCT)
.end((err, res) => {
expect(err).to.not.exist;
expect(res.status).to.equal(400);
expect(res.body).to.equal('REMOVE_PRODUCT_FAILED');
done();
});
});

it('should fail to update - missing productId', (done) => {
request(app)
.delete(PRODUCT_URL)
.set('x-api-key', apiKey)
.send(INVALID_PRODUCT_1, {})
.end((err, res) => {
expect(err).to.not.exist;
expect(res.status).to.equal(400);
expect(res.body).to.equal('REMOVE_PRODUCT_FAILED');
done();
});
});


it('should fail to update - invalid productId', (done) => {
request(app)
.delete(PRODUCT_URL)
.set('x-api-key', apiKey)
.send(INVALID_PRODUCT_2, {})
.end((err, res) => {
expect(err).to.not.exist;
expect(res.status).to.equal(400);
expect(res.body).to.equal('REMOVE_PRODUCT_FAILED');
done();
});
});


it('should succeed', (done) => {
request(app)
.delete(PRODUCT_URL)
.set('x-api-key', apiKey)
.send(VALID_PRODUCT)
.end((err, res) => {
expect(err).to.not.exist;
expect(res.status).to.equal(200);
expect(res.body).to.equal('OK');
done();
});
});


after((done) => {
productServices
.get('testUser')
.then((products) => {
const isVisible =
products
.filter((p) => p.productId === productId)
.length > 0;

expect(isVisible).to.be.false;

done();
});
});
});
});
1 change: 1 addition & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const route = (app) => {
router.get('/store/products', productController.getAll);
router.post('/store/products', productController.add);
router.put('/store/products', productController.update);
router.delete('/store/products', productController.remove);

/* General */
app.use('/api', router);
Expand Down
4 changes: 0 additions & 4 deletions src/services/accountServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ function getUsername(token) {
return new Promise((resolve, reject) => {
const username = jwt.verify(token);

if (!username) {
return reject('Invalid Token');
}

return resolve(username);
});
}
Expand Down
6 changes: 5 additions & 1 deletion src/services/productServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async function update(body) {
async function remove(body) {
const product = new Product(body);

await db
const result = await db
.get(PRODUCTS_COLLECTION)
.findOneAndUpdate({
owner: product.owner,
Expand All @@ -111,6 +111,10 @@ async function remove(body) {

db.close();

if (result.lastErrorObject) {
throw new Error(INVALID_PROPS);
}

return OK;
}

Expand Down
14 changes: 12 additions & 2 deletions src/setup/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ describe('=== prepare ===', () => {

accounts
.insert(validUser)
.then(() => products.insert(validProduct))
.then(() => db.close(done));
.then((account) => {
global.TEST_ACCOUNT = account;

return products.insert(validProduct);
})
.then((product) => {
global.TEST_PRODUCT = product;

done();
});
});

after(() => db.close());
});

0 comments on commit 2a5d29e

Please sign in to comment.