Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit d18fc10

Browse files
committed
[RND-697] Refactor test for simplification
1 parent 265b683 commit d18fc10

File tree

2 files changed

+54
-100
lines changed

2 files changed

+54
-100
lines changed

Meadowlark-js/tests/e2e/helpers/Resources.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
44
// See the LICENSE and NOTICES files in the project root for more information.
55

6+
import request from 'supertest';
67
import { Role, getAccessToken } from './Credentials';
78
import { baseURLRequest, rootURLRequest } from './Shared';
89

@@ -54,3 +55,9 @@ export async function deleteResourceByLocation(location: string, resourceName =
5455
console.warn(`⚠️ Unable to delete ${resourceName}. Location not found. Verify that resource was created correctly ⚠️`);
5556
}
5657
}
58+
59+
export async function getResourceByLocation(location: string): Promise<request.Response> {
60+
return rootURLRequest()
61+
.get(location)
62+
.auth(await getAccessToken('host'), { type: 'bearer' });
63+
}
Lines changed: 47 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Response } from 'supertest';
22
import { getAccessToken } from '../helpers/Credentials';
33
import { baseURLRequest, rootURLRequest } from '../helpers/Shared';
4+
import { createContentClassDescriptor } from '../helpers/DataCreation';
5+
import { deleteResourceByLocation, getResourceByLocation } from '../helpers/Resources';
46

