|
12 | 12 | from enterprise_access.apps.customer_billing.models import CheckoutIntent |
13 | 13 | from enterprise_access.apps.customer_billing.tasks import ( |
14 | 14 | send_enterprise_provision_signup_confirmation_email, |
| 15 | + send_payment_receipt_email, |
15 | 16 | send_trial_cancellation_email_task |
16 | 17 | ) |
17 | 18 |
|
@@ -245,3 +246,168 @@ def test_braze_campaign_send_failure(self, mock_validate_trial, mock_lms_client, |
245 | 246 | send_enterprise_provision_signup_confirmation_email(**self.test_data) |
246 | 247 |
|
247 | 248 | self.assertEqual(str(context.exception), "Braze Campaign Error") |
| 249 | + |
| 250 | + |
| 251 | +class TestSendPaymentReceiptEmail(TestCase): |
| 252 | + """ |
| 253 | + Tests for send_payment_receipt_email task. |
| 254 | + """ |
| 255 | + def setUp(self): |
| 256 | + super().setUp() |
| 257 | + self.mock_invoice_data = { |
| 258 | + 'id': 'in_1SNvVOQ60jNALKNUMk8TZucs', |
| 259 | + 'created': 1761829387, |
| 260 | + 'payment_intent': { |
| 261 | + 'payment_method': { |
| 262 | + 'card': { |
| 263 | + 'brand': 'visa', |
| 264 | + 'last4': '4242' |
| 265 | + }, |
| 266 | + 'billing_details': { |
| 267 | + 'name': 'Test User', |
| 268 | + 'address': { |
| 269 | + 'line1': '123 Test St', |
| 270 | + 'line2': 'Suite 100', |
| 271 | + 'city': 'Test City', |
| 272 | + 'state': 'TS', |
| 273 | + 'postal_code': '12345', |
| 274 | + 'country': 'US' |
| 275 | + } |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + self.mock_subscription_data = { |
| 281 | + 'quantity': 5, |
| 282 | + 'plan': { |
| 283 | + 'amount': 39600 # $396.00 in cents |
| 284 | + } |
| 285 | + } |
| 286 | + self.mock_admin_users = [ |
| 287 | + { |
| 288 | + |
| 289 | + 'lms_user_id': 1, |
| 290 | + }, |
| 291 | + { |
| 292 | + |
| 293 | + 'lms_user_id': 2, |
| 294 | + } |
| 295 | + ] |
| 296 | + self.enterprise_customer_name = 'Test Enterprise' |
| 297 | + self.enterprise_slug = 'test-enterprise' |
| 298 | + |
| 299 | + @mock.patch('enterprise_access.apps.customer_billing.tasks.format_datetime_obj') |
| 300 | + @mock.patch('enterprise_access.apps.customer_billing.tasks.BrazeApiClient') |
| 301 | + @mock.patch('enterprise_access.apps.customer_billing.tasks.LmsApiClient') |
| 302 | + def test_successful_payment_receipt_email(self, mock_lms_client, mock_braze_client, mock_format_datetime): |
| 303 | + """ |
| 304 | + Test successful payment receipt email sending. |
| 305 | + """ |
| 306 | + # Mock the date formatting function |
| 307 | + mock_format_datetime.return_value = '03 November 2025' |
| 308 | + |
| 309 | + mock_lms_client.return_value.get_enterprise_customer_data.return_value = { |
| 310 | + 'admin_users': self.mock_admin_users |
| 311 | + } |
| 312 | + |
| 313 | + mock_braze = mock_braze_client.return_value |
| 314 | + braze_recipients = [] |
| 315 | + actual_calls = [] |
| 316 | + |
| 317 | + def create_recipient_side_effect(user_email, lms_user_id): |
| 318 | + actual_calls.append(mock.call(user_email=user_email, lms_user_id=lms_user_id)) |
| 319 | + recipient = {'external_id': f'braze_{lms_user_id}'} |
| 320 | + braze_recipients.append(recipient) |
| 321 | + return recipient |
| 322 | + mock_braze.create_braze_recipient.side_effect = create_recipient_side_effect |
| 323 | + |
| 324 | + # Call the task |
| 325 | + send_payment_receipt_email( |
| 326 | + invoice_data=self.mock_invoice_data, |
| 327 | + subscription_data=self.mock_subscription_data, |
| 328 | + enterprise_customer_name=self.enterprise_customer_name, |
| 329 | + enterprise_slug=self.enterprise_slug, |
| 330 | + ) |
| 331 | + |
| 332 | + # Verify LMS API was called to get admin users |
| 333 | + mock_lms_client.return_value.get_enterprise_customer_data.assert_called_once_with( |
| 334 | + enterprise_customer_slug=self.enterprise_slug |
| 335 | + ) |
| 336 | + |
| 337 | + # Verify Braze recipients were created for each admin |
| 338 | + expected_recipient_calls = [ |
| 339 | + mock.call(user_email=admin['email'], lms_user_id=admin.get('lms_user_id')) |
| 340 | + for admin in self.mock_admin_users |
| 341 | + ] |
| 342 | + mock_braze.create_braze_recipient.assert_has_calls(expected_recipient_calls, any_order=True) |
| 343 | + |
| 344 | + # Verify the campaign was sent with correct properties |
| 345 | + expected_properties = { |
| 346 | + 'total_paid_amount': Decimal('1980.00'), # $396.00 * 5 licenses = $1,980.00 |
| 347 | + 'date_paid': '03 November 2025', # Based on mock timestamp |
| 348 | + 'payment_method': 'visa - 4242', |
| 349 | + 'license_count': 5, |
| 350 | + 'price_per_license': Decimal('396.00'), |
| 351 | + 'customer_name': 'Test User', |
| 352 | + 'organization': 'Test Enterprise', |
| 353 | + 'billing_address': '123 Test St\nSuite 100\nTest City, TS 12345\nUS', |
| 354 | + 'enterprise_admin_portal_url': f'{settings.ENTERPRISE_ADMIN_PORTAL_URL}/test-enterprise', |
| 355 | + 'receipt_number': 'in_1SNvVOQ60jNALKNUMk8TZucs', |
| 356 | + } |
| 357 | + |
| 358 | + mock_braze.send_campaign_message.assert_called_once_with( |
| 359 | + settings.BRAZE_ENTERPRISE_PROVISION_PAYMENT_RECEIPT_CAMPAIGN, |
| 360 | + recipients=braze_recipients, |
| 361 | + trigger_properties=expected_properties, |
| 362 | + ) |
| 363 | + |
| 364 | + @mock.patch('enterprise_access.apps.customer_billing.tasks.BrazeApiClient') |
| 365 | + @mock.patch('enterprise_access.apps.customer_billing.tasks.LmsApiClient') |
| 366 | + def test_payment_receipt_no_admin_users(self, mock_lms_client, mock_braze_client): |
| 367 | + """ |
| 368 | + Test that email is not sent when no admin users are found. |
| 369 | + """ |
| 370 | + mock_lms_client.return_value.get_enterprise_customer_data.return_value = { |
| 371 | + 'admin_users': [] |
| 372 | + } |
| 373 | + |
| 374 | + send_payment_receipt_email( |
| 375 | + invoice_data=self.mock_invoice_data, |
| 376 | + subscription_data=self.mock_subscription_data, |
| 377 | + enterprise_customer_name=self.enterprise_customer_name, |
| 378 | + enterprise_slug=self.enterprise_slug, |
| 379 | + ) |
| 380 | + |
| 381 | + # Verify LMS API was called but Braze API was not |
| 382 | + mock_lms_client.return_value.get_enterprise_customer_data.assert_called_once() |
| 383 | + mock_braze_client.return_value.send_campaign_message.assert_not_called() |
| 384 | + |
| 385 | + @mock.patch('enterprise_access.apps.customer_billing.tasks.BrazeApiClient') |
| 386 | + @mock.patch('enterprise_access.apps.customer_billing.tasks.LmsApiClient') |
| 387 | + def test_payment_receipt_braze_recipient_error(self, mock_lms_client, mock_braze_client): |
| 388 | + """ |
| 389 | + Test handling of Braze recipient creation errors. |
| 390 | + """ |
| 391 | + mock_lms_client.return_value.get_enterprise_customer_data.return_value = { |
| 392 | + 'admin_users': self.mock_admin_users |
| 393 | + } |
| 394 | + |
| 395 | + # Make first recipient creation fail, second one succeed |
| 396 | + mock_braze = mock_braze_client.return_value |
| 397 | + mock_braze.create_braze_recipient.side_effect = [ |
| 398 | + Exception("Failed to create recipient"), |
| 399 | + {'external_id': 'braze_2'} |
| 400 | + ] |
| 401 | + |
| 402 | + send_payment_receipt_email( |
| 403 | + invoice_data=self.mock_invoice_data, |
| 404 | + subscription_data=self.mock_subscription_data, |
| 405 | + enterprise_customer_name=self.enterprise_customer_name, |
| 406 | + enterprise_slug=self.enterprise_slug, |
| 407 | + ) |
| 408 | + |
| 409 | + # Verify campaign was still sent for the successful recipient |
| 410 | + mock_braze.send_campaign_message.assert_called_once() |
| 411 | + actual_recipients = mock_braze.send_campaign_message.call_args[1]['recipients'] |
| 412 | + self.assertEqual(len(actual_recipients), 1) |
| 413 | + self.assertEqual(actual_recipients[0]['external_id'], 'braze_2') |
0 commit comments