Skip to content

Commit

Permalink
add type safety and refine step files
Browse files Browse the repository at this point in the history
refine race condition Promise.All

improve error catching on createFlow
  • Loading branch information
RODO94 committed Nov 11, 2024
1 parent 24242e2 commit 1623a33
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 187 deletions.
7 changes: 4 additions & 3 deletions e2e/tests/api-driven/src/demo-workspace/demoUser.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Feature: Demo user access

# We create the steps data from these Given statements below
# altering these will alter what data is created for testing
Background:

Given I have the following teams in the database:
Expand Down Expand Up @@ -72,8 +73,8 @@
@demo-user-permissions
Scenario Outline: Actioning flows in other teams
When I am in the "<TEAM>" team
And I want to edit a flow that I did not create
Then I should not have access to modify the flow
Then I should be able to see a flow
But I should not have access to modify the flow

Examples:
| TEAM |
Expand Down
189 changes: 87 additions & 102 deletions e2e/tests/api-driven/src/demo-workspace/helper.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,43 @@
import gql from "graphql-tag";
import { $admin } from "../client";
import { Team } from "@opensystemslab/planx-core/types";
import { UPDATE_FLOW_QUERY } from "../permissions/queries/flows";
import { Team, User } from "@opensystemslab/planx-core/types";
import { UUID } from "crypto";

