11/*
2- * This file is part of CoCalc: Copyright © 2025 Sagemath, Inc.
2+ * This file is part of CoCalc: Copyright © 2025-2026 Sagemath, Inc.
33 * License: MS-RSL – see LICENSE.md for details
44 */
55
@@ -67,64 +67,13 @@ describe("change_email_address", () => {
6767 return result . rows ?. [ 0 ] ?. email_address ;
6868 }
6969
70- /**
71- * Helper to set stripe_customer_id for an account
72- */
73- async function setStripeCustomerId (
74- account_id : string ,
75- customer_id : string ,
76- ) : Promise < void > {
77- await database . async_query ( {
78- query : "UPDATE accounts" ,
79- set : { stripe_customer_id : customer_id } ,
80- where : { "account_id = $::UUID" : account_id } ,
81- } ) ;
82- }
83-
84- /**
85- * Mock Stripe client
86- */
87- function createMockStripe ( options ?: {
88- shouldThrow ?: boolean ;
89- errorMessage ?: string ;
90- } ) {
91- const calls : any [ ] = [ ] ;
92- return {
93- customers : {
94- retrieve : jest . fn ( async ( customer_id ) => {
95- calls . push ( { method : "retrieve" , customer_id } ) ;
96- if ( options ?. shouldThrow ) {
97- throw new Error ( options . errorMessage ?? "Stripe API error" ) ;
98- }
99- return {
100- id : customer_id ,
101- email : "old@example.com" ,
102- subscriptions : { data : [ ] } ,
103- } ;
104- } ) ,
105- update : jest . fn ( async ( customer_id , update ) => {
106- calls . push ( { method : "update" , customer_id, update } ) ;
107- if ( options ?. shouldThrow ) {
108- throw new Error ( options . errorMessage ?? "Stripe API error" ) ;
109- }
110- return {
111- id : customer_id ,
112- email : update . email ,
113- subscriptions : { data : [ ] } ,
114- } ;
115- } ) ,
116- } ,
117- _calls : calls ,
118- } ;
119- }
120-
12170 describe ( "successful email change" , ( ) => {
12271 it ( "should change email address when new email is not taken" , async ( ) => {
12372 const unique_id = uuid ( ) . substring ( 0 , 8 ) ;
12473 const account_id = await createTestAccount (
12574 `original-${ unique_id } @example.com` ,
12675 ) ;
127- const stripe = createMockStripe ( ) ;
76+ const stripe = { } as any ;
12877
12978 await change_email_address_wrapper ( {
13079 account_id,
@@ -135,46 +84,6 @@ describe("change_email_address", () => {
13584 const email = await getAccountEmail ( account_id ) ;
13685 expect ( email ) . toBe ( `new-${ unique_id } @example.com` ) ;
13786 } ) ;
138-
139- it ( "should not call Stripe when account has no stripe_customer_id" , async ( ) => {
140- const unique_id = uuid ( ) . substring ( 0 , 8 ) ;
141- const account_id = await createTestAccount (
142- `test1-${ unique_id } @example.com` ,
143- ) ;
144- const stripe = createMockStripe ( ) ;
145-
146- await change_email_address_wrapper ( {
147- account_id,
148- email_address : `test1-new-${ unique_id } @example.com` ,
149- stripe,
150- } ) ;
151-
152- expect ( stripe . customers . retrieve ) . not . toHaveBeenCalled ( ) ;
153- expect ( stripe . customers . update ) . not . toHaveBeenCalled ( ) ;
154- } ) ;
155-
156- it ( "should call Stripe sync when account has stripe_customer_id" , async ( ) => {
157- const unique_id = uuid ( ) . substring ( 0 , 8 ) ;
158- const account_id = await createTestAccount (
159- `test2-${ unique_id } @example.com` ,
160- ) ;
161- const customer_id = "cus_" + uuid ( ) . replace ( / - / g, "" ) ;
162- await setStripeCustomerId ( account_id , customer_id ) ;
163-
164- const stripe = createMockStripe ( ) ;
165-
166- await change_email_address_wrapper ( {
167- account_id,
168- email_address : `test2-new-${ unique_id } @example.com` ,
169- stripe,
170- } ) ;
171-
172- const email = await getAccountEmail ( account_id ) ;
173- expect ( email ) . toBe ( `test2-new-${ unique_id } @example.com` ) ;
174- expect ( stripe . customers . retrieve ) . toHaveBeenCalledWith ( customer_id , {
175- expand : [ "sources" , "subscriptions" ] ,
176- } ) ;
177- } ) ;
17887 } ) ;
17988
18089 describe ( "email_already_taken error" , ( ) => {
@@ -184,7 +93,7 @@ describe("change_email_address", () => {
18493 const email2 = `user2-${ unique_id } @example.com` ;
18594 const account_id1 = await createTestAccount ( email1 ) ;
18695 await createTestAccount ( email2 ) ;
187- const stripe = createMockStripe ( ) ;
96+ const stripe = { } as any ;
18897
18998 // Try to change account_id1 to user2 email (already taken)
19099 await expect (
@@ -206,9 +115,7 @@ describe("change_email_address", () => {
206115 const email2 = `taken2-${ unique_id } @example.com` ;
207116 const account_id1 = await createTestAccount ( email1 ) ;
208117 await createTestAccount ( email2 ) ;
209- await setStripeCustomerId ( account_id1 , "cus_test" + unique_id ) ;
210-
211- const stripe = createMockStripe ( ) ;
118+ const stripe = { } as any ;
212119
213120 await expect (
214121 change_email_address_wrapper ( {
@@ -217,58 +124,6 @@ describe("change_email_address", () => {
217124 stripe,
218125 } ) ,
219126 ) . rejects . toMatch ( / e m a i l _ a l r e a d y _ t a k e n / ) ;
220-
221- expect ( stripe . customers . retrieve ) . not . toHaveBeenCalled ( ) ;
222- } ) ;
223- } ) ;
224-
225- describe ( "Stripe synchronization errors" , ( ) => {
226- it ( "should propagate Stripe errors when sync fails" , async ( ) => {
227- const unique_id = uuid ( ) . substring ( 0 , 8 ) ;
228- const account_id = await createTestAccount (
229- `stripe-error-${ unique_id } @example.com` ,
230- ) ;
231- const customer_id = "cus_" + uuid ( ) . replace ( / - / g, "" ) ;
232- await setStripeCustomerId ( account_id , customer_id ) ;
233-
234- const stripe = createMockStripe ( {
235- shouldThrow : true ,
236- errorMessage : "Stripe service unavailable" ,
237- } ) ;
238-
239- await expect (
240- change_email_address_wrapper ( {
241- account_id,
242- email_address : `stripe-error-new-${ unique_id } @example.com` ,
243- stripe,
244- } ) ,
245- ) . rejects . toThrow ( / S t r i p e s e r v i c e u n a v a i l a b l e / ) ;
246- } ) ;
247-
248- it ( "should still update email in database even if Stripe sync fails" , async ( ) => {
249- const unique_id = uuid ( ) . substring ( 0 , 8 ) ;
250- const account_id = await createTestAccount (
251- `stripe-error2-${ unique_id } @example.com` ,
252- ) ;
253- const customer_id = "cus_" + uuid ( ) . replace ( / - / g, "" ) ;
254- await setStripeCustomerId ( account_id , customer_id ) ;
255-
256- const stripe = createMockStripe ( {
257- shouldThrow : true ,
258- errorMessage : "Network error" ,
259- } ) ;
260-
261- await expect (
262- change_email_address_wrapper ( {
263- account_id,
264- email_address : `stripe-error2-new-${ unique_id } @example.com` ,
265- stripe,
266- } ) ,
267- ) . rejects . toThrow ( / N e t w o r k e r r o r / ) ;
268-
269- // Email should be updated despite Stripe error (happens in step 2, before Stripe sync in step 3)
270- const email = await getAccountEmail ( account_id ) ;
271- expect ( email ) . toBe ( `stripe-error2-new-${ unique_id } @example.com` ) ;
272127 } ) ;
273128 } ) ;
274129
@@ -277,7 +132,7 @@ describe("change_email_address", () => {
277132 const unique_id = uuid ( ) . substring ( 0 , 8 ) ;
278133 const email = `same-${ unique_id } @example.com` ;
279134 const account_id = await createTestAccount ( email ) ;
280- const stripe = createMockStripe ( ) ;
135+ const stripe = { } as any ;
281136
282137 // CoffeeScript checks account_exists which finds the same account's email
283138 await expect (
@@ -298,7 +153,7 @@ describe("change_email_address", () => {
298153 const account_id = await createTestAccount (
299154 `lower-${ unique_id } @example.com` ,
300155 ) ;
301- const stripe = createMockStripe ( ) ;
156+ const stripe = { } as any ;
302157
303158 await change_email_address_wrapper ( {
304159 account_id,
@@ -316,7 +171,7 @@ describe("change_email_address", () => {
316171 const email2 = `other-${ unique_id } @example.com` ;
317172 await createTestAccount ( email1 ) ;
318173 const account_id2 = await createTestAccount ( email2 ) ;
319- const stripe = createMockStripe ( ) ;
174+ const stripe = { } as any ;
320175
321176 // CoffeeScript uses = which is case-sensitive, so lowercase version is allowed
322177 await change_email_address_wrapper ( {
@@ -336,7 +191,7 @@ describe("change_email_address", () => {
336191 const account_id = await createTestAccount (
337192 `concurrent-${ unique_id } @example.com` ,
338193 ) ;
339- const stripe = createMockStripe ( ) ;
194+ const stripe = { } as any ;
340195
341196 await change_email_address_wrapper ( {
342197 account_id,
0 commit comments