@@ -121,17 +121,48 @@ def test_transaction_found_but_not_doneish(self):
121121
122122 instance .notifications .list .assert_not_called ()
123123
124- def _test_doneish_by_status (self , tx_status ):
124+
125+ class PaddleTransactionCompleteCase (TestCase ):
126+ def setUp (self ):
127+ self .client = RequestClient ()
128+ self .user = User .objects .create (
129+ username = f'test@{ settings .PRIMARY_EMAIL_DOMAIN } ' , oidc_id = '1234' , is_awaiting_payment_verification = False
130+ )
131+ oidc_force_login (self .client , self .user )
132+ self .url = reverse ('paddle_completed' )
133+ self .txid = 'abc123'
134+
135+ def set_paddle_transaction_session_data (self , payment_type ):
136+ """This actually tests set_paddle_transaction_id too!"""
137+ txid_response = self .client .put (
138+ reverse ('paddle_txid' ), data = json .dumps ({'txid' : self .txid , 'payment_type' : payment_type })
139+ )
140+ self .assertEqual (txid_response .status_code , 200 )
141+ data = txid_response .json ()
142+ self .assertTrue (data .get ('success' ))
143+
144+ def _test_doneish_by_status (self , tx_status , is_popup_payment_provider = False ):
125145 """Helper function so we can reduce some code without introducing artifacts between test runs."""
126- # Make sure we have a txid in session
127- self .set_paddle_transaction_id ()
146+ # Make sure we have a txid and payment type in session
147+ payment_type = 'card' if not is_popup_payment_provider else 'paypal'
148+ ok_status_code = 200 if is_popup_payment_provider else 302
149+ ok_payment_verification = tx_status in [
150+ Transaction .StatusValues .PAID .value ,
151+ Transaction .StatusValues .COMPLETED .value ,
152+ ]
153+
154+ self .set_paddle_transaction_session_data (payment_type )
128155
129- transaction = Transaction .objects .create (paddle_id = self .txid , status = tx_status )
156+ transaction = Transaction .objects .create (paddle_id = self .txid , status = tx_status . value )
130157 self .assertIsNotNone (transaction )
131158
132159 with patch ('thunderbird_accounts.subscription.tasks.dev_only_paddle_fake_webhook' , MagicMock ()) as task_mock :
133160 with patch ('thunderbird_accounts.subscription.decorators.Client' , MagicMock ()) as paddle_client_mock :
134161 instance = paddle_client_mock ()
162+ status_mock = MagicMock ()
163+
164+ status_mock .status = tx_status
165+ instance .transactions .get .return_value = status_mock
135166
136167 self .assertFalse (self .user .is_awaiting_payment_verification )
137168
@@ -140,26 +171,31 @@ def _test_doneish_by_status(self, tx_status):
140171 follow = False ,
141172 )
142173 self .assertTrue (response )
143- self .assertEqual (response .status_code , 200 )
144-
145- data = response .json ()
146- self .assertTrue (data )
147- self .assertEqual (data .get ('status' ), tx_status )
174+ self .assertEqual (response .status_code , ok_status_code )
148175
149176 self .user .refresh_from_db ()
150- self .assertTrue (self .user .is_awaiting_payment_verification )
177+ self .assertEqual (self .user .is_awaiting_payment_verification , ok_payment_verification )
151178
179+ instance .transactions .get .assert_called ()
152180 instance .notifications .list .assert_not_called ()
153181 task_mock .assert_not_called ()
154182
183+ @override_settings (IS_DEV = False )
184+ def test_transaction_found_and_is_not_doneish_by_being_ready (self ):
185+ """We found the transaction but it's doneish (status=READY). This shouldn't do anything!
186+
187+ We should also ensure IS_DEV=False does not call Paddle or the fake webhook task."""
188+
189+ self ._test_doneish_by_status (Transaction .StatusValues .READY )
190+
155191 @override_settings (IS_DEV = False )
156192 def test_transaction_found_and_is_doneish_by_being_paid (self ):
157193 """We found the transaction but it's doneish (status=PAID). This should trigger payment verification
158194 and remove txid from session.
159195
160196 We should also ensure IS_DEV=False does not call Paddle or the fake webhook task."""
161197
162- self ._test_doneish_by_status (Transaction .StatusValues .PAID . value )
198+ self ._test_doneish_by_status (Transaction .StatusValues .PAID )
163199
164200 @override_settings (IS_DEV = False )
165201 def test_transaction_found_and_is_doneish_by_being_completed (self ):
@@ -168,4 +204,22 @@ def test_transaction_found_and_is_doneish_by_being_completed(self):
168204
169205 We should also ensure IS_DEV=False does not call Paddle or the fake webhook task."""
170206
171- self ._test_doneish_by_status (Transaction .StatusValues .COMPLETED .value )
207+ self ._test_doneish_by_status (Transaction .StatusValues .COMPLETED )
208+
209+ @override_settings (IS_DEV = False )
210+ def test_transaction_found_and_is_doneish_by_being_paid_popup_popup_edition (self ):
211+ """We found the transaction but it's doneish (status=PAID). This should trigger payment verification
212+ and remove txid from session.
213+
214+ We should also ensure IS_DEV=False does not call Paddle or the fake webhook task."""
215+
216+ self ._test_doneish_by_status (Transaction .StatusValues .PAID , True )
217+
218+ @override_settings (IS_DEV = False )
219+ def test_transaction_found_and_is_doneish_by_being_completed_popup_edition (self ):
220+ """We found the transaction but it's doneish (status=COMPLETED). This should trigger payment verification
221+ and remove txid from session.
222+
223+ We should also ensure IS_DEV=False does not call Paddle or the fake webhook task."""
224+
225+ self ._test_doneish_by_status (Transaction .StatusValues .COMPLETED , True )
0 commit comments