-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathhistory.ts
More file actions
148 lines (132 loc) · 4.69 KB
/
history.ts
File metadata and controls
148 lines (132 loc) · 4.69 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
import { ApolloServer } from '@apollo/server'
import muuid from 'uuid-mongodb'
import { jest } from '@jest/globals'
import MutableAreaDataSource from '../model/MutableAreaDataSource.js'
import MutableOrganizationDataSource from '../model/MutableOrganizationDataSource.js'
import MutableClimbDataSource from '../model/MutableClimbDataSource.js'
import { AreaType } from '../db/AreaTypes.js'
import { OrganizationType, OrgType } from '../db/OrganizationTypes.js'
import { muuidToString } from '../utils/helpers.js'
import { queryAPI, setUpServer } from '../utils/testUtils.js'
import { InMemoryDB } from '../utils/inMemoryDB.js'
import express from 'express'
jest.setTimeout(60000)
describe('history 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 climbs: MutableClimbDataSource
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()
climbs = MutableClimbDataSource.getInstance()
})
afterAll(async () => {
await server.stop()
await inMemoryDB.close()
})
describe('queries', () => {
const FRAGMENT_CHANGE_HISTORY = `
fragment ChangeHistoryFields on History {
id
createdAt
operation
editedBy
changes {
dbOp
changeId
updateDescription {
updatedFields
}
fullDocument {
... on Area {
areaName
uuid
metadata {
leaf
areaId
}
}
... on Climb {
id
name
uuid
}
... on Organization {
orgId
}
}
}
}
`
const QUERY_RECENT_CHANGE_HISTORY = `
${FRAGMENT_CHANGE_HISTORY}
query ($filter: AllHistoryFilter) {
getChangeHistory(filter: $filter) {
...ChangeHistoryFields
}
}
`
let usa: AreaType
let ca: AreaType
let alphaOrg: OrganizationType
let climbIds: string[]
it('queries recent change history successfully', async () => {
// Make changes to be tracked.
usa = await areas.addCountry('usa')
ca = await areas.addArea(user, { areaName: 'CA', parentUuid: usa.metadata.area_id })
const alphaFields = {
displayName: 'Alpha OpenBeta Club',
associatedAreaIds: [usa.metadata.area_id],
email: 'admin@alphaopenbeta.com'
}
alphaOrg = await organizations.addOrganization(user, OrgType.localClimbingOrganization, alphaFields)
climbIds = await climbs.addOrUpdateClimbs(user, ca.metadata.area_id, [{ name: 'Alpha Climb' }])
// Query for changes and ensure they are tracked.
const resp = await queryAPI({
query: QUERY_RECENT_CHANGE_HISTORY,
variables: { filter: {} },
userUuid,
app
})
expect(resp.statusCode).toBe(200)
const histories = resp.body.data.getChangeHistory
expect(histories.length).toBe(3)
// Latest change first
// Note: addCountry is not captured by history.
const [climbChange, orgChange, areaChange] = histories
expect(climbChange.operation).toBe('updateClimb')
expect(climbChange.editedBy).toBe(userUuid)
/**
* Four changes (Ordering is non-deterministic)
* 1. Insert the climb
* 2. Update the parent area
* 3. Update aggregate object on crag
* 4. Update the parent area
*/
expect(climbChange.changes.length).toBe(4)
const insertChange = climbChange.changes.filter(c => c.dbOp === 'insert')[0]
const updateChange = climbChange.changes.filter(c => c.dbOp === 'update')[0]
expect(insertChange.fullDocument.uuid).toBe(climbIds[0])
expect(updateChange.fullDocument.uuid).toBe(muuidToString(ca.metadata.area_id))
expect(orgChange.operation).toBe('addOrganization')
expect(orgChange.editedBy).toBe(userUuid)
expect(orgChange.changes[0].fullDocument.orgId).toBe(muuidToString(alphaOrg.orgId))
expect(areaChange.operation).toBe('addArea')
expect(areaChange.editedBy).toBe(userUuid)
})
})
})