Skip to content

Commit

Permalink
test: cover the new endpoing with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0x29a committed Nov 14, 2023
1 parent 033cbee commit 4d582c2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions enterprise/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def algolia_key(self, request, *args, **kwargs):
"""

if not settings.ENTERPRISE_ALGOLIA_SEARCH_API_KEY:
raise ValidationError("Algolia API key not configured.", code=HTTP_500_INTERNAL_SERVER_ERROR)
return Response("Algolia API key not configured", status=status.HTTP_500_INTERNAL_SERVER_ERROR)

queryset = self.queryset.filter(
**{
Expand All @@ -544,7 +544,7 @@ def algolia_key(self, request, *args, **kwargs):
).values_list("uuid", flat=True)

if len(queryset) == 0:
raise ValidationError("User is not linked to any enterprise customers.", code=HTTP_404_NOT_FOUND)
raise NotFound("User is not linked to any enterprise customers.")

secured_key = SearchClient.generate_secured_api_key(
settings.ENTERPRISE_ALGOLIA_SEARCH_API_KEY,
Expand Down
2 changes: 2 additions & 0 deletions enterprise/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def root(*args):
'status': 'published'
}

ENTERPRISE_ALGOLIA_SEARCH_API_KEY = 'test'

SNOWFLAKE_SERVICE_USER = 'TEST@EDX.ORG'
SNOWFLAKE_SERVICE_USER_PASSWORD = 'secret'

Expand Down
4 changes: 4 additions & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ alabaster==0.7.12
# via
# -r requirements/doc.txt
# sphinx
algoliasearch==2.6.3
# This has been copied from here: https://github.com/openedx/edx-platform/blob/176d0d885a4e182ff8c9607765891e78d459a5b8/requirements/constraints.txt#L91
# New versions of edx-enterprise has this requirement copied from edx-platform to `requirements/edx-platform-constraints.txt`,
# so we can remove this once we upgrade edx-enterprise. Normally, Palm already has this package installed, but not in test environment.
amqp==5.1.1
# via
# -r requirements/doc.txt
Expand Down
4 changes: 4 additions & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ aniso8601==9.0.1
# via
# -r requirements/test-master.txt
# edx-tincan-py35
algoliasearch==2.6.3
# This has been copied from here: https://github.com/openedx/edx-platform/blob/176d0d885a4e182ff8c9607765891e78d459a5b8/requirements/constraints.txt#L91
# New versions of edx-enterprise has this requirement copied from edx-platform to `requirements/edx-platform-constraints.txt`,
# so we can remove this once we upgrade edx-enterprise. Normally, Palm already has this package installed, but not in test environment.
asgiref==3.5.2
# via
# -r requirements/test-master.txt
Expand Down
42 changes: 42 additions & 0 deletions tests/test_enterprise/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests for the `edx-enterprise` api module.
"""

import base64
import json
import uuid
from datetime import datetime, timedelta
Expand Down Expand Up @@ -113,6 +114,7 @@
ENTERPRISE_LEARNER_LIST_ENDPOINT = reverse('enterprise-learner-list')
ENTERPRISE_CUSTOMER_WITH_ACCESS_TO_ENDPOINT = reverse('enterprise-customer-with-access-to')
ENTERPRISE_CUSTOMER_UNLINK_USERS_ENDPOINT = reverse('enterprise-customer-unlink-users', kwargs={'pk': FAKE_UUIDS[0]})
ENTERPRISE_CUSTOMER_ALGOLIA_KEY_ENDPOINT = reverse('enterprise-customer-algolia-key')
PENDING_ENTERPRISE_LEARNER_LIST_ENDPOINT = reverse('pending-enterprise-learner-list')
LICENSED_ENTERPISE_COURSE_ENROLLMENTS_REVOKE_ENDPOINT = reverse(
'licensed-enterprise-course-enrollment-license-revoke'
Expand Down Expand Up @@ -1670,6 +1672,46 @@ def test_unlink_users(self, enterprise_role, enterprise_uuid_for_role, is_relink
assert enterprise_customer_user_2.is_relinkable == is_relinkable
assert enterprise_customer_user_2.is_relinkable == is_relinkable

def test_algolia_key(self):
"""
Tests that the endpoint algolia_key endpoint returns the correct secured key.
"""

username = 'test_learner_portal_user'
self.create_user(username=username, is_staff=False)
self.client.login(username=username, password=TEST_PASSWORD)

# Test that the endpoint returns 500 if the Algolia Search API key is not set.
with override_settings(ENTERPRISE_ALGOLIA_SEARCH_API_KEY=None):
response = self.client.get(ENTERPRISE_CUSTOMER_ALGOLIA_KEY_ENDPOINT)
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR

# Test that the endpoint returns 404 if the user is not linked to any enterprise.
response = self.client.get(ENTERPRISE_CUSTOMER_ALGOLIA_KEY_ENDPOINT)
assert response.status_code == status.HTTP_404_NOT_FOUND

# Test that the endpoint returns 200 if the user is linked to at least one enterprise.
enterprise_customer_1 = factories.EnterpriseCustomerFactory(uuid=FAKE_UUIDS[0])
enterprise_customer_2 = factories.EnterpriseCustomerFactory(uuid=FAKE_UUIDS[1])

factories.EnterpriseCustomerUserFactory(
user_id=self.user.id,
enterprise_customer=enterprise_customer_1
)
factories.EnterpriseCustomerUserFactory(
user_id=self.user.id,
enterprise_customer=enterprise_customer_2
)

response = self.client.get(ENTERPRISE_CUSTOMER_ALGOLIA_KEY_ENDPOINT)
assert response.status_code == status.HTTP_200_OK

# Test that the endpoint returns the key encoding correct filters.
decoded_key = base64.b64decode(response.json()["key"]).decode("utf-8")
assert decoded_key.endswith(
f"filters=enterprise_customer_uuids%3A{FAKE_UUIDS[0]}+OR+enterprise_customer_uuids%3A{FAKE_UUIDS[1]}"
)


@ddt.ddt
@mark.django_db
Expand Down

0 comments on commit 4d582c2

Please sign in to comment.