From b80dd2d0425c372586aecc5535c790ceb8ec5a20 Mon Sep 17 00:00:00 2001 From: Andrew Qian Date: Thu, 31 Oct 2024 10:56:16 -0400 Subject: [PATCH] resolved problems with admin and auth test scripts --- client/src/auth/auth_utils.ts | 4 +++- server/src/admin/admin.controller.ts | 3 --- server/src/admin/admin.router.ts | 1 + server/tests/Admin.test.ts | 28 +++++++++++++--------------- server/tests/Auth.test.ts | 7 ++++++- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/client/src/auth/auth_utils.ts b/client/src/auth/auth_utils.ts index feeece7f..9234fc4c 100644 --- a/client/src/auth/auth_utils.ts +++ b/client/src/auth/auth_utils.ts @@ -127,7 +127,9 @@ export function useAuthOptionalLogin(): { const authToken = getAuthToken(); async function getEmail() { if (authToken && authToken !== '') { - const response = await axios.post('/api/auth/get-email'); + const response = await axios.post('/api/auth/get-email', { + token: authToken, + }); if (response.status === 200) { const email = response.data.result; setNetId(email.substring(0, email.lastIndexOf('@'))); diff --git a/server/src/admin/admin.controller.ts b/server/src/admin/admin.controller.ts index f5d9c152..d010ff87 100644 --- a/server/src/admin/admin.controller.ts +++ b/server/src/admin/admin.controller.ts @@ -1,10 +1,8 @@ import { findPendingReviews, findReportedReviews, - findAllReviewsAfterDate, removeReviewById, updateReviewVisibility, - findStudentById, updateCourseMetrics, findReviewCounts, createCourseCSV, @@ -17,7 +15,6 @@ import { AdminAddSemesterType, AdminPendingReviewType, AdminReviewVisibilityType, - RaffleWinnerRequestType, ReportReviewRequestType, UpdateCourseMetrics, VerifyAdminType, diff --git a/server/src/admin/admin.router.ts b/server/src/admin/admin.router.ts index 3c9296e5..9b43b457 100644 --- a/server/src/admin/admin.router.ts +++ b/server/src/admin/admin.router.ts @@ -199,6 +199,7 @@ adminRouter.post('/reviews/get-pending', async (req, res) => { try { const { token }: AdminRequestType = req.body; const auth = new Auth({ token }); + console.log(auth) const reviews = await getPendingReviews({ auth }); if (reviews === null) { return res.status(400).json({ diff --git a/server/tests/Admin.test.ts b/server/tests/Admin.test.ts index 4f2de41a..5dd161ee 100644 --- a/server/tests/Admin.test.ts +++ b/server/tests/Admin.test.ts @@ -4,13 +4,12 @@ import { beforeAll, afterAll } from 'vitest' import axios from 'axios'; import { Classes, Reviews } from '../db/schema'; -import { testServer, testPort } from './mocks/MockServer'; import * as AdminAuth from '../src/admin/admin.controller'; -import { testClasses, testReviews } from './mocks/InitMockDb'; +import { Auth } from "../src/auth/auth"; -const mockVerification = vi - .spyOn(AdminAuth, 'verifyTokenAdmin') - .mockImplementation(async ({ auth }) => true); +import { testServer, testPort } from './mocks/MockServer'; +import { mockVerificationTicket } from "./mocks/MockAuth"; +import { testClasses, testStudents, testReviews } from './mocks/InitMockDb'; const getReviewDifficultyMetric = async (review) => { const reviews = await Reviews.find({ @@ -32,7 +31,7 @@ const getReviewDifficultyMetric = async (review) => { beforeAll(async () => { await testServer.setUpDB( testReviews, - undefined, + testStudents, testClasses, undefined, undefined, @@ -41,21 +40,20 @@ beforeAll(async () => { afterAll(async () => { await testServer.shutdownTestingServer(); - mockVerification.mockRestore(); + await mockVerificationTicket.mockRestore(); }); describe('Admin functionality unit tests', () => { test('Fetching pending reviews works', async () => { const res = await axios.post( `http://localhost:${testPort}/api/admin/reviews/get-pending`, - { token: 'non-empty' }, + { token: 'fakeTokenDti1' }, ); const ids = res.data.result.map((i) => i._id); - const reviewsPending = await Reviews.findOne({ visible: 0, reported: 0 }).map( - (review) => review?._id, - ); + const pendingReview = await Reviews.findOne({ visible: 0, reported: 0 }) + const pendingId = pendingReview?._id - expect(ids.includes(reviewsPending)).toBeTruthy(); + expect(ids.includes(pendingId)).toBeTruthy(); }); test('Make review visible will not make a reported review visible', async () => { @@ -67,7 +65,7 @@ describe('Admin functionality unit tests', () => { const res = await axios .post(`http://localhost:${testPort}/api/admin/reviews/approve`, { review: pendingReportedReview, - token: 'non-empty', + token: 'fakeTokenDti1', }) .catch((e) => e); @@ -98,7 +96,7 @@ describe('Admin functionality unit tests', () => { `http://localhost:${testPort}/api/admin/reviews/restore`, { review: reportedReview, - token: 'non empty', + token: 'fakeTokenDti1', }, ); @@ -118,7 +116,7 @@ describe('Admin functionality unit tests', () => { const res = await axios .post(`http://localhost:${testPort}/api/admin/reviews/remove`, { review: reportedReview, - token: 'non empty', + token: 'fakeTokenDti1', }) .catch((e) => e); expect(res.status).toEqual(200); diff --git a/server/tests/Auth.test.ts b/server/tests/Auth.test.ts index 648800fb..c48e41a9 100644 --- a/server/tests/Auth.test.ts +++ b/server/tests/Auth.test.ts @@ -54,19 +54,24 @@ describe("Auth functionality unit tests", () => { }); test("tokenIsAdmin works correctly", async () => { + const getInvalidTokenMock = vi + .spyOn(Auth.prototype, "getToken") + .mockImplementation(() => "fakeTokencv4620"); + const failRes = await axios.post( `http://localhost:${testPort}/api/admin/token/validate`, { token: "fakeTokencv4620" }, ); expect(failRes.data.result).toEqual(false); + await getInvalidTokenMock.mockRestore(); const getValidTokenMock = vi .spyOn(Auth.prototype, "getToken") .mockImplementation(() => "fakeTokenDti1"); const successRes = await axios.post( - `http://localhost:${testPort}/api/tokenIsAdmin`, + `http://localhost:${testPort}/api/admin/token/validate`, { token: "fakeTokenDti1" }, );