test: refactor controller tests (@fehmer) (#6925)

This commit is contained in:
Christian Fehmer 2025-09-01 11:53:05 +02:00 committed by GitHub
parent d9887cf37c
commit 01f981cd0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 56 additions and 122 deletions

View file

@ -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 };
}

View file

@ -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();
});

View file

@ -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(() => {

View file

@ -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");

View file

@ -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);

View file

@ -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");

View file

@ -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");

View file

@ -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");

View file

@ -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 () => {

View file

@ -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", () => {

View file

@ -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();
});

View file

@ -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");

View file

@ -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,

View file

@ -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", () => {