1
1
import { Response } from 'supertest' ;
2
2
import { getAccessToken } from '../helpers/Credentials' ;
3
3
import { baseURLRequest , rootURLRequest } from '../helpers/Shared' ;
4
+ import { createContentClassDescriptor } from '../helpers/DataCreation' ;
5
+ import { deleteResourceByLocation , getResourceByLocation } from '../helpers/Resources' ;
4
6
5
7
describe ( 'when performing crud operations' , ( ) => {
6
8
const resource = 'contentClassDescriptors' ;
@@ -29,47 +31,33 @@ describe('when performing crud operations', () => {
29
31
} ) ;
30
32
31
33
describe ( 'when creating a new resource' , ( ) => {
32
- let response : Response ;
34
+ let createdResourceLocation : string ;
33
35
beforeAll ( async ( ) => {
34
- response = await baseURLRequest ( )
35
- . post ( resourceEndpoint )
36
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
37
- . send ( resourceBody ) ;
36
+ createdResourceLocation = await createContentClassDescriptor ( ) ;
38
37
} ) ;
39
38
40
39
afterAll ( async ( ) => {
41
- await rootURLRequest ( )
42
- . delete ( response . headers . location )
43
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
44
- . send ( resourceBody ) ;
40
+ await deleteResourceByLocation ( createdResourceLocation ) ;
45
41
} ) ;
46
42
47
- it ( 'returns 201 ' , ( ) => {
48
- expect ( response . statusCode ) . toBe ( 201 ) ;
43
+ it ( 'is created ' , ( ) => {
44
+ expect ( createdResourceLocation ) . toBeTruthy ( ) ;
49
45
} ) ;
50
46
} ) ;
51
47
52
48
describe ( 'when getting a resource by ID' , ( ) => {
53
49
describe ( 'given the resource exists' , ( ) => {
54
- let createResponse : Response ;
50
+ let createdResourceLocation : string ;
55
51
let getResponse : Response ;
56
52
57
53
beforeAll ( async ( ) => {
58
- createResponse = await baseURLRequest ( )
59
- . post ( resourceEndpoint )
60
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
61
- . send ( resourceBody ) ;
54
+ createdResourceLocation = await createContentClassDescriptor ( ) ;
62
55
63
- getResponse = await rootURLRequest ( )
64
- . get ( createResponse . headers . location )
65
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } ) ;
56
+ getResponse = await getResourceByLocation ( createdResourceLocation ) ;
66
57
} ) ;
67
58
68
59
afterAll ( async ( ) => {
69
- await rootURLRequest ( )
70
- . delete ( createResponse . headers . location )
71
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
72
- . send ( resourceBody ) ;
60
+ await deleteResourceByLocation ( createdResourceLocation ) ;
73
61
} ) ;
74
62
75
63
it ( 'returns 200' , ( ) => {
@@ -81,48 +69,37 @@ describe('when performing crud operations', () => {
81
69
} ) ;
82
70
83
71
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 ) ;
88
74
} ) ;
89
75
90
76
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 ) ;
95
78
96
- await baseURLRequest ( )
79
+ const response = await baseURLRequest ( )
97
80
. 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 ) ;
100
83
101
- expect ( createResponse . headers . location ) . toContain ( `${ resourceEndpoint } /${ id } ` ) ;
84
+ expect ( createdResourceLocation ) . toContain ( `${ resourceEndpoint } /${ id } ` ) ;
102
85
} ) ;
103
86
} ) ;
104
87
} ) ;
105
88
106
89
describe ( 'when getting all resources' , ( ) => {
107
- let createResponse : Response ;
90
+ let createdResourceLocation : string ;
108
91
let getAllResponse : Response ;
109
92
110
93
beforeAll ( async ( ) => {
111
- createResponse = await baseURLRequest ( )
112
- . post ( resourceEndpoint )
113
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
114
- . send ( resourceBody ) ;
94
+ createdResourceLocation = await createContentClassDescriptor ( ) ;
115
95
116
96
getAllResponse = await baseURLRequest ( )
117
97
. get ( resourceEndpoint )
118
98
. auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } ) ;
119
99
} ) ;
120
100
121
101
afterAll ( async ( ) => {
122
- await rootURLRequest ( )
123
- . delete ( createResponse . headers . location )
124
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
125
- . send ( resourceBody ) ;
102
+ await deleteResourceByLocation ( createdResourceLocation ) ;
126
103
} ) ;
127
104
128
105
it ( 'returns 200' , ( ) => {
@@ -135,14 +112,11 @@ describe('when performing crud operations', () => {
135
112
} ) ;
136
113
137
114
describe ( 'when upserting a resource' , ( ) => {
138
- let insertResponse : Response ;
115
+ let createdResourceLocation : string ;
139
116
let upsertResponse : Response ;
140
117
141
118
beforeAll ( async ( ) => {
142
- insertResponse = await baseURLRequest ( )
143
- . post ( resourceEndpoint )
144
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
145
- . send ( resourceBody ) ;
119
+ createdResourceLocation = await createContentClassDescriptor ( ) ;
146
120
147
121
upsertResponse = await baseURLRequest ( )
148
122
. post ( resourceEndpoint )
@@ -151,34 +125,25 @@ describe('when performing crud operations', () => {
151
125
} ) ;
152
126
153
127
afterAll ( async ( ) => {
154
- await rootURLRequest ( )
155
- . delete ( insertResponse . headers . location )
156
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
157
- . send ( resourceBody ) ;
128
+ await deleteResourceByLocation ( createdResourceLocation ) ;
158
129
} ) ;
159
130
160
131
it ( 'returns 200' , ( ) => {
161
132
expect ( upsertResponse . statusCode ) . toEqual ( 200 ) ;
162
133
} ) ;
163
134
164
135
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
+ } ) ;
171
139
} ) ;
172
140
} ) ;
173
141
174
142
describe ( 'when updating a resource' , ( ) => {
175
- let insertResponse : Response ;
143
+ let createdResourceLocation : string ;
176
144
177
145
beforeAll ( async ( ) => {
178
- insertResponse = await baseURLRequest ( )
179
- . post ( resourceEndpoint )
180
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
181
- . send ( resourceBody ) ;
146
+ createdResourceLocation = await createContentClassDescriptor ( ) ;
182
147
183
148
await baseURLRequest ( )
184
149
. post ( resourceEndpoint )
@@ -187,30 +152,24 @@ describe('when performing crud operations', () => {
187
152
} ) ;
188
153
189
154
afterAll ( async ( ) => {
190
- await rootURLRequest ( )
191
- . delete ( insertResponse . headers . location )
192
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
193
- . send ( resourceBody ) ;
155
+ await deleteResourceByLocation ( createdResourceLocation ) ;
194
156
} ) ;
195
157
196
158
describe ( 'when updating a resource with empty body' , ( ) => {
197
159
it ( 'returns 400' , async ( ) => {
198
160
await rootURLRequest ( )
199
- . put ( insertResponse . headers . location )
161
+ . put ( createdResourceLocation )
200
162
. auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
201
163
. send ( { } )
202
164
. expect ( 400 ) ;
203
165
} ) ;
204
166
} ) ;
205
167
206
168
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 ) ;
211
170
212
171
await rootURLRequest ( )
213
- . put ( insertResponse . headers . location )
172
+ . put ( createdResourceLocation )
214
173
. auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
215
174
. send ( {
216
175
id,
@@ -222,7 +181,7 @@ describe('when performing crud operations', () => {
222
181
it ( 'should fail when resource ID is different in body on put' , async ( ) => {
223
182
const id = 'differentId' ;
224
183
await rootURLRequest ( )
225
- . put ( insertResponse . headers . location )
184
+ . put ( createdResourceLocation )
226
185
. auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
227
186
. send ( {
228
187
id,
@@ -240,7 +199,7 @@ describe('when performing crud operations', () => {
240
199
241
200
it ( 'should fail when resource ID is not included in body on put' , async ( ) => {
242
201
await rootURLRequest ( )
243
- . put ( insertResponse . headers . location )
202
+ . put ( createdResourceLocation )
244
203
. auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
245
204
. send ( resourceBody )
246
205
. expect ( 400 )
@@ -260,19 +219,14 @@ describe('when performing crud operations', () => {
260
219
} ) ;
261
220
262
221
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
+ } ) ;
269
225
} ) ;
270
226
271
227
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
+
276
230
await baseURLRequest ( )
277
231
. post ( `${ resourceEndpoint } ` )
278
232
. auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
@@ -298,25 +252,20 @@ describe('when performing crud operations', () => {
298
252
} ) ;
299
253
300
254
describe ( 'when deleting a resource' , ( ) => {
301
- let createResponse : Response ;
255
+ let createdResourceLocation : string ;
302
256
let deleteResponse : Response ;
303
257
beforeAll ( async ( ) => {
304
- createResponse = await baseURLRequest ( )
305
- . post ( resourceEndpoint )
306
- . auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
307
- . send ( resourceBody ) ;
258
+ createdResourceLocation = await createContentClassDescriptor ( ) ;
308
259
309
260
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' } ) ;
313
263
} ) ;
314
264
315
265
it ( 'returns 404 when deleting a resource that does not exist' , async ( ) => {
316
266
await rootURLRequest ( )
317
- . delete ( `${ createResponse . headers . location . slice ( 0 , - 1 ) } F` )
267
+ . delete ( `${ createdResourceLocation . slice ( 0 , - 1 ) } F` )
318
268
. auth ( await getAccessToken ( 'host' ) , { type : 'bearer' } )
319
- . send ( resourceBody )
320
269
. expect ( 404 ) ;
321
270
} ) ;
322
271
@@ -325,10 +274,8 @@ describe('when performing crud operations', () => {
325
274
} ) ;
326
275
327
276
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 ) ;
332
279
} ) ;
333
280
} ) ;
334
281
} ) ;
0 commit comments