/
advancedWithOr.test.ts
149 lines (116 loc) · 5.01 KB
/
advancedWithOr.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { createTestApp } from '@zemble/core/test-utils'
import { expect, describe, it } from 'bun:test'
import plugin from '../../plugin'
import { signJwt } from '../../utils/signJwt'
import { graphql } from '../client.generated'
const AdvancedWithOrQuery = graphql(`
query advancedWithOr($organisationId: String!) {
advancedWithOr(organisationId: $organisationId)
}
`)
describe('advancedWithOr', () => {
it('Should fail authentication', async () => {
const app = await createTestApp(plugin)
const response = await app.gqlRequest(
AdvancedWithOrQuery,
{ organisationId: '123' },
{ silenceErrors: true },
)
expect(response.errors?.[0]?.message).toEqual(`Accessing 'Query.advancedWithOr' requires authentication.`)
})
it('Should fail without array', async () => {
const app = await createTestApp(plugin)
const token = await signJwt({ data: { type: 'user-token' }, sub: '1' })
const response = await app.gqlRequest(AdvancedWithOrQuery, { organisationId: '123' }, {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})
expect(response.errors?.[0]?.message).toEqual(`Accessing 'Query.advancedWithOr' requires token including arrays matching one of [{"includes":{"roles":{"role":"admin","organisationId":"123"}}},{"includes":{"roles":{"role":"superadmin","organisationId":"123"}}}].`)
})
it('Should fail when roles array doesnt include the right role', async () => {
const app = await createTestApp(plugin)
const token = await signJwt({ data: { type: 'user-token', roles: [] }, sub: '1' })
const response = await app.gqlRequest(AdvancedWithOrQuery, { organisationId: '123' }, {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})
expect(response.errors?.[0]?.message).toEqual(`Accessing 'Query.advancedWithOr' requires token including arrays matching one of [{"includes":{"roles":{"role":"admin","organisationId":"123"}}},{"includes":{"roles":{"role":"superadmin","organisationId":"123"}}}].`)
})
it('Should succeed when the role includes admin role', async () => {
const app = await createTestApp(plugin)
const token = await signJwt({ data: { type: 'user-token', roles: [{ role: 'admin', organisationId: '123' }] }, sub: '1' })
const response = await app.gqlRequest(AdvancedWithOrQuery, { organisationId: '123' }, {
headers: {
Authorization: `Bearer ${token}`,
},
})
expect(response.data?.advancedWithOr).toEqual(`private shit`)
})
it('Should succeed when the role includes superadmin role', async () => {
const app = await createTestApp(plugin)
const token = await signJwt({ data: { type: 'user-token', roles: [{ role: 'admin', organisationId: '123' }] }, sub: '1' })
const response = await app.gqlRequest(AdvancedWithOrQuery, { organisationId: '123' }, {
headers: {
Authorization: `Bearer ${token}`,
},
})
expect(response.data?.advancedWithOr).toEqual(`private shit`)
})
it('Should succeed when the role includes superadmin and admin role', async () => {
const app = await createTestApp(plugin)
const token = await signJwt({
data: {
type: 'user-token',
roles: [{ role: 'admin', organisationId: '123' }, { role: 'superadmin', organisationId: '123' }],
},
sub: '1',
})
const response = await app.gqlRequest(AdvancedWithOrQuery, { organisationId: '123' }, {
headers: {
Authorization: `Bearer ${token}`,
},
})
expect(response.data?.advancedWithOr).toEqual(`private shit`)
})
it('Should succeed when the role includes superadmin (and admin role for other org)', async () => {
const app = await createTestApp(plugin)
const token = await signJwt({
data: {
type: 'user-token',
roles: [{ role: 'admin', organisationId: '123' }, { role: 'superadmin', organisationId: '1234' }],
},
sub: '1',
})
const response = await app.gqlRequest(AdvancedWithOrQuery, { organisationId: '123' }, {
headers: {
Authorization: `Bearer ${token}`,
},
})
expect(response.data?.advancedWithOr).toEqual(`private shit`)
})
it('Should fail when the role includes superadmin and admin role for the wrong organization', async () => {
const app = await createTestApp(plugin)
const token = await signJwt({
data:
{
type: 'user-token',
roles: [
{ role: 'admin', organisationId: '1234' },
{ role: 'superadmin', organisationId: '1234' },
],
},
sub: '1',
})
const response = await app.gqlRequest(AdvancedWithOrQuery, { organisationId: '123' }, {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})
expect(response.errors?.[0]?.message).toEqual(`Accessing 'Query.advancedWithOr' requires token including arrays matching one of [{"includes":{"roles":{"role":"admin","organisationId":"123"}}},{"includes":{"roles":{"role":"superadmin","organisationId":"123"}}}].`)
})
})