Skip to content

Commit

Permalink
Merge pull request #41 from eduNEXT/jlc/control-progress-event-by-set…
Browse files Browse the repository at this point in the history
…tings

Jlc/control progress event by settings
  • Loading branch information
johanseto authored Mar 29, 2023
2 parents f6fc221 + 48b6bd7 commit e2651f5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ quality: clean install-test-reqs## check coding style with pycodestyle and pylin

python-test: clean install-test-reqs## Run test suite.
$(TOX) coverage run --source ./eox_nelp manage.py test
$(TOX) coverage report -m --fail-under=92
$(TOX) coverage report -m --fail-under=93

complexity: clean install-test-reqs## Run complexity suite with flake8.
$(TOX) flake8 --max-complexity 10 eox_nelp
Expand Down
4 changes: 4 additions & 0 deletions eox_nelp/signals/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging

from celery import shared_task
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models import Q
from eox_core.edxapp_wrapper.courseware import get_courseware_courses
Expand Down Expand Up @@ -45,6 +46,9 @@ def dispatch_futurex_progress(course_id, user_id, is_complete=None):
user_id (str): User identifier.
is_complete (bool): Determines is that hast complete the course
"""
if not getattr(settings, "ACTIVATE_DISPATCH_FUTUREX_PROGRESS", False):
return

user = User.objects.get(id=user_id)
user_has_passing_grade = is_complete if is_complete is not None else _user_has_passing_grade(user, course_id)

Expand Down
83 changes: 70 additions & 13 deletions eox_nelp/signals/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"""
import unittest

from ddt import data, ddt
from django.conf import settings
from django.contrib.auth import get_user_model
from django.test import override_settings
from mock import patch
from opaque_keys.edx.keys import CourseKey
from social_django.models import UserSocialAuth
Expand All @@ -21,6 +24,8 @@
)

User = get_user_model()
FALSY_ACTIVATION_VALUES = [0, "", None, [], False, {}, ()]
TRUTHY_ACTIVATION_VALUES = [1, "true", "activated", ["activated"], True, {"activated": "true"}]


class UserHasPassingGradeTestCase(unittest.TestCase):
Expand All @@ -42,23 +47,29 @@ def test_call_user_has_passing_grade(self, course_grade_factory_mock):
course_grade_factory_mock().read.assert_called_with(user, course_key=CourseKey.from_string(course_id))


@ddt
class DipatchFuturexProgressTestCase(unittest.TestCase):
"""Test class for function `dispatch_futurex_progress`"""

