3939RSA_KEY = RSA .generate (2048 ).export_key ('PEM' )
4040
4141
42+ def _generate_token_request_data (token , scope ):
43+ """
44+ Helper function to generate requests to the access_token endpoint
45+ """
46+ return {
47+ # We don't actually care about these 2 first values
48+ "grant_type" : "client_credentials" ,
49+ "client_assertion_type" : "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" ,
50+ "client_assertion" : token ,
51+ "scope" : scope ,
52+ }
53+
54+
4255# Test classes
4356@ddt .ddt
4457class TestLti1p3Consumer (TestCase ):
4558 """
4659 Unit tests for LtiConsumer1p3
4760 """
61+
4862 def setUp (self ):
4963 super ().setUp ()
5064
@@ -542,18 +556,31 @@ def test_access_token_invalid_jwt(self):
542556 """
543557 Check if access token with invalid request data raises.
544558 """
545- request_data = {
546- "grant_type" : "client_credentials" ,
547- "client_assertion_type" : "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" ,
548- # This should be a valid JWT
549- "client_assertion" : "invalid-jwt" ,
550- # Scope can be empty
551- "scope" : "" ,
552- }
559+ request_data = _generate_token_request_data ("invalid_jwt" , "" )
553560
554561 with self .assertRaises (exceptions .MalformedJwtToken ):
555562 self .lti_consumer .access_token (request_data )
556563
564+ def test_access_token_no_acs (self ):
565+ """
566+ Check that ACS does not work for the access token in the
567+ default LTI 1.3 consumer
568+ """
569+ # Generate a dummy, but valid JWT
570+ token = self .lti_consumer .key_handler .encode_and_sign (
571+ {
572+ "test" : "test"
573+ },
574+ expiration = 1000
575+ )
576+
577+ request_data = _generate_token_request_data (token , "https://purl.imsglobal.org/spec/lti-ap/scope/control.all" )
578+
579+ response = self .lti_consumer .access_token (request_data )
580+
581+ # Check no ACS scope present in returned token
582+ self .assertEqual (response .get ('scope' ), '' )
583+
557584 def test_access_token (self ):
558585 """
559586 Check if a valid access token is returned.
@@ -570,15 +597,7 @@ def test_access_token(self):
570597 expiration = 1000
571598 )
572599
573- request_data = {
574- # We don't actually care about these 2 first values
575- "grant_type" : "client_credentials" ,
576- "client_assertion_type" : "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" ,
577- # This should be a valid JWT
578- "client_assertion" : token ,
579- # Scope can be empty
580- "scope" : "" ,
581- }
600+ request_data = _generate_token_request_data (token , "" )
582601
583602 response = self .lti_consumer .access_token (request_data )
584603
@@ -656,6 +675,7 @@ class TestLtiAdvantageConsumer(TestCase):
656675 """
657676 Unit tests for LtiAdvantageConsumer
658677 """
678+
659679 def setUp (self ):
660680 super ().setUp ()
661681
@@ -899,6 +919,7 @@ class TestLtiProctoringConsumer(TestCase):
899919 """
900920 Unit tests for LtiProctoringConsumer
901921 """
922+
902923 def setUp (self ):
903924 super ().setUp ()
904925
@@ -1166,6 +1187,46 @@ def test_invalid_check_and_decode_token(self, claim_key):
11661187 with self .assertRaises (MissingRequiredClaim ):
11671188 self .lti_consumer .check_and_decode_token (encoded_token )
11681189
1190+ def test_access_token_no_valid_scopes (self ):
1191+ """
1192+ Ensure that the no scopes are returned in the access token if the request scopes are invalid
1193+ """
1194+ # Generate a dummy, but valid JWT
1195+ token = self .lti_consumer .key_handler .encode_and_sign (
1196+ {
1197+ "test" : "test"
1198+ },
1199+ expiration = 1000
1200+ )
1201+
1202+ # This should be a valid JWT w/ the ACS scope
1203+ request_data = _generate_token_request_data (token , "invalid_scope" )
1204+
1205+ response = self .lti_consumer .access_token (request_data )
1206+
1207+ # Check that the response has the ACS scope
1208+ self .assertEqual (response .get ('scope' ), "" )
1209+
1210+ def test_access_token (self ):
1211+ """
1212+ Ensure that the ACS scope is added based on the request to the access token endpoint
1213+ """
1214+ # Generate a dummy, but valid JWT
1215+ token = self .lti_consumer .key_handler .encode_and_sign (
1216+ {
1217+ "test" : "test"
1218+ },
1219+ expiration = 1000
1220+ )
1221+
1222+ # This should be a valid JWT w/ the ACS scope
1223+ request_data = _generate_token_request_data (token , "https://purl.imsglobal.org/spec/lti-ap/scope/control.all" )
1224+
1225+ response = self .lti_consumer .access_token (request_data )
1226+
1227+ # Check that the response has the ACS scope
1228+ self .assertEqual (response .get ('scope' ), "https://purl.imsglobal.org/spec/lti-ap/scope/control.all" )
1229+
11691230 def test_valid_check_and_decode_token (self ):
11701231 """
11711232 Ensures that a valid LtiStartAssessment JWT is validated successfully.
0 commit comments