-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Update planx-core - Move shared helpers - Scope test lifecycle hooks - Add JWT helper methods
- Loading branch information
1 parent
dc46e96
commit 4c6b04a
Showing
13 changed files
with
297 additions
and
140 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 { TEST_EMAIL } from "../../ui-driven/src/helpers"; | ||
import { $admin } from "./client"; | ||
|
||
export async function createTeam() { | ||
return $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", | ||
}); | ||
} | ||
|
||
export async function createUser() { | ||
return $admin.user.create({ | ||
firstName: "Test", | ||
lastName: "Test", | ||
email: TEST_EMAIL, | ||
}); | ||
} | ||
|
||
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 getTestUser() { | ||
return await $admin.user.getByEmail(TEST_EMAIL); | ||
} |
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,51 @@ | ||
import { sign } from "jsonwebtoken"; | ||
import { User, Role } from "@opensystemslab/planx-core/types"; | ||
import { $admin } from "./client"; | ||
|
||
// This code is copied from api.planx.uk/modules/auth/service.ts | ||
|
||
export const buildJWT = async (email: string): Promise<string | undefined> => { | ||
const user = await $admin.user.getByEmail(email); | ||
if (!user) return; | ||
|
||
const data = { | ||
sub: user.id.toString(), | ||
"https://hasura.io/jwt/claims": generateHasuraClaimsForUser(user), | ||
}; | ||
|
||
const jwt = sign(data, process.env.JWT_SECRET!); | ||
return jwt; | ||
}; | ||
|
||
const generateHasuraClaimsForUser = (user: User) => ({ | ||
"x-hasura-allowed-roles": getAllowedRolesForUser(user), | ||
"x-hasura-default-role": getDefaultRoleForUser(user), | ||
"x-hasura-user-id": user.id.toString(), | ||
}); | ||
|
||
/** | ||
* Get all possible roles for this user | ||
* Requests made outside this scope will not be authorised by Hasura | ||
*/ | ||
const getAllowedRolesForUser = (user: User): Role[] => { | ||
const teamRoles = user.teams.map((teamRole) => teamRole.role); | ||
const allowedRoles: Role[] = [ | ||
"public", // Allow public access | ||
"teamEditor", // Least privileged role for authenticated users - required for Editor access | ||
...teamRoles, // User specific roles | ||
]; | ||
if (user.isPlatformAdmin) allowedRoles.push("platformAdmin"); | ||
|
||
return [...new Set(allowedRoles)]; | ||
}; | ||
|
||
/** | ||
* The default role is used for all requests | ||
* Can be overwritten on a per-request basis in the client using the x-hasura-role header | ||
* set to a role in the x-hasura-allowed-roles list | ||
* | ||
* This is the role of least privilege for the user | ||
*/ | ||
const getDefaultRoleForUser = (user: User): Role => { | ||
return user.isPlatformAdmin ? "platformAdmin" : "teamEditor"; | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.