import 'mocha' ; import HttpStatus from '@xpring-eng/http-status' ; import * as request from 'supertest' ; import App from '../../../../src/app' ; import { appSetup , appCleanup } from '../../../helpers/helpers' ; let app ; const payIdApiVersion = '2020-05-28' ; const contentType = 'application/merge-patch+json' ; describe ( 'E2E - adminApiRouter - PATCH /users/:payId' , function ( ) : void { before ( async function ( ) { app = await appSetup ( ) ; } ) ; it ( 'Returns a 201 when updating a user PayID' , function ( done ) : void { const payId = 'alice$xpring.money' ; const newPayId = { payId : 'john$xpring.money' , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . set ( 'Content-Type' , contentType ) . send ( newPayId ) . expect ( 'Content-Type' , /text\/plain/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( 'Location' , `/users/${ newPayId . payId }` ) . expect ( HttpStatus . Created , done ) ; } ) ; it ( 'Returns a 201 when updating a user PayID with an uppercase PayID' , function ( done ) : void { const payId = 'john$xpring.money' ; const newPayId = { payId : 'johnny$xpring.money' . toUpperCase ( ) , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . set ( 'Content-Type' , contentType ) . send ( newPayId ) . expect ( 'Content-Type' , /text\/plain/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( 'Location' , `/users/${ newPayId . payId . toLowerCase ( ) }` ) . expect ( HttpStatus . Created , done ) ; } ) ; it ( 'Returns a 201 when updating a user PayID with the same PayID' , function ( done ) : void { const payId = 'johnny$xpring.money' ; const newPayId = { payId , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . set ( 'Content-Type' , contentType ) . send ( newPayId ) . expect ( 'Content-Type' , /text\/plain/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( 'Location' , `/users/${ newPayId . payId }` ) . expect ( HttpStatus . Created , done ) ; } ) ; it ( 'Returns a 400 - Bad Request with an error payload for a request changing the PayID on an account with verified addresses' , function ( done ) : void { const payId = 'johnwick$127.0.0.1' ; const newPayId = { payId : 'bambi$xpring.money' , } ; const expectedErrorResponse = { error : 'Bad Request' , message : 'Cannot PATCH a PayID with verified addresses, as that would break the address signatures. Please use the PUT endpoint to update this PayID.' , statusCode : 400 , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . set ( 'Content-Type' , contentType ) . send ( newPayId ) . expect ( 'Content-Type' , /json/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( HttpStatus . BadRequest , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 400 - Bad Request with an error payload for a request with a malformed PayID' , function ( done ) : void { const payId = 'johnnyxpring.money' ; const newPayId = { payId : 'john$xpring.money' , } ; const expectedErrorResponse = { error : 'Bad Request' , message : 'Bad input. PayIDs must contain a "$"' , statusCode : 400 , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . set ( 'Content-Type' , contentType ) . send ( newPayId ) . expect ( 'Content-Type' , /json/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( HttpStatus . BadRequest , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 400 - Bad Request with an error payload for a request with an existing PayID with multiple "$"' , function ( done ) : void { const payId = 'johnny$bob$xpring.money' ; const newPayId = { payId : 'john$xpring.money' , } ; const expectedErrorResponse = { error : 'Bad Request' , message : 'Bad input. PayIDs must contain only one "$"' , statusCode : 400 , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . set ( 'Content-Type' , contentType ) . send ( newPayId ) . expect ( 'Content-Type' , /json/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( HttpStatus . BadRequest , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 400 - Bad Request with an error payload for a request to update a PayID to a new value containing multiple "$"' , function ( done ) : void { const payId = 'johnny$xpring.money' ; const newPayId = { payId : 'johnny$$xpring.money' , } ; const expectedErrorResponse = { error : 'Bad Request' , message : 'Bad input. PayIDs must contain only one "$"' , statusCode : 400 , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . set ( 'Content-Type' , contentType ) . send ( newPayId ) . expect ( 'Content-Type' , /json/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( HttpStatus . BadRequest , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 404 - The original user PayID does not exist' , function ( done ) : void { const payId = 'johndoe$xpring.money' ; const newPayId = { payId : 'john$xpring.money' , } ; const expectedErrorResponse = { statusCode : 404 , error : 'Not Found' , message : `The PayID ${ payId } doesn't exist.` , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . set ( 'Content-Type' , contentType ) . send ( newPayId ) . expect ( 'Content-Type' , /json/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( HttpStatus . NotFound , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 409 - Conflict when attempting to update a user to a PayID that already exists' , function ( done ) : void { const payId = 'alice$127.0.0.1' ; const newPayId = { payId : 'bob$127.0.0.1' , } ; const expectedErrorResponse = { statusCode : 409 , error : 'Conflict' , message : 'There already exists a user with the provided PayID' , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . set ( 'Content-Type' , contentType ) . send ( newPayId ) . expect ( 'Content-Type' , /json/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( HttpStatus . Conflict , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 415 - Unsupported Media Type when omitting the Content-Type header' , function ( done ) : void { const payId = 'alice$127.0.0.1' ; const newPayId = { payId : 'chloe$127.0.0.1' , } ; const expectedErrorResponse = { statusCode : 415 , error : 'Unsupported Media Type' , message : "A 'Content-Type' header is required for this request: 'Content-Type: application/merge-patch+json'." , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . send ( newPayId ) . expect ( 'Content-Type' , /json/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( HttpStatus . UnsupportedMediaType , expectedErrorResponse , done ) ; } ) ; it ( 'Returns a 415 - Unsupported Media Type when setting the Content-Type header to a wrong value' , function ( done ) : void { const payId = 'alice$127.0.0.1' ; const newPayId = { payId : 'chloe$127.0.0.1' , } ; const expectedErrorResponse = { statusCode : 415 , error : 'Unsupported Media Type' , message : "A 'Content-Type' header is required for this request: 'Content-Type: application/merge-patch+json'." , } ; request ( app . adminApiExpress ) . patch ( `/users/${ payId }` ) . set ( 'PayID-API-Version' , payIdApiVersion ) . set ( 'Content-Type' , 'application/json' ) . send ( newPayId ) . expect ( 'Content-Type' , /json/u ) . expect ( 'Accept-Patch' , contentType ) . expect ( HttpStatus . UnsupportedMediaType , expectedErrorResponse , done ) ; } ) ; after ( function ( ) { appCleanup ( app ) ; } ) ; } ) ;