Skip to content

Commit

Permalink
Merge pull request #660 from openedx/ENT-9024/subscriptions-list-endp…
Browse files Browse the repository at this point in the history
…oint

feat: add list endpoint for SubscriptionPlan
  • Loading branch information
hamzawaleed01 committed Jun 13, 2024
2 parents 0b435bd + b521336 commit b576cef
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 3 deletions.
113 changes: 113 additions & 0 deletions license_manager/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ def _provision_license_create_request(api_client, user, params):
return api_client.post(url, params)


def _provision_license_list_request(api_client, user, enterprise_customer_uuid=None):
"""
Helper method that creates a SubscriptionPlan.
"""
if user:
api_client.force_authenticate(user=user)

url = '/api/v1/provisioning-admins/subscriptions'
if enterprise_customer_uuid:
url += f'?enterprise_customer_uuid={enterprise_customer_uuid}'
return api_client.get(url)


def _subscription_get_request(api_client, user, subscription_uuid):
"""
Helper method that creates a SubscriptionPlan.
Expand Down Expand Up @@ -939,6 +952,106 @@ def test_subscription_plan_create_staff_user_403(api_client, staff_user, boolean
assert status.HTTP_403_FORBIDDEN == response.status_code


@pytest.mark.django_db
def test_subscription_plan_list_staff_user_403(api_client, staff_user, boolean_toggle):
"""
Verify that the subcription POST endpoint is accessible to authorised users only
"""
enterprise_customer_uuid = uuid4()
ProductFactory.create_batch(1)
_assign_role_via_jwt_or_db(
api_client, staff_user, enterprise_customer_uuid=enterprise_customer_uuid, assign_via_jwt=boolean_toggle)

response = _provision_license_list_request(
api_client, staff_user)
assert status.HTTP_403_FORBIDDEN == response.status_code


@pytest.mark.django_db
def test_subscription_plan_provisioning_admins_list_staff_user_200(api_client, staff_user, boolean_toggle):
"""
Verify that the subcription POST endpoint is accessible to authorised users only
and returns valid data
"""
enterprise_customer_uuid = uuid4()
customer_agreement = CustomerAgreementFactory.create(
enterprise_customer_uuid=enterprise_customer_uuid)
ProductFactory.create_batch(1)
allowed_group = Group.objects.create(name=PROVISIONING_ADMINS_GROUP)
staff_user.groups.add(allowed_group)
_assign_role_via_jwt_or_db(
api_client, staff_user, enterprise_customer_uuid=enterprise_customer_uuid, assign_via_jwt=boolean_toggle)

params = _prepare_subscription_plan_payload(customer_agreement)

_provision_license_create_request(
api_client, staff_user, params=params)

response = _provision_license_list_request(
api_client, staff_user)

expected_keys = {'count', 'next', 'previous', 'results'}
assert expected_keys.issubset(response.json().keys())
expected_result_keys = {
'title', 'uuid', 'start_date', 'expiration_date',
'enterprise_customer_uuid', 'enterprise_catalog_uuid',
'is_active', 'is_revocation_cap_enabled', 'days_until_expiration',
'days_until_expiration_including_renewals',
'is_locked_for_renewal_processing', 'should_auto_apply_licenses',
'licenses', 'revocations', 'prior_renewals'
}
for result in response.json()['results']:
assert expected_result_keys.issubset(result.keys())
assert status.HTTP_200_OK == response.status_code


@pytest.mark.django_db
def test_subscription_plan_pa_list_staff_user_customer_uuid_200(api_client, staff_user, boolean_toggle):
"""
Verify that the subcription POST endpoint applies enterprise_customer_uuid if passed
in query params
"""
# first create a subscription plan against a enterprise customer
# then query it and test that customer uuid that's passed in query params
# and the one received from `list` response are same.
# We'll have to create two subscription plans to confirm it.

enterprise_customer_uuid_one = uuid4()
enterprise_customer_uuid_two = uuid4()
customer_agreement_one = CustomerAgreementFactory.create(
enterprise_customer_uuid=enterprise_customer_uuid_one)
customer_agreement_two = CustomerAgreementFactory.create(
enterprise_customer_uuid=enterprise_customer_uuid_two)
ProductFactory.create_batch(1)
allowed_group = Group.objects.create(name=PROVISIONING_ADMINS_GROUP)
staff_user.groups.add(allowed_group)
_assign_role_via_jwt_or_db(
api_client, staff_user, enterprise_customer_uuid=enterprise_customer_uuid_one, assign_via_jwt=boolean_toggle)

params_one = _prepare_subscription_plan_payload(customer_agreement_one)
params_two = _prepare_subscription_plan_payload(customer_agreement_two)

_provision_license_create_request(
api_client, staff_user, params=params_one)

response_one = _provision_license_list_request(
api_client, staff_user, enterprise_customer_uuid=enterprise_customer_uuid_one)
assert status.HTTP_200_OK == response_one.status_code
assert response_one.json()['results'][0]['enterprise_customer_uuid'] == str(
enterprise_customer_uuid_one)
_assign_role_via_jwt_or_db(
api_client, staff_user, enterprise_customer_uuid=enterprise_customer_uuid_two, assign_via_jwt=boolean_toggle)

_provision_license_create_request(
api_client, staff_user, params=params_two)
response_two = _provision_license_list_request(
api_client, staff_user, enterprise_customer_uuid=enterprise_customer_uuid_two)

assert status.HTTP_200_OK == response_two.status_code
assert response_two.json()['results'][0]['enterprise_customer_uuid'] == str(
enterprise_customer_uuid_two)


@pytest.mark.django_db
def test_subscription_plan_update_staff_user_403(api_client, staff_user, boolean_toggle):
"""
Expand Down
22 changes: 19 additions & 3 deletions license_manager/apps/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,16 @@ def base_queryset(self):
return queryset.order_by('-start_date')


@extend_schema_view(
list=extend_schema(
summary='List all SubscriptionPlans',
description='List all SubscriptionPlans or for a given enterprise_customer_uuid',
parameters=[serializers.SubscriptionPlanQueryParamsSerializer],
),
)
class SubscriptionPlanProvisioningAdminViewset(
mixins.CreateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet
):
""" Viewset for Provisioning Admins write operations."""
Expand All @@ -462,14 +470,22 @@ def get_serializer_class(self):
return serializers.SubscriptionPlanSerializer

def get_queryset(self):
return SubscriptionPlan.objects.filter(
is_active=True
).order_by('-start_date')
"""
Appends enterprise_customer_uuid in queryset if found in query params
"""
model_filters = {'is_active': True}
if self.requested_enterprise_uuid:
model_filters['customer_agreement__enterprise_customer_uuid'] = self.requested_enterprise_uuid
return SubscriptionPlan.objects.filter(**model_filters).order_by('-start_date')

@property
def requested_subscription_uuid(self):
return self.kwargs.get('subscription_uuid')

@property
def requested_enterprise_uuid(self):
return utils.get_requested_enterprise_uuid(self.request)

def create(self, request, *args, **kwargs):
"""
Creates a new SubscriptionPlan record
Expand Down

0 comments on commit b576cef

Please sign in to comment.