Skip to content

Commit

Permalink
Improve GetValue API (#69)
Browse files Browse the repository at this point in the history
* fix: inaccissible info in scorm_get_value

* chore: code refacorting and adds a test for user info

* chore: update test instructions in readme
  • Loading branch information
ahmed-arb authored Apr 29, 2024
1 parent 6401bba commit 1c2f6da
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 66 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Development
Run unit tests with::
$ NO_PREREQ_INSTALL=1 paver test_system -s lms -t openedxscorm
$ pytest /mnt/openedx-scorm-xblock/openedxscorm/tests.py
Troubleshooting
---------------
Expand Down
10 changes: 10 additions & 0 deletions openedxscorm/scormxblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ def get_current_user_attr(self, attr: str):
def get_current_user(self):
return self.runtime.service(self, "user").get_current_user()

def initialize_student_info(self):
user_id = self.get_current_user_attr("edx-platform.user_id")
username = self.get_current_user_attr("edx-platform.username")

self.scorm_data["cmi.core.student_id"] = user_id
self.scorm_data["cmi.learner_id"] = user_id
self.scorm_data["cmi.learner_name"] = username
self.scorm_data["cmi.core.student_name"] = username

@staticmethod
def resource_string(path):
"""Handy helper for getting static resources from our kit."""
Expand All @@ -204,6 +213,7 @@ def student_view(self, context=None):
"popup_on_launch": self.popup_on_launch,
}
student_context.update(context or {})
self.initialize_student_info()
template = self.render_template("static/html/scormxblock.html", student_context)
frag = Fragment(template)
frag.add_css(self.resource_string("static/css/scormxblock.css"))
Expand Down
1 change: 1 addition & 0 deletions openedxscorm/static/js/src/scormxblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ function ScormXBlock(runtime, element, settings) {
"cmi.success_status",
"cmi.core.score.raw",
"cmi.score.raw",
"cmi.score.scaled",
"cmi.mode"
];
var getValueUrl = runtime.handlerUrl(element, 'scorm_get_value');
Expand Down
76 changes: 11 additions & 65 deletions openedxscorm/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest


from ddt import ddt, data, unpack
from ddt import ddt, data
from freezegun import freeze_time
import mock
from xblock.field_data import DictFieldData
Expand Down Expand Up @@ -255,68 +255,14 @@ def test_scorm_get_lesson_score(self, value):

self.assertEqual(response.json, {"value": 20})

@data(
{"name": "cmi.core.lesson_location"},
{"name": "cmi.location"},
{"name": "cmi.suspend_data"},
)
def test_get_other_scorm_values(self, value):
block = self.make_one(
scorm_data={
"cmi.core.lesson_location": 1,
"cmi.location": 2,
"cmi.suspend_data": [1, 2],
}
)

response = block.scorm_get_value(
mock.Mock(method="POST", body=json.dumps(value))
)

self.assertEqual(response.json, {"value": block.scorm_data[value["name"]]})

@data(
({"name": "cmi.core.student_id"}, "edx-platform.user_id", 23),
({"name": "cmi.core.student_name"}, "edx-platform.username", "supername"),
)
@unpack
def test_scorm_12_get_student_data(self, request_data, key, value):
service_user_mock = mock.Mock()
current_user_mock = mock.Mock()
current_user_mock.opt_attrs = {key: value}
service_user_mock.configure_mock(
**{"get_current_user.return_value": current_user_mock}
)

runtime = mock.Mock()
runtime.service.return_value = service_user_mock

block = self.make_one(runtime=runtime)

response = block.scorm_get_value(
mock.Mock(method="POST", body=json.dumps(request_data))
)
self.assertEqual(response.json, {"value": value})

@data(
({"name": "cmi.learner_id"}, "edx-platform.user_id", 23),
({"name": "cmi.learner_name"}, "edx-platform.username", "supername"),
)
@unpack
def test_scorm_2004_get_student_data(self, request_data, key, value):
service_user_mock = mock.Mock()
current_user_mock = mock.Mock()
current_user_mock.opt_attrs = {key: value}
service_user_mock.configure_mock(
**{"get_current_user.return_value": current_user_mock}
)

runtime = mock.Mock()
runtime.service.return_value = service_user_mock

block = self.make_one(runtime=runtime)
def test_scorm_data_has_user_info_in_student_view(self):
block = self.make_one()

response = block.scorm_get_value(
mock.Mock(method="POST", body=json.dumps(request_data))
)
self.assertEqual(response.json, {"value": value})
block.student_view()
student_info_keys = [
"cmi.core.student_id",
"cmi.learner_id",
"cmi.learner_name",
"cmi.core.student_name",
]
self.assertTrue(key in block.scorm_data for key in student_info_keys)

0 comments on commit 1c2f6da

Please sign in to comment.