|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { INestApplication } from '@nestjs/common'; |
| 3 | +import { AppModule } from '../src/app.module'; |
| 4 | +import { UsersService } from '../src/users/users.service'; |
| 5 | +import { requestWithAuth, haveUserLogged } from './preconditions'; |
| 6 | +import { ProjectsService } from '../src/projects/projects.service'; |
| 7 | +import uuidAPIKey from 'uuid-apikey'; |
| 8 | + |
| 9 | +const project = { |
| 10 | + id: uuidAPIKey.create().uuid, |
| 11 | + name: 'Test project' |
| 12 | +} |
| 13 | + |
| 14 | +const projectServiceMock = { |
| 15 | + findAll: () => ['test'], |
| 16 | + create: () => project, |
| 17 | + remove: () => project, |
| 18 | +}; |
| 19 | + |
| 20 | +describe('Projects (e2e)', () => { |
| 21 | + let app: INestApplication; |
| 22 | + let usersService: UsersService; |
| 23 | + let loggedUser |
| 24 | + |
| 25 | + beforeAll(async () => { |
| 26 | + const moduleFixture: TestingModule = await Test.createTestingModule({ |
| 27 | + imports: [AppModule], |
| 28 | + }) |
| 29 | + .overrideProvider(ProjectsService) |
| 30 | + .useValue(projectServiceMock) |
| 31 | + .compile(); |
| 32 | + |
| 33 | + app = moduleFixture.createNestApplication(); |
| 34 | + usersService = moduleFixture.get<UsersService>(UsersService) |
| 35 | + |
| 36 | + await app.init(); |
| 37 | + loggedUser = await haveUserLogged(usersService) |
| 38 | + }); |
| 39 | + |
| 40 | + afterAll(async () => { |
| 41 | + await app.close(); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('POST /', () => { |
| 45 | + it('200', () => { |
| 46 | + return requestWithAuth(app, 'post', '/projects', project, loggedUser.token) |
| 47 | + .expect(201) |
| 48 | + .expect(res => { |
| 49 | + expect(res.body.name).toBe(project.name); |
| 50 | + }) |
| 51 | + }); |
| 52 | + |
| 53 | + it('401', () => { |
| 54 | + return requestWithAuth(app, 'post', '/projects', project, '') |
| 55 | + .expect(401) |
| 56 | + }); |
| 57 | + }) |
| 58 | + |
| 59 | + describe('GET /', () => { |
| 60 | + it('200', async () => { |
| 61 | + const res = await requestWithAuth(app, 'get', '/projects', {}, loggedUser.token) |
| 62 | + .expect(200) |
| 63 | + |
| 64 | + expect(res.body).toEqual(expect.arrayContaining(projectServiceMock.findAll())) |
| 65 | + }); |
| 66 | + |
| 67 | + it('401', async () => { |
| 68 | + await requestWithAuth(app, 'get', '/projects', {}, '') |
| 69 | + .expect(401) |
| 70 | + }); |
| 71 | + }) |
| 72 | + |
| 73 | + |
| 74 | + describe('DELETE /', () => { |
| 75 | + |
| 76 | + it('can delete', async () => { |
| 77 | + const res = await requestWithAuth(app, 'delete', `/projects/${project.id}`, {}, loggedUser.token) |
| 78 | + .expect(200) |
| 79 | + |
| 80 | + expect(res.body).toStrictEqual(projectServiceMock.remove()) |
| 81 | + }) |
| 82 | + |
| 83 | + it('not valid UUID', async () => { |
| 84 | + await requestWithAuth(app, 'delete', `/projects/123`, {}, loggedUser.token) |
| 85 | + .expect(400) |
| 86 | + }) |
| 87 | + |
| 88 | + it('not valid token', async () => { |
| 89 | + await requestWithAuth(app, 'delete', `/projects/${project.id}`, {}, 'asd') |
| 90 | + .expect(401) |
| 91 | + }) |
| 92 | + }) |
| 93 | +}); |
0 commit comments