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
Changes from 1 commit
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
357 changes: 357 additions & 0 deletions api/__tests__/routes/region.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,357 @@
import { describe, test } from "@jest/globals";
import { AuthenticationLevel, Testrunner } from "../utilities/Testrunner";
import request from "supertest";
import app from "../../src/main";
import {
deleteDatabaseData,
initialiseDatabase,
restoreTables,
} from "../mock/database";
import {
badRequestResponse,
forbiddenResponse,
notFoundResponse,
} from "../utilities/constants";

describe("Region tests", () => {
let runner: Testrunner;

beforeAll(async () => {
const server = request(app);
runner = new Testrunner(server);

await deleteDatabaseData();
await initialiseDatabase();

runner.authLevel(AuthenticationLevel.SUPER_STUDENT);
});

afterEach(async () => {
await restoreTables("region", "user_region");
});

describe("Succesful requests", () => {
TheMessik marked this conversation as resolved.
Show resolved Hide resolved
test("POST /region", async () => {
const newRegion = {
name: "new Region",
};

const expectedRegion = {
name: "new Region",
deleted: false,
users: [],
};

await runner.post({
url: "/region",
data: newRegion,
expectedResponse: expectedRegion,
});
});

test("GET /region", async () => {
const expected = [
{
deleted: false,
id: 1,
name: "Region 1",
users: [
{
id: 1,
region_id: 1,
user: {
address_id: 1,
admin: false,
date_added: "2023-05-04T12:00:00.000Z",
deleted: false,
email: "[email protected]",
first_name: "Dirk",
id: 1,
last_login: "2023-05-04T12:00:00.000Z",
last_name: "De Student",
phone: "0123456789",
student: true,
super_student: false,
},
user_id: 1,
},
],
},
{
deleted: false,
id: 2,
name: "Region 2",
users: [
{
id: 2,
region_id: 2,
user: {
address_id: 2,
admin: false,
date_added: "2023-05-04T12:00:00.000Z",
deleted: false,
email: "[email protected]",
first_name: "Toon",
id: 2,
last_login: "2023-05-04T12:00:00.000Z",
last_name: "De Superstudent",
phone: "9876543210",
student: false,
super_student: true,
},
user_id: 2,
},
],
},
{ deleted: false, id: 3, name: "Region 3", users: [] },
];

await runner.get({ url: "/region", expectedData: expected });
});

test("GET /region/:id", async () => {
const expected = [
{ id: 3, name: "Region 3", deleted: false, users: [] },
];

await runner.get({ url: "/region/3", expectedData: expected });
});

test("PATCH /region/:id", async () => {
const newRegion = {
id: 3,
name: "Updated Region 3",
};

const expectedRegion = {
id: 3,
name: "Updated Region 3",
deleted: false,
users: [],
};

await runner.patch({
url: "/region/3",
data: newRegion,
expectedResponse: expectedRegion,
});
});

test("DELETE /region/:id", async () => {
await runner.delete({ url: "/region/1" });

// verify that the region is truly deleted
const expected = [
{
deleted: false,
id: 2,
name: "Region 2",
users: [
{
id: 2,
region_id: 2,
user: {
address_id: 2,
admin: false,
date_added: "2023-05-04T12:00:00.000Z",
deleted: false,
email: "[email protected]",
first_name: "Toon",
id: 2,
last_login: "2023-05-04T12:00:00.000Z",
last_name: "De Superstudent",
phone: "9876543210",
student: false,
super_student: true,
},
user_id: 2,
},
],
},
{ deleted: false, id: 3, name: "Region 3", users: [] },
];
await runner.get({
url: "/region",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deze test wordt als super student gerunt zonder hardDelete flag, dus er zal soft delete uitgevoerd worden. Verder vraag je niet de deleted velden specifiek op, dus je merkt ook niet dat er zaken soft deleted zijn.

expectedData: expected,
});
});

afterAll(() => {
app.close();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deze afterAll werkt enkel binnen de "Successful requests" blok. Deze moet in de buitenste blok komen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er staat ook een afterAll in "Unsuccesful requests". Is het beter om maar 1 keer afterAll te doen in de buitenste block?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Het gaat er niet om maar 1x te doen.
In de afterAll staat app.close(). Nu blijkt dat de testen alsnog verder kunnen, blijkbaar dankzij de requests library, maar het is overbodig deze resource 2x vrij te geven. Het doet hier geen kwaad, maar we hoeven ons niet onnodig te herhalen

});

describe("Unsuccesful requests", () => {
let runner: Testrunner;
beforeAll(async () => {
const server = request(app);
runner = new Testrunner(server);

await deleteDatabaseData();
await initialiseDatabase();
});

describe("Must be correctly authorized to use any path", () => {
const newRegion = {
name: "new Region",
};

describe("Cannot reach any path without authorisation", () => {
beforeEach(() => {
runner.authLevel(AuthenticationLevel.UNAUTHORIZED);
});

test("Cannot reach GET /region", async () => {
await runner.get({
url: "/region",
expectedData: [forbiddenResponse],
statusCode: 403,
});
});

test("Cannot reach GET /region/:id", async () => {
await runner.get({
url: "/region/1",
expectedData: [forbiddenResponse],
statusCode: 403,
});
});

test("Cannot reach POST /region", async () => {
await runner.post({
url: "/region",
data: newRegion,
expectedResponse: forbiddenResponse,
statusCode: 403,
});
});

test("Cannot reach PATCH /region/:id", async () => {
await runner.patch({
url: "/region/1",
data: newRegion,
expectedResponse: forbiddenResponse,
statusCode: 403,
});
});

test("Cannot reach DELETE /region/:id", async () => {
await runner.delete({
url: "/region/1",
statusCode: 403,
});
});
});

describe("Cannot reach any path as a student", () => {
beforeEach(() => {
runner.authLevel(AuthenticationLevel.STUDENT);
});

test("Cannot reach GET /region", async () => {
await runner.get({
url: "/region",
expectedData: [forbiddenResponse],
statusCode: 403,
});
});

test("Cannot reach POST /region", async () => {
await runner.post({
url: "/region",
data: newRegion,
expectedResponse: forbiddenResponse,
statusCode: 403,
});
});

test("Cannot reach PATCH /region/:id", async () => {
await runner.patch({
url: "/region/1",
data: newRegion,
expectedResponse: forbiddenResponse,
statusCode: 403,
});
});

test("Cannot reach DELETE /region/:id", async () => {
await runner.delete({
url: "/region/1",
statusCode: 403,
});
});
});

describe("The requested path must exist", () => {
beforeEach(() => {
runner.authLevel(AuthenticationLevel.ADMINISTRATOR);
});

test("Find a nonexistent region", async () => {
await runner.get({
url: "/region/0",
expectedData: [notFoundResponse],
statusCode: 404,
});
});

test("Find a nonexistent region", async () => {
await runner.get({
url: "/region/-1",
expectedData: [notFoundResponse],
statusCode: 404,
});
});

test("Update a nonexistent region", async () => {
await runner.get({
url: "/region/0",
expectedData: [notFoundResponse],
statusCode: 404,
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hetzelfde comment hier als in mijn review van Round tests. Ofwel neem deze testen samen, ofwel noem ze anders.

test("Delete a nonexistent region", async () => {
await runner.delete({ url: "/region/0", statusCode: 404 });
});
});

describe("The type of region id must be correct", () => {
beforeEach(() => {
runner.authLevel(AuthenticationLevel.ADMINISTRATOR);
});

test("GET request", async () => {
await runner.get({
url: "/region/wrongtype",
expectedData: [badRequestResponse],
statusCode: 400,
});
});

test("PATCH request", async () => {
const newRegion = {
foo: "wrong name Region",
};

await runner.patch({
url: "/region/wrongtype",
data: newRegion,
expectedResponse: badRequestResponse,
statusCode: 400,
});
});

test("DELETE request", async () => {
await runner.delete({
url: "/region/wrongtype",
statusCode: 400,
});
});
});
});

afterAll(() => {
app.close();
});
});
});