Skip to content

Commit 308a1e6

Browse files
authored
Remove default email from buildJwt, add buildUserJwt with default email (#146)
1 parent 3ed63f6 commit 308a1e6

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@makerx/graphql-apollo-server",
3-
"version": "1.4.1",
3+
"version": "1.4.2",
44
"private": false,
55
"description": "A set of MakerX plugins for Apollo Server",
66
"author": "MakerX",

src/testing/jwt.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ export interface BuildJwtInput {
1111
[key: string]: any
1212
}
1313

14+
/**
15+
*
16+
* @param Optional JWT input
17+
* @returns A JWT payload with oid, tid and sub claims set to random values if not provided
18+
*/
1419
export function buildJwt({
1520
oid = randomUUID(),
1621
tid = randomUUID(),
1722
sub = randomUUID(),
18-
email = randomEmail(),
1923
scopes = [],
2024
roles = [],
2125
...rest
@@ -24,13 +28,24 @@ export function buildJwt({
2428
oid,
2529
tid,
2630
sub,
27-
email,
2831
scp: scopes.join(' '),
2932
roles,
3033
...rest,
3134
}
3235
}
3336

37+
/**
38+
*
39+
* @param Optional JWT input
40+
* @returns A JWT payload with oid, tid, sub and email claims set to random values if not provided
41+
*/
42+
export function buildUserJwt({ email = randomEmail(), ...rest }: Partial<BuildJwtInput> = {}): JwtPayload {
43+
return buildJwt({
44+
email,
45+
...rest,
46+
})
47+
}
48+
3449
export function randomEmail() {
35-
return `${(Math.random() + 1).toString(36).substring(7)}@test.net`
50+
return `${(Math.random() + 1).toString(36).substring(2)}@test.net`
3651
}

src/testing/tests/me-query.test.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect } from 'vitest'
2-
import { buildJwt } from '../jwt'
2+
import { buildJwt, buildUserJwt } from '../jwt'
33
import { graphql } from './gql'
44
import { test } from './test-context'
55

@@ -15,9 +15,10 @@ const meQuery = graphql(`
1515
`)
1616

1717
const jwtPayloads = {
18-
basicUser: buildJwt(),
19-
userWithRoles: buildJwt({ roles: ['Admin', 'Supervisor'] }),
20-
userWithName: buildJwt({ name: 'Magda' }),
18+
app: buildJwt(),
19+
user: buildUserJwt(),
20+
userWithRoles: buildUserJwt({ roles: ['Admin', 'Supervisor'] }),
21+
userWithName: buildUserJwt({ name: 'Magda' }),
2122
}
2223

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

29-
test('returns basic user', async ({ executeOperation }) => {
30-
const result = await executeOperation({ query: meQuery }, jwtPayloads.basicUser)
30+
test('returns basic app (no email)', async ({ executeOperation }) => {
31+
const result = await executeOperation({ query: meQuery }, jwtPayloads.app)
3132
expect(result.data?.me).toMatchObject({
32-
id: jwtPayloads.basicUser.oid,
33-
email: jwtPayloads.basicUser.email,
33+
id: jwtPayloads.app.oid,
34+
})
35+
})
36+
37+
test('returns basic user (with email)', async ({ executeOperation }) => {
38+
const result = await executeOperation({ query: meQuery }, jwtPayloads.user)
39+
expect(result.data?.me).toMatchObject({
40+
id: jwtPayloads.user.oid,
41+
email: jwtPayloads.user.email,
3442
})
3543
})
3644

0 commit comments

Comments
 (0)