refactor: use bearer auth instead of uid auth for tests (@fehmer) (#6318)

This commit is contained in:
Christian Fehmer 2025-03-03 12:08:30 +01:00 committed by GitHub
parent 3a5b3783f2
commit 660d856f96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 404 additions and 346 deletions

View file

@ -4,6 +4,8 @@ import { hash } from "bcrypt";
import { ObjectId } from "mongodb";
import { base64UrlEncode } from "../../src/utils/misc";
import * as ApeKeyDal from "../../src/dal/ape-keys";
import { DecodedIdToken } from "firebase-admin/auth";
import * as AuthUtils from "../../src/utils/auth";
export async function mockAuthenticateWithApeKey(
uid: string,
@ -35,3 +37,45 @@ export async function mockAuthenticateWithApeKey(
return base64UrlEncode(`${apeKeyId}.${apiKey}`);
}
export function mockBearerAuthentication(uid: string) {
const mockDecodedToken = {
uid,
email: "newuser@mail.com",
iat: Date.now(),
} as DecodedIdToken;
const verifyIdTokenMock = vi.spyOn(AuthUtils, "verifyIdToken");
return {
/**
* Reset the mock and return a default token. Call this method in the `beforeEach` of all tests.
*/
beforeEach: (): void => {
verifyIdTokenMock.mockReset();
verifyIdTokenMock.mockResolvedValue(mockDecodedToken);
},
/**
* Reset the mock results in the authentication to fail.
*/
noAuth: (): void => {
verifyIdTokenMock.mockReset();
},
/**
* verify the authentication has been called
*/
expectToHaveBeenCalled: (): void => {
expect(verifyIdTokenMock).toHaveBeenCalled();
},
/**
* modify the token returned by the mock. This can be used to e.g. return a stale token.
* @param customize
*/
modifyToken: (customize: Partial<DecodedIdToken>): void => {
verifyIdTokenMock.mockReset();
verifyIdTokenMock.mockResolvedValue({
...mockDecodedToken,
...customize,
});
},
};
}

View file

@ -9,10 +9,12 @@ import GeorgeQueue from "../../../src/queues/george-queue";
import * as AuthUtil from "../../../src/utils/auth";
import _ from "lodash";
import { enableRateLimitExpects } from "../../__testData__/rate-limit";
import { mockBearerAuthentication } from "../../__testData__/auth";
const mockApp = request(app);
const configuration = Configuration.getCachedConfiguration();
const uid = new ObjectId().toHexString();
const mockAuth = mockBearerAuthentication(uid);
enableRateLimitExpects();
describe("AdminController", () => {
@ -22,6 +24,7 @@ describe("AdminController", () => {
isAdminMock.mockReset();
await enableAdminEndpoints(true);
isAdminMock.mockResolvedValue(true);
mockAuth.beforeEach();
});
describe("check for admin", () => {
@ -31,7 +34,7 @@ describe("AdminController", () => {
//WHEN
const { body } = await mockApp
.get("/admin")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -44,17 +47,17 @@ describe("AdminController", () => {
});
it("should fail if user is no admin", async () => {
await expectFailForNonAdmin(
mockApp.get("/admin").set("authorization", `Uid ${uid}`)
mockApp.get("/admin").set("Authorization", `Bearer ${uid}`)
);
});
it("should fail if admin endpoints are disabled", async () => {
await expectFailForDisabledEndpoint(
mockApp.get("/admin").set("authorization", `Uid ${uid}`)
mockApp.get("/admin").set("Authorization", `Bearer ${uid}`)
);
});
it("should be rate limited", async () => {
await expect(
mockApp.get("/admin").set("authorization", `Uid ${uid}`)
mockApp.get("/admin").set("Authorization", `Bearer ${uid}`)
).toBeRateLimited({ max: 1, windowMs: 5000 });
});
});
@ -82,7 +85,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/toggleBan")
.send({ uid: victimUid })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -109,7 +112,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/toggleBan")
.send({ uid: victimUid })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -132,7 +135,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/toggleBan")
.send({})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -148,7 +151,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/toggleBan")
.send({ uid: new ObjectId().toHexString(), extra: "value" })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -162,7 +165,7 @@ describe("AdminController", () => {
mockApp
.post("/admin/toggleBan")
.send({ uid: new ObjectId().toHexString() })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
it("should fail if admin endpoints are disabled", async () => {
@ -171,7 +174,7 @@ describe("AdminController", () => {
mockApp
.post("/admin/toggleBan")
.send({ uid: new ObjectId().toHexString() })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
it("should be rate limited", async () => {
@ -187,7 +190,7 @@ describe("AdminController", () => {
mockApp
.post("/admin/toggleBan")
.send({ uid: victimUid })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
).toBeRateLimited({ max: 1, windowMs: 5000 });
});
});
@ -220,7 +223,7 @@ describe("AdminController", () => {
.send({
reports: [{ reportId: reportOne.id }, { reportId: reportTwo.id }],
})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
expect(body).toEqual({
@ -236,7 +239,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/report/accept")
.send({})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -249,7 +252,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/report/accept")
.send({ reports: [] })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -264,7 +267,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/report/accept")
.send({ reports: [{ reportId: "1", extra2: "value" }], extra: "value" })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -280,7 +283,7 @@ describe("AdminController", () => {
mockApp
.post("/admin/report/accept")
.send({ reports: [] })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
it("should fail if admin endpoints are disabled", async () => {
@ -289,7 +292,7 @@ describe("AdminController", () => {
mockApp
.post("/admin/report/accept")
.send({ reports: [] })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
it("should be rate limited", async () => {
@ -301,7 +304,7 @@ describe("AdminController", () => {
mockApp
.post("/admin/report/accept")
.send({ reports: [{ reportId: "1" }] })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
).toBeRateLimited({ max: 1, windowMs: 5000 });
});
});
@ -337,7 +340,7 @@ describe("AdminController", () => {
{ reportId: reportTwo.id },
],
})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
expect(body).toEqual({
@ -353,7 +356,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/report/reject")
.send({})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -366,7 +369,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/report/reject")
.send({ reports: [] })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -381,7 +384,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/report/reject")
.send({ reports: [{ reportId: "1", extra2: "value" }], extra: "value" })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -397,7 +400,7 @@ describe("AdminController", () => {
mockApp
.post("/admin/report/reject")
.send({ reports: [] })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
it("should fail if admin endpoints are disabled", async () => {
@ -406,7 +409,7 @@ describe("AdminController", () => {
mockApp
.post("/admin/report/reject")
.send({ reports: [] })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
it("should be rate limited", async () => {
@ -418,7 +421,7 @@ describe("AdminController", () => {
mockApp
.post("/admin/report/reject")
.send({ reports: [{ reportId: "1" }] })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
).toBeRateLimited({ max: 1, windowMs: 5000 });
});
});
@ -439,7 +442,7 @@ describe("AdminController", () => {
const { body } = await mockApp
.post("/admin/sendForgotPasswordEmail")
.send({ email: "meowdec@example.com" })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -458,7 +461,7 @@ describe("AdminController", () => {
mockApp
.post("/admin/sendForgotPasswordEmail")
.send({ email: "meowdec@example.com" })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
).toBeRateLimited({ max: 1, windowMs: 5000 });
});
});

View file

@ -5,10 +5,12 @@ import { ObjectId } from "mongodb";
import * as Configuration from "../../../src/init/configuration";
import * as UserDal from "../../../src/dal/user";
import _ from "lodash";
import { mockBearerAuthentication } from "../../__testData__/auth";
const mockApp = request(app);
const configuration = Configuration.getCachedConfiguration();
const uid = new ObjectId().toHexString();
const mockAuth = mockBearerAuthentication(uid);
describe("ApeKeyController", () => {
const getUserMock = vi.spyOn(UserDal, "getPartialUser");
@ -18,6 +20,7 @@ describe("ApeKeyController", () => {
getUserMock.mockResolvedValue(user(uid, {}));
vi.useFakeTimers();
vi.setSystemTime(1000);
mockAuth.beforeEach();
});
afterEach(() => {
@ -41,7 +44,7 @@ describe("ApeKeyController", () => {
//WHEN
const { body } = await mockApp
.get("/ape-keys")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -66,12 +69,12 @@ describe("ApeKeyController", () => {
});
it("should fail if apeKeys endpoints are disabled", async () => {
await expectFailForDisabledEndpoint(
mockApp.get("/ape-keys").set("authorization", `Uid ${uid}`)
mockApp.get("/ape-keys").set("Authorization", `Bearer ${uid}`)
);
});
it("should fail if user has no apeKey permissions", async () => {
await expectFailForNoPermissions(
mockApp.get("/ape-keys").set("authorization", `Uid ${uid}`)
mockApp.get("/ape-keys").set("Authorization", `Bearer ${uid}`)
);
});
});
@ -96,7 +99,7 @@ describe("ApeKeyController", () => {
//WHEN
const { body } = await mockApp
.post("/ape-keys")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ name: "test", enabled: true })
.expect(200);
@ -131,7 +134,7 @@ describe("ApeKeyController", () => {
const { body } = await mockApp
.post("/ape-keys")
.send({})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -145,7 +148,7 @@ describe("ApeKeyController", () => {
const { body } = await mockApp
.post("/ape-keys")
.send({ name: "test", enabled: true, extra: "value" })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -163,7 +166,7 @@ describe("ApeKeyController", () => {
const { body } = await mockApp
.post("/ape-keys")
.send({ name: "test", enabled: false })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(409);
//THEN
@ -176,7 +179,7 @@ describe("ApeKeyController", () => {
mockApp
.post("/ape-keys")
.send({ name: "test", enabled: false })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
it("should fail if user has no apeKey permissions", async () => {
@ -184,7 +187,7 @@ describe("ApeKeyController", () => {
mockApp
.post("/ape-keys")
.send({ name: "test", enabled: false })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
});
@ -205,7 +208,7 @@ describe("ApeKeyController", () => {
const { body } = await mockApp
.patch(`/ape-keys/${apeKeyId}`)
.send({ name: "new", enabled: false })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -220,7 +223,7 @@ describe("ApeKeyController", () => {
const { body } = await mockApp
.patch(`/ape-keys/${apeKeyId}`)
.send({ name: "new" })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -238,7 +241,7 @@ describe("ApeKeyController", () => {
//WHEN
await mockApp
.patch(`/ape-keys/`)
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(404);
});
it("should fail with extra properties", async () => {
@ -248,7 +251,7 @@ describe("ApeKeyController", () => {
const { body } = await mockApp
.patch(`/ape-keys/${apeKeyId}`)
.send({ name: "new", extra: "value" })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -262,7 +265,7 @@ describe("ApeKeyController", () => {
mockApp
.patch(`/ape-keys/${apeKeyId}`)
.send({ name: "test", enabled: false })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
it("should fail if user has no apeKey permissions", async () => {
@ -270,7 +273,7 @@ describe("ApeKeyController", () => {
mockApp
.patch(`/ape-keys/${apeKeyId}`)
.send({ name: "test", enabled: false })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
});
@ -289,7 +292,7 @@ describe("ApeKeyController", () => {
//WHEN
const { body } = await mockApp
.delete(`/ape-keys/${apeKeyId}`)
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -302,14 +305,14 @@ describe("ApeKeyController", () => {
//WHEN
await mockApp
.delete(`/ape-keys/`)
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(404);
});
it("should fail if apeKeys endpoints are disabled", async () => {
await expectFailForDisabledEndpoint(
mockApp
.delete(`/ape-keys/${apeKeyId}`)
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
@ -317,7 +320,7 @@ describe("ApeKeyController", () => {
await expectFailForNoPermissions(
mockApp
.delete(`/ape-keys/${apeKeyId}`)
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
);
});
});

View file

@ -2,9 +2,15 @@ import request from "supertest";
import app from "../../../src/app";
import * as ConfigDal from "../../../src/dal/config";
import { ObjectId } from "mongodb";
import { mockBearerAuthentication } from "../../__testData__/auth";
const mockApp = request(app);
const uid = new ObjectId().toHexString();
const mockAuth = mockBearerAuthentication(uid);
describe("ConfigController", () => {
beforeEach(() => {
mockAuth.beforeEach();
});
describe("get config", () => {
const getConfigMock = vi.spyOn(ConfigDal, "getConfig");
@ -16,14 +22,14 @@ describe("ConfigController", () => {
//GIVEN
getConfigMock.mockResolvedValue({
_id: new ObjectId(),
uid: "123456789",
uid: uid,
config: { language: "english" },
});
//WHEN
const { body } = await mockApp
.get("/configs")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -32,7 +38,7 @@ describe("ConfigController", () => {
data: { language: "english" },
});
expect(getConfigMock).toHaveBeenCalledWith("123456789");
expect(getConfigMock).toHaveBeenCalledWith(uid);
});
});
describe("update config", () => {
@ -49,7 +55,7 @@ describe("ConfigController", () => {
//WHEN
const { body } = await mockApp
.patch("/configs")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({ language: "english" })
.expect(200);
@ -60,7 +66,7 @@ describe("ConfigController", () => {
data: null,
});
expect(saveConfigMock).toHaveBeenCalledWith("123456789", {
expect(saveConfigMock).toHaveBeenCalledWith(uid, {
language: "english",
});
});
@ -68,7 +74,7 @@ describe("ConfigController", () => {
//WHEN
const { body } = await mockApp
.patch("/configs")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({ unknownValue: "unknown" })
.expect(422);
@ -85,7 +91,7 @@ describe("ConfigController", () => {
//WHEN
const { body } = await mockApp
.patch("/configs")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({ autoSwitchTheme: "yes", confidenceMode: "pretty" })
.expect(422);
@ -117,7 +123,7 @@ describe("ConfigController", () => {
const { body } = await mockApp
.delete("/configs")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -126,7 +132,7 @@ describe("ConfigController", () => {
data: null,
});
expect(deleteConfigMock).toHaveBeenCalledWith("123456789");
expect(deleteConfigMock).toHaveBeenCalledWith(uid);
});
});
});

View file

@ -8,26 +8,20 @@ import * as Configuration from "../../../src/init/configuration";
import type { Configuration as ConfigurationType } from "@monkeytype/contracts/schemas/configuration";
import { ObjectId } from "mongodb";
import * as Misc from "../../../src/utils/misc";
import { DecodedIdToken } from "firebase-admin/auth";
import * as AuthUtils from "../../../src/utils/auth";
import * as AdminUuids from "../../../src/dal/admin-uids";
import { mockBearerAuthentication } from "../../__testData__/auth";
const mockApp = request(app);
const uid = new ObjectId().toHexString();
const mockDecodedToken = {
uid,
email: "newuser@mail.com",
iat: 0,
} as DecodedIdToken;
describe("Configuration Controller", () => {
const isDevEnvironmentMock = vi.spyOn(Misc, "isDevEnvironment");
const verifyIdTokenMock = vi.spyOn(AuthUtils, "verifyIdToken");
const mockAuth = mockBearerAuthentication(uid);
const isAdminMock = vi.spyOn(AdminUuids, "isAdmin");
beforeEach(() => {
isAdminMock.mockReset();
verifyIdTokenMock.mockReset();
mockAuth.beforeEach();
isDevEnvironmentMock.mockReset();
isDevEnvironmentMock.mockReturnValue(true);
@ -52,7 +46,7 @@ describe("Configuration Controller", () => {
describe("getConfigurationSchema", () => {
it("should get without authentication on dev", async () => {
//GIVEN
mockAuth.noAuth();
//WHEN
const { body } = await mockApp.get("/configuration/schema").expect(200);
@ -73,7 +67,6 @@ describe("Configuration Controller", () => {
it("should get with authentication on prod", async () => {
//GIVEN
isDevEnvironmentMock.mockReturnValue(false);
verifyIdTokenMock.mockResolvedValue(mockDecodedToken);
//WHEN
const { body } = await mockApp
@ -87,12 +80,11 @@ describe("Configuration Controller", () => {
data: CONFIGURATION_FORM_SCHEMA,
});
expect(verifyIdTokenMock).toHaveBeenCalled();
mockAuth.expectToHaveBeenCalled();
});
it("should fail with non-admin user on prod", async () => {
//GIVEN
isDevEnvironmentMock.mockReturnValue(false);
verifyIdTokenMock.mockResolvedValue(mockDecodedToken);
isAdminMock.mockResolvedValue(false);
//WHEN
@ -103,7 +95,7 @@ describe("Configuration Controller", () => {
//THEN
expect(body.message).toEqual("You don't have permission to do this.");
expect(verifyIdTokenMock).toHaveBeenCalled();
mockAuth.expectToHaveBeenCalled();
expect(isAdminMock).toHaveBeenCalledWith(uid);
});
});
@ -120,6 +112,7 @@ describe("Configuration Controller", () => {
it("should update without authentication on dev", async () => {
//GIVEN
mockAuth.noAuth();
const patch = {
users: {
premium: {
@ -145,6 +138,7 @@ describe("Configuration Controller", () => {
it("should fail update without authentication on prod", async () => {
//GIVEN
mockAuth.noAuth();
isDevEnvironmentMock.mockReturnValue(false);
//WHEN
@ -159,7 +153,6 @@ describe("Configuration Controller", () => {
it("should update with authentication on prod", async () => {
//GIVEN
isDevEnvironmentMock.mockReturnValue(false);
verifyIdTokenMock.mockResolvedValue(mockDecodedToken);
//WHEN
await mockApp
@ -170,14 +163,13 @@ describe("Configuration Controller", () => {
//THEN
expect(patchConfigurationMock).toHaveBeenCalled();
expect(verifyIdTokenMock).toHaveBeenCalled();
mockAuth.expectToHaveBeenCalled();
});
it("should fail for non admin users on prod", async () => {
//GIVEN
isDevEnvironmentMock.mockReturnValue(false);
isAdminMock.mockResolvedValue(false);
verifyIdTokenMock.mockResolvedValue(mockDecodedToken);
//WHEN
await mockApp

View file

@ -6,12 +6,16 @@ import * as LeaderboardDal from "../../../src/dal/leaderboards";
import * as DailyLeaderboards from "../../../src/utils/daily-leaderboards";
import * as WeeklyXpLeaderboard from "../../../src/services/weekly-xp-leaderboard";
import * as Configuration from "../../../src/init/configuration";
import { mockAuthenticateWithApeKey } from "../../__testData__/auth";
import {
mockAuthenticateWithApeKey,
mockBearerAuthentication,
} from "../../__testData__/auth";
import { XpLeaderboardEntry } from "@monkeytype/contracts/schemas/leaderboards";
const mockApp = request(app);
const configuration = Configuration.getCachedConfiguration();
const uid = new ObjectId().toHexString();
const mockAuth = mockBearerAuthentication(uid);
const allModes = [
"10",
@ -27,6 +31,9 @@ const allModes = [
];
describe("Loaderboard Controller", () => {
beforeEach(() => {
mockAuth.beforeEach();
});
describe("get leaderboard", () => {
const getLeaderboardMock = vi.spyOn(LeaderboardDal, "get");
@ -260,7 +267,7 @@ describe("Loaderboard Controller", () => {
const { body } = await mockApp
.get("/leaderboards/rank")
.query({ language: "english", mode: "time", mode2: "60" })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -291,7 +298,7 @@ describe("Loaderboard Controller", () => {
for (const mode of ["time", "words", "quote", "zen", "custom"]) {
const response = await mockApp
.get("/leaderboards/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.query({ language: "english", mode, mode2: "custom" });
expect(response.status, "for mode " + mode).toEqual(200);
}
@ -302,7 +309,7 @@ describe("Loaderboard Controller", () => {
for (const mode2 of allModes) {
const response = await mockApp
.get("/leaderboards/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.query({ language: "english", mode: "words", mode2 });
expect(response.status, "for mode2 " + mode2).toEqual(200);
@ -311,7 +318,7 @@ describe("Loaderboard Controller", () => {
it("fails for missing query", async () => {
const { body } = await mockApp
.get("/leaderboards/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -331,7 +338,7 @@ describe("Loaderboard Controller", () => {
mode: "unknownMode",
mode2: "unknownMode2",
})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -352,7 +359,7 @@ describe("Loaderboard Controller", () => {
mode2: "60",
extra: "value",
})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -372,7 +379,7 @@ describe("Loaderboard Controller", () => {
mode: "time",
mode2: "60",
})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(503);
expect(body.message).toEqual(
@ -743,7 +750,7 @@ describe("Loaderboard Controller", () => {
//WHEN
const { body } = await mockApp
.get("/leaderboards/daily/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.query({ language: "english", mode: "time", mode2: "60" })
.expect(200);
@ -768,7 +775,7 @@ describe("Loaderboard Controller", () => {
const { body } = await mockApp
.get("/leaderboards/daily/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(503);
expect(body.message).toEqual(
@ -779,7 +786,7 @@ describe("Loaderboard Controller", () => {
for (const mode of ["time", "words", "quote", "zen", "custom"]) {
const response = await mockApp
.get("/leaderboards/daily/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.query({ language: "english", mode, mode2: "custom" });
expect(response.status, "for mode " + mode).toEqual(200);
}
@ -788,7 +795,7 @@ describe("Loaderboard Controller", () => {
for (const mode2 of allModes) {
const response = await mockApp
.get("/leaderboards/daily/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.query({ language: "english", mode: "words", mode2 });
expect(response.status, "for mode2 " + mode2).toEqual(200);
@ -797,7 +804,7 @@ describe("Loaderboard Controller", () => {
it("fails for missing query", async () => {
const { body } = await mockApp
.get("/leaderboards/daily/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -817,7 +824,7 @@ describe("Loaderboard Controller", () => {
mode: "unknownMode",
mode2: "unknownMode2",
})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -838,7 +845,7 @@ describe("Loaderboard Controller", () => {
mode2: "60",
extra: "value",
})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -853,7 +860,7 @@ describe("Loaderboard Controller", () => {
//WHEN
const { body } = await mockApp
.get("/leaderboards/daily/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.query({
language: "english",
mode: "time",
@ -1098,7 +1105,7 @@ describe("Loaderboard Controller", () => {
//WHEN
const { body } = await mockApp
.get("/leaderboards/xp/weekly/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -1136,7 +1143,7 @@ describe("Loaderboard Controller", () => {
const { body } = await mockApp
.get("/leaderboards/xp/weekly/rank")
.query({ weeksBefore: 1 })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -1157,7 +1164,7 @@ describe("Loaderboard Controller", () => {
const { body } = await mockApp
.get("/leaderboards/xp/weekly/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(503);
expect(body.message).toEqual(
@ -1168,7 +1175,7 @@ describe("Loaderboard Controller", () => {
it("fails for weeksBefore not one", async () => {
const { body } = await mockApp
.get("/leaderboards/xp/weekly/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.query({
weeksBefore: 2,
})
@ -1183,7 +1190,7 @@ describe("Loaderboard Controller", () => {
it("fails for unknown query", async () => {
const { body } = await mockApp
.get("/leaderboards/xp/weekly/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.query({
extra: "value",
})
@ -1202,7 +1209,7 @@ describe("Loaderboard Controller", () => {
//WHEN
const { body } = await mockApp
.get("/leaderboards/xp/weekly/rank")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(404);
expect(body.message).toEqual("XP leaderboard for this week not found.");

View file

@ -2,9 +2,16 @@ import request from "supertest";
import app from "../../../src/app";
import * as PresetDal from "../../../src/dal/preset";
import { ObjectId } from "mongodb";
import { mockBearerAuthentication } from "../../__testData__/auth";
const mockApp = request(app);
const uid = new ObjectId().toHexString();
const mockAuth = mockBearerAuthentication(uid);
describe("PresetController", () => {
beforeEach(() => {
mockAuth.beforeEach();
});
describe("get presets", () => {
const getPresetsMock = vi.spyOn(PresetDal, "getPresets");
@ -16,13 +23,13 @@ describe("PresetController", () => {
//GIVEN
const presetOne = {
_id: new ObjectId(),
uid: "123456789",
uid: uid,
name: "test1",
config: { language: "english" },
};
const presetTwo = {
_id: new ObjectId(),
uid: "123456789",
uid: uid,
name: "test2",
settingGroups: ["hideElements"],
config: {
@ -38,7 +45,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.get("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -64,7 +71,7 @@ describe("PresetController", () => {
],
});
expect(getPresetsMock).toHaveBeenCalledWith("123456789");
expect(getPresetsMock).toHaveBeenCalledWith(uid);
});
it("should return empty array if user has no presets", async () => {
//GIVEN
@ -73,7 +80,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.get("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -82,7 +89,7 @@ describe("PresetController", () => {
data: [],
});
expect(getPresetsMock).toHaveBeenCalledWith("123456789");
expect(getPresetsMock).toHaveBeenCalledWith(uid);
});
});
@ -100,7 +107,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.post("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({
name: "new",
@ -117,7 +124,7 @@ describe("PresetController", () => {
data: { presetId: "1" },
});
expect(addPresetMock).toHaveBeenCalledWith("123456789", {
expect(addPresetMock).toHaveBeenCalledWith(uid, {
name: "new",
config: { language: "english", tags: ["one", "two"] },
});
@ -129,7 +136,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.post("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({
name: "new",
@ -149,7 +156,7 @@ describe("PresetController", () => {
data: { presetId: "1" },
});
expect(addPresetMock).toHaveBeenCalledWith("123456789", {
expect(addPresetMock).toHaveBeenCalledWith(uid, {
name: "new",
settingGroups: ["hideElements"],
config: {
@ -164,7 +171,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.post("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({
name: "update",
@ -189,7 +196,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.post("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({ name: "new", config: {} })
.expect(200);
@ -200,7 +207,7 @@ describe("PresetController", () => {
data: { presetId: "1" },
});
expect(addPresetMock).toHaveBeenCalledWith("123456789", {
expect(addPresetMock).toHaveBeenCalledWith(uid, {
name: "new",
config: {},
});
@ -209,7 +216,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.post("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({})
.expect(422);
@ -224,7 +231,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.post("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({
_id: "1",
@ -255,7 +262,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.post("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({
name: "new",
@ -293,7 +300,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.patch("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({
_id: "1",
@ -311,7 +318,7 @@ describe("PresetController", () => {
data: null,
});
expect(editPresetMock).toHaveBeenCalledWith("123456789", {
expect(editPresetMock).toHaveBeenCalledWith(uid, {
_id: "1",
name: "new",
config: { language: "english", tags: ["one", "two"] },
@ -324,7 +331,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.patch("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({
_id: "1",
@ -345,7 +352,7 @@ describe("PresetController", () => {
data: null,
});
expect(editPresetMock).toHaveBeenCalledWith("123456789", {
expect(editPresetMock).toHaveBeenCalledWith(uid, {
_id: "1",
name: "new",
settingGroups: ["hideElements"],
@ -365,7 +372,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.patch("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({ _id: "1", name: "new", config: {} })
.expect(200);
@ -376,7 +383,7 @@ describe("PresetController", () => {
data: null,
});
expect(editPresetMock).toHaveBeenCalledWith("123456789", {
expect(editPresetMock).toHaveBeenCalledWith(uid, {
_id: "1",
name: "new",
config: {},
@ -386,7 +393,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.patch("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({})
.expect(422);
@ -401,7 +408,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.patch("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({
_id: "1",
@ -434,7 +441,7 @@ describe("PresetController", () => {
//WHEN
const { body } = await mockApp
.patch("/presets")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.accept("application/json")
.send({
_id: "1",
@ -473,7 +480,7 @@ describe("PresetController", () => {
const { body } = await mockApp
.delete("/presets/1")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -482,7 +489,7 @@ describe("PresetController", () => {
data: null,
});
expect(deletePresetMock).toHaveBeenCalledWith("123456789", "1");
expect(deletePresetMock).toHaveBeenCalledWith(uid, "1");
});
it("should fail without preset _id", async () => {
//GIVEN
@ -491,7 +498,7 @@ describe("PresetController", () => {
//WHEN
await mockApp
.delete("/presets/")
.set("authorization", "Uid 123456789")
.set("Authorization", `Bearer ${uid}`)
.expect(404);
expect(deletePresetMock).not.toHaveBeenCalled();

View file

@ -3,7 +3,10 @@ import app from "../../../src/app";
import * as PsaDal from "../../../src/dal/psa";
import * as Prometheus from "../../../src/utils/prometheus";
import { ObjectId } from "mongodb";
import { mockBearerAuthentication } from "../../__testData__/auth";
const mockApp = request(app);
const uid = new ObjectId().toHexString();
const mockAuth = mockBearerAuthentication(uid);
describe("Psa Controller", () => {
describe("get psa", () => {
@ -13,6 +16,7 @@ describe("Psa Controller", () => {
afterEach(() => {
getPsaMock.mockReset();
recordClientVersionMock.mockReset();
mockAuth.beforeEach();
});
it("get psas without authorization", async () => {
@ -62,7 +66,7 @@ describe("Psa Controller", () => {
it("get psas with authorization", async () => {
await mockApp
.get("/psas")
.set("authorization", `Uid 123456789`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
});

View file

@ -10,11 +10,13 @@ import * as Captcha from "../../../src/utils/captcha";
import { ObjectId } from "mongodb";
import _ from "lodash";
import { ApproveQuote } from "@monkeytype/contracts/schemas/quotes";
import { mockBearerAuthentication } from "../../__testData__/auth";
const mockApp = request(app);
const configuration = Configuration.getCachedConfiguration();
const uid = new ObjectId().toHexString();
const mockAuth = mockBearerAuthentication(uid);
describe("QuotesController", () => {
const getPartialUserMock = vi.spyOn(UserDal, "getPartialUser");
@ -24,6 +26,7 @@ describe("QuotesController", () => {
const user = { quoteMod: true, name: "Bob" } as any;
getPartialUserMock.mockReset().mockResolvedValue(user);
mockAuth.beforeEach();
});
describe("getQuotes", () => {
@ -58,7 +61,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.get("/quotes")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -82,7 +85,7 @@ describe("QuotesController", () => {
//WHEN
await mockApp
.get("/quotes")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -98,7 +101,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.get("/quotes")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(403);
//THEN
@ -113,7 +116,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.get("/quotes")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(403);
//THEN
@ -178,7 +181,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send(newQuote)
.expect(200);
@ -207,7 +210,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(503);
//THEN
@ -219,7 +222,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
expect(body).toEqual({
@ -243,7 +246,7 @@ describe("QuotesController", () => {
captcha: "captcha",
extra: "value",
})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -265,7 +268,7 @@ describe("QuotesController", () => {
language: "english",
captcha: "captcha",
})
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -297,7 +300,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/approve")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({
quoteId,
editText: "editedText",
@ -329,7 +332,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/approve")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId, editText: null, editSource: null })
.expect(200);
@ -357,7 +360,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/approve")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId })
.expect(200);
@ -378,7 +381,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/approve")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -391,7 +394,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/approve")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId: new ObjectId().toHexString(), extra: "value" })
.expect(422);
@ -408,7 +411,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/approve")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId: new ObjectId().toHexString() })
.expect(403);
@ -436,7 +439,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/reject")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId })
.expect(200);
@ -451,7 +454,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/reject")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
@ -468,7 +471,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/reject")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId, extra: "value" })
.expect(422);
@ -486,7 +489,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/reject")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId })
.expect(403);
@ -523,7 +526,7 @@ describe("QuotesController", () => {
const { body } = await mockApp
.get("/quotes/rating")
.query({ quoteId: 42, language: "english" })
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(200);
//THEN
@ -538,7 +541,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.get("/quotes/rating")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -551,7 +554,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.get("/quotes/rating")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.query({ quoteId: 42, language: "english", extra: "value" })
.expect(422);
@ -586,7 +589,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/rating")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({
quoteId: 23,
rating: 4,
@ -616,7 +619,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/rating")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({
quoteId: 23,
rating: 2,
@ -648,7 +651,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/rating")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({
quoteId: 23,
rating: 4,
@ -674,7 +677,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/rating")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -691,7 +694,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/rating")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId: 23, language: "english", rating: 5, extra: "value" })
.expect(422);
@ -705,7 +708,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/rating")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId: 23, language: "english", rating: 0 })
.expect(422);
@ -721,7 +724,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/rating")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId: 23, language: "english", rating: 6 })
.expect(422);
@ -736,7 +739,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/rating")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({ quoteId: 23, language: "english", rating: 2.5 })
.expect(422);
//THEN
@ -768,7 +771,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/report")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({
quoteId: "23", //quoteId is string on this endpoint
quoteLanguage: "english",
@ -802,7 +805,7 @@ describe("QuotesController", () => {
it("should report quote without comment", async () => {
await mockApp
.post("/quotes/report")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({
quoteId: "23", //quoteId is string on this endpoint
quoteLanguage: "english",
@ -814,7 +817,7 @@ describe("QuotesController", () => {
it("should report quote with empty comment", async () => {
await mockApp
.post("/quotes/report")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.send({
quoteId: "23", //quoteId is string on this endpoint
quoteLanguage: "english",
@ -828,7 +831,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/report")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(422);
//THEN
@ -849,7 +852,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/report")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(503);
//THEN
@ -864,7 +867,7 @@ describe("QuotesController", () => {
//WHEN
const { body } = await mockApp
.post("/quotes/report")
.set("authorization", `Uid ${uid}`)
.set("Authorization", `Bearer ${uid}`)
.expect(403);
//THEN

View file

@ -5,31 +5,23 @@ import * as Configuration from "../../../src/init/configuration";
import * as ResultDal from "../../../src/dal/result";
import * as UserDal from "../../../src/dal/user";
import * as LogsDal from "../../../src/dal/logs";
import * as AuthUtils from "../../../src/utils/auth";
import { DecodedIdToken } from "firebase-admin/lib/auth/token-verifier";
import { ObjectId } from "mongodb";
import { mockAuthenticateWithApeKey } from "../../__testData__/auth";
import {
mockAuthenticateWithApeKey,
mockBearerAuthentication,
} from "../../__testData__/auth";
import { enableRateLimitExpects } from "../../__testData__/rate-limit";
import { DBResult } from "../../../src/utils/result";
const uid = "123456";
const mockDecodedToken: DecodedIdToken = {
uid,
email: "newuser@mail.com",
iat: 0,
} as DecodedIdToken;
const mockApp = request(app);
const configuration = Configuration.getCachedConfiguration();
enableRateLimitExpects();
const uid = new ObjectId().toHexString();
const mockAuth = mockBearerAuthentication(uid);
describe("result controller test", () => {
const verifyIdTokenMock = vi.spyOn(AuthUtils, "verifyIdToken");
beforeEach(() => {
verifyIdTokenMock.mockReset();
verifyIdTokenMock.mockResolvedValue(mockDecodedToken);
mockAuth.beforeEach();
});
describe("getResults", () => {
@ -87,7 +79,7 @@ describe("result controller test", () => {
.expect(200);
//THEN
expect(resultMock).toHaveBeenCalledWith(mockDecodedToken.uid, {
expect(resultMock).toHaveBeenCalledWith(uid, {
limit: 1000,
offset: 0,
onOrAfterTimestamp: NaN,
@ -106,7 +98,7 @@ describe("result controller test", () => {
//THEN
expect(resultMock).toHaveBeenCalledWith(mockDecodedToken.uid, {
expect(resultMock).toHaveBeenCalledWith(uid, {
limit: 1000,
offset: 0,
onOrAfterTimestamp: now,
@ -122,7 +114,7 @@ describe("result controller test", () => {
.expect(200);
//THEN
expect(resultMock).toHaveBeenCalledWith(mockDecodedToken.uid, {
expect(resultMock).toHaveBeenCalledWith(uid, {
limit: 250,
offset: 500,
onOrAfterTimestamp: NaN,
@ -158,7 +150,7 @@ describe("result controller test", () => {
//THEN
expect(resultMock).toHaveBeenCalledWith(mockDecodedToken.uid, {
expect(resultMock).toHaveBeenCalledWith(uid, {
limit: 800,
offset: 600,
onOrAfterTimestamp: NaN,
@ -175,7 +167,7 @@ describe("result controller test", () => {
//THEN
expect(resultMock).toHaveBeenCalledWith(mockDecodedToken.uid, {
expect(resultMock).toHaveBeenCalledWith(uid, {
limit: 10, //limit is reduced to stay within max limit
offset: 990,
onOrAfterTimestamp: NaN,
@ -231,7 +223,7 @@ describe("result controller test", () => {
.expect(200);
//THEN
expect(resultMock).toHaveBeenCalledWith(mockDecodedToken.uid, {
expect(resultMock).toHaveBeenCalledWith(uid, {
limit: 100,
offset: 900,
onOrAfterTimestamp: NaN,
@ -266,7 +258,7 @@ describe("result controller test", () => {
.expect(200);
//THEN
expect(resultMock).toHaveBeenCalledWith(mockDecodedToken.uid, {
expect(resultMock).toHaveBeenCalledWith(uid, {
limit: 1000, //the default limit for regular users
offset: 0,
onOrAfterTimestamp: NaN,
@ -430,10 +422,8 @@ describe("result controller test", () => {
it("should delete", async () => {
//GIVEN
verifyIdTokenMock.mockResolvedValue({
...mockDecodedToken,
iat: Date.now() - 1000,
});
mockAuth.modifyToken({ iat: Date.now() - 1000 });
//WHEN
const { body } = await mockApp
.delete("/results")
@ -449,6 +439,10 @@ describe("result controller test", () => {
expect(logToDbMock).toHaveBeenCalledWith("user_results_deleted", "", uid);
});
it("should fail to delete with non-fresh token", async () => {
//GIVEN
mockAuth.modifyToken({ iat: 0 });
//WHEN/THEN
await mockApp
.delete("/results")
.set("Authorization", `Bearer ${uid}`)
@ -704,7 +698,7 @@ describe("result controller test", () => {
restartCount: 4,
tags: ["tagOneId", "tagTwoId"],
testDuration: 15.1,
uid: "123456",
uid: uid,
wpm: 80,
})
);

File diff suppressed because it is too large Load diff