Skip to content

Commit

Permalink
feat: Initial test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Sep 19, 2023
1 parent c0f10c5 commit 29cf054
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 59 deletions.
2 changes: 1 addition & 1 deletion e2e/tests/api-driven/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"dependencies": {
"@cucumber/cucumber": "^9.3.0",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#17f3bf5",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#f2a22d3",
"axios": "^1.4.0",
"dotenv": "^16.3.1",
"dotenv-expand": "^10.0.0",
Expand Down
99 changes: 53 additions & 46 deletions e2e/tests/api-driven/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions e2e/tests/api-driven/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from "node:assert";
import { CoreDomainClient } from "@opensystemslab/planx-core";
import { TEST_EMAIL } from "../../ui-driven/src/helpers";
import { buildJWT } from "./jwt";

// check env variables are defined
Expand All @@ -18,10 +17,10 @@ export const $admin = new CoreDomainClient({
});

/**
* Get client authorised to the permissions level of the test user
* Get client authorised to the permissions level of the provided user
*/
export const getClient = async () => {
const jwt = await buildJWT(TEST_EMAIL);
export const getClient = async (email: string) => {
const jwt = await buildJWT(email);
if (!jwt) throw Error("Unable to generate JWT for test user");

const client = new CoreDomainClient({
Expand Down
36 changes: 28 additions & 8 deletions e2e/tests/api-driven/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
import { TEST_EMAIL } from "../../ui-driven/src/helpers";
import { $admin } from "./client";

export async function createTeam() {
return $admin.team.create({
export function createTeam(args?: Partial<Parameters<typeof $admin.team.create>[0]>) {
return safely(() => $admin.team.create({
name: "E2E Test Team",
slug: "E2E",
logo: "https://raw.githubusercontent.com/theopensystemslab/planx-team-logos/main/planx-testing.svg",
primaryColor: "#444444",
submissionEmail: TEST_EMAIL,
homepage: "planx.uk",
});
...args,
}));
}

export async function createUser() {
return $admin.user.create({
export function createUser(args?: Partial<Parameters<typeof $admin.user.create>[0]>) {
return safely(() => $admin.user.create({
firstName: "Test",
lastName: "Test",
email: TEST_EMAIL,
});
...args,
}));
}

export function createFlow(args: Omit<Parameters<typeof $admin.flow.create>[0], "data">) {
return safely(() => $admin.flow.create({
data: { dummy: "flowData "},
...args,
}))
}

/**
* Error handling boilerplate for client functions
*/
export function safely<T extends () => ReturnType<T>>(callback: T) {
const result = callback();
if (!result) {
throw new Error("Error setting up E2E test");
}
return result;
}

export async function tearDownTestContext({
Expand Down Expand Up @@ -56,6 +76,6 @@ export async function tearDownTestContext({
}
}

export async function getTestUser() {
return await $admin.user.getByEmail(TEST_EMAIL);
export async function getUser(email: string) {
return await $admin.user.getByEmail(email);
}
Loading

0 comments on commit 29cf054

Please sign in to comment.