Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing insert review test #422

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
167 changes: 130 additions & 37 deletions server/endpoints/Review.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable comma-dangle */
/* 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 +19,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 +71,7 @@ const testReviews: Review[] = [
reported: 0,
likes: 2,
likedBy: ["user1234", "user0"],
tags: ["Participation Matters"],
},
{
_id: "4Y8k7DnX3PLNdwRPq",
Expand All @@ -54,11 +82,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 +96,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
);
});

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

Expand All @@ -138,9 +210,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);
Expand All @@ -154,12 +230,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 +267,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(["", ""]);
});
});
Loading