diff --git a/backend/__tests__/__testData__/controller-test.ts b/backend/__tests__/__testData__/controller-test.ts new file mode 100644 index 000000000..abcc70e43 --- /dev/null +++ b/backend/__tests__/__testData__/controller-test.ts @@ -0,0 +1,17 @@ +import request from "supertest"; +import app from "../../src/app"; +import { ObjectId } from "mongodb"; +import { mockBearerAuthentication } from "./auth"; +import { beforeEach } from "vitest"; + +export function setup() { + const mockApp = request(app); + const uid = new ObjectId().toHexString(); + const mockAuth = mockBearerAuthentication(uid); + + beforeEach(() => { + mockAuth.beforeEach(); + }); + + return { mockApp, uid, mockAuth }; +} diff --git a/backend/__tests__/api/controllers/admin.spec.ts b/backend/__tests__/api/controllers/admin.spec.ts index 9731e2256..2db8391c6 100644 --- a/backend/__tests__/api/controllers/admin.spec.ts +++ b/backend/__tests__/api/controllers/admin.spec.ts @@ -1,6 +1,5 @@ import { describe, it, expect, beforeEach, vi } from "vitest"; -import request, { Test as SuperTest } from "supertest"; -import app from "../../../src/app"; +import { setup } from "../../__testData__/controller-test"; import { ObjectId } from "mongodb"; import * as Configuration from "../../../src/init/configuration"; import * as AdminUuidDal from "../../../src/dal/admin-uids"; @@ -11,12 +10,9 @@ 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 { mockApp, uid } = setup(); const configuration = Configuration.getCachedConfiguration(); -const uid = new ObjectId().toHexString(); -const mockAuth = mockBearerAuthentication(uid); enableRateLimitExpects(); describe("AdminController", () => { @@ -27,7 +23,6 @@ describe("AdminController", () => { isAdminMock.mockClear(); await enableAdminEndpoints(true); isAdminMock.mockResolvedValue(true); - mockAuth.beforeEach(); logsAddImportantLog.mockClear().mockResolvedValue(); }); diff --git a/backend/__tests__/api/controllers/ape-key.spec.ts b/backend/__tests__/api/controllers/ape-key.spec.ts index 2d37c98e5..60aff1724 100644 --- a/backend/__tests__/api/controllers/ape-key.spec.ts +++ b/backend/__tests__/api/controllers/ape-key.spec.ts @@ -1,17 +1,14 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; -import request, { Test as SuperTest } from "supertest"; -import app from "../../../src/app"; +import { setup } from "../../__testData__/controller-test"; +import { Test as SuperTest } from "supertest"; import * as ApeKeyDal from "../../../src/dal/ape-keys"; 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 { mockApp, uid } = setup(); const configuration = Configuration.getCachedConfiguration(); -const uid = new ObjectId().toHexString(); -const mockAuth = mockBearerAuthentication(uid); describe("ApeKeyController", () => { const getUserMock = vi.spyOn(UserDal, "getPartialUser"); @@ -21,7 +18,6 @@ describe("ApeKeyController", () => { getUserMock.mockResolvedValue(user(uid, {})); vi.useFakeTimers(); vi.setSystemTime(1000); - mockAuth.beforeEach(); }); afterEach(() => { diff --git a/backend/__tests__/api/controllers/config.spec.ts b/backend/__tests__/api/controllers/config.spec.ts index c5fc7a237..437054393 100644 --- a/backend/__tests__/api/controllers/config.spec.ts +++ b/backend/__tests__/api/controllers/config.spec.ts @@ -1,17 +1,11 @@ -import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; -import request from "supertest"; -import app from "../../../src/app"; +import { describe, it, expect, afterEach, vi } from "vitest"; +import { setup } from "../../__testData__/controller-test"; 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); + +const { mockApp, uid } = setup(); describe("ConfigController", () => { - beforeEach(() => { - mockAuth.beforeEach(); - }); describe("get config", () => { const getConfigMock = vi.spyOn(ConfigDal, "getConfig"); diff --git a/backend/__tests__/api/controllers/configuration.spec.ts b/backend/__tests__/api/controllers/configuration.spec.ts index ebc8f3eea..c0422137c 100644 --- a/backend/__tests__/api/controllers/configuration.spec.ts +++ b/backend/__tests__/api/controllers/configuration.spec.ts @@ -1,28 +1,24 @@ import { describe, it, expect, beforeEach, vi } from "vitest"; -import request from "supertest"; -import app from "../../../src/app"; +import { setup } from "../../__testData__/controller-test"; import { BASE_CONFIGURATION, CONFIGURATION_FORM_SCHEMA, } from "../../../src/constants/base-configuration"; import * as Configuration from "../../../src/init/configuration"; import type { Configuration as ConfigurationType } from "@monkeytype/schemas/configuration"; -import { ObjectId } from "mongodb"; import * as Misc from "../../../src/utils/misc"; import * as AdminUuids from "../../../src/dal/admin-uids"; -import { mockBearerAuthentication } from "../../__testData__/auth"; -const mockApp = request(app); -const uid = new ObjectId().toHexString(); +const { mockApp, uid, mockAuth } = setup(); describe("Configuration Controller", () => { const isDevEnvironmentMock = vi.spyOn(Misc, "isDevEnvironment"); - const mockAuth = mockBearerAuthentication(uid); + const isAdminMock = vi.spyOn(AdminUuids, "isAdmin"); beforeEach(() => { isAdminMock.mockClear(); - mockAuth.beforeEach(); + isDevEnvironmentMock.mockClear(); isDevEnvironmentMock.mockReturnValue(true); @@ -143,7 +139,7 @@ describe("Configuration Controller", () => { isDevEnvironmentMock.mockReturnValue(false); //WHEN - await request(app) + await mockApp .patch("/configuration") .send({ configuration: {} }) .expect(401); diff --git a/backend/__tests__/api/controllers/dev.spec.ts b/backend/__tests__/api/controllers/dev.spec.ts index cfd713ca9..ec6bf84d7 100644 --- a/backend/__tests__/api/controllers/dev.spec.ts +++ b/backend/__tests__/api/controllers/dev.spec.ts @@ -1,25 +1,9 @@ import { describe, it, expect, beforeEach, vi } from "vitest"; -import request from "supertest"; -import app from "../../../src/app"; -import * as AuthUtils from "../../../src/utils/auth"; -import { ObjectId } from "mongodb"; +import { setup } from "../../__testData__/controller-test"; import * as Misc from "../../../src/utils/misc"; -import { DecodedIdToken } from "firebase-admin/auth"; - -const uid = new ObjectId().toHexString(); -const mockDecodedToken = { - uid, - email: "newuser@mail.com", - iat: 0, -} as DecodedIdToken; -const mockApp = request(app); +const { mockApp } = setup(); describe("DevController", () => { - const verifyIdTokenMock = vi.spyOn(AuthUtils, "verifyIdToken"); - beforeEach(() => { - verifyIdTokenMock.mockClear().mockResolvedValue(mockDecodedToken); - }); - describe("generate testData", () => { const isDevEnvironmentMock = vi.spyOn(Misc, "isDevEnvironment"); diff --git a/backend/__tests__/api/controllers/leaderboard.spec.ts b/backend/__tests__/api/controllers/leaderboard.spec.ts index bce16e114..fb42f52b7 100644 --- a/backend/__tests__/api/controllers/leaderboard.spec.ts +++ b/backend/__tests__/api/controllers/leaderboard.spec.ts @@ -1,22 +1,16 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; +import { setup } from "../../__testData__/controller-test"; import _ from "lodash"; import { ObjectId } from "mongodb"; -import request from "supertest"; -import app from "../../../src/app"; 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, - mockBearerAuthentication, -} from "../../__testData__/auth"; +import { mockAuthenticateWithApeKey } from "../../__testData__/auth"; import { XpLeaderboardEntry } from "@monkeytype/schemas/leaderboards"; -const mockApp = request(app); +const { mockApp, uid } = setup(); const configuration = Configuration.getCachedConfiguration(); -const uid = new ObjectId().toHexString(); -const mockAuth = mockBearerAuthentication(uid); const allModes = [ "10", @@ -32,9 +26,6 @@ const allModes = [ ]; describe("Loaderboard Controller", () => { - beforeEach(() => { - mockAuth.beforeEach(); - }); describe("get leaderboard", () => { const getLeaderboardMock = vi.spyOn(LeaderboardDal, "get"); const getLeaderboardCountMock = vi.spyOn(LeaderboardDal, "getCount"); diff --git a/backend/__tests__/api/controllers/preset.spec.ts b/backend/__tests__/api/controllers/preset.spec.ts index bac4c1ef9..7b6dad709 100644 --- a/backend/__tests__/api/controllers/preset.spec.ts +++ b/backend/__tests__/api/controllers/preset.spec.ts @@ -1,18 +1,11 @@ -import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; -import request from "supertest"; -import app from "../../../src/app"; +import { describe, it, expect, afterEach, vi } from "vitest"; +import { setup } from "../../__testData__/controller-test"; 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); + +const { mockApp, uid } = setup(); describe("PresetController", () => { - beforeEach(() => { - mockAuth.beforeEach(); - }); - describe("get presets", () => { const getPresetsMock = vi.spyOn(PresetDal, "getPresets"); diff --git a/backend/__tests__/api/controllers/psa.spec.ts b/backend/__tests__/api/controllers/psa.spec.ts index 85eac963f..7fd30e12c 100644 --- a/backend/__tests__/api/controllers/psa.spec.ts +++ b/backend/__tests__/api/controllers/psa.spec.ts @@ -1,13 +1,10 @@ import { describe, it, expect, afterEach, vi } from "vitest"; -import request from "supertest"; -import app from "../../../src/app"; +import { setup } from "../../__testData__/controller-test"; 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); + +const { mockApp, uid } = setup(); describe("Psa Controller", () => { describe("get psa", () => { @@ -17,7 +14,6 @@ describe("Psa Controller", () => { afterEach(() => { getPsaMock.mockClear(); recordClientVersionMock.mockClear(); - mockAuth.beforeEach(); }); it("get psas without authorization", async () => { diff --git a/backend/__tests__/api/controllers/public.spec.ts b/backend/__tests__/api/controllers/public.spec.ts index b69509bb5..bbe556fc9 100644 --- a/backend/__tests__/api/controllers/public.spec.ts +++ b/backend/__tests__/api/controllers/public.spec.ts @@ -1,8 +1,8 @@ import { describe, it, expect, afterEach, vi } from "vitest"; -import request from "supertest"; -import app from "../../../src/app"; +import { setup } from "../../__testData__/controller-test"; import * as PublicDal from "../../../src/dal/public"; -const mockApp = request(app); + +const { mockApp } = setup(); describe("PublicController", () => { describe("get speed histogram", () => { diff --git a/backend/__tests__/api/controllers/quotes.spec.ts b/backend/__tests__/api/controllers/quotes.spec.ts index 47c655c60..e2663ebdd 100644 --- a/backend/__tests__/api/controllers/quotes.spec.ts +++ b/backend/__tests__/api/controllers/quotes.spec.ts @@ -1,6 +1,5 @@ import { describe, it, expect, beforeEach, vi } from "vitest"; -import request from "supertest"; -import app from "../../../src/app"; +import { setup } from "../../__testData__/controller-test"; import * as Configuration from "../../../src/init/configuration"; import * as UserDal from "../../../src/dal/user"; import * as NewQuotesDal from "../../../src/dal/new-quotes"; @@ -12,14 +11,10 @@ import * as Captcha from "../../../src/utils/captcha"; import { ObjectId } from "mongodb"; import _ from "lodash"; import { ApproveQuote } from "@monkeytype/schemas/quotes"; -import { mockBearerAuthentication } from "../../__testData__/auth"; -const mockApp = request(app); +const { mockApp, uid } = setup(); const configuration = Configuration.getCachedConfiguration(); -const uid = new ObjectId().toHexString(); -const mockAuth = mockBearerAuthentication(uid); - describe("QuotesController", () => { const getPartialUserMock = vi.spyOn(UserDal, "getPartialUser"); const logsAddLogMock = vi.spyOn(LogsDal, "addLog"); @@ -29,7 +24,6 @@ describe("QuotesController", () => { const user = { quoteMod: true, name: "Bob" } as any; getPartialUserMock.mockClear().mockResolvedValue(user); - mockAuth.beforeEach(); logsAddLogMock.mockClear().mockResolvedValue(); }); diff --git a/backend/__tests__/api/controllers/result.spec.ts b/backend/__tests__/api/controllers/result.spec.ts index b906425bf..0d66ed494 100644 --- a/backend/__tests__/api/controllers/result.spec.ts +++ b/backend/__tests__/api/controllers/result.spec.ts @@ -1,6 +1,5 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; -import request from "supertest"; -import app from "../../../src/app"; +import { setup } from "../../__testData__/controller-test"; import _, { omit } from "lodash"; import * as Configuration from "../../../src/init/configuration"; import * as ResultDal from "../../../src/dal/result"; @@ -8,24 +7,15 @@ import * as UserDal from "../../../src/dal/user"; import * as LogsDal from "../../../src/dal/logs"; import * as PublicDal from "../../../src/dal/public"; import { ObjectId } from "mongodb"; -import { - mockAuthenticateWithApeKey, - mockBearerAuthentication, -} from "../../__testData__/auth"; +import { mockAuthenticateWithApeKey } from "../../__testData__/auth"; import { enableRateLimitExpects } from "../../__testData__/rate-limit"; import { DBResult } from "../../../src/utils/result"; -const mockApp = request(app); +const { mockApp, uid, mockAuth } = setup(); const configuration = Configuration.getCachedConfiguration(); enableRateLimitExpects(); -const uid = new ObjectId().toHexString(); -const mockAuth = mockBearerAuthentication(uid); describe("result controller test", () => { - beforeEach(() => { - mockAuth.beforeEach(); - }); - describe("getResults", () => { const resultMock = vi.spyOn(ResultDal, "getResults"); diff --git a/backend/__tests__/api/controllers/user.spec.ts b/backend/__tests__/api/controllers/user.spec.ts index cf5e62007..1a7634270 100644 --- a/backend/__tests__/api/controllers/user.spec.ts +++ b/backend/__tests__/api/controllers/user.spec.ts @@ -8,8 +8,7 @@ import { afterAll, vi, } from "vitest"; -import request from "supertest"; -import app from "../../../src/app"; +import { setup } from "../../__testData__/controller-test"; import * as Configuration from "../../../src/init/configuration"; import { generateCurrentTestActivity } from "../../../src/api/controllers/user"; import * as UserDal from "../../../src/dal/user"; @@ -30,10 +29,7 @@ import * as ApeKeysDal from "../../../src/dal/ape-keys"; import * as LogDal from "../../../src/dal/logs"; import { ObjectId } from "mongodb"; import { PersonalBest } from "@monkeytype/schemas/shared"; -import { - mockAuthenticateWithApeKey, - mockBearerAuthentication, -} from "../../__testData__/auth"; +import { mockAuthenticateWithApeKey } from "../../__testData__/auth"; import { randomUUID } from "node:crypto"; import _ from "lodash"; import { MonkeyMail, UserStreak } from "@monkeytype/schemas/users"; @@ -42,16 +38,10 @@ import { LeaderboardEntry } from "@monkeytype/schemas/leaderboards"; import * as WeeklyXpLeaderboard from "../../../src/services/weekly-xp-leaderboard"; import { pb } from "../../__testData__/users"; -const mockApp = request(app); +const { mockApp, uid, mockAuth } = setup(); const configuration = Configuration.getCachedConfiguration(); -const uid = new ObjectId().toHexString(); -const mockAuth = mockBearerAuthentication(uid); describe("user controller test", () => { - beforeEach(() => { - mockAuth.beforeEach(); - }); - describe("user signup", () => { const blocklistContainsMock = vi.spyOn(BlocklistDal, "contains"); const firebaseDeleteUserMock = vi.spyOn(AuthUtils, "deleteUser"); @@ -637,7 +627,6 @@ describe("user controller test", () => { const logsDeleteUserMock = vi.spyOn(LogDal, "deleteUserLogs"); beforeEach(() => { - mockAuth.beforeEach(); [ firebaseDeleteUserMock, deleteUserMock, diff --git a/backend/__tests__/api/controllers/webhooks.spec.ts b/backend/__tests__/api/controllers/webhooks.spec.ts index 28c3c9640..72cdd41d2 100644 --- a/backend/__tests__/api/controllers/webhooks.spec.ts +++ b/backend/__tests__/api/controllers/webhooks.spec.ts @@ -1,10 +1,9 @@ import { describe, it, expect, beforeEach, vi } from "vitest"; +import { setup } from "../../__testData__/controller-test"; import GeorgeQueue from "../../../src/queues/george-queue"; import crypto from "crypto"; -import request from "supertest"; -import app from "../../../src/app"; -const mockApp = request(app); +const { mockApp } = setup(); describe("WebhooksController", () => { describe("githubRelease", () => {