'use strict'; const chai = require('chai') const chaiHttp = require('chai-http') const should = chai.should(); const assert = require('assert'); 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 .use(bodyParser.json()) .use(bodyParser.urlencoded({ extended: true })) .configure(authentication()); chai.use(chaiHttp); describe('user service', () => { before((done) => { this.server = app.listen(3030) this.server.once('listening', () => { done() }) }) after((done) => { this.server.close(function(){}) done() }) it('registered the users service', () => { assert.ok(app.service('users')) }) it('should create a new user', (done) => { chai.request(app) .post('/users') .set('Accept', 'application/json') .send({ email: 'test@test.com', password: 'password', goal: 2000, }) .end((err, res) => { res.body.should.have.property('email') res.body.email.should.equal('test@test.com') res.body.should.not.have.property('password') res.body.goal.should.equal(2000) done() }) }) it('should authenticate a new user', (done) => { chai.request(app) .post('/auth/local') .set('Accept', 'application/json') .send({ email: 'test@test.com', password: 'password', }) .end((err, res) => { res.body.should.have.property('token') res.body.should.have.property('data') res.body.data.should.have.property('id') token = res.body.token userid = res.body.data.id done() }) }) it('user should be able to update itself', (done) => { chai.request(app) .patch('/users/'.concat(userid)) .set('Accept', 'application/json') .set('Authorization', 'Bearer '.concat(token)) .send({ goal: 2500, }) .end((err, res) => { res.body.goal.should.equal(2500) done() }) }) it('user should be able to destroy itself', (done) => { chai.request(app) .delete('/users/'.concat(userid)) .set('Accept', 'application/json') .set('Authorization', 'Bearer '.concat(token)) .send() .end((err, res) => { assert.equal(res.statusCode, 200); done() }) }) })