Skip to content

Commit

Permalink
fix(project): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JarodCouprie committed Aug 23, 2024
1 parent b552e5b commit b87b1f6
Show file tree
Hide file tree
Showing 25 changed files with 285 additions and 138 deletions.
6 changes: 6 additions & 0 deletions api-server/src/common/enum/DemandStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum DemandStatus {
ACCEPTED = "ACCEPTED",
WAITING = "WAITING",
DENIED = "DENIED",
DRAFT = "DRAFT",
}
7 changes: 7 additions & 0 deletions api-server/src/common/enum/DemandType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum DemandType {
RTT = "RTT",
TT = "TT",
CA = "CA",
ABSENCE = "ABSENCE",
SICKNESS = "SICKNESS",
}
5 changes: 5 additions & 0 deletions api-server/src/common/enum/ExpenseStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum ExpenseStatus {
REFUNDED = "REFUNDED",
NOT_REFUNDED = "NOT_REFUNDED",
WAITING = "WAITING",
}
6 changes: 6 additions & 0 deletions api-server/src/common/enum/ExpenseType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ExpenseType {
TRAVEL = "TRAVEL",
COMPENSATION = "COMPENSATION",
FOOD = "FOOD",
HOUSING = "HOUSING",
}
9 changes: 8 additions & 1 deletion api-server/src/common/middleware/HasRoleMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import { CustomRequest } from "../helper/CustomRequest.js";
export const hasRole = (roles: RoleEnum[]) => {
return (req: Request, res: Response, next: NextFunction) => {
let userRoles = (req as CustomRequest).token.userRoles;
let hasRoles = roles.some((role) => userRoles.includes(role));
let hasRoles = verifyRoles(roles, userRoles);
if (!hasRoles) {
return res.status(401).json({ message: "Accès refusé" });
}
next();
};
};

export const verifyRoles = (
roles: RoleEnum[],
userRoles: RoleEnum[],
): boolean => {
return roles.some((role) => userRoles.includes(role));
};
18 changes: 3 additions & 15 deletions api-server/src/common/model/Demand.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { DemandStatus } from "../enum/DemandStatus";
import { DemandType } from "../enum/DemandType";

export class Demand {
id: number;
start_date: Date;
Expand Down Expand Up @@ -146,18 +149,3 @@ export class StatusDemand {
this.status = status;
}
}

export enum DemandType {
RTT = "RTT",
TT = "TT",
CA = "CA",
ABSENCE = "ABSENCE",
SICKNESS = "SICKNESS",
}

export enum DemandStatus {
ACCEPTED = "ACCEPTED",
WAITING = "WAITING",
DENIED = "DENIED",
DRAFT = "DRAFT",
}
35 changes: 3 additions & 32 deletions api-server/src/common/model/Expense.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ExpenseStatus } from "../enum/ExpenseStatus";
import { ExpenseType } from "../enum/ExpenseType";

export class Expense {
id: string;
type: ExpenseType;
Expand Down Expand Up @@ -46,35 +49,3 @@ export class Expense {
this.validated_at = validated_at;
}
}

export enum ExpenseType {
TRAVEL = "TRAVEL",
COMPENSATION = "COMPENSATION",
FOOD = "FOOD",
HOUSING = "HOUSING",
}

export enum ExpenseStatus {
REFUNDED = "REFUNDED",
NOT_REFUNDED = "NOT_REFUNDED",
WAITING = "WAITING",
}

