CourseInsights / test / rest / Server.spec.ts
Server.spec.ts
Raw
import { expect } from "chai";
import request from "supertest";
import { StatusCodes } from "http-status-codes";
//import { Log } from "@ubccpsc310/project-support";
import { clearDisk, loadTestQuery } from "../TestUtil";
import Server from "../../src/rest/Server";
import * as fs from "fs-extra";
import { InsightDatasetKind } from "../../src/controller/IInsightFacade";

describe("Facade C3", function () {
	let server: Server;
	// let sections: string;
	// let campus: string;
	let sectionsBinary: any;
	let roomsBinary: any;
	let validQuery: any;
	let expectedValue: any;
	let invalidQuery: any;
	const port = 4321;
	const SERVER_URL = `http://localhost:${port}`;
	const validCode = StatusCodes.OK;
	const invalidCode = StatusCodes.BAD_REQUEST;
	const notFoundCode = StatusCodes.NOT_FOUND;

	before(async function () {
		// TODO: start server here once and handle errors properly
		await clearDisk();
		// sections = await getContentFromArchives("pair.zip");
		// campus = await getContentFromArchives("campus.zip");
		sectionsBinary = await fs.readFile("test/resources/archives/" + "pair.zip");
		roomsBinary = await fs.readFile("test/resources/archives/" + "campus.zip");
		const { input, expected } = await loadTestQuery("[valid/nestedLogic.json] nestedLogic");
		validQuery = input;
		expectedValue = expected;
		const { input: input1 } = await loadTestQuery("[invalid/doubleUnderscoreIsInvalid.json] doubleUnderscoreIsInvalid");
		invalidQuery = input1;

		server = new Server(port);
		await server.start();
	});

	after(async function () {
		// TODO: stop server here once!
		await server.stop();
		await clearDisk();
	});

	beforeEach(function () {
		// might want to add some process logging here to keep track of what is going on
	});

	afterEach(function () {
		// might want to add some process logging here to keep track of what is going on
	});

	// Sample on how to format PUT requests
	it("PUT test for courses dataset sections", async function () {
		const ENDPOINT_URL = "/dataset/sections/sections";
		const ZIP_FILE_DATA = sectionsBinary;

		try {
			const res = await request(SERVER_URL)
				.put(ENDPOINT_URL)
				.send(ZIP_FILE_DATA)
				.set("Content-Type", "application/x-zip-compressed");
			expect(res.status).to.be.equal(validCode);
			// TODO add assertions that check res.body
			expect(res.body.result).to.have.members(["sections"]);
		} catch (_err) {
			//Log.error(err);
			//console.error(err);
			expect.fail();
		}
	});

	it("PUT test for campus dataset rooms", async function () {
		const ENDPOINT_URL = "/dataset/rooms/rooms";
		const ZIP_FILE_DATA = roomsBinary;

		try {
			const res = await request(SERVER_URL)
				.put(ENDPOINT_URL)
				.send(ZIP_FILE_DATA)
				.set("Content-Type", "application/x-zip-compressed");
			expect(res.status).to.be.equal(validCode);
			expect(res.body.result).to.have.members(["sections", "rooms"]);
		} catch (_err) {
			//Log.error(err);
			//console.error(err);
			expect.fail();
		}
	});

	it("PUT test for courses dataset invalid", async function () {
		const ENDPOINT_URL = "/dataset/invalid/sections";
		const ZIP_FILE_DATA = roomsBinary;

		try {
			const res = await request(SERVER_URL)
				.put(ENDPOINT_URL)
				.send(ZIP_FILE_DATA)
				.set("Content-Type", "application/x-zip-compressed");
			expect(res.status).to.be.equal(invalidCode);
		} catch (_err) {
			//Log.error(err);
			//console.error(err);
			expect.fail();
		}
	});

	it("DELETE test for courses dataset", async function () {
		const ENDPOINT_URL = "/dataset/rooms";

		try {
			const res = await request(SERVER_URL).delete(ENDPOINT_URL);
			expect(res.status).to.be.equal(validCode);
			expect(res.body.result).to.be.equal("rooms");
		} catch (_err) {
			//Log.error(err);
			//console.error(err);
			expect.fail();
		}
	});

	it("DELETE test for courses dataset invalid not found", async function () {
		const ENDPOINT_URL = "/dataset/rooms";

		try {
			const res = await request(SERVER_URL).delete(ENDPOINT_URL);
			expect(res.status).to.be.equal(notFoundCode);
		} catch (_err) {
			//Log.error(err);
			//console.error(err);
			expect.fail();
		}
	});

	it("DELETE test for courses dataset invalid", async function () {
		const ENDPOINT_URL = "/dataset/ro_oms";

		try {
			const res = await request(SERVER_URL).delete(ENDPOINT_URL);
			expect(res.status).to.be.equal(invalidCode);
		} catch (_err) {
			//Log.error(err);
			//console.error(err);
			expect.fail();
		}
	});

	it("GET test for courses dataset", async function () {
		const ENDPOINT_URL = "/datasets";

		try {
			const res = await request(SERVER_URL).get(ENDPOINT_URL);
			expect(res.status).to.be.equal(validCode);
			expect(res.body.result).to.deep.equal([
				{
					id: "sections",
					kind: InsightDatasetKind.Sections,
					numRows: 64612,
				},
			]);
		} catch (_err) {
			//Log.error(err);
			//console.error(err);
			expect.fail();
		}
	});

	it("POST test for courses dataset", async function () {
		const ENDPOINT_URL = "/query";

		try {
			const res = await request(SERVER_URL).post(ENDPOINT_URL).send(validQuery).set("Content-Type", "application/json");
			expect(res.status).to.be.equal(validCode);
			const result = res.body.result;
			expect(result).to.have.deep.members(expectedValue);
		} catch (_err) {
			//Log.error(err);
			//console.error(err);
			expect.fail();
		}
	});

	it("POST test for courses dataset invalid", async function () {
		const ENDPOINT_URL = "/query";

		try {
			const res = await request(SERVER_URL)
				.post(ENDPOINT_URL)
				.send(invalidQuery)
				.set("Content-Type", "application/json");
			expect(res.status).to.be.equal(invalidCode);
		} catch (_err) {
			//Log.error(err);
			//console.error(err);
			expect.fail();
		}
	});

	// The other endpoints work similarly. You should be able to find all instructions in the supertest documentation
});