Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: REST API to return a different response if LR MFE is enabled #1747

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion credentials/apps/records/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ def test_login_required(self):
self.assertTrue(response.url.startswith("/login/?next="))

def test_user_creation(self):
"""Verify successful creation of a ProgramCertRecord and return of a uuid"""
"""Verify successful creation of a ProgramCertRecord and return of a URL with the uuid for the public record"""
MaxFrank13 marked this conversation as resolved.
Show resolved Hide resolved
rev = reverse("records:share_program", kwargs={"uuid": self.program.uuid.hex})
data = {"username": self.USERNAME}
jdata = json.dumps(data).encode("utf-8")
Expand All @@ -963,6 +963,22 @@ def test_user_creation(self):
self.assertEqual(response.status_code, 201)
self.assertRegex(json_data["url"], UUID_PATTERN)

@override_settings(USE_LEARNER_RECORD_MFE=True)
@override_settings(LEARNER_RECORD_MFE_RECORDS_PAGE_URL="http://some.website/page/")
def test_user_creation_with_MFE(self):
"""
Verify successful creation of a ProgramCertRecord and return of a URL with the uuid for the public record
(Learner Record MFE)
"""
rev = reverse("records:share_program", kwargs={"uuid": self.program.uuid.hex})
data = {"username": self.USERNAME}
jdata = json.dumps(data).encode("utf-8")
response = self.client.post(rev, data=jdata, content_type=JSON_CONTENT_TYPE)
json_data = response.json()

self.assertEqual(response.status_code, 201)
self.assertRegex(json_data["url"], rf"http://some.website/page/shared/{UUID_PATTERN}")

def test_different_user_creation(self):
"""Verify that the view rejects a User attempting to create a ProgramCertRecord for another"""
diff_username = "diff-user"
Expand Down
16 changes: 14 additions & 2 deletions credentials/apps/records/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,15 @@ def post(self, request, **kwargs):
@method_decorator(ratelimit(key="user", rate=RECORDS_RATE_LIMIT, method="POST", block=True), name="dispatch")
class ProgramRecordCreationView(LoginRequiredMixin, RecordsEnabledMixin, View):
"""
Creates a new Program Certificate Record from given username and program uuid,
returns the uuid of the created Program Certificate Record
Creates a new Program Certificate Record from given username and program uuid

POST: /programs/:program_uuid/share/

Returns:
Dict: Dictionary containing the URL for the program certificate record instance

If Learner Record MFE is enabled, the URL will route there instead.
Make sure to include a base URL for the MFE in the `LEARNER_RECORD_MFE_RECORDS_PAGE_URL` environment variable
"""

def post(self, request, **kwargs):
Expand All @@ -289,7 +296,12 @@ def post(self, request, **kwargs):
pcr, created = ProgramCertRecord.objects.get_or_create(user=user, program=program)
status_code = 201 if created else 200

if settings.USE_LEARNER_RECORD_MFE:
response = {"url": f"{settings.LEARNER_RECORD_MFE_RECORDS_PAGE_URL}shared/{pcr.uuid.hex}"}
return JsonResponse(response, status=status_code)

url = request.build_absolute_uri(reverse("records:public_programs", kwargs={"uuid": pcr.uuid.hex}))

return JsonResponse({"url": url}, status=status_code)


Expand Down