From 7d6c372915bf80e9e4acc5b8a19a323aebd6df60 Mon Sep 17 00:00:00 2001 From: harrisonchin2523 Date: Sun, 3 Dec 2023 15:08:41 -0500 Subject: [PATCH] fixing insert review test --- common/index.d.ts | 1 + server/endpoints/Review.test.ts | 166 +++++++++++++++++++++++++------- 2 files changed, 130 insertions(+), 37 deletions(-) diff --git a/common/index.d.ts b/common/index.d.ts index df35cdef0..513514625 100644 --- a/common/index.d.ts +++ b/common/index.d.ts @@ -50,6 +50,7 @@ export interface Review { isCovid?: boolean; grade?: string; major?: string[]; + tags?: string[]; } export interface Professor { diff --git a/server/endpoints/Review.test.ts b/server/endpoints/Review.test.ts index 5259e626b..422bc4158 100644 --- a/server/endpoints/Review.test.ts +++ b/server/endpoints/Review.test.ts @@ -1,11 +1,11 @@ /* eslint-disable import/prefer-default-export */ -import axios from 'axios'; -import { TokenPayload } from 'google-auth-library'; +import axios from "axios"; +import { TokenPayload } from "google-auth-library"; -import { Review } from 'common'; +import { Review } from "common"; import { Reviews, Students } from "../dbDefs"; import * as Auth from "./Auth"; -import TestingServer from './TestServer'; +import TestingServer from "./TestServer"; const testingPort = 8080; const testServer = new TestingServer(testingPort); @@ -18,13 +18,39 @@ const testClasses = [ classTitle: "Object-Oriented Programming and Data Structures", classPrereq: [], classFull: "cs 2110 object-oriented programming and data structures", - classSems: ["FA14", "SP15", "SU15", "FA15", "SP16", "SU16", "FA16", "SP17", - "SU17", "FA17", "SP18", "FA18", "SU18", "SP19", "FA19", "SU19"], + classSems: [ + "FA14", + "SP15", + "SU15", + "FA15", + "SP16", + "SU16", + "FA16", + "SP17", + "SU17", + "FA17", + "SP18", + "FA18", + "SU18", + "SP19", + "FA19", + "SU19", + ], crossList: ["q75SxmqkTFEfaJwZ3"], - classProfessors: ["David Gries", "Douglas James", "Siddhartha Chaudhuri", - "Graeme Bailey", "John Foster", "Ross Tate", "Michael George", - "Eleanor Birrell", "Adrian Sampson", "Natacha Crooks", "Anne Bracy", - "Michael Clarkson"], + classProfessors: [ + "David Gries", + "Douglas James", + "Siddhartha Chaudhuri", + "Graeme Bailey", + "John Foster", + "Ross Tate", + "Michael George", + "Eleanor Birrell", + "Adrian Sampson", + "Natacha Crooks", + "Anne Bracy", + "Michael Clarkson", + ], classDifficulty: 2.9, classRating: null, classWorkload: 3, @@ -44,6 +70,7 @@ const testReviews: Review[] = [ reported: 0, likes: 2, likedBy: ["user1234", "user0"], + tags: ["Participation Matters"], }, { _id: "4Y8k7DnX3PLNdwRPq", @@ -54,11 +81,12 @@ const testReviews: Review[] = [ visible: 1, reported: 0, likedBy: [], + tags: [], }, ]; const validTokenPayload: TokenPayload = { - email: 'dti1@cornell.edu', + email: "dti1@cornell.edu", iss: undefined, sub: undefined, iat: undefined, @@ -67,12 +95,19 @@ const validTokenPayload: TokenPayload = { hd: "cornell.edu", }; -const mockVerificationTicket = jest.spyOn(Auth, 'getVerificationTicket') +const mockVerificationTicket = jest + .spyOn(Auth, "getVerificationTicket") .mockImplementation(async (token?: string) => validTokenPayload); beforeAll(async () => { // get mongoose all set up - await testServer.setUpDB(testReviews, undefined, testClasses, undefined, undefined); + await testServer.setUpDB( + testReviews, + undefined, + testClasses, + undefined, + undefined + ); }); afterAll(async () => { @@ -80,48 +115,84 @@ afterAll(async () => { await testServer.shutdownTestingServer(); }); -describe('tests', () => { - it('getReviewsByCourseId - getting review of class that exists (cs 2110)', async () => { - const res = await axios.post(`http://localhost:${testingPort}/v2/getReviewsByCourseId`, { courseId: "oH37S3mJ4eAsktypy" }); +describe("tests", () => { + it("getReviewsByCourseId - getting review of class that exists (cs 2110)", async () => { + const res = await axios.post( + `http://localhost:${testingPort}/v2/getReviewsByCourseId`, + { courseId: "oH37S3mJ4eAsktypy" } + ); expect(res.data.result.length).toBe(testReviews.length); const classOfReviews = testReviews.map((r) => r.class); - expect(res.data.result.map((r) => r.class).sort()).toEqual(classOfReviews.sort()); + expect(res.data.result.map((r) => r.class).sort()).toEqual( + classOfReviews.sort() + ); }); it("getReviewsByCourseId - getting review for a class that does not exist", async () => { - const res = await axios.post(`http://localhost:${testingPort}/v2/getReviewsByCourseId`, { courseId: "ert" }); - expect(res.data.result).toEqual({ error: 'Malformed Query' }); + const res = await axios.post( + `http://localhost:${testingPort}/v2/getReviewsByCourseId`, + { courseId: "ert" } + ); + expect(res.data.result).toEqual({ error: "Malformed Query" }); }); it("getCourseById - getting cs2110", async () => { - const res = await axios.post(`http://localhost:${testingPort}/v2/getCourseById`, { courseId: "oH37S3mJ4eAsktypy" }); + const res = await axios.post( + `http://localhost:${testingPort}/v2/getCourseById`, + { courseId: "oH37S3mJ4eAsktypy" } + ); expect(res.data.result._id).toBe("oH37S3mJ4eAsktypy"); - expect(res.data.result.classTitle).toBe("Object-Oriented Programming and Data Structures"); + expect(res.data.result.classTitle).toBe( + "Object-Oriented Programming and Data Structures" + ); }); - it('getCourseById - class does not exist', async () => { - const res = await axios.post(`http://localhost:${testingPort}/v2/getCourseById`, { courseId: "blah" }); + it("getCourseById - class does not exist", async () => { + const res = await axios.post( + `http://localhost:${testingPort}/v2/getCourseById`, + { courseId: "blah" } + ); expect(res.data.result).toBe(null); }); - it('getCourseByInfo - getting cs2110', async () => { - const res = await axios.post(`http://localhost:${testingPort}/v2/getCourseByInfo`, { subject: "cs", number: "2110" }); + it("getCourseByInfo - getting cs2110", async () => { + const res = await axios.post( + `http://localhost:${testingPort}/v2/getCourseByInfo`, + { subject: "cs", number: "2110" } + ); expect(res.data.result._id).toBe("oH37S3mJ4eAsktypy"); - expect(res.data.result.classTitle).toBe("Object-Oriented Programming and Data Structures"); + expect(res.data.result.classTitle).toBe( + "Object-Oriented Programming and Data Structures" + ); }); - it('getCourseByInfo - demonstrate regex irrelevance', async () => { + it("getCourseByInfo - demonstrate regex irrelevance", async () => { // Will not accept non-numeric: - const res1 = await axios.post(`http://localhost:${testingPort}/v2/getCourseByInfo`, { subject: "Vainamoinen", number: "ab2187c" }).catch((e) => e); + const res1 = await axios + .post(`http://localhost:${testingPort}/v2/getCourseByInfo`, { + subject: "Vainamoinen", + number: "ab2187c", + }) + .catch((e) => e); expect(res1.message).toBe("Request failed with status code 400"); // Will not accept non-ascii: - const res2 = await axios.post(`http://localhost:${testingPort}/v2/getCourseByInfo`, { subject: "向岛维纳默宁", number: "1234" }).catch((e) => e); + const res2 = await axios + .post(`http://localhost:${testingPort}/v2/getCourseByInfo`, { + subject: "向岛维纳默宁", + number: "1234", + }) + .catch((e) => e); expect(res2.message).toBe("Request failed with status code 400"); // Both also does not work: - const res3 = await axios.post(`http://localhost:${testingPort}/v2/getCourseByInfo`, { subject: "向岛维纳默宁", number: "ab2187c" }).catch((e) => e); + const res3 = await axios + .post(`http://localhost:${testingPort}/v2/getCourseByInfo`, { + subject: "向岛维纳默宁", + number: "ab2187c", + }) + .catch((e) => e); expect(res3.message).toBe("Request failed with status code 400"); }); @@ -138,9 +209,13 @@ describe('tests', () => { difficulty: 1, likedBy: [], rating: 4, + tags: ["Participation Matters"], }; - const res = await axios.post(`http://localhost:${testingPort}/v2/insertReview`, { classId: cs2110Id, review: reviewToInsert, token: "fakeTokenDti1" }); + const res = await axios.post( + `http://localhost:${testingPort}/v2/insertReview`, + { classId: cs2110Id, review: reviewToInsert, token: "fakeTokenDti1" } + ); expect(res.data.result.resCode).toBe(1); const reviews = await Reviews.find({ text: reviewToInsert.text }).exec(); expect(reviews.length).toBe(1); @@ -154,12 +229,18 @@ describe('tests', () => { }); it("like/dislike - increment and decrement", async () => { - const res1 = await axios.post(`http://localhost:${testingPort}/v2/updateLiked`, { id: "4Y8k7DnX3PLNdwRPr", token: "fakeTokenDti1" }); + const res1 = await axios.post( + `http://localhost:${testingPort}/v2/updateLiked`, + { id: "4Y8k7DnX3PLNdwRPr", token: "fakeTokenDti1" } + ); expect(res1.data.result.resCode).toBe(0); expect((await Reviews.findOne({ _id: "4Y8k7DnX3PLNdwRPr" })).likes).toBe(3); - const res2 = await axios.post(`http://localhost:${testingPort}/v2/updateLiked`, { id: "4Y8k7DnX3PLNdwRPr", token: "fakeTokenDti1" }); + const res2 = await axios.post( + `http://localhost:${testingPort}/v2/updateLiked`, + { id: "4Y8k7DnX3PLNdwRPr", token: "fakeTokenDti1" } + ); expect(res2.data.result.resCode).toBe(0); expect((await Reviews.findOne({ _id: "4Y8k7DnX3PLNdwRPr" })).likes).toBe(2); }); @@ -185,17 +266,28 @@ describe('tests', () => { family_name: user1.lastName, }; - const res = await axios.post(`http://localhost:${testingPort}/v2/insertUser`, { googleObject: gObj1 }); + const res = await axios.post( + `http://localhost:${testingPort}/v2/insertUser`, + { googleObject: gObj1 } + ); expect(res.data.result).toBe(1); - expect((await Students.find({}).exec()).filter((s) => s.netId === "cv4620").length).toBe(1); + expect( + (await Students.find({}).exec()).filter((s) => s.netId === "cv4620") + .length + ).toBe(1); }); it("user id's not being leaked by querying reviews", async () => { - const res = await axios.post(`http://localhost:${testingPort}/v2/getReviewsByCourseId`, { courseId: "oH37S3mJ4eAsktypy" }); + const res = await axios.post( + `http://localhost:${testingPort}/v2/getReviewsByCourseId`, + { courseId: "oH37S3mJ4eAsktypy" } + ); expect(res.data.result.length).toBe(testReviews.length); const classOfReviews = testReviews.map((r) => r.user); - expect(res.data.result.map((r) => r.user).sort()).not.toEqual(classOfReviews.sort()); + expect(res.data.result.map((r) => r.user).sort()).not.toEqual( + classOfReviews.sort() + ); expect(res.data.result.map((r) => r.user).sort()).toEqual(["", ""]); }); });