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

Commit d160f4e

Browse files
authored
feat: e2e testing content-type and accept-patch (#623)
Added E2E testing for Content-Type and Accept-Patch on the GET, POST, PUT and DELETE routes. We need to make sure the correct Headers are included (or not) in the different routes.
1 parent 8e663ff commit d160f4e

File tree

6 files changed

+251
-18
lines changed

6 files changed

+251
-18
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ services:
4646
- POSTGRES_PASSWORD=password
4747
# If POSTGRES_DB != database_development, change DB_NAME in the payid service above
4848
- POSTGRES_DB=database_development
49-
# Uncomment if you want to change the Postgres user (in that case, change DB_NAME in the payid service). Default value: postgres
49+
# Uncomment if you want to change the Postgres user (in that case, change DB_USERNAME in the payid service). Default value: postgres
5050
#- POSTGRES_USER=postgres

src/routes/adminApiRouter.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,10 @@ adminApiRouter
3232
.get('/:payId', wrapAsync(getUser), sendSuccess)
3333

3434
// Create user route
35-
.post(
36-
'/',
37-
express.json(),
38-
// TODO:(hbergren) checkRequestContentType E2E tests,
39-
wrapAsync(postUser),
40-
sendSuccess,
41-
)
35+
.post('/', express.json(), wrapAsync(postUser), sendSuccess)
4236

4337
// Replace user route
44-
.put(
45-
'/:payId',
46-
express.json(),
47-
// TODO:(hbergren) checkRequestContentType E2E tests,
48-
wrapAsync(putUser),
49-
sendSuccess,
50-
)
38+
.put('/:payId', express.json(), wrapAsync(putUser), sendSuccess)
5139

5240
// Delete user route
5341
.delete('/:payId', wrapAsync(deleteUser), sendSuccess)

test/integration/e2e/admin-api/deleteUsers.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { appSetup, appCleanup } from '../../../helpers/helpers'
88
let app: App
99
const payIdApiVersion = '2020-05-28'
1010

11+
const acceptPatch = 'application/merge-patch+json'
12+
1113
describe('E2E - adminApiRouter - DELETE /users', function (): void {
1214
before(async function () {
1315
app = await appSetup()
@@ -26,6 +28,8 @@ describe('E2E - adminApiRouter - DELETE /users', function (): void {
2628
request(app.adminApiExpress)
2729
.delete(`/users/${payId}`)
2830
.set('PayID-API-Version', payIdApiVersion)
31+
// THEN we expect to have an Accept-Patch header in the response
32+
.expect('Accept-Patch', acceptPatch)
2933
// THEN we expect back a 204-No Content, indicating successful deletion
3034
.expect(HttpStatus.NoContent)
3135
.then((_res) => {
@@ -53,6 +57,8 @@ describe('E2E - adminApiRouter - DELETE /users', function (): void {
5357
request(app.adminApiExpress)
5458
.delete(`/users/${payId}`)
5559
.set('PayID-API-Version', payIdApiVersion)
60+
// THEN we expect to have an Accept-Patch header in the response
61+
.expect('Accept-Patch', acceptPatch)
5662
// THEN we expect back a 204-No Content, indicating successful deletion
5763
.expect(HttpStatus.NoContent)
5864
.then((_res) => {
@@ -75,6 +81,8 @@ describe('E2E - adminApiRouter - DELETE /users', function (): void {
7581
request(app.adminApiExpress)
7682
.delete(`/users/${payId}`)
7783
.set('PayID-API-Version', payIdApiVersion)
84+
// THEN we expect to have an Accept-Patch header in the response
85+
.expect('Accept-Patch', acceptPatch)
7886
// THEN we expect back a 204 - No Content
7987
.expect(HttpStatus.NoContent, done)
8088
})

test/integration/e2e/admin-api/getUsers.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { appSetup, appCleanup } from '../../../helpers/helpers'
88
let app: App
99
const payIdApiVersion = '2020-05-28'
1010

11+
const acceptPatch = 'application/merge-patch+json'
12+
1113
describe('E2E - adminApiRouter - GET /users', function (): void {
1214
before(async function () {
1315
app = await appSetup()
@@ -35,6 +37,8 @@ describe('E2E - adminApiRouter - GET /users', function (): void {
3537
.get(`/users/${payId}`)
3638
.set('PayID-API-Version', payIdApiVersion)
3739
.expect('Content-Type', /json/u)
40+
// THEN we expect to have an Accept-Patch header in the response
41+
.expect('Accept-Patch', acceptPatch)
3842
// THEN We expect back a 200 - OK, with the account information
3943
.expect(HttpStatus.OK, expectedResponse, done)
4044
})
@@ -60,6 +64,8 @@ describe('E2E - adminApiRouter - GET /users', function (): void {
6064
.get(`/users/${payId}`)
6165
.set('PayID-API-Version', payIdApiVersion)
6266
.expect('Content-Type', /json/u)
67+
// THEN we expect to have an Accept-Patch header in the response
68+
.expect('Accept-Patch', acceptPatch)
6369
// THEN We expect back a 200 - OK, with the account information
6470
.expect(HttpStatus.OK, expectedResponse, done)
6571
})
@@ -79,6 +85,8 @@ describe('E2E - adminApiRouter - GET /users', function (): void {
7985
.get(`/users/${payId}`)
8086
.set('PayID-API-Version', payIdApiVersion)
8187
.expect('Content-Type', /json/u)
88+
// THEN we expect to have an Accept-Patch header in the response
89+
.expect('Accept-Patch', acceptPatch)
8290
// THEN We expect back a 404 - Not Found, with the expected error response object
8391
.expect(HttpStatus.NotFound, expectedErrorResponse, done)
8492
})

test/integration/e2e/admin-api/postUsers.test.ts

Lines changed: 133 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import HttpStatus from '@xpring-eng/http-status'
2+
import { expect } from 'chai'
23
import * as request from 'supertest'
34
import 'mocha'
45

@@ -43,10 +44,54 @@ describe('E2E - adminApiRouter - POST /users', function (): void {
4344
.expect('Content-Type', /text\/plain/u)
4445
// THEN we expect the Location header to be set to the path of the created user resource
4546
.expect('Location', `/users/${userInformation.payId}`)
46-
// AND we expect back a 201 - CREATED
47+
// THEN we expect back a 201 - CREATED
4748
.expect(HttpStatus.Created, done)
4849
})
4950

51+
it('Returns a 201 when creating a new user, without an Accept-Patch header in the response', function (done): void {
52+
// GIVEN a user with a PayID known to not exist on the PayID service
53+
const userInformation = {
54+
payId: 'johnfoo$xpring.money',
55+
addresses: [
56+
{
57+
paymentNetwork: 'XRPL',
58+
environment: 'TESTNET',
59+
details: {
60+
address: 'TVQWr6BhgBLW2jbFyqqufgq8T9eN7KresB684ZSHKQ3oDtq',
61+
},
62+
},
63+
{
64+
paymentNetwork: 'BTC',
65+
environment: 'TESTNET',
66+
details: {
67+
address: 'mxNEbRXokcdJtT6sbukr1CTGVx8Tkxk3DC',
68+
},
69+
},
70+
],
71+
}
72+
73+
// WHEN we make a POST request to /users with that user information
74+
request(app.adminApiExpress)
75+
.post(`/users`)
76+
.set('PayID-API-Version', payIdApiVersion)
77+
.send(userInformation)
78+
.expect('Content-Type', /text\/plain/u)
79+
// THEN we expect the Location header to be set to the path of the created user resource
80+
.expect('Location', `/users/${userInformation.payId}`)
81+
// THEN we expect back a 201 - CREATED
82+
.expect(HttpStatus.Created)
83+
.end(function (err, res) {
84+
if (err) {
85+
return done(err)
86+
}
87+
88+
// AND ensure Accept-Patch header does not exist
89+
expect(res.header).to.not.have.key('Accept-Patch')
90+
91+
return done()
92+
})
93+
})
94+
5095
it('Returns a 201 when creating a new user with an uppercase PayID', function (done): void {
5196
const payId = 'johnsmith$xpring.money'
5297

@@ -72,7 +117,7 @@ describe('E2E - adminApiRouter - POST /users', function (): void {
72117
.expect('Content-Type', /text\/plain/u)
73118
// THEN we expect the Location header to be set to the path of the created user resource
74119
.expect('Location', `/users/${payId}`)
75-
// AND we expect back a 201 - CREATED
120+
// THEN we expect back a 201 - CREATED
76121
.expect(HttpStatus.Created, done)
77122
})
78123

@@ -99,7 +144,7 @@ describe('E2E - adminApiRouter - POST /users', function (): void {
99144
.expect('Content-Type', /text\/plain/u)
100145
// THEN we expect the Location header to be set to the path of the created user resource
101146
.expect('Location', `/users/${userInformation.payId}`)
102-
// AND we expect back a 201 - CREATED
147+
// THEN we expect back a 201 - CREATED
103148
.expect(HttpStatus.Created, done)
104149
})
105150

@@ -159,6 +204,91 @@ describe('E2E - adminApiRouter - POST /users', function (): void {
159204
.expect(HttpStatus.Conflict, expectedErrorResponse, done)
160205
})
161206

207+
it('Returns a 415 - Unsupported Media Type when sending a wrong Content-Type header', function (done): void {
208+
// GIVEN our new PayID user (must be sent as a String instead of an Object if Content-Type is not application/json)
209+
// Otherwise an error ERR_INVALID_ARG_TYPE will be thrown by Supertest in the send() method.
210+
const userInformation = `{
211+
payId: 'johnfoo$xpring.money',
212+
addresses: [
213+
{
214+
paymentNetwork: 'XRPL',
215+
environment: 'TESTNET',
216+
details: {
217+
address: 'TVQWr6BhgBLW2jbFyqqufgq8T9eN7KresB684ZSHKQ3oDtq',
218+
},
219+
},
220+
{
221+
paymentNetwork: 'BTC',
222+
environment: 'TESTNET',
223+
details: {
224+
address: 'mxNEbRXokcdJtT6sbukr1CTGVx8Tkxk3DC',
225+
},
226+
},
227+
],
228+
}`
229+
230+
// AND our expected error response
231+
const expectedErrorResponse = {
232+
statusCode: 415,
233+
error: 'Unsupported Media Type',
234+
message:
235+
"A 'Content-Type' header is required for this request: 'Content-Type: application/json'.",
236+
}
237+
238+
// WHEN we make a POST request to /users with that user information
239+
request(app.adminApiExpress)
240+
.post(`/users`)
241+
// WITH a wrong Content-Type
242+
.set('Content-Type', 'application/xml')
243+
.set('PayID-API-Version', payIdApiVersion)
244+
.send(userInformation)
245+
.expect('Content-Type', /json/u)
246+
// THEN we expect back a 415 - Unsupported Media Type and our expected error response
247+
.expect(HttpStatus.UnsupportedMediaType, expectedErrorResponse, done)
248+
})
249+
250+
it('Returns a 415 - Unsupported Media Type when Content-Type header is missing', function (done): void {
251+
// GIVEN our new PayID user (must be sent as a String instead of an Object
252+
// otherwise Supertest will automatically add a "Content-Type: application/json" header.)
253+
const userInformation = `{
254+
payId: 'johnfoo$xpring.money',
255+
addresses: [
256+
{
257+
paymentNetwork: 'XRPL',
258+
environment: 'TESTNET',
259+
details: {
260+
address: 'TVQWr6BhgBLW2jbFyqqufgq8T9eN7KresB684ZSHKQ3oDtq',
261+
},
262+
},
263+
{
264+
paymentNetwork: 'BTC',
265+
environment: 'TESTNET',
266+
details: {
267+
address: 'mxNEbRXokcdJtT6sbukr1CTGVx8Tkxk3DC',
268+
},
269+
},
270+
],
271+
}`
272+
273+
// AND our expected error response
274+
const expectedErrorResponse = {
275+
statusCode: 415,
276+
error: 'Unsupported Media Type',
277+
message:
278+
"A 'Content-Type' header is required for this request: 'Content-Type: application/json'.",
279+
}
280+
281+
// WHEN we make a POST request to /users with that user information
282+
request(app.adminApiExpress)
283+
.post(`/users`)
284+
// WITHOUT a Content-Type
285+
.set('PayID-API-Version', payIdApiVersion)
286+
.send(userInformation)
287+
.expect('Content-Type', /json/u)
288+
// THEN we expect back a 415 - Unsupported Media Type and our expected error response
289+
.expect(HttpStatus.UnsupportedMediaType, expectedErrorResponse, done)
290+
})
291+
162292
after(function () {
163293
appCleanup(app)
164294
})

0 commit comments

Comments
 (0)