-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathUserController.e2e.spec.ts
172 lines (153 loc) · 5.29 KB
/
UserController.e2e.spec.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { injectDelete, injectGet, injectPatch, injectPost } from '@lokalise/fastify-api-contracts'
import { DB_MODEL, cleanTables } from '../../../../test/DbCleaner.ts'
import { getTestConfigurationOverrides } from '../../../../test/jwtUtils.ts'
import type { AppInstance } from '../../../app.ts'
import { getApp } from '../../../app.ts'
import { generateJwtToken } from '../../../infrastructure/tokenUtils.ts'
import type { UserRepository } from '../repositories/UserRepository.ts'
import type { UserCreateDTO } from '../services/UserService.ts'
import { UserController } from './UserController.ts'
const NEW_USER_FIXTURE = { name: 'dummy', email: '[email protected]' } satisfies UserCreateDTO
describe('UserController', () => {
let app: AppInstance
let userRepository: UserRepository
beforeAll(async () => {
app = await getApp(getTestConfigurationOverrides())
userRepository = app.diContainer.cradle.userRepository
})
beforeEach(async () => {
await cleanTables(app.diContainer.cradle.drizzle, [DB_MODEL.User])
})
afterAll(async () => {
await app.close()
})
describe('POST /users', () => {
it('validates email format', async () => {
const token = await generateJwtToken(app.jwt, { userId: 1 }, 9999)
const response = await injectPost(app, UserController.contracts.createUser, {
headers: {
authorization: `Bearer ${token}`,
},
body: { name: 'dummy', email: 'test' },
})
expect(response.statusCode).toBe(400)
expect(response.json()).toMatchInlineSnapshot(`
{
"details": {
"error": [
{
"instancePath": "/email",
"keyword": "invalid_string",
"message": "Invalid email",
"params": {
"issue": {
"code": "invalid_string",
"message": "Invalid email",
"path": [
"email",
],
"validation": "email",
},
},
"schemaPath": "#/email/invalid_string",
},
],
},
"errorCode": "VALIDATION_ERROR",
"message": "Invalid params",
}
`)
})
it('creates user with correct payload', async () => {
const token = await generateJwtToken(app.jwt, { userId: 1 }, 9999)
const response = await injectPost(app, UserController.contracts.createUser, {
headers: {
authorization: `Bearer ${token}`,
},
body: NEW_USER_FIXTURE,
})
expect(response.statusCode).toBe(201)
expect(response.json()).toEqual({
data: {
age: null,
email: '[email protected]',
id: expect.any(String),
name: 'dummy',
},
})
})
})
describe('GET /users/:userId', () => {
it('returns user when requested twice', async () => {
const token = await generateJwtToken(app.jwt, { userId: '1' }, 9999)
const newUser = await userRepository.createUser(NEW_USER_FIXTURE)
const { id } = newUser
const response1 = await injectGet(app, UserController.contracts.getUser, {
headers: {
authorization: `Bearer ${token}`,
},
pathParams: {
userId: id,
},
})
const response2 = await injectGet(app, UserController.contracts.getUser, {
headers: {
authorization: `Bearer ${token}`,
},
pathParams: {
userId: id,
},
})
expect(response1.statusCode).toBe(200)
expect(response2.statusCode).toBe(200)
expect(response1.json().data).toMatchObject(NEW_USER_FIXTURE)
expect(response2.json().data).toMatchObject(NEW_USER_FIXTURE)
})
})
describe('DELETE /users/:userId', () => {
it('resets cache after deletion', async () => {
const token = await generateJwtToken(app.jwt, { userId: '1' }, 9999)
const newUser = await userRepository.createUser(NEW_USER_FIXTURE)
const { id } = newUser
const retrievedUser = await userRepository.getUser(id)
await injectDelete(app, UserController.contracts.deleteUser, {
headers: {
authorization: `Bearer ${token}`,
},
pathParams: {
userId: id,
},
})
const retrievedUser2 = await userRepository.getUser(id)
expect(retrievedUser).toBeDefined()
expect(retrievedUser2).toBeNull()
})
})
describe('PATCH /users/:userId', () => {
it('resets cache after update', async () => {
const token = await generateJwtToken(app.jwt, { userId: 1 }, 9999)
const newUser = await userRepository.createUser(NEW_USER_FIXTURE)
const { id } = newUser
const updateResponse = await injectPatch(app, UserController.contracts.updateUser, {
body: {
name: 'updated',
},
pathParams: {
userId: id,
},
headers: {
authorization: `Bearer ${token}`,
},
})
const retrievedUser2 = await userRepository.getUser(id)
expect(updateResponse.statusCode).toBe(204)
expect(retrievedUser2).toEqual({
email: '[email protected]',
age: null,
id,
name: 'updated',
})
})
})
})