Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Region tests #308

Merged
merged 21 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
415 changes: 415 additions & 0 deletions api/__tests__/routes/region.test.ts

Large diffs are not rendered by default.

416 changes: 416 additions & 0 deletions api/__tests__/routes/user_region.test.ts

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions api/__tests__/utilities/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ export const badRequestForeignKey = {
message: "Bad Request",
detail: "Foreign key constraint failed",
};

export const methodNotAllowedResponse = {
message: "Method not allowed",
};
29 changes: 23 additions & 6 deletions api/src/routes/region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Parser } from "../parser";
import { prisma } from "../prisma";
import { Auth } from "../auth/auth";
import { Prisma } from "@selab-2/groep-1-orm";
import RegionInclude = Prisma.RegionInclude;
import { RegionValidator } from "../validators/region.validator";
import { Validator } from "../validators/validator";
import RegionInclude = Prisma.RegionInclude;

export class RegionRouting extends Routing {
private static includes: RegionInclude = {
Expand All @@ -19,6 +19,11 @@ export class RegionRouting extends Routing {

@Auth.authorization({ superStudent: true })
async getAll(req: CustomRequest, res: express.Response) {
let deleted: boolean | undefined = false;
if (req.user?.admin && Parser.bool(req.query["deleted"], false)) {
deleted = undefined;
}

const result = await prisma.region.findMany({
take: Parser.number(req.query["take"], 1024),
skip: Parser.number(req.query["skip"], 0),
Expand All @@ -32,6 +37,7 @@ export class RegionRouting extends Routing {
},
}
: {},
deleted: deleted,
},
include: RegionRouting.includes,
});
Expand Down Expand Up @@ -76,11 +82,22 @@ export class RegionRouting extends Routing {

@Auth.authorization({ superStudent: true })
async deleteOne(req: CustomRequest, res: express.Response) {
await prisma.region.delete({
where: {
id: Parser.number(req.params["id"]),
},
});
if (Parser.bool(req.body["hardDelete"], false)) {
await prisma.region.delete({
where: {
id: Parser.number(req.params["id"]),
},
});
} else {
await prisma.region.update({
data: {
deleted: true,
},
where: {
id: Parser.number(req.params["id"]),
},
});
}

return res.status(200).json({});
}
Expand Down
22 changes: 13 additions & 9 deletions api/src/validators/action.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ export class ActionValidator extends Validator {
}

updateOneValidator() {
return celebrate({
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("params.id"),
description: Joi.string().trim().min(1),
}),
});
return celebrate(
{
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("$params.id"),
description: Joi.string().trim().min(1),
}),
},
undefined,
{ reqContext: true },
);
}

createOneValidator() {
Expand Down
32 changes: 18 additions & 14 deletions api/src/validators/address.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ export class AddressValidator extends Validator {
}

updateOneValidator() {
return celebrate({
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("params.id"),
street: Joi.string().min(1),
city: Joi.string().min(1),
zip_code: Joi.number().positive(),
number: Joi.number().positive(),
longitude: Joi.number().min(-180).max(180),
latitude: Joi.number().min(-90).max(90),
}),
});
return celebrate(
{
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("$params.id"),
street: Joi.string().min(1),
city: Joi.string().min(1),
zip_code: Joi.number().positive(),
number: Joi.number().positive(),
longitude: Joi.number().min(-180).max(180),
latitude: Joi.number().min(-90).max(90),
}),
},
undefined,
{ reqContext: true },
);
}

deleteOneValidator() {
Expand Down
36 changes: 20 additions & 16 deletions api/src/validators/building.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,26 @@ export class BuildingValidator extends Validator {
}

updateOneValidator() {
return celebrate({
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("params.id"),
name: Joi.string().min(1),
ivago_id: Joi.string().min(1),
address_id: Joi.number().positive(),
manual_id: Joi.number().positive(),
syndicus_id: Joi.number().positive(),
deleted: Joi.bool(),
hash: Joi.string().forbidden(),
expected_time: Joi.number().positive(),
}),
});
return celebrate(
{
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("$params.id"),
name: Joi.string().min(1),
ivago_id: Joi.string().min(1),
address_id: Joi.number().positive(),
manual_id: Joi.number().positive(),
syndicus_id: Joi.number().positive(),
deleted: Joi.bool(),
hash: Joi.string().forbidden(),
expected_time: Joi.number().positive(),
}),
},
undefined,
{ reqContext: true },
);
}

