Skip to content

Commit 41ce691

Browse files
committed
fix: fix e2e tests
1 parent 604eacf commit 41ce691

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ docker-data
66
.dev
77
.bruno
88
dump*
9-
CLAUDE.md
9+
CLAUDE.md

api/src/rpc/createTestCaller.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import { Database } from "../core/adapters/dbApi/kysely/kysely.database";
88
import { createPgDialect } from "../core/adapters/dbApi/kysely/kysely.dialect";
99
import { getWikidataSoftware } from "../core/adapters/wikidata/getWikidataSoftware";
1010
import { getWikidataSoftwareOptions } from "../core/adapters/wikidata/getWikidataSoftwareOptions";
11+
import { Session } from "../core/ports/DbApiV2";
1112
import { ExternalDataOrigin } from "../core/ports/GetSoftwareExternalData";
1213
import { testPgUrl } from "../tools/test.helpers";
1314
import { createRouter } from "./router";
1415
import { UserWithId } from "../lib/ApiTypes";
1516

1617
type TestCallerConfig = {
1718
currentUser: UserWithId | undefined;
19+
db?: Kysely<Database>;
1820
};
1921

2022
export const defaultUser: UserWithId = {
@@ -29,9 +31,9 @@ export const defaultUser: UserWithId = {
2931

3032
export type ApiCaller = Awaited<ReturnType<typeof createTestCaller>>["apiCaller"];
3133

32-
export const createTestCaller = async ({ currentUser }: TestCallerConfig = { currentUser: defaultUser }) => {
34+
export const createTestCaller = async ({ currentUser, db }: TestCallerConfig = { currentUser: defaultUser }) => {
35+
const kyselyDb = db ?? new Kysely<Database>({ dialect: createPgDialect(testPgUrl) });
3336
const externalSoftwareDataOrigin: ExternalDataOrigin = "wikidata";
34-
const kyselyDb = new Kysely<Database>({ dialect: createPgDialect(testPgUrl) });
3537

3638
const { dbApi, useCases, uiConfig } = await bootstrapCore({
3739
"dbConfig": { dbKind: "kysely", kyselyDb },
@@ -62,5 +64,27 @@ export const createTestCaller = async ({ currentUser }: TestCallerConfig = { cur
6264
uiConfig
6365
});
6466

67+
if (currentUser) {
68+
// creating the current user with an active session
69+
const { declarations, ...rest } = currentUser;
70+
await dbApi.user.add(rest);
71+
const session: Session = {
72+
id: "11111111-1111-1111-1111-111111111111",
73+
state: "test-state",
74+
redirectUrl: null,
75+
userId: currentUser.id,
76+
email: currentUser.email,
77+
accessToken: "test-access-token",
78+
refreshToken: "test-refresh-token",
79+
idToken: "test-id-token",
80+
expiresAt: new Date(Date.now() + 3600 * 1000), // 1 hour from now
81+
createdAt: new Date(),
82+
updatedAt: new Date(),
83+
loggedOutAt: null
84+
};
85+
await dbApi.session.create(session);
86+
await dbApi.session.update(session);
87+
}
88+
6589
return { apiCaller: router.createCaller({ currentUser }), kyselyDb };
6690
};

api/src/rpc/routes.e2e.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { Kysely } from "kysely";
66
import { beforeAll, describe, expect, it } from "vitest";
77
import { Database } from "../core/adapters/dbApi/kysely/kysely.database";
8+
import { createPgDialect } from "../core/adapters/dbApi/kysely/kysely.dialect";
89
import { stripNullOrUndefinedValues } from "../core/adapters/dbApi/kysely/kysely.utils";
910
import type { DbUser } from "../core/ports/DbApiV2";
1011
import type { InstanceFormData, Source } from "../core/usecases/readWriteSillData";
@@ -13,7 +14,8 @@ import {
1314
createInstanceFormData,
1415
createSoftwareFormData,
1516
expectToEqual,
16-
expectToMatchObject
17+
expectToMatchObject,
18+
testPgUrl
1719
} from "../tools/test.helpers";
1820
import { ApiCaller, createTestCaller, defaultUser } from "./createTestCaller";
1921

@@ -96,7 +98,7 @@ describe("RPC e2e tests", () => {
9698
let user: DbUser;
9799

98100
beforeAll(async () => {
99-
({ apiCaller, kyselyDb } = await createTestCaller());
101+
kyselyDb = new Kysely<Database>({ dialect: createPgDialect(testPgUrl) });
100102
await kyselyDb.deleteFrom("software_referents").execute();
101103
await kyselyDb.deleteFrom("software_users").execute();
102104
await kyselyDb.deleteFrom("instances").execute();
@@ -106,11 +108,13 @@ describe("RPC e2e tests", () => {
106108
await kyselyDb.deleteFrom("sources").execute();
107109

108110
await kyselyDb.insertInto("sources").values(mainSource).executeTakeFirst();
111+
112+
({ apiCaller, kyselyDb } = await createTestCaller({ db: kyselyDb, currentUser: defaultUser }));
109113
});
110114

111115
it("gets the list of users, which is initially empty", async () => {
112116
const { users } = await apiCaller.getUsers();
113-
expect(users).toHaveLength(0);
117+
expect(users).toHaveLength(1);
114118
});
115119

116120
it("adds a new software", async () => {
@@ -128,7 +132,7 @@ describe("RPC e2e tests", () => {
128132
expectToMatchObject(user, {
129133
id: expect.any(Number),
130134
email: defaultUser.email,
131-
organization: null
135+
organization: defaultUser.organization
132136
});
133137

134138
const softwareRows = await getSoftwareRows();
@@ -174,7 +178,7 @@ describe("RPC e2e tests", () => {
174178
expect(users).toHaveLength(1);
175179
expectToMatchObject(users[0], {
176180
"email": defaultUser.email,
177-
"organization": null
181+
"organization": defaultUser.organization
178182
});
179183
});
180184

0 commit comments

Comments
 (0)