-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusers.spec.ts
194 lines (175 loc) · 6.03 KB
/
users.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { test, describe, expect, vi, beforeAll } from 'vitest';
import request from 'supertest';
import app from '../../src/index';
import prisma from '../../src/__mocks__/prisma';
import { userSeed } from './mocks/users.mock';
beforeAll(() => {
// Mock the prisma client
vi.mock('../../src/prisma');
// Mock the authenticateToken function
vi.mock('../../src/authenticateToken', () => {
return {
default: (req, res, next) => {
req.user = { id: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272' };
next();
},
};
});
});
describe('Get Users', () => {
test('No Users returned', async ({}) => {
prisma.user.findMany.mockResolvedValue([]);
const response = await request(app).get('/api/users');
expect(response.status).toBe(200);
expect(response.body).toStrictEqual([]);
});
test('Should get many users returned', async () => {
prisma.user.findMany.mockResolvedValue(userSeed);
const response = await request(app).get('/api/users');
expect(response.status).toBe(200);
const expectedResult = userSeed.map((item) => ({
...item,
createdAt: new Date(item.createdAt).toISOString(),
updatedAt: new Date(item.updatedAt).toISOString(),
}));
expect(response.body[0]).not.toHaveProperty('password');
expect(response.body).toEqual(expectedResult);
});
test('Network Error', async ({}) => {
prisma.user.findMany.mockImplementation(() => {
throw new Error('Test error');
});
const response = await request(app).get('/api/users');
expect(response.status).toBe(500);
expect(response.body).toStrictEqual({
error: 'Oops, something went wrong',
});
});
});
describe('Get user', () => {
test('GET user info for existing user', async () => {
prisma.user.findFirst.mockResolvedValue({
id: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
username: 'Dave',
password: 'check',
email: '[email protected]',
createdAt: new Date('2024-02-05T23:33:42.252Z'),
updatedAt: new Date('2024-02-05T23:33:42.252Z'),
});
const response = await request(app).get('/api/user');
expect(response.status).toBe(200);
expect(response.body).toStrictEqual({
id: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
username: 'Dave',
email: '[email protected]',
createdAt: '2024-02-05T23:33:42.252Z',
updatedAt: '2024-02-05T23:33:42.252Z',
});
});
test('Network Error', async ({}) => {
prisma.user.findFirst.mockImplementation(() => {
throw new Error('Test error');
});
const response = await request(app).get('/api/user');
expect(response.status).toBe(500);
expect(response.body).toStrictEqual({
error: 'Oops, something went wrong',
});
});
});
describe('Create User', () => {
test('POST with email, username and password', async ({}) => {
prisma.user.create.mockResolvedValue({
id: 'gcf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
email: '[email protected]',
username: 'Dave',
password: 'check',
updatedAt: new Date('2024-02-05T23:33:42.252Z'),
createdAt: new Date('2024-02-05T23:33:42.252Z'),
});
const response = await request(app)
.post('/api/users')
.send({ email: 'email', username: 'Dave', password: 'check' });
expect(response.status).toBe(200);
});
test('POST with existing username', async ({}) => {
prisma.user.findFirst.mockResolvedValue({
id: 'gcf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
email: 'email',
username: 'Dave',
password: 'check',
updatedAt: new Date('2024-02-05T23:33:42.252Z'),
createdAt: new Date('2024-02-05T23:33:42.252Z'),
});
const response = await request(app)
.post('/api/users')
.send({ email: 'email', username: 'Dave', password: 'check' });
expect(response.status).toBe(400);
expect(response.body).toStrictEqual({
error: 'Username or email already exists',
});
});
test('POST without email', async ({}) => {
const response = await request(app)
.post('/api/users')
.send({ username: 'Dave', password: 'check' });
expect(response.status).toBe(400);
expect(response.body).toStrictEqual({
error: 'email, password, and username fields required',
});
});
test('POST without username', async ({}) => {
const response = await request(app)
.post('/api/users')
.send({ email: '[email protected]', password: 'check' });
expect(response.status).toBe(400);
expect(response.body).toStrictEqual({
error: 'email, password, and username fields required',
});
});
test('POST without password', async ({}) => {
const response = await request(app)
.post('/api/users')
.send({ email: '[email protected]', username: 'check' });
expect(response.status).toBe(400);
expect(response.body).toStrictEqual({
error: 'email, password, and username fields required',
});
});
test('POST with error', async ({}) => {
prisma.user.create.mockImplementation(() => {
throw new Error('Test error');
});
const response = await request(app)
.post('/api/users')
.send({ email: 'email', username: 'Dave', password: 'check' });
expect(response.status).toBe(500);
expect(response.body).toStrictEqual({
error: 'Oops, something went wrong',
});
});
});
describe('Delete User', () => {
test('DELETE with id', async ({}) => {
prisma.user.delete.mockResolvedValue({
id: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
username: 'Dave',
password: 'check',
email: null,
createdAt: new Date('2024-02-05T23:33:42.252Z'),
updatedAt: new Date('2024-02-05T23:33:42.252Z'),
});
const response = await request(app).delete('/api/users/');
expect(response.status).toBe(204);
});
test('DELETE with error', async ({}) => {
prisma.user.delete.mockImplementation(() => {
throw new Error('Test error');
});
const response = await request(app).delete('/api/users/');
expect(response.status).toBe(500);
expect(response.body).toStrictEqual({
error: 'Oops, something went wrong',
});
});
});