deleteOneValidator() {
Expand Down
26 changes: 15 additions & 11 deletions api/src/validators/garbage.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ export class GarbageValidator extends Validator {
}

updateOneValidator(): express.RequestHandler<any> {
return celebrate({
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("params.id"),
pickup_time: Joi.date().iso(),
action_id: Joi.number().positive(),
building_id: Joi.number().positive(),
}),
});
return celebrate(
{
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("$params.id"),
pickup_time: Joi.date().iso(),
action_id: Joi.number().positive(),
building_id: Joi.number().positive(),
}),
},
undefined,
{ reqContext: true },
);
}

deleteOneValidator(): express.RequestHandler<any> {
Expand Down
26 changes: 15 additions & 11 deletions api/src/validators/mailtemplate.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@ export class MailTemplateValidator extends Validator {
}

updateOneValidator() {
return celebrate({
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("params.id"),
name: Joi.string().trim().min(1),
mail_subject: Joi.string().trim().min(1),
content: Joi.string().trim().min(1),
}),
});
return celebrate(
{
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("$params.id"),
name: Joi.string().trim().min(1),
mail_subject: Joi.string().trim().min(1),
content: Joi.string().trim().min(1),
}),
},
undefined,
{ reqContext: true },
);
}

deleteOneValidator() {
Expand Down
68 changes: 38 additions & 30 deletions api/src/validators/progress.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ProgressValidator extends Validator {
createOneValidator() {
return celebrate({
body: Joi.object({
report: Joi.string().trim().min(1).required(),
report: Joi.string().trim().required(),
arrival: Joi.date().iso(),
departure: Joi.date().iso(),
building_id: Joi.number().positive().required(),
Expand All @@ -44,20 +44,24 @@ export class ProgressValidator extends Validator {
}

updateOneValidator() {
return celebrate({
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("params.id"),
report: Joi.string().trim(),
arrival: Joi.date().iso(),
departure: Joi.date().iso(),
building_id: Joi.number().positive(),
schedule_id: Joi.number().positive(),
deleted: Joi.bool(),
}),
});
return celebrate(
{
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("$params.id"),
report: Joi.string().trim(),
arrival: Joi.date().iso(),
departure: Joi.date().iso(),
building_id: Joi.number().positive(),
schedule_id: Joi.number().positive(),
deleted: Joi.bool(),
}),
},
undefined,
{ reqContext: true },
);
}

deleteOneValidator() {
Expand Down Expand Up @@ -99,21 +103,25 @@ export class ProgressImageValidator extends Validator {
}

updateOneValidator() {
return celebrate({
params: Joi.object({
id: Joi.number().positive().required(),
image_id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.forbidden(),
type: Joi.string()
.trim()
.valid("ARRIVAL", "DEPARTURE", "GARBAGE"),
description: Joi.string().trim().min(1),
image_id: Joi.number().positive(),
progress_id: Joi.number().positive(),
}),
});
return celebrate(
{
params: Joi.object({
id: Joi.number().positive().required(),
image_id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("$params.id"),
type: Joi.string()
.trim()
.valid("ARRIVAL", "DEPARTURE", "GARBAGE"),
description: Joi.string().trim().min(1),
image_id: Joi.number().positive(),
progress_id: Joi.number().positive(),
}),
},
undefined,
{ reqContext: true },
);
}

deleteOneValidator() {
Expand Down
25 changes: 15 additions & 10 deletions api/src/validators/region.validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Validator } from "./validator";
import { celebrate } from "celebrate";
import { celebrate, Segments } from "celebrate";
import Joi from "joi";

export class RegionValidator extends Validator {
Expand All @@ -10,6 +10,7 @@ export class RegionValidator extends Validator {
skip: Joi.number(),
name: Joi.string().trim().min(1),
user_id: Joi.number(),
deleted: Joi.bool(),
}),
});
}
Expand All @@ -31,15 +32,19 @@ export class RegionValidator extends Validator {
}

updateOneValidator() {
return celebrate({
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("params.id"),
name: Joi.string().min(1).required(),
}),
});
return celebrate(
{
params: Joi.object({
id: Joi.number().positive().required(),
}),
body: Joi.object({
id: Joi.ref("$params.id"),
name: Joi.string().min(1).required(),
}),
},
undefined,
{ reqContext: true },
);
}

deleteOneValidator() {
Expand Down
Loading