33
44import datetime
55import textwrap
6- import unittest
6+ from django .conf import settings
7+ from django .test import TestCase , override_settings
78from unittest .mock import Mock
89
910from pytz import UTC
1011from xblock .field_data import DictFieldData
1112
12- from xmodule .lti_2_util import LTIError
13- from xmodule .lti_block import LTIBlock
13+ from xmodule import lti_block
1414from xmodule .tests .helpers import StubUserService
1515
1616from . import get_test_system
1717
1818
19- class LTI20RESTResultServiceTest (unittest .TestCase ):
19+ from xmodule .lti_2_util import LTIError as BuiltInLTIError
20+ from xblocks_contrib .lti .lti_2_util import LTIError as ExtractedLTIError
21+
22+
23+ class _LTI20RESTResultServiceTestBase (TestCase ):
2024 """Logic tests for LTI block. LTI2.0 REST ResultService"""
2125
26+ __test__ = False
2227 USER_STANDIN = Mock ()
2328 USER_STANDIN .id = 999
2429
30+ @classmethod
31+ def setUpClass (cls ):
32+ super ().setUpClass ()
33+ cls .lti_class = lti_block .reset_class ()
34+ if settings .USE_EXTRACTED_LTI_BLOCK :
35+ cls .LTIError = ExtractedLTIError
36+ else :
37+ cls .LTIError = BuiltInLTIError
38+
2539 def setUp (self ):
2640 super ().setUp ()
2741 self .runtime = get_test_system (user = self .USER_STANDIN )
2842 self .environ = {'wsgi.url_scheme' : 'http' , 'REQUEST_METHOD' : 'POST' }
2943 self .runtime .publish = Mock ()
3044 self .runtime ._services ['rebind_user' ] = Mock () # pylint: disable=protected-access
3145
32- self .xblock = LTIBlock (self .runtime , DictFieldData ({}), Mock ())
46+ self .xblock = self . lti_class (self .runtime , DictFieldData ({}), Mock ())
3347 self .lti_id = self .xblock .lti_id
3448 self .xblock .due = None
3549 self .xblock .graceperiod = None
@@ -56,7 +70,7 @@ def test_lti20_rest_bad_contenttype(self):
5670 """
5771 Input with bad content type
5872 """
59- with self .assertRaisesRegex (LTIError , "Content-Type must be" ):
73+ with self .assertRaisesRegex (self . LTIError , "Content-Type must be" ):
6074 request = Mock (headers = {'Content-Type' : 'Non-existent' })
6175 self .xblock .verify_lti_2_0_result_rest_headers (request )
6276
@@ -65,8 +79,8 @@ def test_lti20_rest_failed_oauth_body_verify(self):
6579 Input with bad oauth body hash verification
6680 """
6781 err_msg = "OAuth body verification failed"
68- self .xblock .verify_oauth_body_sign = Mock (side_effect = LTIError (err_msg ))
69- with self .assertRaisesRegex (LTIError , err_msg ):
82+ self .xblock .verify_oauth_body_sign = Mock (side_effect = self . LTIError (err_msg ))
83+ with self .assertRaisesRegex (self . LTIError , err_msg ):
7084 request = Mock (headers = {'Content-Type' : 'application/vnd.ims.lis.v2.result+json' })
7185 self .xblock .verify_lti_2_0_result_rest_headers (request )
7286
@@ -99,7 +113,7 @@ def test_lti20_rest_bad_dispatch(self):
99113 fit the form user/<anon_id>
100114 """
101115 for einput in self .BAD_DISPATCH_INPUTS :
102- with self .assertRaisesRegex (LTIError , "No valid user id found in endpoint URL" ):
116+ with self .assertRaisesRegex (self . LTIError , "No valid user id found in endpoint URL" ):
103117 self .xblock .parse_lti_2_0_handler_suffix (einput )
104118
105119 GOOD_DISPATCH_INPUTS = [
@@ -160,7 +174,7 @@ def test_lti20_bad_json(self):
160174 """
161175 for error_inputs , error_message in self .BAD_JSON_INPUTS :
162176 for einput in error_inputs :
163- with self .assertRaisesRegex (LTIError , error_message ):
177+ with self .assertRaisesRegex (self . LTIError , error_message ):
164178 self .xblock .parse_lti_2_0_result_json (einput )
165179
166180 GOOD_JSON_INPUTS = [
@@ -341,7 +355,7 @@ def test_lti20_request_handler_bad_headers(self):
341355 Test that we get a 401 when header verification fails
342356 """
343357 self .setup_system_xblock_mocks_for_lti20_request_test ()
344- self .xblock .verify_lti_2_0_result_rest_headers = Mock (side_effect = LTIError ())
358+ self .xblock .verify_lti_2_0_result_rest_headers = Mock (side_effect = self . LTIError ())
345359 mock_request = self .get_signed_lti20_mock_request (self .GOOD_JSON_PUT )
346360 response = self .xblock .lti_2_0_result_rest_handler (mock_request , "user/abcd" )
347361 assert response .status_code == 401
@@ -360,7 +374,7 @@ def test_lti20_request_handler_bad_json(self):
360374 Test that we get a 404 when json verification fails
361375 """
362376 self .setup_system_xblock_mocks_for_lti20_request_test ()
363- self .xblock .parse_lti_2_0_result_json = Mock (side_effect = LTIError ())
377+ self .xblock .parse_lti_2_0_result_json = Mock (side_effect = self . LTIError ())
364378 mock_request = self .get_signed_lti20_mock_request (self .GOOD_JSON_PUT )
365379 response = self .xblock .lti_2_0_result_rest_handler (mock_request , "user/abcd" )
366380 assert response .status_code == 404
@@ -385,3 +399,13 @@ def test_lti20_request_handler_grade_past_due(self):
385399 mock_request = self .get_signed_lti20_mock_request (self .GOOD_JSON_PUT )
386400 response = self .xblock .lti_2_0_result_rest_handler (mock_request , "user/abcd" )
387401 assert response .status_code == 404
402+
403+
404+ @override_settings (USE_EXTRACTED_LTI_BLOCK = True )
405+ class TestLTI20RESTResultServiceWithExtracted (_LTI20RESTResultServiceTestBase ):
406+ __test__ = True
407+
408+
409+ @override_settings (USE_EXTRACTED_LTI_BLOCK = False )
410+ class TestLTI20RESTResultServiceWithBuiltIn (_LTI20RESTResultServiceTestBase ):
411+ __test__ = True
0 commit comments