-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: [ACI-994] increase test coverage
- Loading branch information
Andrii
committed
May 30, 2024
1 parent
8613e01
commit 61c8082
Showing
6 changed files
with
616 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
from unittest import mock | ||
|
||
from attrs import asdict | ||
from django.test import TestCase | ||
from faker import Faker | ||
from openedx_events.learning.data import BadgeData, BadgeTemplateData, UserData, UserPersonalData | ||
from requests.models import Response | ||
|
||
from credentials.apps.badges.credly.api_client import CredlyAPIClient | ||
from credentials.apps.badges.credly.exceptions import CredlyAPIError, CredlyError | ||
from credentials.apps.badges.models import CredlyOrganization | ||
|
||
|
||
class CredlyApiClientTestCase(TestCase): | ||
def setUp(self): | ||
fake = Faker() | ||
self.api_client = CredlyAPIClient("test_organization_id", "test_api_key") | ||
self.badge_data = BadgeData( | ||
uuid=fake.uuid4(), | ||
user=UserData( | ||
id=1, | ||
is_active=True, | ||
pii=UserPersonalData( | ||
username="test_user", email="[email protected]", name="Test User" | ||
), | ||
), | ||
template=BadgeTemplateData( | ||
uuid=fake.uuid4(), | ||
name="Test Badge", | ||
origin="Credly", | ||
description="Test Badge Description", | ||
image_url="https://test.com/image.png", | ||
), | ||
) | ||
|
||
def test_get_organization_nonexistent(self): | ||
with mock.patch( | ||
"credentials.apps.badges.credly.api_client.CredlyOrganization.objects.get" | ||
) as mock_get: | ||
mock_get.side_effect = CredlyOrganization.DoesNotExist | ||
with self.assertRaises(CredlyError) as cm: | ||
self.api_client._get_organization("nonexistent_organization_id") | ||
self.assertEqual( | ||
str(cm.exception), | ||
"CredlyOrganization with the uuid nonexistent_organization_id does not exist!", | ||
) | ||
|
||
def test_perform_request(self): | ||
with mock.patch( | ||
"credentials.apps.badges.credly.api_client.requests.request" | ||
) as mock_request: | ||
mock_response = mock.Mock() | ||
mock_response.json.return_value = {"key": "value"} | ||
mock_request.return_value = mock_response | ||
result = self.api_client.perform_request("GET", "/api/endpoint") | ||
mock_request.assert_called_once_with( | ||
"GET", | ||
"https://sandbox-api.credly.com/api/endpoint", | ||
headers=self.api_client._get_headers(), | ||
json=None, | ||
) | ||
self.assertEqual(result, {"key": "value"}) | ||
|
||
def test_raise_for_error_success(self): | ||
response = mock.Mock(spec=Response) | ||
response.status_code = 200 | ||
self.api_client._raise_for_error(response) | ||
|
||
def test_raise_for_error_error(self): | ||
response = mock.Mock(spec=Response) | ||
response.status_code = 404 | ||
response.text = "Not Found" | ||
response.raise_for_status.side_effect = CredlyAPIError( | ||
f"Credly API: {response.text} ({response.status_code})" | ||
) | ||
|
||
with self.assertRaises(CredlyAPIError) as cm: | ||
self.api_client._raise_for_error(response) | ||
self.assertEqual(str(cm.exception), "Credly API: Not Found (404)") | ||
|
||
def test_fetch_organization(self): | ||
with mock.patch.object( | ||
CredlyAPIClient, "perform_request" | ||
) as mock_perform_request: | ||
mock_perform_request.return_value = {"organization": "data"} | ||
result = self.api_client.fetch_organization() | ||
mock_perform_request.assert_called_once_with("get", "") | ||
self.assertEqual(result, {"organization": "data"}) | ||
|
||
def test_fetch_badge_templates(self): | ||
with mock.patch.object( | ||
CredlyAPIClient, "perform_request" | ||
) as mock_perform_request: | ||
mock_perform_request.return_value = { | ||
"badge_templates": ["template1", "template2"] | ||
} | ||
result = self.api_client.fetch_badge_templates() | ||
mock_perform_request.assert_called_once_with( | ||
"get", "badge_templates/?filter=state::active" | ||
) | ||
self.assertEqual(result, {"badge_templates": ["template1", "template2"]}) | ||
|
||
def test_fetch_event_information(self): | ||
event_id = "event123" | ||
with mock.patch.object( | ||
CredlyAPIClient, "perform_request" | ||
) as mock_perform_request: | ||
mock_perform_request.return_value = {"event": "data"} | ||
result = self.api_client.fetch_event_information(event_id) | ||
mock_perform_request.assert_called_once_with("get", f"events/{event_id}/") | ||
self.assertEqual(result, {"event": "data"}) | ||
|
||
def test_issue_badge(self): | ||
issue_badge_data = self.badge_data | ||
with mock.patch.object( | ||
CredlyAPIClient, "perform_request" | ||
) as mock_perform_request: | ||
mock_perform_request.return_value = {"badge": "issued"} | ||
result = self.api_client.issue_badge(issue_badge_data) | ||
mock_perform_request.assert_called_once_with( | ||
"post", "badges/", asdict(issue_badge_data) | ||
) | ||
self.assertEqual(result, {"badge": "issued"}) | ||
|
||
def test_revoke_badge(self): | ||
badge_id = "badge123" | ||
data = {"data": "value"} | ||
with mock.patch.object( | ||
CredlyAPIClient, "perform_request" | ||
) as mock_perform_request: | ||
mock_perform_request.return_value = {"badge": "revoked"} | ||
result = self.api_client.revoke_badge(badge_id, data) | ||
mock_perform_request.assert_called_once_with( | ||
"put", f"badges/{badge_id}/revoke/", data=data | ||
) | ||
self.assertEqual(result, {"badge": "revoked"}) |
Oops, something went wrong.