57
describe('when performing crud operations', () => {
68
const resource = 'contentClassDescriptors';
@@ -29,47 +31,33 @@ describe('when performing crud operations', () => {
2931
});
3032

3133
describe('when creating a new resource', () => {
32-
let response: Response;
34+
let createdResourceLocation: string;
3335
beforeAll(async () => {
34-
response = await baseURLRequest()
35-
.post(resourceEndpoint)
36-
.auth(await getAccessToken('host'), { type: 'bearer' })
37-
.send(resourceBody);
36+
createdResourceLocation = await createContentClassDescriptor();
3837
});
3938

4039
afterAll(async () => {
41-
await rootURLRequest()
42-
.delete(response.headers.location)
43-
.auth(await getAccessToken('host'), { type: 'bearer' })
44-
.send(resourceBody);
40+
await deleteResourceByLocation(createdResourceLocation);
4541
});
4642

47-
it('returns 201', () => {
48-
expect(response.statusCode).toBe(201);
43+
it('is created', () => {
44+
expect(createdResourceLocation).toBeTruthy();
4945
});
5046
});
5147

5248
describe('when getting a resource by ID', () => {
5349
describe('given the resource exists', () => {
54-
let createResponse: Response;
50+
let createdResourceLocation: string;
5551
let getResponse: Response;
5652

5753
beforeAll(async () => {
58-
createResponse = await baseURLRequest()
59-
.post(resourceEndpoint)
60-
.auth(await getAccessToken('host'), { type: 'bearer' })
61-
.send(resourceBody);
54+
createdResourceLocation = await createContentClassDescriptor();
6255

63-
getResponse = await rootURLRequest()
64-
.get(createResponse.headers.location)
65-
.auth(await getAccessToken('host'), { type: 'bearer' });
56+
getResponse = await getResourceByLocation(createdResourceLocation);
6657
});
6758

6859
afterAll(async () => {
69-
await rootURLRequest()
70-
.delete(createResponse.headers.location)
71-
.auth(await getAccessToken('host'), { type: 'bearer' })
72-
.send(resourceBody);
60+
await deleteResourceByLocation(createdResourceLocation);
7361
});
7462

7563
it('returns 200', () => {
@@ -81,48 +69,37 @@ describe('when performing crud operations', () => {
8169
});
8270

8371
it('returns 404 when the resource does not exist', async () => {
84-
await rootURLRequest()
85-
.get(`${createResponse.headers.location.slice(0, -1)}F`)
86-
.auth(await getAccessToken('host'), { type: 'bearer' })
87-
.expect(404);
72+
const response = await getResourceByLocation(`${createdResourceLocation.slice(0, -1)}F`);
73+
expect(response.statusCode).toEqual(404);
8874
});
8975

9076
it('should match the location', async () => {
91-
const id = await rootURLRequest()
92-
.get(createResponse.headers.location)
93-
.auth(await getAccessToken('vendor'), { type: 'bearer' })
94-
.then((response) => response.body.id);
77+
const id = await getResourceByLocation(createdResourceLocation).then((response) => response.body.id);
9578

96-
await baseURLRequest()
79+
const response = await baseURLRequest()
9780
.get(`${resourceEndpoint}/${id}`)
98-
.auth(await getAccessToken('vendor'), { type: 'bearer' })
99-
.expect(200);
81+
.auth(await getAccessToken('host'), { type: 'bearer' });
82+
expect(response.statusCode).toEqual(200);
10083

101-
expect(createResponse.headers.location).toContain(`${resourceEndpoint}/${id}`);
84+
expect(createdResourceLocation).toContain(`${resourceEndpoint}/${id}`);
10285
});
10386
});
10487
});
10588

10689
describe('when getting all resources', () => {
107-
let createResponse: Response;
90+
let createdResourceLocation: string;
10891
let getAllResponse: Response;
10992

11093
beforeAll(async () => {
111-
createResponse = await baseURLRequest()
112-
.post(resourceEndpoint)
113-
.auth(await getAccessToken('host'), { type: 'bearer' })
114-
.send(resourceBody);
94+
createdResourceLocation = await createContentClassDescriptor();
11595

11696
getAllResponse = await baseURLRequest()
11797
.get(resourceEndpoint)
11898
.auth(await getAccessToken('host'), { type: 'bearer' });
11999
});
120100

121101
afterAll(async () => {
122-
await rootURLRequest()
123-
.delete(createResponse.headers.location)
124-
.auth(await getAccessToken('host'), { type: 'bearer' })
125-
.send(resourceBody);
102+
await deleteResourceByLocation(createdResourceLocation);
126103
});
127104

128105
it('returns 200', () => {
@@ -135,14 +112,11 @@ describe('when performing crud operations', () => {
135112
});
136113

137114
describe('when upserting a resource', () => {
138-
let insertResponse: Response;
115+
let createdResourceLocation: string;
139116
let upsertResponse: Response;
140117

141118
beforeAll(async () => {
142-
insertResponse = await baseURLRequest()
143-
.post(resourceEndpoint)
144-
.auth(await getAccessToken('host'), { type: 'bearer' })
145-
.send(resourceBody);
119+
createdResourceLocation = await createContentClassDescriptor();
146120

147121
upsertResponse = await baseURLRequest()
148122
.post(resourceEndpoint)
@@ -151,34 +125,25 @@ describe('when performing crud operations', () => {
151125
});
152126

153127
afterAll(async () => {
154-
await rootURLRequest()
155-
.delete(insertResponse.headers.location)
156-
.auth(await getAccessToken('host'), { type: 'bearer' })
157-
.send(resourceBody);
128+
await deleteResourceByLocation(createdResourceLocation);
158129
});
159130

160131
it('returns 200', () => {
161132
expect(upsertResponse.statusCode).toEqual(200);
162133
});
163134

164135
it('returns updated resource on get', async () => {
165-
await rootURLRequest()
166-
.get(upsertResponse.headers.location)
167-
.auth(await getAccessToken('host'), { type: 'bearer' })
168-
.then((updatedResponse) => {
169-
expect(updatedResponse.body).toEqual(expect.objectContaining(resourceBodyUpdated));
170-
});
136+
await getResourceByLocation(upsertResponse.headers.location).then((updatedResponse) => {
137+
expect(updatedResponse.body).toEqual(expect.objectContaining(resourceBodyUpdated));
138+
});
171139
});
172140
});
173141

174142
describe('when updating a resource', () => {
175-
let insertResponse: Response;
143+
let createdResourceLocation: string;
176144

177145
beforeAll(async () => {
178-
insertResponse = await baseURLRequest()
179-
.post(resourceEndpoint)
180-
.auth(await getAccessToken('host'), { type: 'bearer' })
181-
.send(resourceBody);
146+
createdResourceLocation = await createContentClassDescriptor();
182147

183148
await baseURLRequest()
184149
.post(resourceEndpoint)
@@ -187,30 +152,24 @@ describe('when performing crud operations', () => {
187152
});
188153

189154
afterAll(async () => {
190-
await rootURLRequest()
191-
.delete(insertResponse.headers.location)
192-
.auth(await getAccessToken('host'), { type: 'bearer' })
193-
.send(resourceBody);
155+
await deleteResourceByLocation(createdResourceLocation);
194156
});
195157

196158
describe('when updating a resource with empty body', () => {
197159
it('returns 400', async () => {
198160
await rootURLRequest()
199-
.put(insertResponse.headers.location)
161+
.put(createdResourceLocation)
200162
.auth(await getAccessToken('host'), { type: 'bearer' })
201163
.send({})
202164
.expect(400);
203165
});
204166
});
205167

206168
it('returns 204', async () => {
207-
const id = await rootURLRequest()
208-
.get(insertResponse.headers.location)
209-
.auth(await getAccessToken('vendor'), { type: 'bearer' })
210-
.then((response) => response.body.id);
169+
const id = await getResourceByLocation(createdResourceLocation).then((response) => response.body.id);
211170

212171
await rootURLRequest()
213-
.put(insertResponse.headers.location)
172+
.put(createdResourceLocation)
214173
.auth(await getAccessToken('host'), { type: 'bearer' })
215174
.send({
216175
id,
@@ -222,7 +181,7 @@ describe('when performing crud operations', () => {
222181
it('should fail when resource ID is different in body on put', async () => {
223182
const id = 'differentId';
224183
await rootURLRequest()
225-
.put(insertResponse.headers.location)
184+
.put(createdResourceLocation)
226185
.auth(await getAccessToken('host'), { type: 'bearer' })
227186
.send({
228187
id,
@@ -240,7 +199,7 @@ describe('when performing crud operations', () => {
240199

241200
it('should fail when resource ID is not included in body on put', async () => {
242201
await rootURLRequest()
243-
.put(insertResponse.headers.location)
202+
.put(createdResourceLocation)
244203
.auth(await getAccessToken('host'), { type: 'bearer' })
245204
.send(resourceBody)
246205
.expect(400)
@@ -260,19 +219,14 @@ describe('when performing crud operations', () => {
260219
});
261220

262221
it('returns updated resource on get', async () => {
263-
await rootURLRequest()
264-
.get(insertResponse.headers.location)
265-
.auth(await getAccessToken('host'), { type: 'bearer' })
266-
.then((response) => {
267-
expect(response.body).toEqual(expect.objectContaining(resourceBody));
268-
});
222+
await getResourceByLocation(createdResourceLocation).then((response) => {
223+
expect(response.body).toEqual(expect.objectContaining(resourceBody));
224+
});
269225
});
270226

271227
it('should fail when resource ID is included in body on post', async () => {
272-
const id = await rootURLRequest()
273-
.get(insertResponse.headers.location)
274-
.auth(await getAccessToken('host'), { type: 'bearer' })
275-
.then((response) => response.body.id);
228+
const id = await getResourceByLocation(createdResourceLocation).then((response) => response.body.id);
229+
276230
await baseURLRequest()
277231
.post(`${resourceEndpoint}`)
278232
.auth(await getAccessToken('host'), { type: 'bearer' })
@@ -298,25 +252,20 @@ describe('when performing crud operations', () => {
298252
});
299253

300254
describe('when deleting a resource', () => {
301-
let createResponse: Response;
255+
let createdResourceLocation: string;
302256
let deleteResponse: Response;
303257
beforeAll(async () => {
304-
createResponse = await baseURLRequest()
305-
.post(resourceEndpoint)
306-
.auth(await getAccessToken('host'), { type: 'bearer' })
307-
.send(resourceBody);
258+
createdResourceLocation = await createContentClassDescriptor();
308259

309260
deleteResponse = await rootURLRequest()
310-
.delete(createResponse.headers.location)
311-
.auth(await getAccessToken('host'), { type: 'bearer' })
312-
.send(resourceBody);
261+
.delete(createdResourceLocation)
262+
.auth(await getAccessToken('host'), { type: 'bearer' });
313263
});
314264

315265
it('returns 404 when deleting a resource that does not exist', async () => {
316266
await rootURLRequest()
317-
.delete(`${createResponse.headers.location.slice(0, -1)}F`)
267+
.delete(`${createdResourceLocation.slice(0, -1)}F`)
318268
.auth(await getAccessToken('host'), { type: 'bearer' })
319-
.send(resourceBody)
320269
.expect(404);
321270
});
322271

@@ -325,10 +274,8 @@ describe('when performing crud operations', () => {
325274
});
326275

327276
it('returns 404 on get', async () => {
328-
await rootURLRequest()
329-
.get(createResponse.headers.location)
330-
.auth(await getAccessToken('host'), { type: 'bearer' })
331-
.expect(404);
277+
const response = await getResourceByLocation(createdResourceLocation);
278+
expect(response.statusCode).toEqual(404);
332279
});
333280
});
334281
});

0 commit comments

Comments
 (0)