-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathbulkImport.test.ts
136 lines (122 loc) · 4 KB
/
bulkImport.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
import {ApolloServer} from "@apollo/server";
import muuid from "uuid-mongodb";
import express from "express";
import {InMemoryDB} from "../utils/inMemoryDB.js";
import {queryAPI, setUpServer} from "../utils/testUtils.js";
import {muuidToString} from "../utils/helpers.js";
import exampleImportData from './import-example.json' assert {type: 'json'};
import {AreaType} from "../db/AreaTypes.js";
import {BulkImportResultType} from "../db/BulkImportTypes.js";
import MutableClimbDataSource from "../model/MutableClimbDataSource.js";
import BulkImportDataSource from "../model/BulkImportDataSource.js";
describe('bulkImportAreas', () => {
const query = `
mutation bulkImportAreas($input: BulkImportInput!) {
bulkImportAreas(input: $input) {
addedAreas {
uuid
metadata {
area_id
}
}
updatedAreas {
uuid
metadata {
area_id
}
}
addedOrUpdatedClimbs {
id
}
}
}
`
let server: ApolloServer
let user: muuid.MUUID
let userUuid: string
let app: express.Application
let inMemoryDB: InMemoryDB
let testArea: AreaType
let bulkImport: BulkImportDataSource
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)
bulkImport = BulkImportDataSource.getInstance()
climbs = MutableClimbDataSource.getInstance()
})
beforeEach(async () => {
await inMemoryDB.clear()
await bulkImport.addCountry('usa')
testArea = await bulkImport.addArea(user, "Test Area", null, "us")
})
afterAll(async () => {
await server.stop()
await inMemoryDB.close()
})
it('should return 403 if no user', async () => {
const res = await queryAPI({
app,
query,
operationName: 'bulkImportAreas',
variables: {input: exampleImportData}
})
expect(res.statusCode).toBe(200)
expect(res.body.errors[0].message).toBe('Not Authorised!')
})
it('should return 403 if user is not an editor', async () => {
const res = await queryAPI({
app,
userUuid,
query,
operationName: 'bulkImportAreas',
variables: {input: exampleImportData}
})
expect(res.statusCode).toBe(200)
expect(res.body.errors[0].message).toBe('Not Authorised!')
})
it('should return 200 if user is an editor', async () => {
const res = await queryAPI({
app,
userUuid,
roles: ['editor'],
query,
operationName: 'bulkImportAreas',
variables: {input: exampleImportData}
})
expect(res.status).toBe(200)
})
it('should import data', async () => {
const res = await queryAPI({
app,
userUuid,
roles: ['editor'],
query,
operationName: 'bulkImportAreas',
variables: {
input: {
areas: [
...exampleImportData.areas,
{
uuid: testArea.metadata.area_id,
areaName: "Updated Test Area",
}
]
}
}
});
expect(res.body.errors).toBeFalsy()
const result = res.body.data.bulkImportAreas as BulkImportResultType
expect(result.addedAreas.length).toBe(4)
const committedAreas = await Promise.all(result.addedAreas.map((area) => bulkImport.findOneAreaByUUID(muuid.from(area.metadata.area_id))));
expect(committedAreas.length).toBe(4);
const committedClimbs = await Promise.all(result.addedOrUpdatedClimbs.map((climb) => climbs.findOneClimbByMUUID(climb._id)));
expect(committedClimbs.length).toBe(2);
const updatedAreas = await Promise.all(result.updatedAreas.map((area) => bulkImport.findOneAreaByUUID(muuid.from(area.metadata.area_id))));
expect(updatedAreas.length).toBe(1);
expect(updatedAreas[0].area_name).toBe("Updated Test Area");
})
});