From 010052e5beeb622feb438d43b869e98c25427f59 Mon Sep 17 00:00:00 2001 From: Marcos Date: Fri, 22 Nov 2024 13:08:43 -0300 Subject: [PATCH 1/2] fix: Gates the chat history endpoint behind a waffle flag --- learning_assistant/views.py | 4 ++ tests/test_views.py | 75 ++++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/learning_assistant/views.py b/learning_assistant/views.py index b474984..764adac 100644 --- a/learning_assistant/views.py +++ b/learning_assistant/views.py @@ -247,6 +247,10 @@ def get(self, request, course_run_id): data={'detail': 'Learning assistant not enabled for course.'} ) + # If chat history is disabled, we return no messages as response. + if not chat_history_enabled(courserun_key): + return Response(status=http_status.HTTP_200_OK, data=[]) + # If user does not have an enrollment record, or is not staff, they should not have access user_role = get_user_role(request.user, courserun_key) enrollment_object = CourseEnrollment.get_enrollment(request.user, courserun_key) diff --git a/tests/test_views.py b/tests/test_views.py index 98c0ab6..11b8ad8 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -321,12 +321,21 @@ def test_learning_assistant_not_enabled(self, mock_learning_assistant_enabled): self.assertEqual(response.status_code, 403) + @patch('learning_assistant.views.chat_history_enabled') @patch('learning_assistant.views.learning_assistant_enabled') @patch('learning_assistant.views.get_user_role') @patch('learning_assistant.views.CourseEnrollment.get_enrollment') @patch('learning_assistant.views.CourseMode') - def test_user_no_enrollment_not_staff(self, mock_mode, mock_enrollment, mock_role, mock_waffle): - mock_waffle.return_value = True + def test_user_no_enrollment_not_staff( + self, + mock_mode, + mock_enrollment, + mock_role, + mock_assistant_waffle, + mock_history_waffle + ): + mock_assistant_waffle.return_value = True + mock_history_waffle.return_value = True mock_role.return_value = 'student' mock_mode.VERIFIED_MODES = ['verified'] mock_enrollment.return_value = None @@ -338,12 +347,21 @@ def test_user_no_enrollment_not_staff(self, mock_mode, mock_enrollment, mock_rol ) self.assertEqual(response.status_code, 403) + @patch('learning_assistant.views.chat_history_enabled') @patch('learning_assistant.views.learning_assistant_enabled') @patch('learning_assistant.views.get_user_role') @patch('learning_assistant.views.CourseEnrollment.get_enrollment') @patch('learning_assistant.views.CourseMode') - def test_user_audit_enrollment_not_staff(self, mock_mode, mock_enrollment, mock_role, mock_waffle): - mock_waffle.return_value = True + def test_user_audit_enrollment_not_staff( + self, + mock_mode, + mock_enrollment, + mock_role, + mock_assistant_waffle, + mock_history_waffle + ): + mock_assistant_waffle.return_value = True + mock_history_waffle.return_value = True mock_role.return_value = 'student' mock_mode.VERIFIED_MODES = ['verified'] mock_enrollment.return_value = MagicMock(mode='audit') @@ -355,6 +373,7 @@ def test_user_audit_enrollment_not_staff(self, mock_mode, mock_enrollment, mock_ ) self.assertEqual(response.status_code, 403) + @patch('learning_assistant.views.chat_history_enabled') @patch('learning_assistant.views.learning_assistant_enabled') @patch('learning_assistant.views.get_user_role') @patch('learning_assistant.views.CourseEnrollment.get_enrollment') @@ -366,9 +385,11 @@ def test_learning_message_history_view_get( mock_mode, mock_enrollment, mock_role, - mock_waffle + mock_assistant_waffle, + mock_history_waffle, ): - mock_waffle.return_value = True + mock_assistant_waffle.return_value = True + mock_history_waffle.return_value = True mock_role.return_value = 'student' mock_mode.VERIFIED_MODES = ['verified'] mock_enrollment.return_value = MagicMock(mode='verified') @@ -407,3 +428,45 @@ def test_learning_message_history_view_get( self.assertEqual(message['role'], db_messages[i].role) self.assertEqual(message['content'], db_messages[i].content) self.assertEqual(message['timestamp'], db_messages[i].created.isoformat()) + + @patch('learning_assistant.views.chat_history_enabled') + @patch('learning_assistant.views.learning_assistant_enabled') + @patch('learning_assistant.views.get_course_id') + def test_learning_message_history_view_get_disabled( + self, + mock_get_course_id, + mock_assistant_waffle, + mock_history_waffle, + ): + mock_assistant_waffle.return_value = True + mock_history_waffle.return_value = False + + LearningAssistantMessage.objects.create( + course_id=self.course_id, + user=self.user, + role='staff', + content='Older message', + created=date(2024, 10, 1) + ) + + LearningAssistantMessage.objects.create( + course_id=self.course_id, + user=self.user, + role='staff', + content='Newer message', + created=date(2024, 10, 3) + ) + + db_messages = LearningAssistantMessage.objects.all().order_by('created') + db_messages_count = len(db_messages) + + mock_get_course_id.return_value = self.course_id + response = self.client.get( + reverse('message-history', kwargs={'course_run_id': self.course_id})+f'?message_count={db_messages_count}', + content_type='application/json' + ) + data = response.data + + # Ensure returning an empty list + self.assertEqual(len(data), 0) + self.assertEqual(data, []) From cceafad9e86577143fc622bd07ccf6ee7e45acca Mon Sep 17 00:00:00 2001 From: Marcos Date: Fri, 22 Nov 2024 13:40:24 -0300 Subject: [PATCH 2/2] chore: Bumped version and updated changelog --- CHANGELOG.rst | 4 ++++ learning_assistant/__init__.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3cf14ca..a9ac2da 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,10 @@ Change Log Unreleased ********** +4.4.6 - 2024-11-22 +****************** +* Gates the chat history endpoint behind a waffle flag + 4.4.5 - 2024-11-12 ****************** * Updated Learning Assistant History payload to return in ascending order diff --git a/learning_assistant/__init__.py b/learning_assistant/__init__.py index 8ef32fa..b91c801 100644 --- a/learning_assistant/__init__.py +++ b/learning_assistant/__init__.py @@ -2,6 +2,6 @@ Plugin for a learning assistant backend, intended for use within edx-platform. """ -__version__ = '4.4.5' +__version__ = '4.4.6' default_app_config = 'learning_assistant.apps.LearningAssistantConfig' # pylint: disable=invalid-name