1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
'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) => {
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) => {
done()
})
})
})
|