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..e57dc8d 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -321,12 +321,14 @@ 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 +340,14 @@ 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 +359,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 +371,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 +414,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( + 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, [])