Skip to content

Commit 709f124

Browse files
authored
Ownership Band aid (canada-ca#1320)
* add ownership flag to loader functions * add ownership arg to findMyDomains * add ownership arg to organizations domains field
1 parent b2f9347 commit 709f124

6 files changed

Lines changed: 276 additions & 33 deletions

File tree

api-js/src/loaders/domains/__tests__/load-domain-conn-org-id.test.js

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('given the load domain connection using org id function', () => {
2929
let consoleOutput = []
3030
const mockedError = (output) => consoleOutput.push(output)
3131
const mockedWarn = (output) => consoleOutput.push(output)
32+
3233
beforeAll(async () => {
3334
console.error = mockedError
3435
console.warn = mockedWarn
@@ -39,7 +40,6 @@ describe('given the load domain connection using org id function', () => {
3940
})
4041

4142
beforeEach(async () => {
42-
await truncate()
4343
consoleOutput = []
4444
user = await collections.users.save({
4545
userName: 'test.account@istio.actually.exists',
@@ -93,9 +93,14 @@ describe('given the load domain connection using org id function', () => {
9393
})
9494
})
9595

96+
afterEach(async () => {
97+
await truncate()
98+
})
99+
96100
afterAll(async () => {
97101
await drop()
98102
})
103+
99104
describe('given a successful load', () => {
100105
describe('using no cursor', () => {
101106
it('returns multiple domains', async () => {
@@ -366,6 +371,124 @@ describe('given the load domain connection using org id function', () => {
366371
expect(domains).toEqual(expectedStructure)
367372
})
368373
})
374+
describe('using ownership filter', () => {
375+
let domainThree
376+
beforeEach(async () => {
377+
domainThree = await collections.domains.save({
378+
domain: 'test3.domain.canada.ca',
379+
})
380+
await collections.claims.save({
381+
_from: org._id,
382+
_to: domainThree._id,
383+
})
384+
await collections.ownership.save({
385+
_from: org._id,
386+
_to: domainThree._id,
387+
})
388+
})
389+
describe('ownership is set to true', () => {
390+
it('returns only a domain belonging to a domain that owns it', async () => {
391+
const connectionLoader = domainLoaderConnectionsByOrgId(
392+
query,
393+
user._key,
394+
cleanseInput,
395+
)
396+
397+
const domainLoader = domainLoaderByKey(query)
398+
const expectedDomains = await domainLoader.loadMany([
399+
domainThree._key,
400+
])
401+
402+
expectedDomains[0].id = expectedDomains[0]._key
403+
404+
const connectionArgs = {
405+
first: 5,
406+
}
407+
const domains = await connectionLoader({
408+
orgId: org._id,
409+
ownership: true,
410+
...connectionArgs,
411+
})
412+
413+
const expectedStructure = {
414+
edges: [
415+
{
416+
cursor: toGlobalId('domains', expectedDomains[0]._key),
417+
node: {
418+
...expectedDomains[0],
419+
},
420+
},
421+
],
422+
pageInfo: {
423+
hasNextPage: false,
424+
hasPreviousPage: false,
425+
startCursor: toGlobalId('domains', expectedDomains[0]._key),
426+
endCursor: toGlobalId('domains', expectedDomains[0]._key),
427+
},
428+
totalCount: 1,
429+
}
430+
431+
expect(domains).toEqual(expectedStructure)
432+
})
433+
})
434+
describe('ownership is set to false', () => {
435+
it('returns all domains', async () => {
436+
const connectionLoader = domainLoaderConnectionsByOrgId(
437+
query,
438+
user._key,
439+
cleanseInput,
440+
)
441+
442+
const domainLoader = domainLoaderByKey(query)
443+
const expectedDomains = await domainLoader.loadMany([
444+
domain._key,
445+
domainTwo._key,
446+
domainThree._key,
447+
])
448+
449+
const connectionArgs = {
450+
first: 5,
451+
}
452+
const domains = await connectionLoader({
453+
orgId: org._id,
454+
ownership: false,
455+
...connectionArgs,
456+
})
457+
458+
const expectedStructure = {
459+
edges: [
460+
{
461+
cursor: toGlobalId('domains', expectedDomains[0]._key),
462+
node: {
463+
...expectedDomains[0],
464+
},
465+
},
466+
{
467+
cursor: toGlobalId('domains', expectedDomains[1]._key),
468+
node: {
469+
...expectedDomains[1],
470+
},
471+
},
472+
{
473+
cursor: toGlobalId('domains', expectedDomains[2]._key),
474+
node: {
475+
...expectedDomains[2],
476+
},
477+
},
478+
],
479+
pageInfo: {
480+
hasNextPage: false,
481+
hasPreviousPage: false,
482+
startCursor: toGlobalId('domains', expectedDomains[0]._key),
483+
endCursor: toGlobalId('domains', expectedDomains[2]._key),
484+
},
485+
totalCount: 3,
486+
}
487+
488+
expect(domains).toEqual(expectedStructure)
489+
})
490+
})
491+
})
369492
})
370493
describe('users language is set to english', () => {
371494
beforeAll(() => {

api-js/src/loaders/domains/__tests__/load-domain-connections-by-user-id.test.js

Lines changed: 117 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('given the load domain connections by user id function', () => {
2626
let consoleOutput = []
2727
const mockedError = (output) => consoleOutput.push(output)
2828
const mockedWarn = (output) => consoleOutput.push(output)
29+
2930
beforeAll(async () => {
3031
console.error = mockedError
3132
console.warn = mockedWarn
@@ -37,7 +38,7 @@ describe('given the load domain connections by user id function', () => {
3738

3839
beforeEach(async () => {
3940
await truncate()
40-
await collections.users.save({
41+
user = await collections.users.save({
4142
userName: 'test.account@istio.actually.exists',
4243
displayName: 'Test Account',
4344
preferredLang: 'french',
@@ -68,12 +69,6 @@ describe('given the load domain connections by user id function', () => {
6869
},
6970
},
7071
})
71-
const userCursor = await query`
72-
FOR user IN users
73-
FILTER user.userName == "test.account@istio.actually.exists"
74-
RETURN user
75-
`
76-
user = await userCursor.next()
7772
await collections.affiliations.save({
7873
_from: org._id,
7974
_to: user._id,
@@ -99,26 +94,11 @@ describe('given the load domain connections by user id function', () => {
9994
})
10095
consoleOutput = []
10196
})
97+
10298
afterEach(async () => {
103-
await query`
104-
LET userEdges = (FOR v, e IN 1..1 ANY ${org._id} affiliations RETURN { edgeKey: e._key, userKey: e._to })
105-
LET removeUserEdges = (FOR userEdge IN userEdges REMOVE userEdge.edgeKey IN affiliations)
106-
RETURN true
107-
`
108-
await query`
109-
FOR affiliation IN affiliations
110-
REMOVE affiliation IN affiliations
111-
`
112-
await query`
113-
LET domainEdges = (FOR v, e IN 1..1 ANY ${org._id} claims RETURN { edgeKey: e._key, userKey: e._to })
114-
LET removeDomainEdges = (FOR domainEdge IN domainEdges REMOVE domainEdge.edgeKey IN claims)
115-
RETURN true
116-
`
117-
await query`
118-
FOR claim IN claims
119-
REMOVE claim IN claims
120-
`
99+
await truncate()
121100
})
101+
122102
afterAll(async () => {
123103
await drop()
124104
})
@@ -348,6 +328,118 @@ describe('given the load domain connections by user id function', () => {
348328
expect(domains).toEqual(expectedStructure)
349329
})
350330
})
331+
describe('using ownership', () => {
332+
let domainThree
333+
beforeEach(async () => {
334+
domainThree = await collections.domains.save({
335+
domain: 'test3.gc.ca',
336+
lastRan: null,
337+
selectors: ['selector1._domainkey', 'selector2._domainkey'],
338+
})
339+
await collections.claims.save({
340+
_to: domainThree._id,
341+
_from: org._id,
342+
})
343+
await collections.ownership.save({
344+
_to: domainThree._id,
345+
_from: org._id,
346+
})
347+
})
348+
describe('ownership is set to true', () => {
349+
it('returns only a domain belonging to a domain that owns it', async () => {
350+
const connectionLoader = domainLoaderConnectionsByUserId(
351+
query,
352+
user._key,
353+
cleanseInput,
354+
)
355+
356+
const domainLoader = domainLoaderByKey(query)
357+
const expectedDomains = await domainLoader.loadMany([
358+
domainThree._key,
359+
])
360+
361+
const connectionArgs = {
362+
first: 1,
363+
ownership: true,
364+
}
365+
const domains = await connectionLoader({ ...connectionArgs })
366+
367+
const expectedStructure = {
368+
edges: [
369+
{
370+
cursor: toGlobalId('domains', expectedDomains[0]._key),
371+
node: {
372+
...expectedDomains[0],
373+
},
374+
},
375+
],
376+
totalCount: 1,
377+
pageInfo: {
378+
hasNextPage: false,
379+
hasPreviousPage: false,
380+
startCursor: toGlobalId('domains', expectedDomains[0]._key),
381+
endCursor: toGlobalId('domains', expectedDomains[0]._key),
382+
},
383+
}
384+
385+
expect(domains).toEqual(expectedStructure)
386+
})
387+
})
388+
describe('ownership is set to false', () => {
389+
it('returns all domains an org has claimed', async () => {
390+
const connectionLoader = domainLoaderConnectionsByUserId(
391+
query,
392+
user._key,
393+
cleanseInput,
394+
)
395+
396+
const domainLoader = domainLoaderByKey(query)
397+
const expectedDomains = await domainLoader.loadMany([
398+
domainOne._key,
399+
domainTwo._key,
400+
domainThree._key,
401+
])
402+
403+
const connectionArgs = {
404+
first: 3,
405+
ownership: false,
406+
}
407+
const domains = await connectionLoader({ ...connectionArgs })
408+
409+
const expectedStructure = {
410+
edges: [
411+
{
412+
cursor: toGlobalId('domains', expectedDomains[0]._key),
413+
node: {
414+
...expectedDomains[0],
415+
},
416+
},
417+
{
418+
cursor: toGlobalId('domains', expectedDomains[1]._key),
419+
node: {
420+
...expectedDomains[1],
421+
},
422+
},
423+
{
424+
cursor: toGlobalId('domains', expectedDomains[2]._key),
425+
node: {
426+
...expectedDomains[2],
427+
},
428+
},
429+
],
430+
totalCount: 3,
431+
pageInfo: {
432+
hasNextPage: false,
433+
hasPreviousPage: false,
434+
startCursor: toGlobalId('domains', expectedDomains[0]._key),
435+
endCursor: toGlobalId('domains', expectedDomains[2]._key),
436+
},
437+
}
438+
439+
expect(domains).toEqual(expectedStructure)
440+
})
441+
})
442+
})
351443
})
352444
describe('given there are no domain connections to be returned', () => {
353445
it('returns no domain connections', async () => {

api-js/src/loaders/domains/load-domain-connections-by-organizations-id.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ const domainLoaderConnectionsByOrgId = (
77
userKey,
88
cleanseInput,
99
i18n,
10-
) => async ({ orgId, after, before, first, last }) => {
10+
) => async ({ orgId, after, before, first, last, ownership }) => {
1111
let afterTemplate = aql``
1212
let beforeTemplate = aql``
1313

1414
const userDBId = `users/${userKey}`
1515

16+
let ownershipOrgsOnly = aql`LET claimKeys = (FOR v, e IN 1..1 OUTBOUND ${orgId} claims RETURN v._key)`
17+
if (typeof ownership !== 'undefined') {
18+
if (ownership) {
19+
ownershipOrgsOnly = aql`LET claimKeys = (FOR v, e IN 1..1 OUTBOUND ${orgId} ownership RETURN v._key)`
20+
}
21+
}
22+
1623
let afterId
1724
if (typeof after !== 'undefined') {
1825
afterId = fromGlobalId(cleanseInput(after)).id
@@ -98,7 +105,7 @@ const domainLoaderConnectionsByOrgId = (
98105
LET affiliationKeys = (FOR v, e IN 1..1 INBOUND ${userDBId} affiliations RETURN v._key)
99106
LET superAdminOrgs = (FOR org IN organizations RETURN org._key)
100107
LET keys = ('super_admin' IN superAdmin ? superAdminOrgs : affiliationKeys)
101-
LET claimKeys = (FOR v, e IN 1..1 OUTBOUND ${orgId} claims RETURN v._key)
108+
${ownershipOrgsOnly}
102109
LET orgKeys = INTERSECTION(keys, claimKeys)
103110
RETURN claimKeys
104111
))

api-js/src/loaders/domains/load-domain-connections-by-user-id.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ const domainLoaderConnectionsByUserId = (
77
userKey,
88
cleanseInput,
99
i18n,
10-
) => async ({ after, before, first, last }) => {
10+
) => async ({ after, before, first, last, ownership }) => {
1111
let afterTemplate = aql``
1212
let beforeTemplate = aql``
1313

1414
const userDBId = `users/${userKey}`
1515

16+
let ownershipOrgsOnly = aql`LET claimDomainKeys = (FOR v, e IN 1..1 OUTBOUND orgId claims RETURN v._key)`
17+
if (typeof ownership !== 'undefined') {
18+
if (ownership) {
19+
ownershipOrgsOnly = aql`LET claimDomainKeys = (FOR v, e IN 1..1 OUTBOUND orgId ownership RETURN v._key)`
20+
}
21+
}
22+
1623
if (typeof after !== 'undefined') {
1724
const { id: afterId } = fromGlobalId(cleanseInput(after))
1825
afterTemplate = aql`FILTER TO_NUMBER(domain._key) > TO_NUMBER(${afterId})`
@@ -93,8 +100,9 @@ const domainLoaderConnectionsByUserId = (
93100
requestedDomainInfo = await query`
94101
LET domainKeys = UNIQUE(FLATTEN(
95102
LET keys = []
96-
FOR userAffiliation IN (FOR v, e IN 1..1 ANY ${userDBId} affiliations RETURN e._from)
97-
LET claimDomainKeys = (FOR v, e IN 1..1 OUTBOUND userAffiliation claims RETURN v._key)
103+
LET orgIds = (FOR v, e IN 1..1 ANY ${userDBId} affiliations RETURN e._from)
104+
FOR orgId IN orgIds
105+
${ownershipOrgsOnly}
98106
RETURN APPEND(keys, claimDomainKeys)
99107
))
100108

0 commit comments

Comments
 (0)