Skip to content

Commit

Permalink
feat: unlink degreed2 inactive user
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzawaleed01 committed Nov 6, 2023
1 parent 43c2e12 commit 939ee8a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
43 changes: 35 additions & 8 deletions integrated_channels/degreed2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from integrated_channels.exceptions import ClientError
from integrated_channels.integrated_channel.client import IntegratedChannelApiClient
from integrated_channels.utils import generate_formatted_log, refresh_session_if_expired
from enterprise.models import EnterpriseCustomerUser

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -125,11 +126,11 @@ def create_course_completion(self, user_id, payload):
f'payload:{json_payload}'
)
)
LOGGER.info(self.make_log_msg(
json_payload.get('data').get('attributes').get('content-id'),
f'Attempting find course via url: {self.get_completions_url()}'),
user_id
)
# LOGGER.info(self.make_log_msg(
# json_payload.get('data').get('attributes').get('content-id'),
# f'Attempting find course via url: {self.get_completions_url()}'),
# user_id
# )
code, body = self._post(
self.get_completions_url(),
json_payload,
Expand All @@ -139,9 +140,35 @@ def create_course_completion(self, user_id, payload):
error_response = json.loads(body)
for error in error_response['errors']:
if 'detail' in error and 'Invalid user identifier' in error['detail']:
raise ClientError(f'Degreed2 create_course_completion failed due to '
f'deleted user: {body}, code:{code}'
)
try:
enterprise_customer = self.enterprise_configuration.enterprise_customer
LOGGER.info(
generate_formatted_log(
self.enterprise_configuration.channel_code(),
self.enterprise_configuration.enterprise_customer.uuid,
None,
None,
f'User {user_id} was deleted on degreed side,'
f"so marking it as inactive and unlinking from enterprise"
)
)
# Unlink user email from related Enterprise Customer
EnterpriseCustomerUser.objects.unlink_user(
enterprise_customer=enterprise_customer,
user_email=json_payload.get('data').get('attributes').get('user-id'),
)

except Exception as e:
LOGGER.error(
generate_formatted_log(
self.enterprise_configuration.channel_code(),
self.enterprise_configuration.enterprise_customer.uuid,
None,
None,
f'Error occurred while unlinking a degreed2 learner: {user_id}. '
f'Payload: {json_payload}, Error: {e}'
)
)
return code, body

def delete_course_completion(self, user_id, payload):
Expand Down
17 changes: 11 additions & 6 deletions tests/test_integrated_channels/test_degreed2/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from integrated_channels.degreed2.client import Degreed2APIClient
from integrated_channels.exceptions import ClientError
from test_utils import factories
from enterprise.models import (
EnterpriseCustomerUser,
)

NOW = datetime.datetime(2017, 1, 2, 3, 4, 5, tzinfo=datetime.timezone.utc)
NOW_TIMESTAMP_FORMATTED = NOW.strftime('%F')
Expand Down Expand Up @@ -158,6 +161,7 @@ def test_create_course_completion(self):
def test_create_course_completion_for_deleted_user(self):
"""
``create_course_completion`` should handle exception for deleted users gracefully
by unlinking that user from enterprise
"""
degreed_api_client = Degreed2APIClient(self.enterprise_config)
responses.add(
Expand Down Expand Up @@ -186,12 +190,13 @@ def test_create_course_completion_for_deleted_user(self):
}
}
}

with pytest.raises(ClientError, match="Degreed2 create_course_completion failed due to deleted user:"):
degreed_api_client.create_course_completion('[email protected]', json.dumps(payload))
assert len(responses.calls) == 2
assert responses.calls[0].request.url == degreed_api_client.get_oauth_url()
assert responses.calls[1].request.url == degreed_api_client.get_completions_url()
email = payload.get("data").get("attributes").get("user-id")
with mock.patch.object(EnterpriseCustomerUser.objects, 'unlink_user') as unlink_user_mock:
degreed_api_client.create_course_completion(email, json.dumps(payload))
unlink_user_mock.assert_called_once()
assert len(responses.calls) == 2
assert responses.calls[0].request.url == degreed_api_client.get_oauth_url()
assert responses.calls[1].request.url == degreed_api_client.get_completions_url()

@responses.activate
def test_delete_course_completion(self):
Expand Down

0 comments on commit 939ee8a

Please sign in to comment.