@@ -9,12 +9,21 @@ import { StripeService, EventType } from '@/stripe-integration/service'
99import { GateHubClient } from '@/gatehub/client'
1010import { TransactionTypeEnum } from '@/gatehub/consts'
1111import { StripeWebhookType } from '../../src/stripe-integration/validation'
12+ import { Transaction } from '@/transaction/model'
13+ import { Account } from '@/account/model'
14+ import { WalletAddress } from '@/walletAddress/model'
15+ import { loginUser } from '@/tests/utils'
16+ import { mockedListAssets } from '@/tests/mocks'
1217
1318describe ( 'Stripe Service' , ( ) : void => {
1419 let bindings : AwilixContainer < Cradle >
1520 let appContainer : TestApp
1621 let knex : Knex
1722 let stripeService : StripeService
23+ let userId : string
24+ let account : Account
25+ let walletAddress : WalletAddress
26+ let walletId : string
1827
1928 const mockGateHubClient = {
2029 createTransaction : jest . fn ( ) ,
@@ -78,6 +87,33 @@ describe('Stripe Service', (): void => {
7887 } )
7988
8089 beforeEach ( async ( ) : Promise < void > => {
90+ // Necessary db setup for transaction creation
91+ const { user } = await loginUser ( {
92+ authService : await bindings . resolve ( 'authService' ) ,
93+ extraUserArgs : {
94+ isEmailVerified : true ,
95+ gateHubUserId : 'test-gatehub-user'
96+ }
97+ } )
98+ userId = user . id
99+ walletId = faker . string . uuid ( )
100+
101+ account = await Account . query ( ) . insert ( {
102+ name : faker . string . alpha ( 10 ) ,
103+ userId : userId ,
104+ assetCode : 'USD' ,
105+ assetId : mockedListAssets [ 0 ] . id ,
106+ assetScale : 2 ,
107+ gateHubWalletId : 'gatehub-wallet-123'
108+ } )
109+
110+ walletAddress = await WalletAddress . query ( ) . insert ( {
111+ url : 'wallet_address_123' ,
112+ publicName : 'Test Wallet' ,
113+ accountId : account . id ,
114+ id : walletId
115+ } )
116+
81117 Reflect . set (
82118 stripeService ,
83119 'gateHubClient' ,
@@ -87,19 +123,12 @@ describe('Stripe Service', (): void => {
87123 Reflect . set ( stripeService , 'walletAddressService' , mockWalletAddressService )
88124 Reflect . set ( stripeService , 'accountService' , mockAccountService )
89125
90- // Mock vault UUID lookup
91126 mockGateHubClient . getVaultUuid . mockReturnValue ( 'vault-uuid-123' )
92127
93- // Mock successful transaction creation
94128 mockGateHubClient . createTransaction . mockResolvedValue ( undefined )
95129
96- // Mock wallet address lookup
97- mockWalletAddressService . getByUrl . mockResolvedValue ( {
98- id : 'wallet-123' ,
99- url : 'wallet_address_123'
100- } )
130+ mockWalletAddressService . getByUrl . mockResolvedValue ( walletAddress )
101131
102- // Mock gatehub wallet address lookup
103132 mockAccountService . getGateHubWalletAddress . mockResolvedValue ( {
104133 gateHubWalletId : 'gatehub-wallet-123'
105134 } )
@@ -119,6 +148,19 @@ describe('Stripe Service', (): void => {
119148 type : TransactionTypeEnum . HOSTED ,
120149 message : 'Stripe Transfer'
121150 } )
151+
152+ const transactions = await Transaction . query ( )
153+ expect ( transactions ) . toHaveLength ( 1 )
154+ expect ( transactions [ 0 ] ) . toMatchObject ( {
155+ walletAddressId : walletAddress . id ,
156+ accountId : account . id ,
157+ paymentId : webhook . data . object . id ,
158+ assetCode : webhook . data . object . currency . toUpperCase ( ) ,
159+ type : 'INCOMING' ,
160+ status : 'COMPLETED' ,
161+ description : 'Stripe Payment' ,
162+ source : 'Stripe'
163+ } )
122164 } )
123165
124166 it ( 'should handle payment_intent_payment_failed event type' , async ( ) : Promise < void > => {
@@ -135,6 +177,9 @@ describe('Stripe Service', (): void => {
135177 } )
136178 )
137179 expect ( mockGateHubClient . createTransaction ) . not . toHaveBeenCalled ( )
180+
181+ const transactions = await Transaction . query ( )
182+ expect ( transactions ) . toHaveLength ( 0 )
138183 } )
139184
140185 it ( 'should handle payment_intent_canceled event type' , async ( ) : Promise < void > => {
@@ -150,6 +195,9 @@ describe('Stripe Service', (): void => {
150195 } )
151196 )
152197 expect ( mockGateHubClient . createTransaction ) . not . toHaveBeenCalled ( )
198+
199+ const transactions = await Transaction . query ( )
200+ expect ( transactions ) . toHaveLength ( 0 )
153201 } )
154202
155203 it ( 'should log information about the received webhook' , async ( ) : Promise < void > => {
@@ -162,6 +210,9 @@ describe('Stripe Service', (): void => {
162210 `received webhook of type : ${ webhook . type } for : ${ webhook . id } `
163211 )
164212 )
213+
214+ const transactions = await Transaction . query ( )
215+ expect ( transactions ) . toHaveLength ( 1 )
165216 } )
166217 } )
167218
@@ -182,6 +233,19 @@ describe('Stripe Service', (): void => {
182233 type : TransactionTypeEnum . HOSTED ,
183234 message : 'Stripe Transfer'
184235 } )
236+
237+ const transactions = await Transaction . query ( )
238+ expect ( transactions ) . toHaveLength ( 1 )
239+ expect ( transactions [ 0 ] ) . toMatchObject ( {
240+ walletAddressId : walletAddress . id ,
241+ accountId : account . id ,
242+ paymentId : webhook . data . object . id ,
243+ assetCode : webhook . data . object . currency . toUpperCase ( ) ,
244+ type : 'INCOMING' ,
245+ status : 'COMPLETED' ,
246+ description : 'Stripe Payment' ,
247+ source : 'Stripe'
248+ } )
185249 } )
186250
187251 it ( 'should throw error when GateHub transaction creation fails' , async ( ) : Promise < void > => {
@@ -204,6 +268,9 @@ describe('Stripe Service', (): void => {
204268 error : expect . any ( Error )
205269 } )
206270 )
271+
272+ const transactions = await Transaction . query ( )
273+ expect ( transactions ) . toHaveLength ( 0 )
207274 } )
208275 } )
209276
@@ -224,6 +291,9 @@ describe('Stripe Service', (): void => {
224291 error : webhook . data . object . last_payment_error
225292 } )
226293 )
294+
295+ const transactions = await Transaction . query ( )
296+ expect ( transactions ) . toHaveLength ( 0 )
227297 } )
228298 } )
229299
@@ -243,6 +313,9 @@ describe('Stripe Service', (): void => {
243313 receiving_address : webhook . data . object . metadata . receiving_address
244314 } )
245315 )
316+
317+ const transactions = await Transaction . query ( )
318+ expect ( transactions ) . toHaveLength ( 0 )
246319 } )
247320 } )
248321} )
0 commit comments