Skip to content

Commit 01f981c

Browse files
authored
test: refactor controller tests (@fehmer) (monkeytypegame#6925)
1 parent d9887cf commit 01f981c

File tree

14 files changed

+56
-122
lines changed

14 files changed

+56
-122
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import request from "supertest";
2+
import app from "../../src/app";
3+
import { ObjectId } from "mongodb";
4+
import { mockBearerAuthentication } from "./auth";
5+
import { beforeEach } from "vitest";
6+
7+
export function setup() {
8+
const mockApp = request(app);
9+
const uid = new ObjectId().toHexString();
10+
const mockAuth = mockBearerAuthentication(uid);
11+
12+
beforeEach(() => {
13+
mockAuth.beforeEach();
14+
});
15+
16+
return { mockApp, uid, mockAuth };
17+
}

backend/__tests__/api/controllers/admin.spec.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, it, expect, beforeEach, vi } from "vitest";
2-
import request, { Test as SuperTest } from "supertest";
3-
import app from "../../../src/app";
2+
import { setup } from "../../__testData__/controller-test";
43
import { ObjectId } from "mongodb";
54
import * as Configuration from "../../../src/init/configuration";
65
import * as AdminUuidDal from "../../../src/dal/admin-uids";
@@ -11,12 +10,9 @@ import GeorgeQueue from "../../../src/queues/george-queue";
1110
import * as AuthUtil from "../../../src/utils/auth";
1211
import _ from "lodash";
1312
import { enableRateLimitExpects } from "../../__testData__/rate-limit";
14-
import { mockBearerAuthentication } from "../../__testData__/auth";
1513

16-
const mockApp = request(app);
14+
const { mockApp, uid } = setup();
1715
const configuration = Configuration.getCachedConfiguration();
18-
const uid = new ObjectId().toHexString();
19-
const mockAuth = mockBearerAuthentication(uid);
2016
enableRateLimitExpects();
2117

