|
| 1 | +import { randomBytes } from 'node:crypto'; |
| 2 | +import type { IncomingMessage, Server, ServerResponse } from 'node:http'; |
| 3 | + |
| 4 | +import type { NestExpressApplication } from '@nestjs/platform-express'; |
| 5 | +import request from 'supertest'; |
| 6 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 7 | + |
| 8 | +import appContainer from './app.js'; |
| 9 | + |
| 10 | +import type { BaseEnv } from '../src/schemas/env.schema.js'; |
| 11 | +import type { CreateCatDto } from './cats/dto/create-cat.dto.js'; |
| 12 | + |
| 13 | +const env = { |
| 14 | + API_PORT: '5500', |
| 15 | + DEBUG: 'false', |
| 16 | + MONGO_URI: 'mongodb://localhost:27017', |
| 17 | + NODE_ENV: 'test', |
| 18 | + SECRET_KEY: '2622d72669dd194b98cffd9098b0d04b', |
| 19 | + VERBOSE: 'false' |
| 20 | +} satisfies { [K in keyof BaseEnv]?: string }; |
| 21 | + |
| 22 | +describe('e2e (example)', () => { |
| 23 | + let app: NestExpressApplication; |
| 24 | + let server!: Server<typeof IncomingMessage, typeof ServerResponse>; |
| 25 | + |
| 26 | + beforeAll(async () => { |
| 27 | + Object.entries(env).forEach(([key, value]) => { |
| 28 | + vi.stubEnv(key, value); |
| 29 | + }); |
| 30 | + app = appContainer.getApplicationInstance(); |
| 31 | + await app.init(); |
| 32 | + server = app.getHttpServer(); |
| 33 | + }); |
| 34 | + |
| 35 | + afterAll(async () => { |
| 36 | + if (app) { |
| 37 | + await app.close(); |
| 38 | + app.flushLogs(); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + describe('/spec.json', () => { |
| 43 | + it('should configure the documentation', async () => { |
| 44 | + const response = await request(server).get('/spec.json'); |
| 45 | + expect(response.status).toBe(200); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('/auth/login', () => { |
| 50 | + it('should return status code 400 if the request body does not include login credentials', async () => { |
| 51 | + const response = await request(server).post('/v1/auth/login'); |
| 52 | + expect(response.status).toBe(400); |
| 53 | + }); |
| 54 | + it('should return status code 400 if the request body does not include a username', async () => { |
| 55 | + const response = await request(server).post('/v1/auth/login').send({ username: 'admin' }); |
| 56 | + expect(response.status).toBe(400); |
| 57 | + }); |
| 58 | + it('should return status code 400 if the request body does not include a password', async () => { |
| 59 | + const response = await request(server).post('/v1/auth/login').send({ password: 'password' }); |
| 60 | + expect(response.status).toBe(400); |
| 61 | + }); |
| 62 | + it('should return status code 400 if the request body includes a username and password, but are empty strings', async () => { |
| 63 | + const response = await request(server).post('/v1/auth/login').send({ password: '', username: '' }); |
| 64 | + expect(response.status).toBe(400); |
| 65 | + }); |
| 66 | + it('should return status code 400 if the request body includes a username and password, but password is a number', async () => { |
| 67 | + const response = await request(server).post('/v1/auth/login').send({ password: 123, username: 'admin' }); |
| 68 | + expect(response.status).toBe(400); |
| 69 | + }); |
| 70 | + it('should return status code 401 if the user does not exist', async () => { |
| 71 | + const response = await request(server).post('/v1/auth/login').send({ password: 'password', username: 'user' }); |
| 72 | + expect(response.status).toBe(401); |
| 73 | + }); |
| 74 | + it('should return status code 200 and an access token if the credentials are correct', async () => { |
| 75 | + const response = await request(server).post('/v1/auth/login').send({ password: 'password', username: 'admin' }); |
| 76 | + expect(response.status).toBe(200); |
| 77 | + expect(response.body).toStrictEqual({ |
| 78 | + accessToken: expect.stringMatching(/^[A-Za-z0-9-_]+\.([A-Za-z0-9-_]+)\.[A-Za-z0-9-_]+$/) |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('/cats', () => { |
| 84 | + let accessToken: string; |
| 85 | + |
| 86 | + beforeAll(async () => { |
| 87 | + const response = await request(server).post('/v1/auth/login').send({ password: 'password', username: 'admin' }); |
| 88 | + accessToken = response.body.accessToken; |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return status code 401 if there is no access token provided', async () => { |
| 92 | + const response = await request(server).get('/v1/cats'); |
| 93 | + expect(response.status).toBe(401); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should return status code 401 if there is an invalid access token provided', async () => { |
| 97 | + const response = await request(server) |
| 98 | + .get('/v1/cats') |
| 99 | + .set('Authorization', `Bearer ${randomBytes(12).toString('base64')}`); |
| 100 | + expect(response.status).toBe(401); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should allow a GET request', async () => { |
| 104 | + const response = await request(server).get('/v1/cats').set('Authorization', `Bearer ${accessToken}`); |
| 105 | + expect(response.status).toBe(200); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should allow a POST request', async () => { |
| 109 | + const response = await request(server) |
| 110 | + .post('/v1/cats') |
| 111 | + .set('Authorization', `Bearer ${accessToken}`) |
| 112 | + .send({ |
| 113 | + age: 1, |
| 114 | + name: 'Winston' |
| 115 | + } satisfies CreateCatDto); |
| 116 | + expect(response.status).toBe(201); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments