Skip to content

Commit

Permalink
Add initial tests of educator endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Oct 3, 2024
1 parent 26bbdfa commit f882606
Showing 1 changed file with 153 additions and 0 deletions.
153 changes: 153 additions & 0 deletions tests/educators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/* eslint-disable @typescript-eslint/no-floating-promises */

import { beforeAll, afterAll, describe, it, expect } from "@jest/globals";
import request from "supertest";
import type { InferAttributes, Sequelize } from "sequelize";
import type { Express } from "express";

import { authorize, getTestDatabaseConnection } from "./utils";
import { setupApp } from "../src/app";
import { Class, Educator, Student, StudentsClasses } from "../src/models";
import { createApp } from "../src/server";
import { v4 } from "uuid";

let testDB: Sequelize;
let testApp: Express;
beforeAll(async () => {
testDB = await getTestDatabaseConnection();
testApp = createApp(testDB);
setupApp(testApp, testDB);
});

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

describe("Test educator routes", () => {

it("Should sign up an educator (minimal info)", async () => {
const data = {
first_name: v4(),
last_name: v4(),
password: v4(),
email: v4(),
username: v4(),
};

await authorize(request(testApp).post("/educators/create"))
.send(data)
.expect(201)
.expect("Content-Type", /json/)
.expect({
success: true,
status: "ok",
educator_info: data,
});

const educator = await Educator.findOne({ where: { email: data.email } });
expect(educator).not.toBeNull();

await educator?.destroy();

});

it("Should sign up an educator (full info)", async () => {
const data = {
first_name: v4(),
last_name: v4(),
password: v4(),
email: v4(),
username: v4(),
institution: "Test School",
age: 5,
gender: "Male",
};

await authorize(request(testApp).post("/educators/create"))
.send(data)
.expect(201)
.expect("Content-Type", /json/)
.expect({
success: true,
status: "ok",
educator_info: data,
});

const educator = await Educator.findOne({ where: { email: data.email } });
expect(educator).not.toBeNull();

await educator?.destroy();

});

it("Should return the correct educator by ID", async () => {
const educator = await Educator.create({
first_name: v4(),
last_name: v4(),
password: v4(),
email: v4(),
username: v4(),
verified: 1,
verification_code: "abcde",
});

const json: Partial<InferAttributes<Educator>> = educator.toJSON();
// The Sequelize object will return the `CURRENT_TIMESTAMP` literals,
// not the actual date values
delete json.profile_created;
delete json.last_visit;

await authorize(request(testApp).get(`/educators/${educator.id}`))
.expect(200)
.expect("Content-Type", /json/)
.then((res) => {
const resEducator = res.body.educator;
expect(resEducator).toMatchObject(json);

// Check that the timestamp fields are present
expect(resEducator).toHaveProperty("profile_created");
expect(typeof resEducator.profile_created).toBe("string");
expect(resEducator).toHaveProperty("last_visit");
expect(typeof resEducator.last_visit).toBe("string");
});

await educator.destroy();

});

it("Should return the correct educator by username", async () => {
const educator = await Educator.create({
first_name: v4(),
last_name: v4(),
password: v4(),
email: v4(),
username: v4(),
verified: 1,
verification_code: v4(),
});

const json: Partial<InferAttributes<Educator>> = educator.toJSON();
// The Sequelize object will return the `CURRENT_TIMESTAMP` literals,
// not the actual date values
delete json.profile_created;
delete json.last_visit;

await authorize(request(testApp).get(`/educators/${educator.username}`))
.expect(200)
.expect("Content-Type", /json/)
.then((res) => {
const resEducator = res.body.educator;
expect(resEducator).toMatchObject(json);

// Check that the timestamp fields are present
expect(resEducator).toHaveProperty("profile_created");
expect(typeof resEducator.profile_created).toBe("string");
expect(resEducator).toHaveProperty("last_visit");
expect(typeof resEducator.last_visit).toBe("string");
});

await educator.destroy();

});

});

0 comments on commit f882606

Please sign in to comment.