Skip to content

Commit

Permalink
Merge pull request #137 from edx/rijuma/gate-chat-history
Browse files Browse the repository at this point in the history
fix: Gates the chat history endpoint behind a waffle flag
  • Loading branch information
rijuma authored Nov 22, 2024
2 parents ada1330 + cceafad commit dcf45e2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion learning_assistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions learning_assistant/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
75 changes: 69 additions & 6 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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, [])

0 comments on commit dcf45e2

Please sign in to comment.