This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateLegacyUserApi.test.js
147 lines (132 loc) · 4.39 KB
/
createLegacyUserApi.test.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
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
/* eslint-env jest */
const createLegacyUserApi = require('./createLegacyUserApi')
const request = require('supertest')
const express = require('express')
describe('legacy users', () => {
const API_KEY = '__dummy_api_key__'
const DUMMY_USER = {
_id: 'zzz',
username: 'ABC',
email: 'abc@test.test',
createdAt: new Date(0),
hashedPassword: '$2a$08$slf.HjrpyEjFgg/HvVW0FuWzCoRNI8eW0Ei4PM.5o6ImHt7lA/Xze'
}
const DUMMY_PLAYER = {
_id: 'playerZ',
playerName: 'ABC'
}
let app
beforeAll(() => {
const api = createLegacyUserApi({
legacyUserApiKey: API_KEY,
playerRepository: {
findByName: (playerName) => Promise.resolve(
playerName === DUMMY_PLAYER.playerName ? DUMMY_PLAYER : null
),
findById: (playerId) => Promise.resolve(
playerId === DUMMY_PLAYER._id ? DUMMY_PLAYER : null
)
},
legacyUserRepository: {
findByEmail: (email) => Promise.resolve(
email === DUMMY_USER.email ? DUMMY_USER : null
),
findByUsername: (username) => Promise.resolve(
username === DUMMY_USER.username ? DUMMY_USER : null
)
}
})
app = express()
app.use('/legacyusers', api)
})
describe('authentication', () => {
it('can authenticate using player ID', () => {
return request(app)
.post('/legacyusers/check')
.type('form').send({ playerIdOrEmail: 'playerZ', password: 'meow', apiKey: API_KEY })
.expect(200)
})
it('can authenticate using email', () => {
return request(app)
.post('/legacyusers/check')
.type('form').send({ playerIdOrEmail: 'abc@test.test', password: 'meow', apiKey: API_KEY })
.expect(200)
})
describe('a successful request', () => {
function successfulRequest () {
return request(app)
.post('/legacyusers/check')
.type('form').send({ playerIdOrEmail: 'abc@test.test', password: 'meow', apiKey: API_KEY })
.expect(200)
}
itContainsUserData(successfulRequest)
})
it('returns 401 if user not found', () => {
return request(app)
.post('/legacyusers/check')
.type('form').send({ playerIdOrEmail: 'ABCX', password: 'meow', apiKey: API_KEY })
.expect(401)
})
it('returns 401 if user password incorrect', () => {
return request(app)
.post('/legacyusers/check')
.type('form').send({ playerIdOrEmail: 'abc@test.test', password: 'meoww', apiKey: API_KEY })
.expect(401)
})
it('returns 400 if bad api key', () => {
return request(app)
.post('/legacyusers/check')
.type('form').send({ playerIdOrEmail: 'abc@test.test', password: 'meow', apiKey: 'bad' })
.expect(400)
})
})
describe('get user', () => {
it('can get using email', () => {
return request(app)
.post('/legacyusers/get')
.type('form').send({ playerIdOrEmail: 'abc@test.test', apiKey: API_KEY })
.expect(200)
})
it('can get using player ID', () => {
return request(app)
.post('/legacyusers/get')
.type('form').send({ playerIdOrEmail: 'playerZ', apiKey: API_KEY })
.expect(200)
})
describe('a successful request', () => {
function successfulRequest () {
return request(app)
.post('/legacyusers/get')
.type('form').send({ playerIdOrEmail: 'abc@test.test', apiKey: API_KEY })
.expect(200)
}
itContainsUserData(successfulRequest)
})
it('returns 404 if user not found', () => {
return request(app)
.post('/legacyusers/get')
.type('form').send({ playerIdOrEmail: 'xyz@test.test', apiKey: API_KEY })
.expect(404)
})
it('returns 400 if bad api key', () => {
return request(app)
.post('/legacyusers/get')
.type('form').send({ playerIdOrEmail: 'abc@test.test', apiKey: 'bad' })
.expect(400)
})
})
function itContainsUserData (successfulRequest) {
it('returns email', () => {
return successfulRequest().expect(/"email":"abc@test.test"/)
})
it('returns player ID as username', () => {
return successfulRequest().expect(/"username":"playerZ"/)
})
it('returns parse ID', () => {
return successfulRequest().expect(/"_id":"zzz"/)
})
it('returns created at', () => {
return successfulRequest().expect(/"createdAt":"1970-/)
})
}
})