@@ -166,7 +166,7 @@ function createCheckBody(transactionId: number, amount: string, data: string) {
166166 } ;
167167}
168168
169- function createPayBody ( transactionId : number , amount : string , data : string , overrides : Record < string , unknown > = { } ) {
169+ function createPayBody ( transactionId : number , amount : string , data ? : string , overrides : Record < string , unknown > = { } ) {
170170 return {
171171 TransactionId : transactionId ,
172172 Amount : amount ,
@@ -758,6 +758,37 @@ describe('CloudPaymentsWebhooks', () => {
758758 expect ( res . json ) . toHaveBeenCalledWith ( { code : PayCodes . SUCCESS } ) ;
759759 } ) ;
760760
761+ it ( 'should complete subscription renewal without Data and without promo usage' , async ( ) => {
762+ const webhooks = new CloudPaymentsWebhooks ( ) as any ;
763+ const workspaceId = new ObjectId ( ) . toString ( ) ;
764+ const userId = new ObjectId ( ) . toString ( ) ;
765+ const plan = createPlan ( 1000 ) ;
766+ const { context, workspace, changePlan, createUsage } = createWebhookContext ( {
767+ workspaceId,
768+ userId,
769+ plan,
770+ subscriptionId : 'subscription-id' ,
771+ } ) ;
772+
773+ context . factories . workspacesFactory . findBySubscriptionId = jest . fn ( ) . mockResolvedValue ( workspace ) ;
774+
775+ const res = createMockResponse ( ) ;
776+
777+ await webhooks . pay ( {
778+ context,
779+ body : createPayBody ( 2004 , '1000' , undefined , {
780+ AccountId : userId ,
781+ Data : undefined ,
782+ SubscriptionId : 'subscription-id' ,
783+ } ) ,
784+ } , res ) ;
785+
786+ expect ( context . factories . workspacesFactory . findBySubscriptionId ) . toHaveBeenCalledWith ( 'subscription-id' ) ;
787+ expect ( changePlan ) . toHaveBeenCalledWith ( plan . _id ) ;
788+ expect ( createUsage ) . not . toHaveBeenCalled ( ) ;
789+ expect ( res . json ) . toHaveBeenCalledWith ( { code : PayCodes . SUCCESS } ) ;
790+ } ) ;
791+
761792 it ( 'should cancel old subscription when a new subscription id is received' , async ( ) => {
762793 const webhooks = new CloudPaymentsWebhooks ( ) as any ;
763794 const workspaceId = new ObjectId ( ) . toString ( ) ;
@@ -775,7 +806,7 @@ describe('CloudPaymentsWebhooks', () => {
775806
776807 await webhooks . pay ( {
777808 context,
778- body : createPayBody ( 2004 , '1000' , Data , { SubscriptionId : 'new-subscription' } ) ,
809+ body : createPayBody ( 2005 , '1000' , Data , { SubscriptionId : 'new-subscription' } ) ,
779810 } , res ) ;
780811
781812 expect ( cloudPaymentsClientMocks . cancelSubscription ) . toHaveBeenCalledWith ( { Id : 'old-subscription' } ) ;
@@ -807,10 +838,10 @@ describe('CloudPaymentsWebhooks', () => {
807838 } ,
808839 } ) ;
809840
810- await webhooks . pay ( { context, body : createPayBody ( 2005 , '1' , Data ) } , res ) ;
841+ await webhooks . pay ( { context, body : createPayBody ( 2006 , '1' , Data ) } , res ) ;
811842
812843 expect ( changePlan ) . not . toHaveBeenCalled ( ) ;
813- expect ( cloudPaymentsApi . cancelPayment ) . toHaveBeenCalledWith ( 2005 ) ;
844+ expect ( cloudPaymentsApi . cancelPayment ) . toHaveBeenCalledWith ( 2006 ) ;
814845 expect ( createBusinessOperation ) . toHaveBeenCalledWith ( expect . objectContaining ( {
815846 type : BusinessOperationType . CardLinkRefund ,
816847 status : BusinessOperationStatus . Confirmed ,
@@ -831,7 +862,7 @@ describe('CloudPaymentsWebhooks', () => {
831862
832863 ( publish as jest . Mock ) . mockRejectedValueOnce ( new Error ( 'rabbit down' ) ) ;
833864
834- await webhooks . pay ( { context, body : createPayBody ( 2006 , '1000' , Data ) } , res ) ;
865+ await webhooks . pay ( { context, body : createPayBody ( 2007 , '1000' , Data ) } , res ) ;
835866
836867 expect ( res . json ) . toHaveBeenCalledWith ( { code : PayCodes . SUCCESS } ) ;
837868 } ) ;
@@ -848,7 +879,7 @@ describe('CloudPaymentsWebhooks', () => {
848879
849880 ( sendNotification as jest . Mock ) . mockRejectedValueOnce ( new Error ( 'notify failed' ) ) ;
850881
851- await webhooks . pay ( { context, body : createPayBody ( 2007 , '1000' , Data ) } , res ) ;
882+ await webhooks . pay ( { context, body : createPayBody ( 2008 , '1000' , Data ) } , res ) ;
852883
853884 expect ( res . json ) . toHaveBeenCalledWith ( { code : PayCodes . SUCCESS } ) ;
854885 } ) ;
0 commit comments