Skip to content

Commit

Permalink
fixing insert review test
Browse files Browse the repository at this point in the history
  • Loading branch information
harrisonchin2523 committed Dec 3, 2023
1 parent 2427e0d commit 7d6c372
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 37 deletions.
1 change: 1 addition & 0 deletions common/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface Review {
isCovid?: boolean;
grade?: string;
major?: string[];
tags?: string[];
}

export interface Professor {
Expand Down
166 changes: 129 additions & 37 deletions server/endpoints/Review.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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,
Expand All @@ -44,6 +70,7 @@ const testReviews: Review[] = [
reported: 0,
likes: 2,
likedBy: ["user1234", "user0"],
tags: ["Participation Matters"],
},
{
_id: "4Y8k7DnX3PLNdwRPq",
Expand All @@ -54,11 +81,12 @@ const testReviews: Review[] = [
visible: 1,
reported: 0,
likedBy: [],
tags: [],
},
];

const validTokenPayload: TokenPayload = {
email: '[email protected]',
email: "[email protected]",
iss: undefined,
sub: undefined,
iat: undefined,
Expand All @@ -67,61 +95,104 @@ 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

Check failure on line 109 in server/endpoints/Review.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
);
});

afterAll(async () => {
await mockVerificationTicket.mockRestore();
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" }

Check failure on line 122 in server/endpoints/Review.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
);
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()

Check failure on line 128 in server/endpoints/Review.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
);
});

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" }

Check failure on line 135 in server/endpoints/Review.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
);
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" }

Check failure on line 143 in server/endpoints/Review.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
);
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"

Check failure on line 147 in server/endpoints/Review.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
);
});

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" }

Check failure on line 154 in server/endpoints/Review.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
);
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" }

Check failure on line 162 in server/endpoints/Review.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
);
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"

Check failure on line 166 in server/endpoints/Review.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
);
});

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");
});

Expand All @@ -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" }

Check failure on line 217 in server/endpoints/Review.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
);
expect(res.data.result.resCode).toBe(1);
const reviews = await Reviews.find({ text: reviewToInsert.text }).exec();
expect(reviews.length).toBe(1);
Expand All @@ -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);
});
Expand All @@ -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(["", ""]);
});
});

0 comments on commit 7d6c372

Please sign in to comment.