Skip to content

Commit

Permalink
chore: Update ITP tests to follow new format
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Sep 19, 2023
1 parent 29cf054 commit ba880b2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,6 @@ export function safely<T extends () => ReturnType<T>>(callback: T) {
return result;
}

export async function tearDownTestContext({
teamId,
userId,
flowId,
publishedFlowId,
sessionId,
paymentRequestId,
}: {
teamId?: number;
userId?: number;
flowId?: string;
publishedFlowId?: number;
sessionId?: string;
paymentRequestId?: string;
}) {
if (paymentRequestId) {
await $admin.paymentRequest._destroy(paymentRequestId);
}
if (sessionId) {
await $admin.application._destroyAll(sessionId);
await $admin.session._destroy(sessionId);
}
if (publishedFlowId) {
await $admin.flow._destroyPublished(publishedFlowId);
}
if (flowId) {
await $admin.flow._destroy(flowId);
}
if (userId) {
await $admin.user._destroy(userId);
}
if (teamId) {
await $admin.team._destroy(teamId);
}
}

export async function getUser(email: string) {
return await $admin.user.getByEmail(email);
}
41 changes: 41 additions & 0 deletions e2e/tests/api-driven/src/invite-to-pay/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CustomWorld } from "./steps";
import axios from "axios";
import { readFileSync } from "node:fs";
import type {
Expand All @@ -12,6 +13,7 @@ import {
} from "./mocks";
import { $admin } from "../client";
import { TEST_EMAIL } from "../../../ui-driven/src/helpers";
import { createTeam, createUser } from "../globalHelpers";

export async function setUpMocks() {
const serverMockFile = readFileSync(`${__dirname}/mocks/server-mocks.yaml`);
Expand Down Expand Up @@ -127,3 +129,42 @@ export async function getSessionSubmittedAt(
const detailedSession = await $admin.session.findDetails(sessionId);
return detailedSession?.submittedAt;
}

export async function cleanup({
teamId,
userId,
flowId,
publishedFlowId,
sessionId,
paymentRequestId,
}: CustomWorld) {
if (paymentRequestId) {
await $admin.paymentRequest._destroy(paymentRequestId);
}
if (sessionId) {
await $admin.application._destroyAll(sessionId);
await $admin.session._destroy(sessionId);
}
if (publishedFlowId) {
await $admin.flow._destroyPublished(publishedFlowId);
}
if (flowId) {
await $admin.flow._destroy(flowId);
}
if (userId) {
await $admin.user._destroy(userId);
}
if (teamId) {
await $admin.team._destroy(teamId);
}
}

export const setup = async () => {
await setUpMocks();
const world = {
teamId: await createTeam(),
userId: await createUser(),
};

return world;
}
68 changes: 31 additions & 37 deletions e2e/tests/api-driven/src/invite-to-pay/steps.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,69 @@
import { strict as assert } from "node:assert";
import { Given, When, Then, Before, After, AfterAll, AfterStep, setDefaultTimeout } from "@cucumber/cucumber";
import { Given, When, Then, Before, After, World } from "@cucumber/cucumber";
import {
setUpMocks,
buildITPFlow,
buildSessionForFlow,
buildPaymentRequestForSession,
markPaymentRequestAsPaid,
getSendResponse,
getSessionSubmittedAt,
waitForResponse,
cleanup,
setup,
} from "./helpers";
import { createTeam, createUser, tearDownTestContext } from "../helpers";

const context: {
teamId?: number;
userId?: number;
export class CustomWorld extends World {
teamId!: number;
userId!: number;
flowId?: string;
publishedFlowId?: number;
sessionId?: string;
paymentRequestId?: string;
} = {};
}

Before("@invite-to-pay", async () => {
await setUpMocks();
context.teamId = await createTeam();
if (!context.teamId) {
throw new Error("team not found");
}
context.userId = await createUser();
if (!context.userId) {
throw new Error("user not found");
}
Before<CustomWorld>("@invite-to-pay", async function() {
const { teamId, userId } = await setup();
this.teamId = teamId;
this.userId = userId;
});

After("@invite-to-pay", async () => {
await tearDownTestContext(context);
After("@invite-to-pay", async function(this: CustomWorld) {
await cleanup(this);
});

Given(
"a session with a payment request for an invite to pay flow where {string} is a send destination", { timeout: 60 * 1000 },
async (destination) => {
async function (this: CustomWorld, destination: string) {
const { flowId, publishedFlowId } = await buildITPFlow({
destination,
teamId: context.teamId!,
userId: context.userId!,
teamId: this.teamId,
userId: this.userId,
});
context.flowId = flowId;
if (!context.flowId) {
this.flowId = flowId;
if (!this.flowId) {
throw new Error("flow not found");
}
context.publishedFlowId = publishedFlowId;
if (!context.publishedFlowId) {
this.publishedFlowId = publishedFlowId;
if (!this.publishedFlowId) {
throw new Error("publishedFlowId not found");
}
context.sessionId = await buildSessionForFlow(context.flowId);
if (!context.sessionId) {
this.sessionId = await buildSessionForFlow(this.flowId);
if (!this.sessionId) {
throw new Error("session not found");
}
const paymentRequest = await buildPaymentRequestForSession(
context.sessionId,
this.sessionId,
);
context.paymentRequestId = paymentRequest.id;
this.paymentRequestId = paymentRequest.id;
},
);

When("the payment request's `paid_at` date is set", async () => {
if (!context.paymentRequestId) {
When("the payment request's `paid_at` date is set", async function (this: CustomWorld) {
if (!this.paymentRequestId) {
throw new Error("payment request not found");
}
const operationSucceeded = await markPaymentRequestAsPaid(
context.paymentRequestId,
this.paymentRequestId,
);
if (!operationSucceeded) {
throw new Error("payment request was not marked as paid");
Expand All @@ -79,18 +73,18 @@ When("the payment request's `paid_at` date is set", async () => {
Then(
"there should be an audit entry for a successful {string} submission",
{ timeout: 6 * 15000 + 1000 },
async (destination) => {
async function (this: CustomWorld, destination: string) {
const response = await waitForResponse({
name: `Application submission for ${destination}`,
request: getSendResponse.bind(null, destination, context.sessionId!),
request: getSendResponse.bind(null, destination, this.sessionId!),
retries: 5,
delay: 15000,
});
assert(response);
},
);

Then("the session's `submitted_at` date should be set", async () => {
const submittedAt = await getSessionSubmittedAt(context.sessionId!);
Then("the session's `submitted_at` date should be set", async function (this: CustomWorld) {
const submittedAt = await getSessionSubmittedAt(this.sessionId!);
assert(submittedAt);
});
6 changes: 3 additions & 3 deletions e2e/tests/api-driven/src/permissions/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { $admin, getClient } from "../client";
import { CustomWorld } from "./steps";
import { queries } from "./queries";
import { createFlow, createTeam, createUser } from "../helpers";
import { createFlow, createTeam, createUser } from "../globalHelpers";

interface PerformGQLQueryArgs {
world: CustomWorld;
Expand All @@ -17,13 +17,13 @@ export const addUserToTeam = async (userId: number, teamId: number) => {
});
};

export const cleanupPermissionsTest = async () => {
export const cleanup = async () => {
await $admin.flow._destroyAll();
await $admin.team._destroyAll();
await $admin.user._destroyAll();
}

export const setupPermissionsTest = async () => {
export const setup = async () => {
const teamId1 = await createTeam({ name: "E2E Team 1", slug: "e2e-team1" });
const teamId2 = await createTeam({ name: "E2E Team 2", slug: "e2e-team2" });
const user1 = {
Expand Down
10 changes: 5 additions & 5 deletions e2e/tests/api-driven/src/permissions/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { After, Before, Given, Then, When, World } from "@cucumber/cucumber";
import { strict as assert } from "node:assert";
import {
getUser,
} from "../helpers";
} from "../globalHelpers";
import {
addUserToTeam,
cleanupPermissionsTest,
cleanup,
performGQLQuery,
setupPermissionsTest,
setup,
} from "./helpers";

interface TestUser {
Expand All @@ -28,7 +28,7 @@ export class CustomWorld extends World {
}

Before<CustomWorld>("@team-admin-permissions", async function () {
const { user1, user2, teamId1, teamId2, team1Flow, team2Flow } = await setupPermissionsTest();
const { user1, user2, teamId1, teamId2, team1Flow, team2Flow } = await setup();
this.user1 = user1
this.user2 = user2
this.teamId1 = teamId1
Expand All @@ -38,7 +38,7 @@ Before<CustomWorld>("@team-admin-permissions", async function () {
});

After("@team-admin-permissions", async function () {
await cleanupPermissionsTest();
await cleanup();
});

Given("a teamAdmin is a member of a team", async function (this: CustomWorld) {
Expand Down

0 comments on commit ba880b2

Please sign in to comment.