Skip to content

Commit

Permalink
test: completed terminate action tests
Browse files Browse the repository at this point in the history
- added test to ensure ACResult created on success
  • Loading branch information
ilee2u committed Jul 27, 2023
1 parent 69fab2e commit e9dea60
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions edx_exams/apps/lti/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from lti_consumer.lti_1p3.extensions.rest_framework.authentication import Lti1p3ApiAuthentication
from lti_consumer.models import LtiConfiguration, LtiProctoringConsumer

from edx_exams.apps.core.models import AssessmentControlResult
from edx_exams.apps.api.test_utils import ExamsAPITestCase, UserFactory
from edx_exams.apps.api.test_utils.factories import (
CourseExamConfigurationFactory,
Expand All @@ -38,7 +39,11 @@ def setUp(self):

self.course_exam_config = CourseExamConfigurationFactory()
self.exam = ExamFactory()

# Create an Exam Attempt that has already been submitted.
self.attempt = ExamAttemptFactory()
self.attempt.status = ExamAttemptStatus.submitted
self.attempt.save()

# Variables required for testing and verification
ISS = 'http://test-platform.example/'
Expand Down Expand Up @@ -145,7 +150,7 @@ def create_request_body(self, attempt_number, action, reason_code=None, incident
@ ddt.unpack
@ patch.object(Lti1p3ApiAuthentication, 'authenticate', return_value=(AnonymousUser(), None))
@ patch('edx_exams.apps.lti.views.LtiProctoringAcsPermissions.has_permission')
@ patch('edx_exams.apps.lti`.`views.get_attempt_for_user_with_attempt_number_and_resource_id')
@ patch('edx_exams.apps.lti.views.get_attempt_for_user_with_attempt_number_and_resource_id')
def test_acs_attempt_status(self,
attempt_status,
expected_response_status,
Expand Down Expand Up @@ -212,7 +217,7 @@ def test_acs_no_attempt_found(self,
@ patch.object(Lti1p3ApiAuthentication, 'authenticate', return_value=(AnonymousUser(), None))
@ patch('edx_exams.apps.lti.views.LtiProctoringAcsPermissions.has_permission')
@ patch('edx_exams.apps.lti.views.get_attempt_for_user_with_attempt_number_and_resource_id')
def test_acs_bad_request(self,
def test_acs_base_parameter_missing_errors(self,
acs_parameter,
acs_sub_parameter,
mock_get_attempt,
Expand All @@ -224,9 +229,6 @@ def test_acs_bad_request(self,
# Make requests missing these items (create request but set the item passed in to undefined/None)
# Make the request
# Assert the correct error is thrown in the response with status 400
self.attempt.status = ExamAttemptStatus.submitted
self.attempt.save()

mock_get_attempt.return_value = self.attempt
mock_permissions.return_value = True

Expand Down Expand Up @@ -254,25 +256,47 @@ def test_acs_bad_request(self,
self.assertEqual(response.data, f'ERROR: required parameter \'{key_to_fail}\' was not found.')

# TODO: Add tests for termination errors
# reason_code
# incident_time
# incident_severity
@ ddt.data(
['reason_code'],
['incident_time'],
['incident_severity'],
)
@ ddt.unpack
@ patch.object(Lti1p3ApiAuthentication, 'authenticate', return_value=(AnonymousUser(), None))
@ patch('edx_exams.apps.lti.views.LtiProctoringAcsPermissions.has_permission')
@ patch('edx_exams.apps.lti`.`views.get_attempt_for_user_with_attempt_number_and_resource_id')
def test_acs_attempt_status(self,
# attempt_status,
mock_get_attempt,
mock_permissions,
mock_authentication): # pylint: disable=unused-argument
@ patch('edx_exams.apps.lti.views.get_attempt_for_user_with_attempt_number_and_resource_id')
def test_acs_terminate_parameter_errors(self,
acs_parameter,
mock_get_attempt,
mock_permissions,
mock_authentication): # pylint: disable=unused-argument
"""
a
Test the endpoint errors correctly if request parameters required for the terminate action are not present
"""
return
# Make requests missing these items (create request but set the item passed in to undefined/None)
# Make the request
# Assert the correct error is thrown in the response with status 400
mock_get_attempt.return_value = self.attempt
mock_permissions.return_value = True

token = self.make_access_token('https://purl.imsglobal.org/spec/lti-ap/scope/control.all')

request_body = self.create_request_body(
self.attempt.attempt_number,
action='terminate',
reason_code='1',
incident_severity='0.1',
)

del request_body[acs_parameter]

# Even though the client.post function below uses json.dumps to serialize the request as json,
# The json serialization needs to happen before the request for an unknown reason
request_body = json.dumps(request_body)
response = self.client.post(self.url, data=request_body, content_type='application/json',
HTTP_AUTHORIZATION='Bearer {}'.format(token))
self.attempt.refresh_from_db()
self.assertEqual(response.data, f'ERROR: required parameter \'{acs_parameter}\' was not found.')

@ ddt.data(
# Testing reason codes with severity > 0.25
Expand Down Expand Up @@ -342,6 +366,10 @@ def test_acs_terminate(self,

self.assertEqual(self.attempt.status, expected_attempt_status)

# Assure an entry was added to the ACResult model
data = AssessmentControlResult.objects.all()
self.assertEqual(len(data), 1)

def test_auth_failures(self):
"""
Test that an exception occurs if basic access token authentication fails
Expand Down

0 comments on commit e9dea60

Please sign in to comment.