From 660d856f9617c36784f3b13e5d430206c690123c Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Mon, 3 Mar 2025 12:08:30 +0100 Subject: [PATCH] refactor: use bearer auth instead of uid auth for tests (@fehmer) (#6318) --- backend/__tests__/__testData__/auth.ts | 44 +++ .../__tests__/api/controllers/admin.spec.ts | 57 ++-- .../__tests__/api/controllers/ape-key.spec.ts | 41 +-- .../__tests__/api/controllers/config.spec.ts | 24 +- .../api/controllers/configuration.spec.ts | 26 +- .../api/controllers/leaderboard.spec.ts | 51 +-- .../__tests__/api/controllers/preset.spec.ts | 63 ++-- backend/__tests__/api/controllers/psa.spec.ts | 6 +- .../__tests__/api/controllers/quotes.spec.ts | 75 +++-- .../__tests__/api/controllers/result.spec.ts | 48 ++- .../__tests__/api/controllers/user.spec.ts | 315 +++++++++--------- 11 files changed, 404 insertions(+), 346 deletions(-) diff --git a/backend/__tests__/__testData__/auth.ts b/backend/__tests__/__testData__/auth.ts index bd836d580..844bf9bbd 100644 --- a/backend/__tests__/__testData__/auth.ts +++ b/backend/__tests__/__testData__/auth.ts @@ -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): void => { + verifyIdTokenMock.mockReset(); + verifyIdTokenMock.mockResolvedValue({ + ...mockDecodedToken, + ...customize, + }); + }, + }; +} diff --git a/backend/__tests__/api/controllers/admin.spec.ts b/backend/__tests__/api/controllers/admin.spec.ts index dfe81861e..fcf26a005 100644 --- a/backend/__tests__/api/controllers/admin.spec.ts +++ b/backend/__tests__/api/controllers/admin.spec.ts @@ -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 }); }); }); diff --git a/backend/__tests__/api/controllers/ape-key.spec.ts b/backend/__tests__/api/controllers/ape-key.spec.ts index 7c05fbf2b..5933f486a 100644 --- a/backend/__tests__/api/controllers/ape-key.spec.ts +++ b/backend/__tests__/api/controllers/ape-key.spec.ts @@ -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}`) ); }); }); diff --git a/backend/__tests__/api/controllers/config.spec.ts b/backend/__tests__/api/controllers/config.spec.ts index a3c50fb68..3cbf6dc89 100644 --- a/backend/__tests__/api/controllers/config.spec.ts +++ b/backend/__tests__/api/controllers/config.spec.ts @@ -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); }); }); }); diff --git a/backend/__tests__/api/controllers/configuration.spec.ts b/backend/__tests__/api/controllers/configuration.spec.ts index e5cc0fb81..ce3852019 100644 --- a/backend/__tests__/api/controllers/configuration.spec.ts +++ b/backend/__tests__/api/controllers/configuration.spec.ts @@ -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 diff --git a/backend/__tests__/api/controllers/leaderboard.spec.ts b/backend/__tests__/api/controllers/leaderboard.spec.ts index 46a612f87..a0afc50c2 100644 --- a/backend/__tests__/api/controllers/leaderboard.spec.ts +++ b/backend/__tests__/api/controllers/leaderboard.spec.ts @@ -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."); diff --git a/backend/__tests__/api/controllers/preset.spec.ts b/backend/__tests__/api/controllers/preset.spec.ts index 4239f26eb..3d9f5eb80 100644 --- a/backend/__tests__/api/controllers/preset.spec.ts +++ b/backend/__tests__/api/controllers/preset.spec.ts @@ -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(); diff --git a/backend/__tests__/api/controllers/psa.spec.ts b/backend/__tests__/api/controllers/psa.spec.ts index 9dcff487f..99ec2249b 100644 --- a/backend/__tests__/api/controllers/psa.spec.ts +++ b/backend/__tests__/api/controllers/psa.spec.ts @@ -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); }); diff --git a/backend/__tests__/api/controllers/quotes.spec.ts b/backend/__tests__/api/controllers/quotes.spec.ts index 3a6a5fe8a..4aeafa62b 100644 --- a/backend/__tests__/api/controllers/quotes.spec.ts +++ b/backend/__tests__/api/controllers/quotes.spec.ts @@ -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 diff --git a/backend/__tests__/api/controllers/result.spec.ts b/backend/__tests__/api/controllers/result.spec.ts index bffa1e538..7e76109b4 100644 --- a/backend/__tests__/api/controllers/result.spec.ts +++ b/backend/__tests__/api/controllers/result.spec.ts @@ -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, }) ); diff --git a/backend/__tests__/api/controllers/user.spec.ts b/backend/__tests__/api/controllers/user.spec.ts index edbf16c66..bd277df3d 100644 --- a/backend/__tests__/api/controllers/user.spec.ts +++ b/backend/__tests__/api/controllers/user.spec.ts @@ -3,7 +3,6 @@ import app from "../../../src/app"; import * as Configuration from "../../../src/init/configuration"; import { generateCurrentTestActivity } from "../../../src/api/controllers/user"; import * as UserDal from "../../../src/dal/user"; -import { DecodedIdToken } from "firebase-admin/auth"; import * as AuthUtils from "../../../src/utils/auth"; import * as BlocklistDal from "../../../src/dal/blocklist"; import * as ApeKeys from "../../../src/dal/ape-keys"; @@ -23,7 +22,10 @@ import * as LogDal from "../../../src/dal/logs"; import { ObjectId } from "mongodb"; import { PersonalBest } from "@monkeytype/contracts/schemas/shared"; import { pb } from "../../dal/leaderboards.spec"; -import { mockAuthenticateWithApeKey } from "../../__testData__/auth"; +import { + mockAuthenticateWithApeKey, + mockBearerAuthentication, +} from "../../__testData__/auth"; import { randomUUID } from "node:crypto"; import _ from "lodash"; import { MonkeyMail, UserStreak } from "@monkeytype/contracts/schemas/users"; @@ -33,17 +35,12 @@ import * as WeeklyXpLeaderboard from "../../../src/services/weekly-xp-leaderboar const mockApp = request(app); const configuration = Configuration.getCachedConfiguration(); -const uid = "123456789"; - -const mockDecodedToken: DecodedIdToken = { - uid, - email: "newuser@mail.com", - iat: Date.now(), -} as DecodedIdToken; +const uid = new ObjectId().toHexString(); +const mockAuth = mockBearerAuthentication(uid); describe("user controller test", () => { beforeEach(() => { - vi.spyOn(AuthUtils, "verifyIdToken").mockResolvedValue(mockDecodedToken); + mockAuth.beforeEach(); }); describe("user creation flow", () => { beforeEach(async () => { @@ -54,20 +51,20 @@ describe("user controller test", () => { const newUser = { name: "NewUser", - uid: "123456789", + uid, email: "newuser@mail.com", captcha: "captcha", }; await mockApp .post("/users/signup") - .set("authorization", "Uid 123456789|newuser@mail.com") + .set("Authorization", `Bearer ${uid}`) .send(newUser) .expect(200); const response = await mockApp .get("/users") - .set("authorization", "Uid 123456789") + .set("Authorization", `Bearer ${uid}`) .send() .expect(200); @@ -106,7 +103,7 @@ describe("user controller test", () => { const newUser = { name: "NewUser", - uid: "123456789", + uid: uid, email: "newuser@mail.com", captcha: "captcha", }; @@ -114,7 +111,7 @@ describe("user controller test", () => { //WHEN const result = await mockApp .post("/users/signup") - .set("authorization", "Uid 123456789|newuser@mail.com") + .set("Authorization", `Bearer ${uid}`) .send(newUser) .expect(409); @@ -126,7 +123,7 @@ describe("user controller test", () => { }); //user will be created in firebase from the frontend, make sure we remove it - expect(firebaseDeleteUserMock).toHaveBeenCalledWith("123456789"); + expect(firebaseDeleteUserMock).toHaveBeenCalledWith(uid); expect(verifyCaptchaMock).toHaveBeenCalledWith("captcha"); }); @@ -134,10 +131,13 @@ describe("user controller test", () => { for (const domain of ["tidal.lol", "selfbot.cc"]) { //GIVEN firebaseDeleteUserMock.mockResolvedValue(); + mockAuth.modifyToken({ + email: `newuser@${domain}`, + }); const newUser = { name: "NewUser", - uid: "123456789", + uid: uid, email: `newuser@${domain}`, captcha: "captcha", }; @@ -145,7 +145,7 @@ describe("user controller test", () => { //WHEN const result = await mockApp .post("/users/signup") - .set("authorization", `Uid 123456789|newuser@${domain}`) + .set("Authorization", `Bearer ${uid}`) .send(newUser) .set({ Accept: "application/json", @@ -156,7 +156,7 @@ describe("user controller test", () => { expect(result.body.message).toEqual("Invalid domain"); //user will be created in firebase from the frontend, make sure we remove it - expect(firebaseDeleteUserMock).toHaveBeenCalledWith("123456789"); + expect(firebaseDeleteUserMock).toHaveBeenCalledWith(uid); } }); @@ -167,7 +167,7 @@ describe("user controller test", () => { const newUser = { name: "NewUser", - uid: "123456789", + uid: uid, email: "newuser@mail.com", captcha: "captcha", }; @@ -175,19 +175,16 @@ describe("user controller test", () => { //WHEN const result = await mockApp .post("/users/signup") - .set("authorization", "Uid 123456789|newuser@mail.com") + .set("Authorization", `Bearer ${uid}`) .send(newUser) .expect(409); //THEN expect(result.body.message).toEqual("Username unavailable"); - expect(usernameAvailableMock).toHaveBeenCalledWith( - "NewUser", - "123456789" - ); + expect(usernameAvailableMock).toHaveBeenCalledWith("NewUser", uid); //user will be created in firebase from the frontend, make sure we remove it - expect(firebaseDeleteUserMock).toHaveBeenCalledWith("123456789"); + expect(firebaseDeleteUserMock).toHaveBeenCalledWith(uid); }); it("should fail if capture is invalid", async () => { //GIVEN @@ -195,7 +192,7 @@ describe("user controller test", () => { const newUser = { name: "NewUser", - uid: "123456789", + uid: uid, email: "newuser@mail.com", captcha: "captcha", }; @@ -203,7 +200,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/signup") - .set("authorization", "Uid 123456789|newuser@mail.com") + .set("Authorization", `Bearer ${uid}`) .send(newUser) .expect(422); @@ -213,7 +210,7 @@ describe("user controller test", () => { it("should fail if username too long", async () => { //GIVEN const newUser = { - uid: "123456789", + uid: uid, email: "newuser@mail.com", captcha: "captcha", }; @@ -221,7 +218,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/signup") - .set("authorization", "Uid 123456789|newuser@mail.com") + .set("Authorization", `Bearer ${uid}`) .send({ ...newUser, name: new Array(17).fill("x").join("") }) .expect(422); @@ -236,7 +233,7 @@ describe("user controller test", () => { it("should fail if username contains profanity", async () => { //GIVEN const newUser = { - uid: "123456789", + uid: uid, email: "newuser@mail.com", captcha: "captcha", }; @@ -244,7 +241,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/signup") - .set("authorization", "Uid 123456789|newuser@mail.com") + .set("Authorization", `Bearer ${uid}`) .send({ ...newUser, name: "miodec" }) .expect(422); @@ -289,7 +286,7 @@ describe("user controller test", () => { //"HEN const { body } = await mockApp .get("/users/verificationEmail") - .set("authorization", `Uid ${uid}|newuser@mail.com`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -316,7 +313,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/verificationEmail") - .set("authorization", `Uid ${uid}|newuser@mail.com`) + .set("Authorization", `Bearer ${uid}`) .expect(500); //THEN @@ -331,7 +328,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/verificationEmail") - .set("authorization", `Uid ${uid}|newuser@mail.com`) + .set("Authorization", `Bearer ${uid}`) .expect(400); //THEN @@ -346,7 +343,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/verificationEmail") - .set("authorization", `Uid ${uid}|newuser@mail.com`) + .set("Authorization", `Bearer ${uid}`) .expect(400); //THEN @@ -371,7 +368,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/verificationEmail") - .set("authorization", `Uid ${uid}|newuser@mail.com`) + .set("Authorization", `Bearer ${uid}`) .expect(429); //THEN @@ -393,7 +390,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/verificationEmail") - .set("authorization", `Uid ${uid}|newuser@mail.com`) + .set("Authorization", `Bearer ${uid}`) .expect(500); //THEN @@ -413,7 +410,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/verificationEmail") - .set("authorization", `Uid ${uid}|newuser@mail.com`) + .set("Authorization", `Bearer ${uid}`) .expect(500); //THEN @@ -496,7 +493,7 @@ describe("user controller test", () => { //when await mockApp .get("/users/testActivity") - .set("authorization", "Uid 123456789") + .set("Authorization", `Bearer ${uid}`) .send() .expect(503); }); @@ -511,7 +508,7 @@ describe("user controller test", () => { //when const response = await mockApp .get("/users/testActivity") - .set("authorization", "Uid 123456789") + .set("Authorization", `Bearer ${uid}`) .send() .expect(200); @@ -608,6 +605,7 @@ describe("user controller test", () => { const blocklistAddMock = vi.spyOn(BlocklistDal, "add"); beforeEach(() => { + mockAuth.beforeEach(); [ firebaseDeleteUserMock, deleteUserMock, @@ -639,7 +637,6 @@ describe("user controller test", () => { it("should add user to blocklist if banned", async () => { //GIVEN - const uid = mockDecodedToken.uid; const user = { uid, name: "name", @@ -652,7 +649,7 @@ describe("user controller test", () => { //WHEN await mockApp .delete("/users/") - .set("Authorization", "Bearer 123456789") + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -675,7 +672,6 @@ describe("user controller test", () => { }); it("should delete user without adding to blocklist if not banned", async () => { //GIVEN - const uid = mockDecodedToken.uid; const user = { uid, name: "name", @@ -687,7 +683,7 @@ describe("user controller test", () => { //WHEN await mockApp .delete("/users/") - .set("Authorization", "Bearer 123456789") + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -754,7 +750,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/reset") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -792,7 +788,7 @@ describe("user controller test", () => { //WHEN await mockApp .patch("/users/reset") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -805,7 +801,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/reset") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(403); //THEN @@ -834,7 +830,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/name") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ name: "newName" }) .expect(200); @@ -859,7 +855,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/name") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ name: "newName" }) .expect(409); @@ -875,7 +871,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/name") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ name: "newName" }) .expect(403); @@ -892,7 +888,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/name") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ name: "newName" }) .expect(409); @@ -912,7 +908,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/name") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ name: "newName" }) .expect(200); @@ -928,7 +924,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/name") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -941,7 +937,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/name") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ name: "newName", extra: "value" }) .expect(422); @@ -955,7 +951,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/name") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ name: "miodec" }) .expect(422); @@ -988,7 +984,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete("/users/personalBests") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -1027,7 +1023,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/optOutOfLeaderboards") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -1051,7 +1047,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/optOutOfLeaderboards") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ extra: "value" }); //TODO.expect(422); @@ -1078,7 +1074,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/email") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newEmail, previousEmail: "previousEmail@example.com" }) .expect(200); @@ -1118,7 +1114,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/email") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newEmail: "newEmail@example.com", previousEmail: "previousEmail@example.com", @@ -1148,7 +1144,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/email") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newEmail: "newEmail@example.com", previousEmail: "previousEmail@example.com", @@ -1175,7 +1171,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/email") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newEmail: "newEmail@example.com", previousEmail: "previousEmail@example.com", @@ -1202,7 +1198,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/email") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newEmail: "newEmail@example.com", previousEmail: "previousEmail@example.com", @@ -1229,7 +1225,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/email") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newEmail: "newEmail@example.com", previousEmail: "previousEmail@example.com", @@ -1247,7 +1243,7 @@ describe("user controller test", () => { //WHEN await mockApp .patch("/users/email") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newEmail: "newEmail@example.com", previousEmail: "previousEmail@example.com", @@ -1258,7 +1254,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/email") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); expect(body).toEqual({ @@ -1270,7 +1266,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/email") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newEmail: "newEmail@example.com", previousEmail: "previousEmail@example.com", @@ -1295,7 +1291,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/password") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newPassword: "sw0rdf1sh" }) .expect(200); @@ -1310,7 +1306,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/password") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -1323,7 +1319,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/password") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newPassword: "sw0rdf1sh", extra: "value" }) .expect(422); @@ -1337,7 +1333,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/password") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ newPassword: "test" }) .expect(422); @@ -1362,7 +1358,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/discord/oauth") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -1379,7 +1375,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/discord/oauth") - .set("authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(503); //THEN @@ -1435,7 +1431,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/discord/link") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ tokenType: "tokenType", accessToken: "accessToken", @@ -1488,7 +1484,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/discord/link") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ tokenType: "tokenType", accessToken: "accessToken", @@ -1521,7 +1517,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/discord/link") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ tokenType: "tokenType", accessToken: "accessToken", @@ -1539,7 +1535,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/discord/link") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ tokenType: "tokenType", accessToken: "accessToken", @@ -1557,7 +1553,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/discord/link") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ tokenType: "tokenType", accessToken: "accessToken", @@ -1580,7 +1576,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/discord/link") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ tokenType: "tokenType", accessToken: "accessToken", @@ -1599,7 +1595,6 @@ describe("user controller test", () => { it("should fail if discordId is blocked", async () => { //GIVEN - const uid = mockDecodedToken.uid; const user = { uid, name: "name", @@ -1611,7 +1606,7 @@ describe("user controller test", () => { //WHEN const result = await mockApp .post("/users/discord/link") - .set("Authorization", "Bearer 123456789") + .set("Authorization", `Bearer ${uid}`) .send({ tokenType: "tokenType", accessToken: "accessToken", @@ -1630,7 +1625,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/discord/link") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -1647,7 +1642,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/discord/link") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ tokenType: "tokenType", accessToken: "accessToken", @@ -1684,7 +1679,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/discord/unlink") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -1709,7 +1704,7 @@ describe("user controller test", () => { const { body } = await mockApp .post("/users/discord/unlink") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(403); //THEN @@ -1725,7 +1720,7 @@ describe("user controller test", () => { const { body } = await mockApp .post("/users/discord/unlink") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(404); //THEN @@ -1811,7 +1806,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/resultFilterPresets") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send(validPreset) .expect(200); @@ -1832,7 +1827,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/resultFilterPresets") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -1860,7 +1855,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/resultFilterPresets") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ ...validPreset, extra: "value" }) .expect(422); @@ -1876,7 +1871,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/resultFilterPresets") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ validPreset }) .expect(503); @@ -1901,7 +1896,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete("/users/resultFilterPresets/myId") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -1918,7 +1913,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete("/users/resultFilterPresets/myId") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(503); //THEN @@ -1952,7 +1947,7 @@ describe("user controller test", () => { const { body } = await mockApp .post("/users/tags") .send({ tagName: "tagName" }) - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -1968,7 +1963,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/tags") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -1983,7 +1978,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/tags") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ tagName: "tagName", extra: "value" }) .expect(422); @@ -2007,7 +2002,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete(`/users/tags/${tagId}/personalBest`) - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -2032,7 +2027,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch(`/users/tags`) - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ tagId, newName: "newName" }) .expect(200); @@ -2047,7 +2042,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch(`/users/tags`) - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -2060,7 +2055,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch(`/users/tags`) - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ tagId: new ObjectId().toHexString(), newName: "newName", @@ -2089,7 +2084,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete(`/users/tags/${tagId}`) - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -2126,7 +2121,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/tags") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -2156,7 +2151,7 @@ describe("user controller test", () => { language: "english", rank: 7, }) - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -2178,7 +2173,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/leaderboardMemory") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -2196,7 +2191,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/leaderboardMemory") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ mode: "time", mode2: "60", @@ -2235,7 +2230,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -2266,7 +2261,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ name: "customTheme", colors: new Array(10).fill("#000000") as any, @@ -2287,7 +2282,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -2300,7 +2295,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ name: "customTheme", colors: new Array(10).fill("#000000") as any, @@ -2318,7 +2313,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ name: "customThemecustomThemecustomThemecustomTheme", colors: new Array(9).fill("#000") as any, @@ -2349,7 +2344,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ themeId }) .expect(200); @@ -2364,7 +2359,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -2377,7 +2372,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ themeId: new ObjectId().toHexString(), extra: "value" }) .expect(422); @@ -2405,7 +2400,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ themeId, theme, @@ -2423,7 +2418,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -2436,7 +2431,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/customThemes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ themeId: new ObjectId().toHexString(), theme: { @@ -2472,7 +2467,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/personalBests") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .query({ mode: "time", mode2: "15" }) .expect(200); @@ -2499,7 +2494,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/personalBests") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -2512,7 +2507,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/personalBests") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .query({ mode: "time", mode2: "15", extra: "value" }) .expect(422); @@ -2526,7 +2521,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/personalBests") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .query({ mode: "mood", mode2: "happy" }) .expect(422); @@ -2562,7 +2557,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/stats") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -2602,7 +2597,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/favoriteQuotes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -2622,7 +2617,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/favoriteQuotes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ language: "english", quoteId: "7" }) .expect(200); @@ -2642,7 +2637,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/favoriteQuotes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -2655,7 +2650,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/favoriteQuotes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ language: "english", quoteId: "7", extra: "value" }) .expect(422); @@ -2676,7 +2671,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete("/users/favoriteQuotes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ language: "english", quoteId: "7" }) .expect(200); @@ -2691,7 +2686,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete("/users/favoriteQuotes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -2704,7 +2699,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .delete("/users/favoriteQuotes") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ language: "english", quoteId: "7", extra: "value" }) .expect(422); @@ -2954,7 +2949,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/profile") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ ...newProfile, selectedBadgeId: 2, @@ -2998,7 +2993,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/profile") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ ...newProfile, selectedBadgeId: -1, @@ -3030,7 +3025,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/profile") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ extra: "value", socialProfiles: { @@ -3052,7 +3047,7 @@ describe("user controller test", () => { //WHEN await mockApp .patch("/users/profile") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ bio: "Line1\n\n\nLine2\n\n\n\nLine3", keyboard: " string with many spaces ", @@ -3074,7 +3069,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/profile") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ bio: "miodec", keyboard: "miodec", @@ -3102,7 +3097,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/profile") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ bio: new Array(251).fill("x").join(""), keyboard: new Array(76).fill("x").join(""), @@ -3132,7 +3127,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/profile") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ socialProfiles: { website: "http://monkeytype.com", @@ -3155,7 +3150,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/profile") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({}) .expect(503); @@ -3194,7 +3189,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/inbox") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -3215,7 +3210,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .get("/users/inbox") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(503); //THEN @@ -3235,7 +3230,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/inbox") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ mailIdsToDelete: [mailIdOne], mailIdsToMarkRead: [mailIdOne, mailIdTwo], @@ -3258,7 +3253,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/inbox") - .set("Authorization", `Uid ${uid}`); + .set("Authorization", `Bearer ${uid}`); //.expect(200); console.log(body); @@ -3274,7 +3269,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/inbox") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ mailIdsToDelete: [], mailIdsToMarkRead: [], @@ -3297,7 +3292,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .patch("/users/inbox") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(503); //THEN @@ -3327,7 +3322,7 @@ describe("user controller test", () => { const { body } = await mockApp .post("/users/report") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ uid: uidToReport, reason: "Suspected cheating", @@ -3361,7 +3356,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/report") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -3378,7 +3373,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/report") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ uid: new ObjectId().toHexString(), reason: "Suspected cheating", @@ -3401,7 +3396,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/report") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ uid: new ObjectId().toHexString(), reason: "Suspected cheating", @@ -3420,7 +3415,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/report") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ uid: new Array(51).fill("x").join(""), reason: "unfriendly", @@ -3445,7 +3440,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/report") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ uid: new ObjectId().toHexString(), reason: "Suspected cheating", @@ -3464,7 +3459,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/report") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ uid: new ObjectId().toHexString(), reason: "Suspected cheating", @@ -3492,7 +3487,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/setStreakHourOffset") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ hourOffset: -2 }) .expect(200); @@ -3518,7 +3513,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/setStreakHourOffset") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ hourOffset: -2 }) .expect(403); @@ -3531,7 +3526,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/setStreakHourOffset") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(422); //THEN @@ -3543,19 +3538,19 @@ describe("user controller test", () => { it("should fail with invalid offset", async () => { await mockApp .post("/users/setStreakHourOffset") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ hourOffset: -12 }) .expect(422); await mockApp .post("/users/setStreakHourOffset") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ hourOffset: 13 }) .expect(422); await mockApp .post("/users/setStreakHourOffset") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .send({ hourOffset: "UTC-8" }) .expect(422); }); @@ -3572,7 +3567,7 @@ describe("user controller test", () => { //WHEN const { body } = await mockApp .post("/users/revokeAllTokens") - .set("Authorization", `Uid ${uid}`) + .set("Authorization", `Bearer ${uid}`) .expect(200); //THEN @@ -3598,7 +3593,7 @@ describe("user controller test", () => { //GIVEN vi.useFakeTimers().setSystemTime(1712102400000); const user = { - uid: mockDecodedToken.uid, + uid: uid, testActivity: { "2024": fillYearWithDay(94), }, @@ -3608,7 +3603,7 @@ describe("user controller test", () => { //WHEN const result = await mockApp .get("/users/currentTestActivity") - .set("Authorization", "Bearer 123456789") + .set("Authorization", `Bearer ${uid}`) .send() .expect(200); @@ -3631,7 +3626,7 @@ describe("user controller test", () => { it("gets", async () => { //GIVEN const user = { - uid: mockDecodedToken.uid, + uid: uid, streak: { lastResultTimestamp: 1712102400000, length: 42, @@ -3644,7 +3639,7 @@ describe("user controller test", () => { //WHEN const result = await mockApp .get("/users/streak") - .set("Authorization", "Bearer 123456789") + .set("Authorization", `Bearer ${uid}`) .send() .expect(200);