Skip to content

Commit

Permalink
feat: return serialized subs license in license-activation POST (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz authored Oct 17, 2024
1 parent 53bf5cf commit 5f7ba4f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion license_manager/apps/api/v1/tests/test_api_eventing.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def test_activate_an_assigned_license(self, _):
url = reverse('api:v1:license-activation') + '/?' + query_params.urlencode()
response = self.api_client.post(url)

assert status.HTTP_204_NO_CONTENT == response.status_code
assert status.HTTP_200_OK == response.status_code
license_to_be_activated.refresh_from_db()

assert mock_activated_track_event.call_count == 1
Expand Down
44 changes: 38 additions & 6 deletions license_manager/apps/api/v1/tests/test_license_activation_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,18 @@ def test_activate_an_assigned_license(self, mock_send_post_activation_email_task
with freeze_time(self.now):
response = self._post_request(str(self.activation_key))

assert status.HTTP_204_NO_CONTENT == response.status_code
# Verify that the response contains activated subscription license.
assert status.HTTP_200_OK == response.status_code
activated_license = response.json()
assert activated_license['uuid'] == str(license_to_be_activated.uuid)
assert activated_license['status'] == constants.ACTIVATED
expected_activation_date = self.now.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
assert activated_license['activation_date'] == expected_activation_date

# Refresh license from the database
license_to_be_activated.refresh_from_db()

# Verify that the license has been activated in the DB
assert constants.ACTIVATED == license_to_be_activated.status
assert self.lms_user_id == license_to_be_activated.lms_user_id
assert self.now == license_to_be_activated.activation_date
Expand All @@ -159,7 +169,7 @@ def test_activate_an_assigned_license(self, mock_send_post_activation_email_task
self.user.email,
)

def test_license_already_activated_returns_204(self):
def test_license_already_activated_returns_200(self):
self._assign_learner_roles(
jwt_payload_extra={
'user_id': self.lms_user_id,
Expand All @@ -174,7 +184,13 @@ def test_license_already_activated_returns_204(self):

response = self._post_request(str(self.activation_key))

assert status.HTTP_204_NO_CONTENT == response.status_code
assert status.HTTP_200_OK == response.status_code
activated_license = response.json()
assert activated_license['uuid'] == str(already_activated_license.uuid)
assert activated_license['status'] == constants.ACTIVATED
expected_activation_date = self.now.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
assert activated_license['activation_date'] == expected_activation_date

already_activated_license.refresh_from_db()
assert constants.ACTIVATED == already_activated_license.status
assert self.lms_user_id == already_activated_license.lms_user_id
Expand Down Expand Up @@ -217,7 +233,12 @@ def test_duplicate_licenses_are_cleaned_up(self, mock_license_clean, mock_email_
with freeze_time(self.now):
response = self._post_request(str(self.activation_key))

assert status.HTTP_204_NO_CONTENT == response.status_code
assert status.HTTP_200_OK == response.status_code
activated_license = response.json()
assert activated_license['uuid'] == str(license_b.uuid)
assert activated_license['status'] == constants.ACTIVATED
expected_activation_date = self.now.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
assert activated_license['activation_date'] == expected_activation_date

license_b.refresh_from_db()
assert constants.ACTIVATED == license_b.status
Expand Down Expand Up @@ -268,7 +289,12 @@ def test_activated_license_exists_with_duplicates(self, mock_license_clean, mock
with freeze_time(self.now):
response = self._post_request(str(license_a_activation_key))

assert status.HTTP_204_NO_CONTENT == response.status_code
assert status.HTTP_200_OK == response.status_code
activated_license = response.json()
assert activated_license['uuid'] == str(license_b.uuid)
assert activated_license['status'] == constants.ACTIVATED
expected_activation_date = self.now.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
assert activated_license['activation_date'] == expected_activation_date

license_b.refresh_from_db()
assert constants.ACTIVATED == license_b.status
Expand Down Expand Up @@ -341,7 +367,13 @@ def test_activating_renewed_assigned_license(self, mock_send_post_activation_ema
with freeze_time(self.now):
response = self._post_request(str(self.activation_key))

assert status.HTTP_204_NO_CONTENT == response.status_code
assert status.HTTP_200_OK == response.status_code
activated_license = response.json()
assert activated_license['uuid'] == str(current_assigned_license.uuid)
assert activated_license['status'] == constants.ACTIVATED
expected_activation_date = self.now.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
assert activated_license['activation_date'] == expected_activation_date

current_assigned_license.refresh_from_db()
prior_assigned_license.refresh_from_db()
assert prior_assigned_license.activation_date != self.now
Expand Down
7 changes: 4 additions & 3 deletions license_manager/apps/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,10 +1779,10 @@ def post(self, request):
license's subscription plan.
* 404 Not Found - if the email found in the request's JWT and the provided ``activation_key``
do not match those of any existing license in an activate subscription plan.
* 204 No Content - if such a license was found, and if the license is currently ``assigned``,
* 200 OK - if such a license was found, and if the license is currently ``assigned``,
it is updated with a status of ``activated``, its ``activation_date`` is set, and its ``lms_user_id``
is updated to the value found in the request's JWT. If the license is already ``activated``,
no update is made to it.
no update is made to it. The activated license is then returned in the response.
* 422 Unprocessable Entity - if we find a license, but it's status is not currently ``assigned``
or ``activated``, we do nothing and return a 422 with a message indicating that the license
cannot be activated.
Expand All @@ -1801,7 +1801,8 @@ def post(self, request):

# There's an implied logical branch where the license is already activated
# in which case we also return as if the activation action was successful.
return Response(status=status.HTTP_204_NO_CONTENT)
serialized_license = serializers.LicenseSerializer(user_license)
return Response(serialized_license.data, status=status.HTTP_200_OK)

def _track_and_notify(self, user_license):
"""
Expand Down

0 comments on commit 5f7ba4f

Please sign in to comment.