-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial tests of educator endpoints.
- Loading branch information
Showing
1 changed file
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
}); | ||
|
||
}); |