|
| 1 | +""" |
| 2 | +Tests for the Checkout BFF ViewSet. |
| 3 | +""" |
| 4 | +import uuid |
| 5 | + |
| 6 | +from django.urls import reverse |
| 7 | +from rest_framework import status |
| 8 | + |
| 9 | +from enterprise_access.apps.api.serializers.checkout_bff import ( |
| 10 | + CheckoutContextResponseSerializer, |
| 11 | + EnterpriseCustomerSerializer, |
| 12 | + PriceSerializer |
| 13 | +) |
| 14 | +from enterprise_access.apps.core.constants import SYSTEM_ENTERPRISE_LEARNER_ROLE |
| 15 | +from test_utils import APITest |
| 16 | + |
| 17 | + |
| 18 | +class CheckoutBFFViewSetTests(APITest): |
| 19 | + """ |
| 20 | + Tests for the Checkout BFF ViewSet. |
| 21 | + """ |
| 22 | + |
| 23 | + def setUp(self): |
| 24 | + super().setUp() |
| 25 | + self.url = reverse('api:v1:checkout-bff-context') |
| 26 | + |
| 27 | + def test_context_endpoint_unauthenticated_access(self): |
| 28 | + """ |
| 29 | + Test that unauthenticated users can access the context endpoint. |
| 30 | + """ |
| 31 | + response = self.client.post(self.url, {}, format='json') |
| 32 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 33 | + |
| 34 | + # Verify response structure matches our expectations |
| 35 | + self.assertIn('existing_customers_for_authenticated_user', response.data) |
| 36 | + self.assertIn('pricing', response.data) |
| 37 | + self.assertIn('field_constraints', response.data) |
| 38 | + |
| 39 | + # For unauthenticated users, existing_customers should be empty |
| 40 | + self.assertEqual(len(response.data['existing_customers_for_authenticated_user']), 0) |
| 41 | + |
| 42 | + def test_context_endpoint_authenticated_access(self): |
| 43 | + """ |
| 44 | + Test that authenticated users can access the context endpoint. |
| 45 | + """ |
| 46 | + self.set_jwt_cookie([{ |
| 47 | + 'system_wide_role': SYSTEM_ENTERPRISE_LEARNER_ROLE, |
| 48 | + 'context': str(uuid.uuid4()), |
| 49 | + }]) |
| 50 | + |
| 51 | + response = self.client.post(self.url, {}, format='json') |
| 52 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 53 | + |
| 54 | + # Verify response structure matches our expectations |
| 55 | + self.assertIn('existing_customers_for_authenticated_user', response.data) |
| 56 | + self.assertIn('pricing', response.data) |
| 57 | + self.assertIn('field_constraints', response.data) |
| 58 | + |
| 59 | + def test_response_serializer_validation(self): |
| 60 | + """ |
| 61 | + Test that our response serializer validates the expected response structure. |
| 62 | + """ |
| 63 | + # Create sample data matching our expected response structure |
| 64 | + sample_data = { |
| 65 | + 'existing_customers_for_authenticated_user': [], |
| 66 | + 'pricing': { |
| 67 | + 'default_by_lookup_key': 'b2b_enterprise_self_service_yearly', |
| 68 | + 'prices': [] |
| 69 | + }, |
| 70 | + 'field_constraints': { |
| 71 | + 'quantity': {'min': 5, 'max': 30}, |
| 72 | + 'enterprise_slug': { |
| 73 | + 'min_length': 3, |
| 74 | + 'max_length': 30, |
| 75 | + 'pattern': '^[a-z0-9-]+$' |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + # Validate using our serializer |
| 81 | + serializer = CheckoutContextResponseSerializer(data=sample_data) |
| 82 | + self.assertTrue(serializer.is_valid(), serializer.errors) |
| 83 | + |
| 84 | + def test_enterprise_customer_serializer(self): |
| 85 | + """ |
| 86 | + Test that EnterpriseCustomerSerializer correctly validates data. |
| 87 | + """ |
| 88 | + sample_data = { |
| 89 | + 'customer_uuid': 'abc123', |
| 90 | + 'customer_name': 'Test Enterprise', |
| 91 | + 'customer_slug': 'test-enterprise', |
| 92 | + 'stripe_customer_id': 'cus_123ABC', |
| 93 | + 'is_self_service': True, |
| 94 | + 'admin_portal_url': 'https://example.com/enterprise/test-enterprise' |
| 95 | + } |
| 96 | + |
| 97 | + serializer = EnterpriseCustomerSerializer(data=sample_data) |
| 98 | + self.assertTrue(serializer.is_valid(), serializer.errors) |
| 99 | + |
| 100 | + def test_price_serializer(self): |
| 101 | + """ |
| 102 | + Test that PriceSerializer correctly validates data. |
| 103 | + """ |
| 104 | + sample_data = { |
| 105 | + 'id': 'price_123ABC', |
| 106 | + 'product': 'prod_123ABC', |
| 107 | + 'lookup_key': 'b2b_enterprise_self_service_yearly', |
| 108 | + 'recurring': { |
| 109 | + 'interval': 'month', |
| 110 | + 'interval_count': 12, |
| 111 | + 'trial_period_days': 14, |
| 112 | + }, |
| 113 | + 'currency': 'usd', |
| 114 | + 'unit_amount': 10000, |
| 115 | + 'unit_amount_decimal': '10000' |
| 116 | + } |
| 117 | + |
| 118 | + serializer = PriceSerializer(data=sample_data) |
| 119 | + self.assertTrue(serializer.is_valid(), serializer.errors) |
0 commit comments