/
auth.e2e-spec.ts
105 lines (87 loc) · 2.84 KB
/
auth.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
import { INestApplication } from '@nestjs/common';
import { prepareApp } from '../helper';
import request from '../helper/request';
describe('AuthController (e2e)', () => {
let app: INestApplication;
let server: any;
beforeAll(async () => {
app = await prepareApp([]);
server = app?.getHttpServer();
return;
});
describe('Post /auth/login', () => {
const subject = () => request(server).post('/auth/login');
it(`with right auth info, return 200`, async () => {
const response = await subject().send({
email: 'user.1@example.com',
password: 'AndyBobCharrie',
});
expect(response.status).toEqual(200);
expect(response.body).toMatchObject({
data: {
uuid: 'fa66f863-1040-48bd-a156-11bb7cce796e',
accessToken: expect.any(String),
refreshToken: expect.any(String),
},
});
});
it(`with wrong mail address, return 401: 1`, async () => {
const response = await subject().send({
email: 'wrong@example.com',
password: 'AndyBobCharrie',
});
expect(response.status).toEqual(401);
});
it(`with wrong password, return 401: 2`, async () => {
const response = await subject().send({
email: 'user.1@example.com',
password: 'wrongpassword',
});
expect(response.status).toEqual(401);
});
});
describe('Post /auth/refresh', () => {
const subject = () => request(server).post('/auth/refresh');
it(`with right refreshToken, return 200`, async () => {
const client = request(server);
const r1 = await client.post('/auth/login').send({
email: 'user.1@example.com',
password: 'AndyBobCharrie',
});
expect(r1.status).toEqual(200);
const { uuid, refreshToken } = r1.body.data;
const response = await client
.post('/auth/refresh')
.set('Cookie', `refreshToken=${refreshToken}`)
.send({
uuid,
});
expect(response.status).toEqual(200);
expect(response.body).toMatchObject({
data: {
uuid: 'fa66f863-1040-48bd-a156-11bb7cce796e',
accessToken: expect.any(String),
refreshToken: expect.any(String),
},
});
expect(response.body.data.refreshToken).not.toEqual(refreshToken);
});
it(`with wrong refreshToken, return 401: 1`, async () => {
const uuid = 'fa66f863-1040-48bd-a156-11bb7cce796e';
const response = await subject().withAuth().send({
uuid,
});
expect(response.status).toEqual(401);
});
});
describe('Delete /auth/refresh', () => {
const subject = () => request(server).delete('/auth/refresh');
it(`return 200`, async () => {
const response = await subject();
expect(response.status).toEqual(200);
});
});
afterAll(async () => {
await app.close();
});
});