Skip to content

Commit b4e383a

Browse files
authored
Adding Proper Auth Checks to 'My' Queries (canada-ca#1422)
* update find-my-domains to have auth check * update find-my-organizations to have auth check
1 parent 270c3a3 commit b4e383a

4 files changed

Lines changed: 89 additions & 168 deletions

File tree

api-js/src/queries/domains/__tests__/find-my-domains.test.js

Lines changed: 34 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { ArangoTools, dbNameFromFile } = require('arango-tools')
2-
const bcrypt = require('bcrypt')
32
const { graphql, GraphQLSchema, GraphQLError } = require('graphql')
43
const { toGlobalId } = require('graphql-relay')
54
const { setupI18n } = require('@lingui/core')
@@ -10,25 +9,30 @@ const { makeMigrations } = require('../../../../migrations')
109
const { createQuerySchema } = require('../..')
1110
const { createMutationSchema } = require('../../../mutations')
1211
const { cleanseInput } = require('../../../validators')
13-
const { tokenize } = require('../../../auth')
12+
const { userRequired } = require('../../../auth')
1413
const {
1514
domainLoaderConnectionsByUserId,
16-
userLoaderByUserName,
15+
userLoaderByKey,
1716
} = require('../../../loaders')
1817
const { DB_PASS: rootPass, DB_URL: url } = process.env
1918

2019
describe('given findMyDomainsQuery', () => {
21-
let query, drop, truncate, migrate, schema, collections, org, i18n
20+
let query, drop, truncate, migrate, schema, collections, org, i18n, user
2221

2322
beforeAll(async () => {
2423
// Create GQL Schema
2524
schema = new GraphQLSchema({
2625
query: createQuerySchema(),
2726
mutation: createMutationSchema(),
2827
})
28+
// Generate DB Items
29+
;({ migrate } = await ArangoTools({ rootPass, url }))
30+
;({ query, drop, truncate, collections } = await migrate(
31+
makeMigrations({ databaseName: dbNameFromFile(__filename), rootPass }),
32+
))
2933
})
3034

31-
let consoleOutput = []
35+
const consoleOutput = []
3236
const mockedInfo = (output) => consoleOutput.push(output)
3337
const mockedWarn = (output) => consoleOutput.push(output)
3438
const mockedError = (output) => consoleOutput.push(output)
@@ -37,49 +41,13 @@ describe('given findMyDomainsQuery', () => {
3741
console.info = mockedInfo
3842
console.warn = mockedWarn
3943
console.error = mockedError
40-
// Generate DB Items
41-
;({ migrate } = await ArangoTools({ rootPass, url }))
42-
;({ query, drop, truncate, collections } = await migrate(
43-
makeMigrations({ databaseName: dbNameFromFile(__filename), rootPass }),
44-
))
45-
await truncate()
46-
await graphql(
47-
schema,
48-
`
49-
mutation {
50-
signUp(
51-
input: {
52-
displayName: "Test Account"
53-
userName: "test.account@istio.actually.exists"
54-
password: "testpassword123"
55-
confirmPassword: "testpassword123"
56-
preferredLang: FRENCH
57-
}
58-
) {
59-
authResult {
60-
user {
61-
id
62-
}
63-
}
64-
}
65-
}
66-
`,
67-
null,
68-
{
69-
query,
70-
auth: {
71-
bcrypt,
72-
tokenize,
73-
},
74-
validators: {
75-
cleanseInput,
76-
},
77-
loaders: {
78-
userLoaderByUserName: userLoaderByUserName(query),
79-
},
80-
},
81-
)
82-
consoleOutput = []
44+
consoleOutput.length = 0
45+
46+
user = await collections.users.save({
47+
displayName: 'Test Account',
48+
userName: 'test.account@istio.actually.exists',
49+
preferredLang: 'french',
50+
})
8351

8452
org = await collections.organizations.save({
8553
orgDetails: {
@@ -108,18 +76,16 @@ describe('given findMyDomainsQuery', () => {
10876
})
10977

11078
afterEach(async () => {
79+
await truncate()
80+
})
81+
82+
afterAll(async () => {
11183
await drop()
11284
})
11385

11486
describe('given successful retrieval of domains', () => {
115-
let user, domainOne, domainTwo
87+
let domainOne, domainTwo
11688
beforeEach(async () => {
117-
const userCursor = await query`
118-
FOR user IN users
119-
FILTER user.userName == "test.account@istio.actually.exists"
120-
RETURN user
121-
`
122-
user = await userCursor.next()
12389
await collections.affiliations.save({
12490
_from: org._id,
12591
_to: user._id,
@@ -158,26 +124,6 @@ describe('given findMyDomainsQuery', () => {
158124
_from: org._id,
159125
})
160126
})
161-
afterEach(async () => {
162-
await query`
163-
LET userEdges = (FOR v, e IN 1..1 ANY ${org._id} affiliations RETURN { edgeKey: e._key, userKey: e._to })
164-
LET removeUserEdges = (FOR userEdge IN userEdges REMOVE userEdge.edgeKey IN affiliations)
165-
RETURN true
166-
`
167-
await query`
168-
FOR affiliation IN affiliations
169-
REMOVE affiliation IN affiliations
170-
`
171-
await query`
172-
LET domainEdges = (FOR v, e IN 1..1 ANY ${org._id} claims RETURN { edgeKey: e._key, userKey: e._to })
173-
LET removeDomainEdges = (FOR domainEdge IN domainEdges REMOVE domainEdge.edgeKey IN claims)
174-
RETURN true
175-
`
176-
await query`
177-
FOR claim IN claims
178-
REMOVE claim IN claims
179-
`
180-
})
181127
describe('user queries for their domains', () => {
182128
it('returns domains', async () => {
183129
const response = await graphql(
@@ -208,6 +154,13 @@ describe('given findMyDomainsQuery', () => {
208154
{
209155
i18n,
210156
userKey: user._key,
157+
auth: {
158+
userRequired: userRequired({
159+
i18n,
160+
userKey: user._key,
161+
userLoaderByKey: userLoaderByKey(query, user._key, i18n),
162+
}),
163+
},
211164
loaders: {
212165
domainLoaderConnectionsByUserId: domainLoaderConnectionsByUserId(
213166
query,
@@ -305,6 +258,9 @@ describe('given findMyDomainsQuery', () => {
305258
{
306259
i18n,
307260
userKey: 1,
261+
auth: {
262+
userRequired: jest.fn(),
263+
},
308264
loaders: {
309265
domainLoaderConnectionsByUserId: mockedLoader,
310266
},
@@ -370,6 +326,9 @@ describe('given findMyDomainsQuery', () => {
370326
{
371327
i18n,
372328
userKey: 1,
329+
auth: {
330+
userRequired: jest.fn(),
331+
},
373332
loaders: {
374333
domainLoaderConnectionsByUserId: mockedLoader,
375334
},

api-js/src/queries/domains/find-my-domains.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ const findMyDomains = {
1818
resolve: async (
1919
_,
2020
args,
21-
{ i18n, userKey, loaders: { domainLoaderConnectionsByUserId } },
21+
{
22+
i18n,
23+
userKey,
24+
auth: { userRequired },
25+
loaders: { domainLoaderConnectionsByUserId },
26+
},
2227
) => {
2328
let domainConnections
2429

30+
await userRequired()
31+
2532
try {
2633
domainConnections = await domainLoaderConnectionsByUserId(args)
2734
} catch (err) {

0 commit comments

Comments
 (0)