11import HttpStatus from '@xpring-eng/http-status'
2+ import { expect } from 'chai'
23import * as request from 'supertest'
34import 'mocha'
45
@@ -43,10 +44,54 @@ describe('E2E - adminApiRouter - POST /users', function (): void {
4344 . expect ( 'Content-Type' , / t e x t \/ p l a i n / 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' , / t e x t \/ p l a i n / 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' , / t e x t \/ p l a i n / 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' , / t e x t \/ p l a i n / 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' , / j s o n / 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' , / j s o n / 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