export type User = {
export type Flow = {
creator_id?: number;
slug: string;
id: UUID;
};

export type TeamsAndFlows = {
id: number;
first_name: string;
last_name: string;
email: string;
slug: string;
flows: Flow[];
};
export type FlowArgs = { teamId: number; slug: string; name: string };

export type DataTableRecord = Record<string, string>;
export type DataTableArray = Record<string, string>[];

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

export const userExistsCheck = async (userId: number) => {
const userOne = await $admin.user.getById(userId);
return Boolean(userOne);
};

export const addDemoUserToTeam = async (userId: number, teamId: number) => {
try {
const memberAdded = await upsertMember({
userId,
teamId,
role: "demoUser",
});
return true;
} catch (error) {
console.error(error);
return false;
}
};

export const checkTeamsExist = async (teamArray) => {
export const checkTeamsExist = async (
teamArray: DataTableArray,
): Promise<Team[]> => {
const existenceArray = await Promise.all(
teamArray.map(async (team) => {
teamArray.map(async (team: DataTableRecord) => {
const teamObj = await $admin.team.getBySlug(team.slug);
return teamObj;
}),
);
return existenceArray;
};

export const createTeams = async (array) => {
const teamIdArray = await Promise.all(
array.map(async (team) => {
const id = await createTeam(team);
return id;
}),
);
return teamIdArray;
};

export const createFlow = async (client, args) => {
try {
const { flow } = await client.request(
gql`
mutation InsertFlow($teamId: Int!, $slug: String!, $name: String!) {
flow: insert_flows_one(
object: { team_id: $teamId, slug: $slug, name: $name }
) {
id
slug
creator_id
}
}
`,
{
teamId: args.teamId,
slug: args.slug,
name: args.name,
},
);

return flow;
} catch (error) {
return false;
}
};

export async function createTeam(
newTeam: Team & { id: number },
): Promise<number> {
export async function createTeam(newTeam: DataTableRecord): Promise<number> {
try {
const response: { insert_teams_one: { id: number } } =
await $admin.client.request(
Expand All @@ -96,7 +48,6 @@ export async function createTeam(
id: $id
name: $name
slug: $slug
# Create empty records for associated tables - these can get populated later
team_settings: { data: {} }
integrations: { data: {} }
}
Expand All @@ -115,32 +66,60 @@ export async function createTeam(
}
}

export async function upsertMember(args): Promise<boolean> {
const response: { insert_team_members_one: { id: number } | null } =
await $admin.client.request(
gql`
mutation UpsertTeamMember(
$role: user_roles_enum = demoUser
$team_id: Int
$user_id: Int
export const createTeamFromArray = async (array: DataTableArray) => {
const teamIdArray = await Promise.all(
array.map(async (team) => {
const id = await createTeam(team);
return id;
}),
);
return teamIdArray;
};

export const createFlowFromArray = async (
client,
flow: DataTableRecord,
): Promise<Flow | false> => {
try {
const flowId = await createFlow(client, {
teamId: Number(flow.team_id),
name: flow.name,
slug: flow.slug,
});
return flowId;
} catch (error: any) {

Check warning on line 90 in e2e/tests/api-driven/src/demo-workspace/helper.ts

View workflow job for this annotation

GitHub Actions / E2E tests

Unexpected any. Specify a different type
console.error(`Error adding flow ${flow.slug}`, error.message);
return false;
}
};

export const createFlow = async (
client,
args: FlowArgs,
): Promise<Flow | false> => {
const { flow } = await client.request(
gql`
mutation InsertFlow($teamId: Int!, $slug: String!, $name: String!) {
flow: insert_flows_one(
object: { team_id: $teamId, slug: $slug, name: $name }
) {
insert_team_members_one(
object: { team_id: $team_id, user_id: $user_id, role: $role }
) {
id
}
id
slug
creator_id
}
`,
{
team_id: args.teamId,
user_id: args.userId,
role: args.role,
},
);
return Boolean(response.insert_team_members_one?.id);
}
}
`,
{
teamId: args.teamId,
slug: args.slug,
name: args.name,
},
);

return flow;
};

export async function createUser(args): Promise<number> {
export async function createUser(args: Omit<User, "teams">): Promise<number> {
const response: { insert_users_one: { id: number } } =
await $admin.client.request(
gql`
Expand All @@ -166,15 +145,18 @@ export async function createUser(args): Promise<number> {
`,
{
id: args.id,
first_name: args.first_name,
last_name: args.last_name,
first_name: args.firstName,
last_name: args.lastName,
email: args.email,
is_platform_admin: args.isPlatformAdmin,
},
);
return response.insert_users_one.id;
}
export async function createDemoUser(args): Promise<number> {

export async function createDemoUser(
args: Omit<User, "teams">,
): Promise<number> {
const response: { insert_users_one: { id: number } } =
await $admin.client.request(
gql`
Expand Down Expand Up @@ -202,8 +184,8 @@ export async function createDemoUser(args): Promise<number> {
`,
{
id: args.id,
first_name: args.first_name,
last_name: args.last_name,
first_name: args.firstName,
last_name: args.lastName,
email: args.email,
is_platform_admin: args.isPlatformAdmin,
role: "demoUser",
Expand All @@ -212,7 +194,7 @@ export async function createDemoUser(args): Promise<number> {
return response.insert_users_one.id;
}

export async function getTeams(client) {
export async function getTeams(client): Promise<Team[]> {
const { teams } = await client.request(gql`
query getTeams {
teams {
Expand All @@ -228,7 +210,10 @@ export async function getTeams(client) {
return teams;
}

export async function getTeamAndFlowsBySlug(client, slug) {
export async function getTeamAndFlowsBySlug(
client,
slug: string,
): Promise<TeamsAndFlows> {
const {
teams: [team],
} = await client.request(
Expand All @@ -239,6 +224,7 @@ export async function getTeamAndFlowsBySlug(client, slug) {
slug
flows {
creator_id
slug
id
}
}
Expand All @@ -252,7 +238,7 @@ export async function getTeamAndFlowsBySlug(client, slug) {
return team;
}

export async function getFlowBySlug(client, slug) {
export async function getFlowBySlug(client, slug: string): Promise<Flow> {
const {
flows: [flow],
} = await client.request(
Expand All @@ -272,7 +258,7 @@ export async function getFlowBySlug(client, slug) {
return flow;
}

export async function updateFlow(client, flowId: UUID) {
export async function updateFlow(client, flowId: UUID): Promise<UUID> {
const { update_flows_by_pk: response } = await client.request(
gql`
mutation updateFlow($flowId: uuid!) {
Expand All @@ -290,7 +276,7 @@ export async function updateFlow(client, flowId: UUID) {
return response;
}

export async function deleteFlow(client, flowId: UUID) {
export async function deleteFlow(client, flowId: UUID): Promise<UUID> {
const { delete_flows_by_pk: response } = await client.request(
gql`
mutation MyMutation($flowId: uuid!) {
Expand All @@ -304,7 +290,6 @@ export async function deleteFlow(client, flowId: UUID) {
);
return response;
}

export async function updateTeamSettings(client, teamId: number) {
try {
const { update_team_settings: response } = await client.request(
Expand Down
Loading

0 comments on commit 1623a33

Please sign in to comment.