summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2017-03-17 00:25:32 +0100
committerJules Laplace <jules@okfoc.us>2017-03-17 00:25:32 +0100
commita4b496b8e7cb215dc9be2fa0e627382364889eb4 (patch)
treeef49beb0b7f76d938c864e7712ace7652d3a02bb
parent927b907518ba83ae68718d62bd3095c97fd9a3fb (diff)
meal tests
-rw-r--r--src/services/user/hooks/index.js20
-rw-r--r--test/services/meal/index.test.js108
-rw-r--r--test/services/user/index.test.js1
3 files changed, 122 insertions, 7 deletions
diff --git a/src/services/user/hooks/index.js b/src/services/user/hooks/index.js
index 85f8e04..c4ed466 100644
--- a/src/services/user/hooks/index.js
+++ b/src/services/user/hooks/index.js
@@ -69,6 +69,21 @@ function validateRoleOnUpdate () {
}
}
+function removeUserMeals () {
+ return function(hook) {
+ var _this = this;
+
+ return new Promise(function (resolve, reject) {
+ // Set provider as undefined so we avoid an infinite loop if this hook is
+ // set on the resource we are requesting.
+ var params = Object.assign({}, hook.params, { provider: undefined });
+ return hook.app.service('meals').remove(null, { userid: hook.userid }).then(function (data) {
+ resolve(hook);
+ }).catch(reject);
+ });
+ }
+}
+
const roleConfig = {
fieldName: 'role',
roles: ['manager','admin'],
@@ -112,6 +127,7 @@ exports.before = {
auth.populateUser(),
auth.restrictToAuthenticated(),
validateRoleOnUpdate(),
+ removeUserMeals(),
]
};
@@ -122,7 +138,5 @@ exports.after = {
create: [],
update: [],
patch: [],
- remove: [
- // remove user's meals
- ],
+ remove: [],
};
diff --git a/test/services/meal/index.test.js b/test/services/meal/index.test.js
index 92dd70f..c01ba93 100644
--- a/test/services/meal/index.test.js
+++ b/test/services/meal/index.test.js
@@ -1,10 +1,110 @@
'use strict';
+const chai = require('chai')
+const chaiHttp = require('chai-http')
+const should = chai.should();
const assert = require('assert');
const app = require('../../../src/app');
-describe('meal service', function() {
+const User = app.service('users')
+const Meal = app.service('meals')
+const authentication = require('feathers-authentication/client');
+const bodyParser = require('body-parser');
+
+var token, userid, mealid
+
+app
+ .use(bodyParser.json())
+ .use(bodyParser.urlencoded({ extended: true }))
+ .configure(authentication());
+chai.use(chaiHttp);
+
+
+describe('meal service', () => {
+ before((done) => {
+ this.server = app.listen(3030)
+ this.server.once('listening', () => {
+ User.create({
+ email: 'test@test.com',
+ password: 'password',
+ goal: 2000,
+ }, () => {
+ chai.request(app)
+ .post('/auth/local')
+ .set('Accept', 'application/json')
+ .send({
+ email: 'test@test.com',
+ password: 'password',
+ })
+ .end((err, res) => {
+ token = res.body.token
+ userid = res.body.data.id
+ done()
+ })
+ })
+ })
+ })
+
+ after((done) => {
+ chai.request(app)
+ .delete('/users/'.concat(userid))
+ .set('Accept', 'application/json')
+ .set('Authorization', 'Bearer '.concat(token))
+ .send()
+ .end((err, res) => {
+ this.server.close(function(){})
+ done()
+ })
+ })
+
it('registered the meals service', () => {
- assert.ok(app.service('meals'));
- });
-});
+ assert.ok(app.service('meals'))
+ })
+
+ it('should create a new meal', (done) => {
+ chai.request(app)
+ .post('/meals')
+ .set('Accept', 'application/json')
+ .set('Authorization', 'Bearer '.concat(token))
+ .send({
+ name: 'breakfast',
+ date: new Date('Thu Mar 16 2017 22:00:00 GMT+0100 (CET)'),
+ calories: 500,
+ userid: userid,
+ })
+ .end((err, res) => {
+ res.body.should.have.property('name')
+ res.body.name.should.equal('breakfast')
+ res.body.calories.should.equal(500)
+ mealid = res.body.id
+ done()
+ })
+ })
+
+ it('should update an existing meal', (done) => {
+ chai.request(app)
+ .patch('/meals/'.concat(mealid))
+ .set('Accept', 'application/json')
+ .set('Authorization', 'Bearer '.concat(token))
+ .send({
+ calories: 650,
+ })
+ .end((err, res) => {
+ res.body.calories.should.equal(650)
+ done()
+ })
+ })
+
+ it('should be able to destroy a meal', (done) => {
+ chai.request(app)
+ .delete('/meals/'.concat(mealid))
+ .set('Accept', 'application/json')
+ .set('Authorization', 'Bearer '.concat(token))
+ .send()
+ .end((err, res) => {
+ assert.equal(res.statusCode, 200);
+ done()
+ })
+ })
+
+})
diff --git a/test/services/user/index.test.js b/test/services/user/index.test.js
index 4302422..a9072c0 100644
--- a/test/services/user/index.test.js
+++ b/test/services/user/index.test.js
@@ -9,6 +9,7 @@ const app = require('../../../src/app');
const User = app.service('users')
const authentication = require('feathers-authentication/client');
const bodyParser = require('body-parser');
+
var token, userid
app