2218
describe("AdminController", () => {
@@ -27,7 +23,6 @@ describe("AdminController", () => {
2723
isAdminMock.mockClear();
2824
await enableAdminEndpoints(true);
2925
isAdminMock.mockResolvedValue(true);
30-
mockAuth.beforeEach();
3126
logsAddImportantLog.mockClear().mockResolvedValue();
3227
});
3328

backend/__tests__/api/controllers/ape-key.spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
2-
import request, { Test as SuperTest } from "supertest";
3-
import app from "../../../src/app";
2+
import { setup } from "../../__testData__/controller-test";
3+
import { Test as SuperTest } from "supertest";
44
import * as ApeKeyDal from "../../../src/dal/ape-keys";
55
import { ObjectId } from "mongodb";
66
import * as Configuration from "../../../src/init/configuration";
77
import * as UserDal from "../../../src/dal/user";
88
import _ from "lodash";
9-
import { mockBearerAuthentication } from "../../__testData__/auth";
109

11-
const mockApp = request(app);
10+
const { mockApp, uid } = setup();
1211
const configuration = Configuration.getCachedConfiguration();
13-
const uid = new ObjectId().toHexString();
14-
const mockAuth = mockBearerAuthentication(uid);
1512

1613
describe("ApeKeyController", () => {
1714
const getUserMock = vi.spyOn(UserDal, "getPartialUser");
@@ -21,7 +18,6 @@ describe("ApeKeyController", () => {
2118
getUserMock.mockResolvedValue(user(uid, {}));
2219
vi.useFakeTimers();
2320
vi.setSystemTime(1000);
24-
mockAuth.beforeEach();
2521
});
2622

2723
afterEach(() => {

backend/__tests__/api/controllers/config.spec.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
2-
import request from "supertest";
3-
import app from "../../../src/app";
1+
import { describe, it, expect, afterEach, vi } from "vitest";
2+
import { setup } from "../../__testData__/controller-test";
43
import * as ConfigDal from "../../../src/dal/config";
54
import { ObjectId } from "mongodb";
6-
import { mockBearerAuthentication } from "../../__testData__/auth";
7-
const mockApp = request(app);
8-
const uid = new ObjectId().toHexString();
9-
const mockAuth = mockBearerAuthentication(uid);
5+
6+
const { mockApp, uid } = setup();
107

118
describe("ConfigController", () => {
12-
beforeEach(() => {
13-
mockAuth.beforeEach();
14-
});
159
describe("get config", () => {
1610
const getConfigMock = vi.spyOn(ConfigDal, "getConfig");
1711

backend/__tests__/api/controllers/configuration.spec.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
import { describe, it, expect, beforeEach, vi } from "vitest";
2-
import request from "supertest";
3-
import app from "../../../src/app";
2+
import { setup } from "../../__testData__/controller-test";
43
import {
54
BASE_CONFIGURATION,
65
CONFIGURATION_FORM_SCHEMA,
76
} from "../../../src/constants/base-configuration";
87
import * as Configuration from "../../../src/init/configuration";
98
import type { Configuration as ConfigurationType } from "@monkeytype/schemas/configuration";
10-
import { ObjectId } from "mongodb";
119
import * as Misc from "../../../src/utils/misc";
1210
import * as AdminUuids from "../../../src/dal/admin-uids";
13-
import { mockBearerAuthentication } from "../../__testData__/auth";
1411

15-
const mockApp = request(app);
16-
const uid = new ObjectId().toHexString();
12+
const { mockApp, uid, mockAuth } = setup();
1713

1814
describe("Configuration Controller", () => {
1915
const isDevEnvironmentMock = vi.spyOn(Misc, "isDevEnvironment");
20-
const mockAuth = mockBearerAuthentication(uid);
16+
2117
const isAdminMock = vi.spyOn(AdminUuids, "isAdmin");
2218

2319
beforeEach(() => {
2420
isAdminMock.mockClear();
25-
mockAuth.beforeEach();
21+
2622
isDevEnvironmentMock.mockClear();
2723

2824
isDevEnvironmentMock.mockReturnValue(true);
@@ -143,7 +139,7 @@ describe("Configuration Controller", () => {
143139
isDevEnvironmentMock.mockReturnValue(false);
144140

145141
//WHEN
146-
await request(app)
142+
await mockApp
147143
.patch("/configuration")
148144
.send({ configuration: {} })
149145
.expect(401);

backend/__tests__/api/controllers/dev.spec.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
import { describe, it, expect, beforeEach, vi } from "vitest";
2-
import request from "supertest";
3-
import app from "../../../src/app";
4-
import * as AuthUtils from "../../../src/utils/auth";
5-
import { ObjectId } from "mongodb";
2+
import { setup } from "../../__testData__/controller-test";
63
import * as Misc from "../../../src/utils/misc";
7-
import { DecodedIdToken } from "firebase-admin/auth";
8-
9-
const uid = new ObjectId().toHexString();
10-
const mockDecodedToken = {
11-
uid,
12-
13-
iat: 0,
14-
} as DecodedIdToken;
15-
const mockApp = request(app);
164

5+
const { mockApp } = setup();
176
describe("DevController", () => {
18-
const verifyIdTokenMock = vi.spyOn(AuthUtils, "verifyIdToken");
19-
beforeEach(() => {
20-
verifyIdTokenMock.mockClear().mockResolvedValue(mockDecodedToken);
21-
});
22-
237
describe("generate testData", () => {
248
const isDevEnvironmentMock = vi.spyOn(Misc, "isDevEnvironment");
259

backend/__tests__/api/controllers/leaderboard.spec.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
2+
import { setup } from "../../__testData__/controller-test";
23
import _ from "lodash";
34
import { ObjectId } from "mongodb";
4-
import request from "supertest";
5-
import app from "../../../src/app";
65
import * as LeaderboardDal from "../../../src/dal/leaderboards";
76
import * as DailyLeaderboards from "../../../src/utils/daily-leaderboards";
87
import * as WeeklyXpLeaderboard from "../../../src/services/weekly-xp-leaderboard";
98
import * as Configuration from "../../../src/init/configuration";
10-
import {
11-
mockAuthenticateWithApeKey,
12-
mockBearerAuthentication,
13-
} from "../../__testData__/auth";
9+
import { mockAuthenticateWithApeKey } from "../../__testData__/auth";
1410
import { XpLeaderboardEntry } from "@monkeytype/schemas/leaderboards";
1511

16-
const mockApp = request(app);
12+
const { mockApp, uid } = setup();
1713
const configuration = Configuration.getCachedConfiguration();
18-
const uid = new ObjectId().toHexString();
19-
const mockAuth = mockBearerAuthentication(uid);
2014

2115
const allModes = [
2216
"10",
@@ -32,9 +26,6 @@ const allModes = [
3226
];
3327

3428
describe("Loaderboard Controller", () => {
35-
beforeEach(() => {
36-
mockAuth.beforeEach();
37-
});
3829
describe("get leaderboard", () => {
3930
const getLeaderboardMock = vi.spyOn(LeaderboardDal, "get");
4031
const getLeaderboardCountMock = vi.spyOn(LeaderboardDal, "getCount");

backend/__tests__/api/controllers/preset.spec.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
2-
import request from "supertest";
3-
import app from "../../../src/app";
1+
import { describe, it, expect, afterEach, vi } from "vitest";
2+
import { setup } from "../../__testData__/controller-test";
43
import * as PresetDal from "../../../src/dal/preset";
54
import { ObjectId } from "mongodb";
6-
import { mockBearerAuthentication } from "../../__testData__/auth";
7-
const mockApp = request(app);
8-
const uid = new ObjectId().toHexString();
9-
const mockAuth = mockBearerAuthentication(uid);
105

11-
describe("PresetController", () => {
12-
beforeEach(() => {
13-
mockAuth.beforeEach();
14-
});
6+
const { mockApp, uid } = setup();
157

8+
describe("PresetController", () => {
169
describe("get presets", () => {
1710
const getPresetsMock = vi.spyOn(PresetDal, "getPresets");
1811

backend/__tests__/api/controllers/psa.spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { describe, it, expect, afterEach, vi } from "vitest";
2-
import request from "supertest";
3-
import app from "../../../src/app";
2+
import { setup } from "../../__testData__/controller-test";
43
import * as PsaDal from "../../../src/dal/psa";
54
import * as Prometheus from "../../../src/utils/prometheus";
65
import { ObjectId } from "mongodb";
7-
import { mockBearerAuthentication } from "../../__testData__/auth";
8-
const mockApp = request(app);
9-
const uid = new ObjectId().toHexString();
10-
const mockAuth = mockBearerAuthentication(uid);
6+
7+
const { mockApp, uid } = setup();
118

129
describe("Psa Controller", () => {
1310
describe("get psa", () => {
@@ -17,7 +14,6 @@ describe("Psa Controller", () => {
1714
afterEach(() => {
1815
getPsaMock.mockClear();
1916
recordClientVersionMock.mockClear();
20-
mockAuth.beforeEach();
2117
});
2218

2319
it("get psas without authorization", async () => {

backend/__tests__/api/controllers/public.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { describe, it, expect, afterEach, vi } from "vitest";
2-
import request from "supertest";
3-
import app from "../../../src/app";
2+
import { setup } from "../../__testData__/controller-test";
43
import * as PublicDal from "../../../src/dal/public";
5-
const mockApp = request(app);
4+
5+
const { mockApp } = setup();
66

77
describe("PublicController", () => {
88
describe("get speed histogram", () => {

0 commit comments

Comments
 (0)