export class ExpenseResponse {
status: ExpenseStatus;
motivation: string;
answeredAt: Date;
answeredBy: number;

constructor(
status: ExpenseStatus,
motivation: string,
answeredAt: Date,
answeredBy: number,
) {
this.status = status;
this.motivation = motivation;
this.answeredAt = answeredAt;
this.answeredBy = answeredBy;
}
}
10 changes: 2 additions & 8 deletions api-server/src/resources/demand/DemandService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { DemandRepository } from "./DemandRepository.js";
import {
ConfirmDemand,
Demand,
DemandStatus,
DemandType,
RejectDemand,
StatusDemand,
ValidatedDemand,
Expand All @@ -18,13 +16,10 @@ import { UserService } from "../user/UserService.js";
import { MinioClient } from "../../common/helper/MinioClient.js";
import { UserRepository } from "../user/UserRepository.js";
import { DemandValidatedDTO } from "./dto/DemandValidatedDTO.js";
import {
ExpenseListDTO,
ExpenseValidation,
} from "../expense/dto/ExpenseListDTO.js";
import { ExpenseRepository } from "../expense/ExpenseRepository.js";
import { User } from "../../common/model/User.js";
import { DemandEntity } from "../../common/entity/demand/demand.entity.js";
import { DemandType } from "../../common/enum/DemandType";
import { DemandStatus } from "../../common/enum/DemandStatus";

export function calculateNumberOfDays(
start_date: Date,
Expand Down Expand Up @@ -136,7 +131,6 @@ export class DemandService {
public static async getDemandById(id: string) {
try {
const demand_: any = await DemandRepository.getDemandById(+id);
console.log(demand_);
const signedUrl = await MinioClient.getSignedUrl(demand_.file_key);
const demand: DemandDTO = new DemandDTO(demand_, signedUrl);

Expand Down
2 changes: 1 addition & 1 deletion api-server/src/resources/demand/dto/CreateDemandDTO.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DemandType } from "../../../common/model/Demand.js";
import { DemandType } from "../../../common/enum/DemandType";

export class CreateDemand {
start_date: Date;
Expand Down
8 changes: 3 additions & 5 deletions api-server/src/resources/demand/dto/DemandDTO.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
Demand,
DemandType,
DemandStatus,
} from "../../../common/model/Demand.js";
import { Demand } from "../../../common/model/Demand.js";
import { DemandStatus } from "../../../common/enum/DemandStatus";
import { DemandType } from "../../../common/enum/DemandType";

export class DemandDTO {
id: number;
Expand Down
8 changes: 3 additions & 5 deletions api-server/src/resources/demand/dto/DemandValidatedDTO.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
DemandStatus,
DemandType,
ValidatedDemand,
} from "../../../common/model/Demand.js";
import { ValidatedDemand } from "../../../common/model/Demand.js";
import { DemandStatus } from "../../../common/enum/DemandStatus";
import { DemandType } from "../../../common/enum/DemandType";

export class DemandValidatedDTO {
id: number;
Expand Down
2 changes: 1 addition & 1 deletion api-server/src/resources/demand/dto/EditDemandDTO.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DemandType } from "../../../common/model/Demand.js";
import { DemandType } from "../../../common/enum/DemandType";

export class EditDemandDTO {
start_date: Date;
Expand Down
1 change: 0 additions & 1 deletion api-server/src/resources/department/DepartmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export class DepartmentService {
id_user_lead_service,
+idAgency,
);
console.log(newDepartment);
const createdDepartment =
await DepartmentRepository.createDepartment(newDepartment);

Expand Down
3 changes: 2 additions & 1 deletion api-server/src/resources/expense/ExpenseRepository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { DatabaseClient } from "../../common/helper/DatabaseClient.js";
import { Expense, ExpenseStatus } from "../../common/model/Expense.js";
import { Expense } from "../../common/model/Expense.js";
import {
ExpenseInvalidation,
ExpenseValidation,
} from "./dto/ExpenseListDTO.js";
import { ExpenseStatus } from "../../common/enum/ExpenseStatus";

export class ExpenseRepository {
private static pool = DatabaseClient.mysqlPool;
Expand Down
3 changes: 2 additions & 1 deletion api-server/src/resources/expense/ExpenseService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExpenseRepository } from "./ExpenseRepository.js";
import { Expense, ExpenseStatus } from "../../common/model/Expense.js";
import { Expense } from "../../common/model/Expense.js";
import {
ExpenseInvalidation,
ExpenseListDTO,
Expand All @@ -10,6 +10,7 @@ import { logger } from "../../common/helper/Logger.js";
import { Request } from "express";
import { ExpenseAmountDateAndStatusDTO } from "./dto/ExpenseAmountDateAndStatusDTO.js";
import { MinioClient } from "../../common/helper/MinioClient.js";
import { ExpenseStatus } from "../../common/enum/ExpenseStatus";

export class ExpenseService {
public static async getExpensesValuesByUserId(req: Request, userId: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Expense, ExpenseStatus } from "../../../common/model/Expense.js";
import { Expense } from "../../../common/model/Expense.js";
import { ExpenseStatus } from "../../../common/enum/ExpenseStatus";

export class ExpenseAmountDateAndStatusDTO {
amount: number;
Expand Down
8 changes: 3 additions & 5 deletions api-server/src/resources/expense/dto/ExpenseListDTO.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
Expense,
ExpenseStatus,
ExpenseType,
} from "../../../common/model/Expense.js";
import { Expense } from "../../../common/model/Expense.js";
import { ExpenseType } from "../../../common/enum/ExpenseType";
import { ExpenseStatus } from "../../../common/enum/ExpenseStatus";

export class ExpenseListDTO {
id: string;
Expand Down
18 changes: 18 additions & 0 deletions api-server/src/test/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Address } from "../common/model/Address";
import { User } from "../common/model/User";
import { RoleEnum } from "../common/enum/RoleEnum";
import { Agency } from "../common/model/Agency";
import { CreateOrUpdateAddressDTO } from "../resources/address/dto/CreateOrUpdateAddressDTO";

describe("Address models should be what we give it", () => {
test("Test Address Model from User Model", () => {
Expand Down Expand Up @@ -59,4 +60,21 @@ describe("Address models should be what we give it", () => {
expect(address.lat).toBe(247);
expect(address.lng).toBe(14);
});
test("Test CreateOrUpdateAddressDTO", () => {
const addressDTO = new CreateOrUpdateAddressDTO(
"street",
"streetNumber",
"locality",
"zipcode",
24487,
14888884,
);

expect(addressDTO.street).toBe("street");
expect(addressDTO.streetNumber).toBe("streetNumber");
expect(addressDTO.locality).toBe("locality");
expect(addressDTO.zipcode).toBe("zipcode");
expect(addressDTO.lat).toBe(24487);
expect(addressDTO.lng).toBe(14888884);
});
});
37 changes: 27 additions & 10 deletions api-server/src/test/agency.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { describe, expect, test } from "vitest";
import { Agency, CreateAgency } from "../common/model/Agency";
import {
AgencyCoord,
AgencyDTO,
AgencyList,
} from "../resources/agency/dto/AgencyDTO";

const agency = new Agency(
1477,
"label",
"street",
"streetNumber",
"locality",
"zipcode",
2477,
147,
);
describe("Agency models should be what we give it", () => {
test("Test Agency Model", () => {
const agency = new Agency(
1477,
"label",
"street",
"streetNumber",
"locality",
"zipcode",
2477,
147,
);
expect(agency.id).toBe(1477);
expect(agency.label).toBe("label");
expect(agency.street).toBe("street");
Expand All @@ -27,4 +32,16 @@ describe("Agency models should be what we give it", () => {
expect(agency.label).toBe("Agence de Metz");
expect(agency.id_address).toBe(1747);
});
test("Test AgencyList", () => {
const agencyList = new AgencyList(agency);
expect(agencyList.id).toBe(1477);
});
test("Test AgencyDTO", () => {
const agencyDTO = new AgencyDTO(agency);
expect(agencyDTO.id).toBe(1477);
});
test("Test AgencyCoord", () => {
const agencyCoord = new AgencyCoord(agency);
expect(agencyCoord.id).toBe(1477);
});
});
Loading

0 comments on commit b87b1f6

Please sign in to comment.