1
1
import { err , ok } from "neverthrow" ;
2
+ import Stripe from "stripe" ;
2
3
import { beforeEach , describe , expect , it , vi } from "vitest" ;
3
4
4
5
import { mockedAppConfigRepo } from "@/__tests__/mocks/app-config-repo" ;
@@ -7,16 +8,23 @@ import { mockedGraphqlClient } from "@/__tests__/mocks/graphql-client";
7
8
import { mockedStripePublishableKey } from "@/__tests__/mocks/mocked-stripe-publishable-key" ;
8
9
import { mockedStripeRestrictedKey } from "@/__tests__/mocks/mocked-stripe-restricted-key" ;
9
10
import { mockedSaleorApiUrl } from "@/__tests__/mocks/saleor-api-url" ;
11
+ import { mockStripeWebhookSecret } from "@/__tests__/mocks/stripe-webhook-secret" ;
10
12
import { TEST_Procedure } from "@/__tests__/trpc-testing-procedure" ;
11
13
import { BaseError } from "@/lib/errors" ;
12
14
import { NewStripeConfigTrpcHandler } from "@/modules/app-config/trpc-handlers/new-stripe-config-trpc-handler" ;
15
+ import { StripeClient } from "@/modules/stripe/stripe-client" ;
16
+ import { StripeWebhookManager } from "@/modules/stripe/stripe-webhook-manager" ;
13
17
import { router } from "@/modules/trpc/trpc-server" ;
14
18
19
+ const webhookCreator = new StripeWebhookManager ( ) ;
20
+
15
21
/**
16
22
* TODO: Probably create some test abstraction to bootstrap trpc handler for testing
17
23
*/
18
24
const getTestCaller = ( ) => {
19
- const instance = new NewStripeConfigTrpcHandler ( ) ;
25
+ const instance = new NewStripeConfigTrpcHandler ( {
26
+ webhookManager : webhookCreator ,
27
+ } ) ;
20
28
21
29
// @ts -expect-error - context doesnt match but its applied in test
22
30
instance . baseProcedure = TEST_Procedure ;
@@ -27,19 +35,38 @@ const getTestCaller = () => {
27
35
28
36
return {
29
37
mockedAppConfigRepo,
38
+ webhookCreator,
30
39
caller : testRouter . createCaller ( {
31
40
appId : mockedSaleorAppId ,
32
41
saleorApiUrl : mockedSaleorApiUrl ,
33
42
token : mockedAppToken ,
34
43
configRepo : mockedAppConfigRepo ,
35
44
apiClient : mockedGraphqlClient ,
45
+ appUrl : "https://localhost:3000" ,
36
46
} ) ,
37
47
} ;
38
48
} ;
39
49
40
50
describe ( "NewStripeConfigTrpcHandler" , ( ) => {
51
+ const stripe = new Stripe ( "key" ) ;
52
+
41
53
beforeEach ( ( ) => {
42
54
vi . resetAllMocks ( ) ;
55
+
56
+ vi . spyOn ( stripe . paymentIntents , "list" ) . mockImplementation ( ( ) => {
57
+ return Promise . resolve ( { } ) as Stripe . ApiListPromise < Stripe . PaymentIntent > ;
58
+ } ) ;
59
+ vi . spyOn ( StripeClient , "createFromRestrictedKey" ) . mockImplementation ( ( ) => {
60
+ return {
61
+ nativeClient : stripe ,
62
+ } ;
63
+ } ) ;
64
+ vi . spyOn ( webhookCreator , "createWebhook" ) . mockImplementation ( async ( ) =>
65
+ ok ( {
66
+ id : "whid_1234" ,
67
+ secret : mockStripeWebhookSecret ,
68
+ } ) ,
69
+ ) ;
43
70
} ) ;
44
71
45
72
it ( "Returns error 500 if repository fails to save config" , async ( ) => {
@@ -109,20 +136,42 @@ describe("NewStripeConfigTrpcHandler", () => {
109
136
config : {
110
137
id : expect . any ( String ) ,
111
138
} ,
112
- } ,
113
- `
139
+ } , `
114
140
{
115
141
"appId": "saleor-app-id",
116
142
"config": {
117
143
"id": Any<String>,
118
144
"name": "Test config",
119
145
"publishableKey": "pk_live_1",
120
146
"restrictedKey": "rk_live_AAAAABBBBCCCCCEEEEEEEFFFFFGGGGG",
121
- "webhookSecret": "whsec_TODO",
147
+ "webhookId": "whid_1234",
148
+ "webhookSecret": "whsec_XYZ",
122
149
},
123
150
"saleorApiUrl": "https://foo.bar.saleor.cloud/graphql/",
124
151
}
125
- ` ,
126
- ) ;
152
+ ` ) ;
153
+ } ) ;
154
+
155
+ describe ( "Stripe Auth" , ( ) => {
156
+ it ( "Calls auth service and returns error if Stripe RK is invalid" , ( ) => {
157
+ // @ts -expect-error - mocking stripe client
158
+ vi . spyOn ( stripe . paymentIntents , "list" ) . mockImplementationOnce ( async ( ) => {
159
+ throw new Error ( "Invalid key" ) ;
160
+ } ) ;
161
+
162
+ const { caller } = getTestCaller ( ) ;
163
+
164
+ return expect ( ( ) =>
165
+ caller . testProcedure ( {
166
+ name : "Test config" ,
167
+ publishableKey : mockedStripePublishableKey ,
168
+ restrictedKey : mockedStripeRestrictedKey ,
169
+ } ) ,
170
+ ) . rejects . toThrowErrorMatchingInlineSnapshot (
171
+ `[TRPCError: Failed to create Stripe configuration. Restricted key is invalid]` ,
172
+ ) ;
173
+
174
+ expect ( stripe . paymentIntents . list ) . toHaveBeenCalledOnce ( ) ;
175
+ } ) ;
127
176
} ) ;
128
177
} ) ;
0 commit comments