-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathareas.ts
More file actions
195 lines (173 loc) · 6.01 KB
/
areas.ts
File metadata and controls
195 lines (173 loc) · 6.01 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { ApolloServer } from 'apollo-server-express'
import muuid, { MUUID } from 'uuid-mongodb'
import { jest } from '@jest/globals'
import MutableAreaDataSource from '../model/MutableAreaDataSource.js'
import MutableOrganizationDataSource from '../model/MutableOrganizationDataSource.js'
import { AreaType } from '../db/AreaTypes.js'
import { OrganizationEditableFieldsType, OrganizationType, OrgType } from '../db/OrganizationTypes.js'
import { queryAPI, setUpServer } from '../utils/testUtils.js'
import { muuidToString } from '../utils/helpers.js'
import { InMemoryDB } from '../utils/inMemoryDB.js'
import express from 'express'
jest.setTimeout(60000)
describe('areas API', () => {
let server: ApolloServer
let user: muuid.MUUID
let userUuid: string
let app: express.Application
let inMemoryDB: InMemoryDB
// Mongoose models for mocking pre-existing state.
let areas: MutableAreaDataSource
let organizations: MutableOrganizationDataSource
let usa: AreaType
let ca: AreaType
let wa: AreaType
beforeAll(async () => {
({ server, inMemoryDB, app } = await setUpServer())
// Auth0 serializes uuids in "relaxed" mode, resulting in this hex string format
// "59f1d95a-627d-4b8c-91b9-389c7424cb54" instead of base64 "WfHZWmJ9S4yRuTicdCTLVA==".
user = muuid.mode('relaxed').v4()
userUuid = muuidToString(user)
})
beforeEach(async () => {
await inMemoryDB.clear()
areas = MutableAreaDataSource.getInstance()
organizations = MutableOrganizationDataSource.getInstance()
usa = await areas.addCountry('usa')
ca = await areas.addArea(user, 'CA', usa.metadata.area_id)
wa = await areas.addArea(user, 'WA', usa.metadata.area_id)
})
afterAll(async () => {
await server.stop()
await inMemoryDB.close()
})
describe('queries', () => {
const areaQuery = `
query area($input: ID) {
area(uuid: $input) {
uuid
organizations {
orgId
}
}
}
`
let alphaFields: OrganizationEditableFieldsType
let alphaOrg: OrganizationType
beforeEach(async () => {
alphaFields = {
displayName: 'USA without CA Org',
associatedAreaIds: [usa.metadata.area_id],
excludedAreaIds: [ca.metadata.area_id]
}
alphaOrg = await organizations.addOrganization(user, OrgType.localClimbingOrganization, alphaFields)
.then((res: OrganizationType | null) => {
if (res === null) throw new Error('Failure mocking organization.')
return res
})
})
it('retrieves an area omitting organizations that exclude it', async () => {
const response = await queryAPI({
query: areaQuery,
operationName: 'area',
variables: { input: ca.metadata.area_id },
userUuid,
app
})
expect(response.statusCode).toBe(200)
const areaResult = response.body.data.area
expect(areaResult.uuid).toBe(muuidToString(ca.metadata.area_id))
// Even though alphaOrg associates with ca's parent, usa, it excludes
// ca and so should not be listed.
expect(areaResult.organizations).toHaveLength(0)
})
it.each([userUuid, undefined])('retrieves an area and lists associated organizations', async (userId) => {
const response = await queryAPI({
query: areaQuery,
operationName: 'area',
variables: { input: wa.metadata.area_id },
userUuid: userId,
app
})
expect(response.statusCode).toBe(200)
const areaResult = response.body.data.area
expect(areaResult.uuid).toBe(muuidToString(wa.metadata.area_id))
expect(areaResult.organizations).toHaveLength(1)
expect(areaResult.organizations[0].orgId).toBe(muuidToString(alphaOrg.orgId))
})
})
describe('area structure API', () => {
const structureQuery = `
query structure($parent: ID!) {
structure(parent: $parent) {
parent
uuid
area_name
climbs
}
}
`
// Structure queries do not do write operations so we can build this once
beforeEach(async () => {
const maxDepth = 4
const maxBreadth = 3
// So for the purposes of this test we will do a simple tree
async function grow (from: MUUID, depth: number = 0): Promise<void> {
if (depth >= maxDepth) return
for (const idx of Array.from({ length: maxBreadth }, (_, i) => i + 1)) {
const newArea = await areas.addArea(user, `${depth}-${idx}`, from)
await grow(newArea.metadata.area_id, depth + 1)
}
}
await grow(usa.metadata.area_id)
})
it('retrieves the structure of a given area', async () => {
const response = await queryAPI({
query: structureQuery,
operationName: 'structure',
variables: { parent: usa.metadata.area_id },
userUuid,
app
})
expect(response.statusCode).toBe(200)
})
it('should allow no parent to be supplied and get a shallow result', async () => {
const response = await queryAPI({
query: `
query structure {
structure {
parent
uuid
area_name
climbs
}
}
`,
operationName: 'structure',
userUuid,
app
})
expect(response.statusCode).toBe(200)
})
it('should allow calling of the setAreaParent gql endpoint.', async () => {
const testArea = await areas.addArea(muuid.from(userUuid), 'A Rolling Stone', usa.metadata.area_id)
const response = await queryAPI({
query: `
mutation SetAreaParent($area: ID!, $newParent: ID!) {
setAreaParent(area: $area, newParent: $newParent) {
areaName
area_name
}
}
`,
operationName: 'SetAreaParent',
userUuid,
app,
// Move it to canada
variables: { area: testArea.metadata.area_id, newParent: ca.metadata.area_id }
})
console.log(response.body)
expect(response.statusCode).toBe(200)
})
})
})