-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.controller.test.ts
64 lines (57 loc) · 1.84 KB
/
app.controller.test.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
import { beforeEach, describe, expect, it } from 'vitest';
import { AppController } from './app.controller';
import { TestContainer, Supertest } from '@spuxx/nest-testing';
import { AuthModule } from '@spuxx/nest-auth';
import { authConfig, AuthRole } from './auth/auth.config';
describe('AppController', () => {
let supertest: Supertest;
beforeEach(async () => {
const container = await TestContainer.create({
imports: [AuthModule.forRoot(authConfig)],
controllers: [AppController],
enableEndToEnd: true,
});
supertest = container.supertest;
});
describe('root', () => {
it('should be successful', async () => {
const response = await supertest.get('/');
expect(response.statusCode).toBe(200);
expect(response.body.message).toBe('Hello there!');
expect(response.body.session).toBe('Not logged in');
});
it('should indicate the current session', async () => {
const response = await supertest.get('/', {
session: {
sub: '123',
preferred_username: 'John Doe',
},
});
expect(response.statusCode).toBe(200);
expect(response.body.session).toBe('Logged in as John Doe');
});
});
describe('protected', () => {
it('should be successful', async () => {
const response = await supertest.get('/protected', {
session: {
sub: '123',
groups: [AuthRole.user],
},
});
expect(response.statusCode).toBe(200);
});
it('should return 401', async () => {
const response = await supertest.get('/protected');
expect(response.statusCode).toBe(401);
});
it('should return 403', async () => {
const response = await supertest.get('/protected', {
session: {
sub: '123',
},
});
expect(response.statusCode).toBe(403);
});
});
});