-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathauth.js
More file actions
157 lines (125 loc) · 3.94 KB
/
auth.js
File metadata and controls
157 lines (125 loc) · 3.94 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import request from 'supertest'
import app from '../../src/app'
import * as database from '../../src/database'
import config from '../../src/config/server'
import {
badEmail,
badUnknownEmail,
badKnownEmail,
badKnownName,
goodRegister,
goodToken,
goodUserUpdate,
badRegistrationsDisabled,
} from '@rctf/api-types/responses'
import * as auth from '../../src/auth'
import { removeUserByEmail } from '../../src/database/users'
import { generateTestUser } from '../_util'
const testUser = generateTestUser()
let oldEmail = config.email
beforeAll(async () => {
oldEmail = config.email
await app.ready()
})
afterAll(async () => {
config.email = oldEmail
await removeUserByEmail({
email: testUser.email,
})
})
test('fails with badEmail', async () => {
const resp = await request(app.server)
.post(process.env.API_ENDPOINT + '/auth/register')
.send({
...testUser,
email: 'notanemail',
})
.expect(badEmail.status)
expect(resp.body.kind).toBe('badEmail')
})
test('fails with badUnknownEmail', async () => {
config.email = oldEmail
const unknownEmail = 'non-existent-email' + Math.random() + '@gmail.com'
const resp = await request(app.server)
.post(process.env.API_ENDPOINT + '/auth/recover')
.send({
email: unknownEmail,
})
.expect(badUnknownEmail.status)
expect(resp.body.kind).toBe('badUnknownEmail')
})
test('when not email, succeeds with goodRegister', async () => {
config.email = null
let resp = await request(app.server)
.post(process.env.API_ENDPOINT + '/auth/register')
.send(testUser)
.expect(goodRegister.status)
expect(resp.body.kind).toBe('goodRegister')
expect(typeof resp.body.data.authToken === 'string').toBe(true)
resp = await request(app.server)
.get(process.env.API_ENDPOINT + '/auth/test')
.set('Authorization', ' Bearer ' + resp.body.data.authToken)
.expect(goodToken.status)
expect(resp.body.kind).toBe('goodToken')
})
test('duplicate email fails with badKnownEmail', async () => {
config.email = null
const resp = await request(app.server)
.post(process.env.API_ENDPOINT + '/auth/register')
.send({
...testUser,
name: String(Math.random()),
})
.expect(badKnownEmail.status)
expect(resp.body.kind).toBe('badKnownEmail')
})
test('duplicate name fails with badKnownName', async () => {
config.email = null
const resp = await request(app.server)
.post(process.env.API_ENDPOINT + '/auth/register')
.send({
...testUser,
email: 'non-existent-email' + String(Math.random()) + '@gmail.com',
})
.expect(badKnownName.status)
expect(resp.body.kind).toBe('badKnownName')
})
test('succeeds with goodUserUpdate', async () => {
config.email = null
const user = await database.users.getUserByEmail({
email: testUser.email,
})
const nextUser = generateTestUser()
const authToken = await auth.token.getToken(
auth.token.tokenKinds.auth,
user.id
)
const resp = await request(app.server)
.patch(process.env.API_ENDPOINT + '/users/me')
.set('Authorization', ' Bearer ' + authToken)
.send({
name: nextUser.name,
division: nextUser.division,
})
.expect(goodUserUpdate.status)
const respUser = resp.body.data.user
testUser.name = respUser.name
testUser.email = respUser.email
testUser.division = respUser.division
expect(resp.body.kind).toBe('goodUserUpdate')
expect(respUser.name).toBe(nextUser.name)
expect(respUser.email).toBe(testUser.email)
expect(respUser.division).toBe(nextUser.division)
})
test('fails with badRegistrationsDisabled', async () => {
const oldRegistrations = config.registrationsEnabled
config.registrationsEnabled = false
const resp = await request(app.server)
.post(process.env.API_ENDPOINT + '/auth/register')
.send({
...testUser,
})
.expect(badRegistrationsDisabled.status)
expect(resp.body.kind).toBe('badRegistrationsDisabled')
config.registrationsEnabled = oldRegistrations
})