@override_settings()
@patch("eox_nelp.signals.tasks._generate_progress_enrollment_data")
@patch("eox_nelp.signals.tasks._post_futurex_progress")
def test_dispatch_futurex_progress(self, post_futurex_progress_mock, generate_progress_enrollment_data_mock):
@data(*TRUTHY_ACTIVATION_VALUES)
def test_call_dispatch_futurex_progress(
self, truthy_value, post_futurex_progress_mock, generate_progress_enrollment_data_mock,
):
"""Test when `dispatch_futurex_progress` is called
with the required parameters. Check the functions inside are called with
their desired values.
their desired values. Also with the setting `ACTIVATE_DISPATCH_FUTUREX_PROGRESS` configurated
with truthy value.
Expected behavior:
- `_generate_progress_enrollment_data` is called with the right values.
- `post_futurex_progress` is called with the right values.
"""
user, _ = User.objects.get_or_create(username="vader")
course_id = "course-v1:test+Cx105+2022_T4"
data = {
progress_enrollment_data = {
"courseId": "course-v1:edX+213+2121",
"userId": 16734,
"approxTotalCourseHrs": None,
Expand All @@ -67,7 +78,8 @@ def test_dispatch_futurex_progress(self, post_futurex_progress_mock, generate_pr
"enrolledAt": "2023-03-16T20:24:19.494709Z",
"isCompleted": False,
}
generate_progress_enrollment_data_mock.return_value = data
generate_progress_enrollment_data_mock.return_value = progress_enrollment_data
setattr(settings, "ACTIVATE_DISPATCH_FUTUREX_PROGRESS", truthy_value)

dispatch_futurex_progress(course_id, user.id, is_complete=True)

Expand All @@ -76,7 +88,55 @@ def test_dispatch_futurex_progress(self, post_futurex_progress_mock, generate_pr
course_id=course_id,
user_has_passing_grade=True,
)
post_futurex_progress_mock.assert_called_with(data)
post_futurex_progress_mock.assert_called_with(progress_enrollment_data)

@override_settings()
@patch("eox_nelp.signals.tasks._generate_progress_enrollment_data")
@patch("eox_nelp.signals.tasks._post_futurex_progress")
def test_not_call_dispatch_logic_setting_not_configured(
self, post_futurex_progress_mock, generate_progress_enrollment_data_mock
):
"""Test `dispatch_futurex_progress` is called but the logic inside not.
So `generate_progress_enrollment_data` and `post_futurex_progress` are not called
due no setting configured.
Expected behavior:
- generate_progress_enrollment_data is not called due settings.
- post_futurex_progress_mock is not called due settings.
"""
user, _ = User.objects.get_or_create(username="vader")
course_id = "course-v1:test+Cx105+2022_T4"
if hasattr(settings, "ACTIVATE_DISPATCH_FUTUREX_PROGRESS"):
delattr(settings, "ACTIVATE_DISPATCH_FUTUREX_PROGRESS")

dispatch_futurex_progress(course_id, user.id, is_complete=True)

generate_progress_enrollment_data_mock.assert_not_called()
post_futurex_progress_mock.assert_not_called()

@override_settings()
@patch("eox_nelp.signals.tasks._generate_progress_enrollment_data")
@patch("eox_nelp.signals.tasks._post_futurex_progress")
@data(*FALSY_ACTIVATION_VALUES)
def test_not_call_dispatch_logic_setting_falsy(
self, falsy_value, post_futurex_progress_mock, generate_progress_enrollment_data_mock,
):
"""Test `dispatch_futurex_progress` is called but the logic inside not.
So `generate_progress_enrollment_data` and `post_futurex_progress` are not called
due setting configured with falsy value.
Expected behavior:
- generate_progress_enrollment_data is not called due settings.
- post_futurex_progress_mock is not called due settings.
"""
user, _ = User.objects.get_or_create(username="vader")
course_id = "course-v1:test+Cx105+2022_T4"
setattr(settings, "ACTIVATE_DISPATCH_FUTUREX_PROGRESS", falsy_value)

dispatch_futurex_progress(course_id, user.id, is_complete=True)

generate_progress_enrollment_data_mock.assert_not_called()
post_futurex_progress_mock.assert_not_called()


class PostFuturexProgressTestCase(unittest.TestCase):
Expand All @@ -92,7 +152,7 @@ def test_dispatch_futurex_progress(self, futurex_api_client_mock):
- FuturexApiClient is used with the right values.
- Log successful sent to service message.
"""
data = {
progress_enrollment_data = {
"courseId": "course-v1:edX+213+2121",
"userId": 16734,
"approxTotalCourseHrs": None,
Expand All @@ -105,16 +165,16 @@ def test_dispatch_futurex_progress(self, futurex_api_client_mock):
service_response = {'status': {'success': True, 'message': 'successful', 'code': 1}}
log_post = (
f"send_futurex_progress --- "
f"The data {data} was sent to the futurex service host {service_base_url}. "
f"The data {progress_enrollment_data} was sent to the futurex service host {service_base_url}. "
f"The response was: {service_response}"
)
futurex_api_client_mock().base_url = service_base_url
futurex_api_client_mock().enrollment_progress.return_value = service_response

with self.assertLogs(tasks.__name__, level="INFO") as logs:
_post_futurex_progress(data)
_post_futurex_progress(progress_enrollment_data)

futurex_api_client_mock().enrollment_progress.assert_called_with(data)
futurex_api_client_mock().enrollment_progress.assert_called_with(progress_enrollment_data)
self.assertEqual(logs.output, [f"INFO:{tasks.__name__}:{log_post}"])


Expand All @@ -134,10 +194,7 @@ def test_get_course_blocks(self, courses_mock):

_get_completion_summary(user, course_id)

courses_mock.get_course_blocks_completion_summary.assert_called_with(
course_key,
user
)
courses_mock.get_course_blocks_completion_summary.assert_called_with(course_key, user)


class GenerateProgressEnrollmentDataTestCase(unittest.TestCase):
Expand Down

0 comments on commit e2651f5

Please sign in to comment.