-
Notifications
You must be signed in to change notification settings - Fork 2
/
create.spec.js
88 lines (77 loc) · 2.76 KB
/
create.spec.js
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
'use strict'
import faker from 'faker'
import { describe, it } from 'mocha'
import { expect, request } from 'chai'
import { User } from '../../models/user'
import config from '../../../config'
import * as jwt from 'jsonwebtoken'
let user = {}
let token = ''
const agent = request.agent(require('../../server'))
const createToken = () => {
return jwt.sign({}, config.jwt.secret)
}
/* eslint-disable no-undef */
describe('Passwords: controllers', () => {
context('/password', () => {
beforeEach(async () => {
user = await User.register({
password: faker.internet.password(),
email: faker.internet.email().toLowerCase()
})
token = createToken()
})
it('POST. Should throw exception if body is not provided', async () => {
try {
await agent.post(`/password`)
} catch (error) {
expect(error).to.have.status(400)
expect(error.response.body).to.have.all.keys('code', 'message')
expect(error.response.body.message).to.have.all.keys('warn', 'context')
}
})
it('POST. Should throw exception if email is not provided', async () => {
try {
await agent.post(`/password`).send({ payload: {} })
} catch (error) {
expect(error).to.have.status(400)
expect(error.response.body).to.have.all.keys('code', 'message')
expect(error.response.body.message).to.have.all.keys('warn', 'context')
}
})
it('POST. Should throw exception if email invalid', async () => {
try {
await agent.post(`/password`).send({ payload: { email: 'email' } })
} catch (error) {
expect(error).to.have.status(400)
expect(error.response.body).to.have.all.keys('code', 'message')
expect(error.response.body.message).to.have.all.keys('warn', 'context')
}
})
it('POST. Should throw exception if token invalid', async () => {
try {
await agent.post(`/password?token=abc123`)
.send({ payload: { email: user.email } })
} catch (error) {
expect(error).to.have.status(401)
}
})
it('POST. Should throw exception if email not have confirmed', async () => {
try {
await agent.post(`/password?token=${token}`)
.send({ payload: { email: user.email } })
.set('accept-language', 'pt-BR')
} catch (error) {
expect(error).to.have.status(400)
expect(error.response.body.code).to.be.equal('BadRequest')
expect(error.response.body.message).to.be.equal('Conta não confirmada')
}
})
it('POST. 200 ok', async () => {
await User.confirm(user.confirmationToken)
const res = await agent.post(`/password?token=${token}`)
.send({ payload: { email: user.email } })
expect(res).to.have.status(204)
})
})
})