Skip to content

Commit

Permalink
resolved problems with admin and auth test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
qiandrewj committed Oct 31, 2024
1 parent 7305082 commit b80dd2d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
4 changes: 3 additions & 1 deletion client/src/auth/auth_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('@')));
Expand Down
3 changes: 0 additions & 3 deletions server/src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {
findPendingReviews,
findReportedReviews,
findAllReviewsAfterDate,
removeReviewById,
updateReviewVisibility,
findStudentById,
updateCourseMetrics,
findReviewCounts,
createCourseCSV,
Expand All @@ -17,7 +15,6 @@ import {
AdminAddSemesterType,
AdminPendingReviewType,
AdminReviewVisibilityType,
RaffleWinnerRequestType,
ReportReviewRequestType,
UpdateCourseMetrics,
VerifyAdminType,
Expand Down
1 change: 1 addition & 0 deletions server/src/admin/admin.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
28 changes: 13 additions & 15 deletions server/tests/Admin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -32,7 +31,7 @@ const getReviewDifficultyMetric = async (review) => {
beforeAll(async () => {
await testServer.setUpDB(
testReviews,
undefined,
testStudents,
testClasses,
undefined,
undefined,
Expand All @@ -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 () => {
Expand All @@ -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);

Expand Down Expand Up @@ -98,7 +96,7 @@ describe('Admin functionality unit tests', () => {
`http://localhost:${testPort}/api/admin/reviews/restore`,
{
review: reportedReview,
token: 'non empty',
token: 'fakeTokenDti1',
},
);

Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion server/tests/Auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
);

Expand Down

0 comments on commit b80dd2d

Please sign in to comment.