Skip to content

Commit

Permalink
Remove default email from buildJwt, add buildUserJwt with default…
Browse files Browse the repository at this point in the history
… email (#146)
  • Loading branch information
cuzzlor authored Jun 4, 2024
1 parent 3ed63f6 commit 308a1e6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@makerx/graphql-apollo-server",
"version": "1.4.1",
"version": "1.4.2",
"private": false,
"description": "A set of MakerX plugins for Apollo Server",
"author": "MakerX",
Expand Down
21 changes: 18 additions & 3 deletions src/testing/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ export interface BuildJwtInput {
[key: string]: any
}

/**
*
* @param Optional JWT input
* @returns A JWT payload with oid, tid and sub claims set to random values if not provided
*/
export function buildJwt({
oid = randomUUID(),
tid = randomUUID(),
sub = randomUUID(),
email = randomEmail(),
scopes = [],
roles = [],
...rest
Expand All @@ -24,13 +28,24 @@ export function buildJwt({
oid,
tid,
sub,
email,
scp: scopes.join(' '),
roles,
...rest,
}
}

/**
*
* @param Optional JWT input
* @returns A JWT payload with oid, tid, sub and email claims set to random values if not provided
*/
export function buildUserJwt({ email = randomEmail(), ...rest }: Partial<BuildJwtInput> = {}): JwtPayload {
return buildJwt({
email,
...rest,
})
}

export function randomEmail() {
return `${(Math.random() + 1).toString(36).substring(7)}@test.net`
return `${(Math.random() + 1).toString(36).substring(2)}@test.net`
}
24 changes: 16 additions & 8 deletions src/testing/tests/me-query.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect } from 'vitest'
import { buildJwt } from '../jwt'
import { buildJwt, buildUserJwt } from '../jwt'
import { graphql } from './gql'
import { test } from './test-context'

Expand All @@ -15,9 +15,10 @@ const meQuery = graphql(`
`)

const jwtPayloads = {
basicUser: buildJwt(),
userWithRoles: buildJwt({ roles: ['Admin', 'Supervisor'] }),
userWithName: buildJwt({ name: 'Magda' }),
app: buildJwt(),
user: buildUserJwt(),
userWithRoles: buildUserJwt({ roles: ['Admin', 'Supervisor'] }),
userWithName: buildUserJwt({ name: 'Magda' }),
}

describe('me query operation', () => {
Expand All @@ -26,11 +27,18 @@ describe('me query operation', () => {
expect(result.data?.me).toBeNull()
})

test('returns basic user', async ({ executeOperation }) => {
const result = await executeOperation({ query: meQuery }, jwtPayloads.basicUser)
test('returns basic app (no email)', async ({ executeOperation }) => {
const result = await executeOperation({ query: meQuery }, jwtPayloads.app)
expect(result.data?.me).toMatchObject({
id: jwtPayloads.basicUser.oid,
email: jwtPayloads.basicUser.email,
id: jwtPayloads.app.oid,
})
})

test('returns basic user (with email)', async ({ executeOperation }) => {
const result = await executeOperation({ query: meQuery }, jwtPayloads.user)
expect(result.data?.me).toMatchObject({
id: jwtPayloads.user.oid,
email: jwtPayloads.user.email,
})
})

Expand Down

0 comments on commit 308a1e6

Please sign in to comment.