-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
309 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { CompiledData } from "../../ports/CompileData"; | ||
import type { Db, DbApi } from "../../ports/DbApi"; | ||
|
||
export class InMemoryDbApi implements DbApi { | ||
#softwareRows: Db.SoftwareRow[] = []; | ||
#agentRows: Db.AgentRow[] = []; | ||
#softwareReferentRows: Db.SoftwareReferentRow[] = []; | ||
#softwareUserRows: Db.SoftwareUserRow[] = []; | ||
#instanceRows: Db.InstanceRow[] = []; | ||
|
||
#compiledData: CompiledData<"private"> = []; | ||
|
||
async fetchDb() { | ||
console.log("fetchDb"); | ||
return { | ||
softwareRows: this.#softwareRows, | ||
agentRows: this.#agentRows, | ||
softwareReferentRows: this.#softwareReferentRows, | ||
softwareUserRows: this.#softwareUserRows, | ||
instanceRows: this.#instanceRows | ||
}; | ||
} | ||
|
||
async updateDb({ newDb }: { newDb: Db }) { | ||
this.#softwareRows = newDb.softwareRows; | ||
this.#agentRows = newDb.agentRows; | ||
this.#softwareReferentRows = newDb.softwareReferentRows; | ||
this.#softwareUserRows = newDb.softwareUserRows; | ||
this.#instanceRows = newDb.instanceRows; | ||
} | ||
|
||
async fetchCompiledData() { | ||
return this.#compiledData; | ||
} | ||
|
||
async updateCompiledData({ newCompiledData }: { newCompiledData: CompiledData<"private"> }) { | ||
this.#compiledData = newCompiledData; | ||
} | ||
|
||
// test helpers | ||
|
||
get softwareRows() { | ||
return this.#softwareRows; | ||
} | ||
|
||
get agentRows() { | ||
return this.#agentRows; | ||
} | ||
|
||
get softwareReferentRows() { | ||
return this.#softwareReferentRows; | ||
} | ||
|
||
get softwareUserRows() { | ||
return this.#softwareUserRows; | ||
} | ||
|
||
get instanceRows() { | ||
return this.#instanceRows; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { bootstrapCore } from "../core"; | ||
import { InMemoryDbApi } from "../core/adapters/dbApi/createInMemoryDbApi"; | ||
import { ExternalDataOrigin } from "../core/ports/GetSoftwareExternalData"; | ||
import { createRouter } from "./router"; | ||
import { User } from "./user"; | ||
|
||
type TestCallerConfig = { | ||
user: User | undefined; | ||
}; | ||
|
||
export const defaultUser: User = { | ||
id: "1", | ||
email: "[email protected]", | ||
organization: "Default Organization" | ||
}; | ||
|
||
export type ApiCaller = Awaited<ReturnType<typeof createTestCaller>>["apiCaller"]; | ||
|
||
export const createTestCaller = async ({ user }: TestCallerConfig = { user: defaultUser }) => { | ||
const externalSoftwareDataOrigin: ExternalDataOrigin = "wikidata"; | ||
|
||
const { core, context } = await bootstrapCore({ | ||
"dbConfig": { dbKind: "inMemory" }, | ||
"keycloakUserApiParams": undefined, | ||
"githubPersonalAccessTokenForApiRateLimit": "fake-token", | ||
"doPerPerformPeriodicalCompilation": false, | ||
"doPerformCacheInitialization": false, | ||
"externalSoftwareDataOrigin": externalSoftwareDataOrigin | ||
}); | ||
|
||
const jwtClaimByUserKey = { | ||
"id": "sub", | ||
"email": "email", | ||
"organization": "organization" | ||
}; | ||
|
||
const { router } = createRouter({ | ||
core, | ||
coreContext: context, | ||
keycloakParams: undefined, | ||
redirectUrl: undefined, | ||
externalSoftwareDataOrigin, | ||
readmeUrl: "http://readme.url", | ||
termsOfServiceUrl: "http://terms.url", | ||
jwtClaimByUserKey | ||
}); | ||
|
||
return { apiCaller: router.createCaller({ user }), inMemoryDb: context.dbApi as InMemoryDbApi }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,110 @@ | ||
import { expectToEqual } from "../tools/test.helpers"; | ||
import { describe, it } from "vitest"; | ||
import { describe, expect, it, beforeAll } from "vitest"; | ||
import { InMemoryDbApi } from "../core/adapters/dbApi/createInMemoryDbApi"; | ||
import { CompiledData } from "../core/ports/CompileData"; | ||
import { | ||
createDeclarationFormData, | ||
createSoftwareFormData, | ||
expectToEqual, | ||
expectToMatchObject | ||
} from "../tools/test.helpers"; | ||
import { ApiCaller, createTestCaller, defaultUser } from "./createTestCaller"; | ||
|
||
const softwareFormData = createSoftwareFormData(); | ||
const declarationFormData = createDeclarationFormData(); | ||
|
||
describe("RPC e2e tests", () => { | ||
describe("route something", () => { | ||
it("works", () => { | ||
expectToEqual({ not: "bad" }, { not: "good" }); | ||
let apiCaller: ApiCaller; | ||
let inMemoryDb: InMemoryDbApi; | ||
|
||
describe("Add new agent as user - Wrong paths", () => { | ||
it("fails with UNAUTHORIZED if user is not logged in", async () => { | ||
({ apiCaller, inMemoryDb } = await createTestCaller({ user: undefined })); | ||
expect( | ||
apiCaller.createUserOrReferent({ | ||
formData: declarationFormData, | ||
softwareName: "Some software" | ||
}) | ||
).rejects.toThrow("UNAUTHORIZED"); | ||
}); | ||
|
||
it("fails when software is not found in SILL", async () => { | ||
({ apiCaller, inMemoryDb } = await createTestCaller()); | ||
expect( | ||
apiCaller.createUserOrReferent({ | ||
formData: declarationFormData, | ||
softwareName: "Some software" | ||
}) | ||
).rejects.toThrow("Software not in SILL"); | ||
}); | ||
}); | ||
|
||
describe("Scenario - Add a new software then mark an agent as user of this software", () => { | ||
beforeAll(async () => { | ||
({ apiCaller, inMemoryDb } = await createTestCaller()); | ||
}); | ||
|
||
it("adds a new software", async () => { | ||
expect(inMemoryDb.softwareRows).toHaveLength(0); | ||
const initialSoftwares = await apiCaller.getSoftwares(); | ||
expectToEqual(initialSoftwares, []); | ||
|
||
await apiCaller.createSoftware({ | ||
formData: softwareFormData | ||
}); | ||
expect(inMemoryDb.softwareRows).toHaveLength(1); | ||
const expectedSoftware: Partial<CompiledData.Software<"public">> = { | ||
"description": softwareFormData.softwareDescription, | ||
"externalId": softwareFormData.externalId, | ||
"doRespectRgaa": softwareFormData.doRespectRgaa, | ||
"isFromFrenchPublicService": softwareFormData.isFromFrenchPublicService, | ||
"isPresentInSupportContract": softwareFormData.isPresentInSupportContract, | ||
"keywords": softwareFormData.softwareKeywords, | ||
"license": softwareFormData.softwareLicense, | ||
"logoUrl": softwareFormData.softwareLogoUrl, | ||
"name": softwareFormData.softwareName, | ||
"softwareType": softwareFormData.softwareType, | ||
"versionMin": softwareFormData.softwareMinimalVersion, | ||
"testUrls": [], | ||
"workshopUrls": [], | ||
"categories": [], | ||
"isStillInObservation": false | ||
}; | ||
|
||
expectToMatchObject(inMemoryDb.softwareRows[0], { | ||
...expectedSoftware, | ||
"addedByAgentEmail": defaultUser.email, | ||
"similarSoftwareExternalDataIds": softwareFormData.similarSoftwareExternalDataIds | ||
}); | ||
}); | ||
|
||
it("gets the new software in the list", async () => { | ||
const softwares = await apiCaller.getSoftwares(); | ||
expect(softwares).toHaveLength(1); | ||
expectToMatchObject(softwares[0], { softwareName: softwareFormData.softwareName }); | ||
}); | ||
|
||
it("adds an agent as user of the software", async () => { | ||
expect(inMemoryDb.agentRows).toHaveLength(1); | ||
expect(inMemoryDb.softwareRows).toHaveLength(1); | ||
expect(inMemoryDb.softwareUserRows).toHaveLength(0); | ||
await apiCaller.createUserOrReferent({ | ||
formData: declarationFormData, | ||
softwareName: "Some software" | ||
}); | ||
|
||
if (declarationFormData.declarationType !== "user") | ||
throw new Error("This test is only for user declaration"); | ||
|
||
expect(inMemoryDb.softwareUserRows).toHaveLength(1); | ||
|
||
expectToEqual(inMemoryDb.softwareUserRows[0], { | ||
"agentEmail": defaultUser.email, | ||
"softwareId": inMemoryDb.softwareRows[0].id, | ||
"os": declarationFormData.os, | ||
"serviceUrl": declarationFormData.serviceUrl, | ||
"useCaseDescription": declarationFormData.usecaseDescription, | ||
"version": declarationFormData.version | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.