diff --git a/src/army-list/army-list.controller.ts b/src/army-list/army-list.controller.ts index 845ad18..b202d61 100644 --- a/src/army-list/army-list.controller.ts +++ b/src/army-list/army-list.controller.ts @@ -45,6 +45,10 @@ import { ArmyService } from "@army/army.service"; import { Army } from "@army/army.entity"; import { UnitService } from "@army/unit/unit.service"; +type Id = { + id: string; +} + @Controller("armies-lists") export class ArmyListController { constructor( @@ -68,7 +72,7 @@ export class ArmyListController { @Body("valuePoints") valuePoints: number, @Body("units") units: ArmyListUnitCredentialsDTO[], @Body("isShared") isShared: boolean, - @Body("isFavorite") isFavorite: boolean) { + @Body("isFavorite") isFavorite: boolean): Promise { if (!ParamHelper.isValid(name) || !ParamHelper.isValid(armyId) || !ParamHelper.isValid(valuePoints) || !ParamHelper.isValid(units) || !ParamHelper.isValid(isFavorite)) { throw new BadRequestException(); @@ -83,20 +87,19 @@ export class ArmyListController { try { await this.armyListService.save(list); list.units = await this.saveUnits(list.id, units, list); + return { id: list.id }; } catch (error) { console.error(error); if (error instanceof QueryFailedError) { throw new NotFoundException(`The army ${list.armyId} was not found`); } - } - // await this.saveUnits(list.id, units); } @UseGuards(JwtAuthGuard) @Get("") @HttpCode(HttpStatus.OK) - async lookup(@Request() req) { + async lookup(@Request() req): Promise { const lists: ArmyList[] = await this.armyListService.findByOwner(req.user.username); let credentials: ArmyListCredentialsDTO[] = []; @@ -109,7 +112,7 @@ export class ArmyListController { @UseGuards(JwtAuthGuard) @Get(":id") @HttpCode(HttpStatus.OK) - async get(@Request() req, @Param("id") id: string) { + async get(@Request() req, @Param("id") id: string): Promise { let list: ArmyList = await this.armyListService.findOneById(id, { loadAll: true }); if (list === null) { @@ -124,7 +127,7 @@ export class ArmyListController { @UseGuards(JwtAuthGuard) @Delete("/:id") @HttpCode(HttpStatus.OK) - async delete(@Request() req, @Param("id") id: string) { + async delete(@Request() req, @Param("id") id: string): Promise { let list: ArmyList = await this.armyListService.findOneById(id); if (list === null) { @@ -146,7 +149,7 @@ export class ArmyListController { @Body("valuePoints") valuePoints: number, @Body("units") units: ArmyListUnitCredentialsDTO[], @Body("isShared") isShared: boolean, - @Body("isFavorite") isFavorite) { + @Body("isFavorite") isFavorite): Promise { let list: ArmyList = await this.armyListService.findOneById(id, { loadUnits: true}); if (list === null) { diff --git a/tests/account/profile/profile.service.spec.ts b/tests/account/profile/profile.service.spec.ts index 3cb1e1e..bd5ad3e 100644 --- a/tests/account/profile/profile.service.spec.ts +++ b/tests/account/profile/profile.service.spec.ts @@ -2,29 +2,30 @@ import { Test } from "@nestjs/testing"; import { TypeOrmModule } from "@nestjs/typeorm"; import * as dotenv from "dotenv"; import * as bcrypt from "bcrypt"; +import { faker } from "@faker-js/faker"; import { AccountType, Profile } from "@account/profile/profile.entity"; import { ProfileService } from "@account/profile/profile.service"; import { ProfileModule } from "@account/profile/profile.module"; -import { ArmyList } from "@army-list/army-list.entity"; +dotenv.config(); -dotenv.config() +jest.setTimeout(30000); -const DB = process.env.POSTGRES_DB; -const DB_HOST = process.env.DATABASE_IP; +const DB: string = process.env.POSTGRES_DB; +const DB_HOST: string = process.env.DATABASE_IP; const DB_PORT: number = +process.env.DATABASE_PORT; -const DB_USERNAME = process.env.POSTGRES_USER; -const DB_PASSWORD = process.env.POSTGRES_PASSWORD; -const DB_DIALECT = "postgres" +const DB_USERNAME: string = process.env.POSTGRES_USER; +const DB_PASSWORD: string = process.env.POSTGRES_PASSWORD; +const DB_DIALECT = "postgres"; -const USERNAME = "username"; -const EMAIL = "email@prophecy.com" -const PASSWORD = "password"; +const USERNAME: string = faker.internet.userName(); +const EMAIL: string = faker.internet.email(); +const PASSWORD: string = faker.internet.password(); -const USERNAME1 = "username1"; -const EMAIL1 = "email1@prophecy.com"; -const PASSWORD1 = "password1"; +const USERNAME1: string = faker.internet.userName(); +const EMAIL1: string = faker.internet.email(); +const PASSWORD1: string = faker.internet.password(); function initDefaultProfile(username: string, email: string, password: string): Profile { const profile = new Profile(); @@ -44,16 +45,17 @@ describe("ProfileService", () => { beforeAll(async () => { const moduleRef = await Test.createTestingModule(({ imports: [TypeOrmModule.forRoot({ - type: DB_DIALECT, - host: DB_HOST, - port: DB_PORT, - username: DB_USERNAME, - password: DB_PASSWORD, - database: DB, - entities: [Profile, ArmyList], - synchronize: true, - }), - ProfileModule], + type: DB_DIALECT, + host: DB_HOST, + port: DB_PORT, + username: DB_USERNAME, + password: DB_PASSWORD, + database: DB, + entities: [Profile], + synchronize: true, + }), + ProfileModule, + ], })).compile(); service = moduleRef.get(ProfileService); diff --git a/tests/api/army-list/army-list.spec.ts b/tests/api/army-list/army-list.spec.ts deleted file mode 100644 index bce6d85..0000000 --- a/tests/api/army-list/army-list.spec.ts +++ /dev/null @@ -1,312 +0,0 @@ -import { Test, TestingModule } from "@nestjs/testing"; -import { HttpStatus, INestApplication } from "@nestjs/common"; -import * as request from "supertest"; -import { faker } from "@faker-js/faker"; - -import { AppModule } from "../../../src/app.module"; -import { TestsHelper } from "../../tests.helper"; -import { ARMY1, ARMY2, List } from "../../fixtures/army-list/armies-lists"; -import ArmyListHelper from "../../helper/army-list.helper"; - -jest.setTimeout(100000000); - -const LIST_NAME: string = "my list"; -const ARMY_ID: string = "3557-241b-5999-a3b1"; -const COST: string = "50 pts"; -const UNIT_ID: string = "46e2-fcb1-5337-a50e"; -const OPTIONS_ID: string[] = ["0a9c-5e46-6e74-afae"]; -const UPGRADES_ID: string[] = ["8534-d1f1-73a1-48e9"]; -const NUMBER: number = 3; -const FORMATION = "3x1"; -const IS_SHARED: boolean = false; - -const USERNAME = faker.internet.userName(); -const EMAIL = faker.internet.email(); -const PASSWORD = faker.internet.password(); - -const USERNAME1 = faker.internet.userName(); -const EMAIL1 = faker.internet.email(); -const PASSWORD1 = faker.internet.password(); - -let app: INestApplication; -let token: string; -let token1: string; - -describe("Armies lists route", () => { - - beforeAll(async () => { - const module: TestingModule = await Test.createTestingModule({ - imports: [AppModule] - }).compile(); - - app = module.createNestApplication(); - await app.init(); - - token = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME, EMAIL, - PASSWORD); - token1 = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME1, EMAIL1, - PASSWORD); - }); - - afterAll(async () => { - const res = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - - for (const a of res.body) { - await request(app.getHttpServer()) - .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${a.id}`) - .set("Authorization", `Bearer ${token}`); - } - await TestsHelper.deleteAccount(app.getHttpServer(), token); - await TestsHelper.deleteAccount(app.getHttpServer(), token1); - }); - - /** - * CREATE - */ - it("create: create basic lists - then should return 201 (created)", async () => { - const res1 = await request(app.getHttpServer()) - .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) - .set("Authorization", `Bearer ${token}`).send(ARMY1); - - expect(res1.status).toEqual(HttpStatus.CREATED); - - const a1 = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id1: string = a1.body[0].id; - const listRes1 = await request(app.getHttpServer()) - .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id1}`) - .set("Authorization", `Bearer ${token}`); - ArmyListHelper.compareLists(ARMY1, listRes1.body); - - const res2 = await request(app.getHttpServer()) - .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) - .set("Authorization", `Bearer ${token}`).send(ARMY2); - - expect(res2.status).toEqual(HttpStatus.CREATED); - - const a2 = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id2: string = a2.body[1].id; - const listRes2 = await request(app.getHttpServer()) - .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id2}`) - .set("Authorization", `Bearer ${token}`); - ArmyListHelper.compareLists(ARMY2, listRes2.body); - }); - - it("create: create list with invalid armyId - then should return 404 (not found)", async () => { - const list: List = new List(LIST_NAME, 123456, 123, [], false, false); - const res = await request(app.getHttpServer()) - .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) - .set("Authorization", `Bearer ${token}`).send(list); - - expect(res.status).toEqual(HttpStatus.NOT_FOUND); - }); - - it("create: create list with invalid token - then should return 401 (unauthorized)", async () => { - const res = await request(app.getHttpServer()) - .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) - .set("Authorization", `Bearer abcd`).send(ARMY1); - - expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); - }); - - /** - * LOOKUP - */ - it("lookup: basic lookup - then return armies lists credentials", async () => { - const res = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - - expect(res.status).toEqual(HttpStatus.OK); - expect(res.body).toBeDefined(); - }); - - it("lookup: with invalid token - then return 401 (unauthorized)", async () => { - const res = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer abcd`); - - expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); - }); - - /** - * :ID - */ - it(":id: basic get - then return 200 (ok)", async () => { - const a = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id: string = a.body[0].id; - const res = await request(app.getHttpServer()) - .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token}`); - - expect(res.status).toEqual(HttpStatus.OK); - expect(res.status).toBeDefined(); - }); - - it(":id: with invalid token - then return 401 (unauthorized)", async () => { - const a = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id: string = a.body[0].id; - const res = await request(app.getHttpServer()) - .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id}`) - .set("Authorization", `Bearer abcd`); - - expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); - }); - - it(":id: user does not own a not-shared list - then return 403 (forbidden)", async () => { - const a = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id: string = a.body.find(army => army.isShared === false).id; - const res = await request(app.getHttpServer()) - .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token1}`); - - expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); - }); - - it(":id: user does not own a shared list - then should return 200 (OK)", async () => { - const a = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id: string = a.body.find(army => army.isShared === true).id; - const res = await request(app.getHttpServer()) - .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token1}`); - - expect(res.status).toEqual(HttpStatus.OK); - }); - - it(":id: with invalid id - then return 404 (not found)", async () => { - const id: string = "abcd"; - const res = await request(app.getHttpServer()) - .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token}`); - - expect(res.status).toEqual(HttpStatus.NOT_FOUND) - }); - - - /** - * DELETE - */ - - it("delete: basic delete - then should return 200 (ok)", async () => { - const a = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id: string = a.body[0].id; - const res = await request(app.getHttpServer()) - .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token}`); - - expect(res.status).toEqual(HttpStatus.OK); - - const res2 = await request(app.getHttpServer()) - .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token}`); - - expect(res2.status).toEqual(HttpStatus.NOT_FOUND); - }); - - it("delete: use invalid token - then should return unauthorised (401)", async () => { - const a = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id: string = a.body[0].id; - const res = await request(app.getHttpServer()) - .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${id}`) - .set("Authorization", `Bearer abcd`); - - expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); - }); - - - it("delete: not the owner - then should return forbidden (403)", async () => { - const a = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id: string = a.body[0].id; - const res = await request(app.getHttpServer()) - .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token1}`); - - expect(res.status).toEqual(HttpStatus.FORBIDDEN); - }); - - it("delete: use invalid army list id - then should return not found (404)", async () => { - const id: string = "abcd"; - const res = await request(app.getHttpServer()) - .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token}`); - - expect(res.status).toEqual(HttpStatus.NOT_FOUND); - }); - - /** - * UPDATE - */ - - it("update: basic - then should return 200 (ok) and values should have changed", async () => { - const a = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id: string = a.body[0].id; - const res = await request(app.getHttpServer()) - .put(`${TestsHelper.ARMIES_LISTS_UPDATE_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token}`) - .send(ARMY2); - - expect(res.status).toEqual(HttpStatus.OK); - - const listRes = await request(app.getHttpServer()) - .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token}`) - ArmyListHelper.compareLists(ARMY2, listRes.body); - }); - - it("update: use invalid armyId - then should return 404 (not found)", async () => { - const id: string = "abcd"; - const res = await request(app.getHttpServer()) - .put(`${TestsHelper.ARMIES_LISTS_UPDATE_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token}`) - .send(ARMY2); - - expect(res.status).toEqual(HttpStatus.NOT_FOUND); - }); - - it("update: try update not owned list - then should return 403 (forbidden)", async () => { - const a = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id: string = a.body[0].id; - const res = await request(app.getHttpServer()) - .put(`${TestsHelper.ARMIES_LISTS_UPDATE_ROUTE}/${id}`) - .set("Authorization", `Bearer ${token1}`) - .send(ARMY2); - - expect(res.status).toEqual(HttpStatus.FORBIDDEN); - }); - - it("update: use invalid token - then should return 401 (unauthorized)", async () => { - const a = await request(app.getHttpServer()) - .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) - .set("Authorization", `Bearer ${token}`); - const id: string = a.body[0].id; - const res = await request(app.getHttpServer()) - .put(`${TestsHelper.ARMIES_LISTS_UPDATE_ROUTE}/${id}`) - .set("Authorization", `Bearer abcd`) - .send(ARMY2); - - expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); - }); -}); diff --git a/tests/api/army-list/create.spec.ts b/tests/api/army-list/create.spec.ts new file mode 100644 index 0000000..d0ddf16 --- /dev/null +++ b/tests/api/army-list/create.spec.ts @@ -0,0 +1,79 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { AppModule } from "../../../src/app.module"; +import { TestsHelper } from "../../tests.helper"; +import { faker } from "@faker-js/faker"; +import { HttpStatus, INestApplication } from "@nestjs/common"; +import * as request from "supertest"; +import { ARMY1, ARMY2, List } from "../../fixtures/army-list/armies-lists"; + +jest.setTimeout(25000) + +const LIST_NAME: string = "my list"; + +const USERNAME = faker.internet.userName(); +const EMAIL = faker.internet.email(); +const PASSWORD = faker.internet.password(); + +const USERNAME1 = faker.internet.userName(); +const EMAIL1 = faker.internet.email(); +const PASSWORD1 = faker.internet.password(); + +let app: INestApplication; +let token: string; +let token1: string; + +describe("armies-lists/create", () => { + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [AppModule] + }).compile(); + + app = module.createNestApplication(); + await app.init(); + + token = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME, EMAIL, + PASSWORD); + token1 = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME1, EMAIL1, + PASSWORD); + }); + + afterAll(async () => { + const res = await request(app.getHttpServer()) + .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) + .set("Authorization", `Bearer ${token}`); + + for (const a of res.body) { + await request(app.getHttpServer()) + .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${a.id}`) + .set("Authorization", `Bearer ${token}`); + } + await TestsHelper.deleteAccount(app.getHttpServer(), token); + await TestsHelper.deleteAccount(app.getHttpServer(), token1); + }); + + it("create: create basic lists - then should return 201 (created)", async () => { + const res1 = await request(app.getHttpServer()) + .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) + .set("Authorization", `Bearer ${token}`).send(ARMY1); + + expect(res1.status).toEqual(HttpStatus.CREATED); + expect(res1.body.id).toBeDefined(); + }); + + it("create: create list with invalid armyId - then should return 404 (not found)", async () => { + const list: List = new List(LIST_NAME, 123456, 123, [], false, false); + const res = await request(app.getHttpServer()) + .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) + .set("Authorization", `Bearer ${token}`).send(list); + + expect(res.status).toEqual(HttpStatus.NOT_FOUND); + }); + + it("create: create list with invalid token - then should return 401 (unauthorized)", async () => { + const res = await request(app.getHttpServer()) + .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) + .set("Authorization", `Bearer abcd`).send(ARMY1); + + expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); + }); +}); diff --git a/tests/api/army-list/delete.spec.ts b/tests/api/army-list/delete.spec.ts new file mode 100644 index 0000000..d0125a1 --- /dev/null +++ b/tests/api/army-list/delete.spec.ts @@ -0,0 +1,113 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { AppModule } from "../../../src/app.module"; +import { TestsHelper } from "../../tests.helper"; +import * as request from "supertest"; +import { ARMY1, ARMY2 } from "../../fixtures/army-list/armies-lists"; +import { faker } from "@faker-js/faker"; +import { HttpStatus, INestApplication } from "@nestjs/common"; + +jest.setTimeout(60000); + +const USERNAME = faker.internet.userName(); +const EMAIL = faker.internet.email(); +const PASSWORD = faker.internet.password(); + +const USERNAME1 = faker.internet.userName(); +const EMAIL1 = faker.internet.email(); +const PASSWORD1 = faker.internet.password(); + +let app: INestApplication; +let token: string; +let token1: string; + +describe("armies-lists/create", () => { + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [AppModule] + }).compile(); + + app = module.createNestApplication(); + await app.init(); + + token = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME, EMAIL, + PASSWORD); + token1 = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME1, EMAIL1, + PASSWORD); + }); + + beforeEach(async () => { + await request(app.getHttpServer()) + .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) + .set("Authorization", `Bearer ${token}`).send(ARMY1).then(res => res.body.id); + + await request(app.getHttpServer()) + .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) + .set("Authorization", `Bearer ${token1}`).send(ARMY2).then(res => res.body.id); + }); + + afterAll(async () => { + const res = await request(app.getHttpServer()) + .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) + .set("Authorization", `Bearer ${token}`); + + for (const a of res.body) { + await request(app.getHttpServer()) + .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${a.id}`) + .set("Authorization", `Bearer ${token}`); + } + await TestsHelper.deleteAccount(app.getHttpServer(), token); + await TestsHelper.deleteAccount(app.getHttpServer(), token1); + }); + + it("delete: basic delete - then should return 200 (ok)", async () => { + const a = await request(app.getHttpServer()) + .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) + .set("Authorization", `Bearer ${token}`); + const id: string = a.body[0].id; + const res = await request(app.getHttpServer()) + .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${id}`) + .set("Authorization", `Bearer ${token}`); + + expect(res.status).toEqual(HttpStatus.OK); + + const res2 = await request(app.getHttpServer()) + .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id}`) + .set("Authorization", `Bearer ${token}`); + + expect(res2.status).toEqual(HttpStatus.NOT_FOUND); + }); + + it("delete: use invalid token - then should return unauthorised (401)", async () => { + const a = await request(app.getHttpServer()) + .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) + .set("Authorization", `Bearer ${token}`); + const id: string = a.body[0].id; + const res = await request(app.getHttpServer()) + .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${id}`) + .set("Authorization", `Bearer abcd`); + + expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); + }); + + + it("delete: not the owner - then should return forbidden (403)", async () => { + const a = await request(app.getHttpServer()) + .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) + .set("Authorization", `Bearer ${token}`); + const id: string = a.body[0].id; + const res = await request(app.getHttpServer()) + .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${id}`) + .set("Authorization", `Bearer ${token1}`); + + expect(res.status).toEqual(HttpStatus.FORBIDDEN); + }); + + it("delete: use invalid army list id - then should return not found (404)", async () => { + const id: string = "abcd"; + const res = await request(app.getHttpServer()) + .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${id}`) + .set("Authorization", `Bearer ${token}`); + + expect(res.status).toEqual(HttpStatus.NOT_FOUND); + }); +}); diff --git a/tests/api/army-list/get.spec.ts b/tests/api/army-list/get.spec.ts new file mode 100644 index 0000000..36fdda9 --- /dev/null +++ b/tests/api/army-list/get.spec.ts @@ -0,0 +1,106 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { AppModule } from "../../../src/app.module"; +import { TestsHelper } from "../../tests.helper"; +import * as request from "supertest"; +import { ARMY1, ARMY2 } from "../../fixtures/army-list/armies-lists"; +import { faker } from "@faker-js/faker"; +import { HttpStatus, INestApplication } from "@nestjs/common"; +import ArmyListHelper from "../../helper/army-list.helper"; + +jest.setTimeout(60000); + +const USERNAME = faker.internet.userName(); +const EMAIL = faker.internet.email(); +const PASSWORD = faker.internet.password(); + +const USERNAME1 = faker.internet.userName(); +const EMAIL1 = faker.internet.email(); +const PASSWORD1 = faker.internet.password(); + +let app: INestApplication; +let token: string; +let token1: string; +let user1ListId: string; +let user2ListId: string; + +describe("armies-lists/create", () => { + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [AppModule] + }).compile(); + + app = module.createNestApplication(); + await app.init(); + + token = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME, EMAIL, + PASSWORD); + token1 = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME1, EMAIL1, + PASSWORD); + + user1ListId = await request(app.getHttpServer()) + .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) + .set("Authorization", `Bearer ${token}`).send(ARMY1).then(res => res.body.id); + + user2ListId = await request(app.getHttpServer()) + .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) + .set("Authorization", `Bearer ${token1}`).send(ARMY2).then(res => res.body.id); + }); + + afterAll(async () => { + const res = await request(app.getHttpServer()) + .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) + .set("Authorization", `Bearer ${token}`); + + for (const a of res.body) { + await request(app.getHttpServer()) + .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${a.id}`) + .set("Authorization", `Bearer ${token}`); + } + await TestsHelper.deleteAccount(app.getHttpServer(), token); + await TestsHelper.deleteAccount(app.getHttpServer(), token1); + }); + + it(":id: basic get - then return 200 (ok)", async () => { + const res = await request(app.getHttpServer()) + .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${user1ListId}`) + .set("Authorization", `Bearer ${token}`); + expect(res.status).toEqual(HttpStatus.OK); + expect(res.status).toBeDefined(); + ArmyListHelper.compareLists(ARMY1, res.body); + + }); + + it(":id: with invalid token - then return 401 (unauthorized)", async () => { + const res = await request(app.getHttpServer()) + .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${user1ListId}`) + .set("Authorization", `Bearer abcd`); + + expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); + }); + + it(":id: user does not own a not-shared list - then return 403 (forbidden)", async () => { + const res = await request(app.getHttpServer()) + .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${user1ListId}`) + .set("Authorization", `Bearer ${token1}`); + + expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); + }); + + it(":id: user does not own a shared list - then should return 200 (OK)", async () => { + const res = await request(app.getHttpServer()) + .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${user2ListId}`) + .set("Authorization", `Bearer ${token}`); + + expect(res.status).toEqual(HttpStatus.OK); + }); + + it(":id: with invalid id - then return 404 (not found)", async () => { + const id: string = "abcd"; + const res = await request(app.getHttpServer()) + .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${id}`) + .set("Authorization", `Bearer ${token}`); + + expect(res.status).toEqual(HttpStatus.NOT_FOUND) + }); + +}); diff --git a/tests/api/army-list/lookup.spec.ts b/tests/api/army-list/lookup.spec.ts new file mode 100644 index 0000000..858a627 --- /dev/null +++ b/tests/api/army-list/lookup.spec.ts @@ -0,0 +1,73 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { AppModule } from "../../../src/app.module"; +import { TestsHelper } from "../../tests.helper"; +import * as request from "supertest"; +import { faker } from "@faker-js/faker"; +import { HttpStatus, INestApplication } from "@nestjs/common"; +import { ARMY1 } from "../../fixtures/army-list/armies-lists"; + +jest.setTimeout(25000); + +const USERNAME = faker.internet.userName(); +const EMAIL = faker.internet.email(); +const PASSWORD = faker.internet.password(); + +const USERNAME1 = faker.internet.userName(); +const EMAIL1 = faker.internet.email(); +const PASSWORD1 = faker.internet.password(); + +let app: INestApplication; +let token: string; +let token1: string; + +describe("armies-lists/create", () => { + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [AppModule] + }).compile(); + + app = module.createNestApplication(); + await app.init(); + + token = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME, EMAIL, + PASSWORD); + token1 = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME1, EMAIL1, + PASSWORD); + + await request(app.getHttpServer()) + .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) + .set("Authorization", `Bearer ${token}`).send(ARMY1); + }); + + afterAll(async () => { + const res = await request(app.getHttpServer()) + .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) + .set("Authorization", `Bearer ${token}`); + + for (const a of res.body) { + await request(app.getHttpServer()) + .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${a.id}`) + .set("Authorization", `Bearer ${token}`); + } + await TestsHelper.deleteAccount(app.getHttpServer(), token); + await TestsHelper.deleteAccount(app.getHttpServer(), token1); + }); + + it("lookup: basic lookup - then return armies lists credentials", async () => { + const res = await request(app.getHttpServer()) + .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) + .set("Authorization", `Bearer ${token}`); + + expect(res.status).toEqual(HttpStatus.OK); + expect(res.body).toBeDefined(); + }); + + it("lookup: with invalid token - then return 401 (unauthorized)", async () => { + const res = await request(app.getHttpServer()) + .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) + .set("Authorization", `Bearer abcd`); + + expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); + }); + +}); diff --git a/tests/api/army-list/update.spec.ts b/tests/api/army-list/update.spec.ts new file mode 100644 index 0000000..2a3813f --- /dev/null +++ b/tests/api/army-list/update.spec.ts @@ -0,0 +1,104 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { AppModule } from "../../../src/app.module"; +import { TestsHelper } from "../../tests.helper"; +import * as request from "supertest"; +import { ARMY1, ARMY2 } from "../../fixtures/army-list/armies-lists"; +import { faker } from "@faker-js/faker"; +import { HttpStatus, INestApplication } from "@nestjs/common"; +import ArmyListHelper from "../../helper/army-list.helper"; + +jest.setTimeout(70000); + +const USERNAME = faker.internet.userName(); +const EMAIL = faker.internet.email(); +const PASSWORD = faker.internet.password(); + +const USERNAME1 = faker.internet.userName(); +const EMAIL1 = faker.internet.email(); +const PASSWORD1 = faker.internet.password(); + +let app: INestApplication; +let token: string; +let token1: string; +let user1ListId: string; +let user2ListId: string; + +describe("armies-lists/create", () => { + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [AppModule] + }).compile(); + + app = module.createNestApplication(); + await app.init(); + + token = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME, EMAIL, + PASSWORD); + token1 = await TestsHelper.createAccountAndGetToken(app.getHttpServer(), USERNAME1, EMAIL1, + PASSWORD); + + user1ListId = await request(app.getHttpServer()) + .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) + .set("Authorization", `Bearer ${token}`).send(ARMY1).then(res => res.body.id); + + user2ListId = await request(app.getHttpServer()) + .post(TestsHelper.ARMIES_LISTS_CREATE_ROUTE) + .set("Authorization", `Bearer ${token1}`).send(ARMY2).then(res => res.body.id); + }); + + afterAll(async () => { + const res = await request(app.getHttpServer()) + .get(TestsHelper.ARMIES_LISTS_LOOKUP_ROUTE) + .set("Authorization", `Bearer ${token}`); + + for (const a of res.body) { + await request(app.getHttpServer()) + .delete(`${TestsHelper.ARMIES_LISTS_DELETE_ROUTE}/${a.id}`) + .set("Authorization", `Bearer ${token}`); + } + await TestsHelper.deleteAccount(app.getHttpServer(), token); + await TestsHelper.deleteAccount(app.getHttpServer(), token1); + }); + + it("update: basic - then should return 200 (ok) and values should have changed", async () => { + const res = await request(app.getHttpServer()) + .put(`${TestsHelper.ARMIES_LISTS_UPDATE_ROUTE}/${user1ListId}`) + .set("Authorization", `Bearer ${token}`) + .send(ARMY2); + + expect(res.status).toEqual(HttpStatus.OK); + + const listRes = await request(app.getHttpServer()) + .get(`${TestsHelper.ARMIES_LISTS_ROUTE}/${user1ListId}`) + .set("Authorization", `Bearer ${token}`) + ArmyListHelper.compareLists(ARMY2, listRes.body); + }); + + it("update: use invalid armyId - then should return 404 (not found)", async () => { + const id: string = "abcd"; + const res = await request(app.getHttpServer()) + .put(`${TestsHelper.ARMIES_LISTS_UPDATE_ROUTE}/${id}`) + .set("Authorization", `Bearer ${token}`) + .send(ARMY2); + + expect(res.status).toEqual(HttpStatus.NOT_FOUND); + }); + + it("update: try update not owned list - then should return 403 (forbidden)", async () => { + const res = await request(app.getHttpServer()) + .put(`${TestsHelper.ARMIES_LISTS_UPDATE_ROUTE}/${user1ListId}`) + .set("Authorization", `Bearer ${token1}`) + .send(ARMY2); + + expect(res.status).toEqual(HttpStatus.FORBIDDEN); + }); + + it("update: use invalid token - then should return 401 (unauthorized)", async () => { + const res = await request(app.getHttpServer()) + .put(`${TestsHelper.ARMIES_LISTS_UPDATE_ROUTE}/${user1ListId}`) + .set("Authorization", `Bearer abcd`) + .send(ARMY2); + + expect(res.status).toEqual(HttpStatus.UNAUTHORIZED); + }); +}); diff --git a/tests/api/game/game.spec.ts b/tests/api/game/game.spec.ts index 5445d33..ed45441 100644 --- a/tests/api/game/game.spec.ts +++ b/tests/api/game/game.spec.ts @@ -7,7 +7,7 @@ import { AppModule } from "../../../src/app.module"; import { TestsHelper } from "../../tests.helper"; import { ARMY1, ARMY2 } from "../../fixtures/army-list/armies-lists"; -jest.setTimeout(10000) +jest.setTimeout(55000) const OWNER_USERNAME = faker.internet.userName(); const OWNER_EMAIL = faker.internet.email();