def test_get_claim_set_with_optional_claims(self): """ Test getting the claim set if optional claims are set. Expected Result: A dictionary with the entries for the class and the optional claims is returned. """ claim_set = dict( _easyjwt_class='EasyJWT', aud=self.audience, exp=self.expiration_date, iat=self.issued_at_date, iss=self.issuer, jti=self.JWT_ID, nbf=self.not_before_date, sub=self.subject, ) easyjwt = EasyJWT(self.key) easyjwt.audience = self.audience easyjwt.expiration_date = self.expiration_date easyjwt.issued_at_date = self.issued_at_date easyjwt.issuer = self.issuer easyjwt.JWT_ID = self.JWT_ID easyjwt.not_before_date = self.not_before_date easyjwt.subject = self.subject self.assertDictEqual(claim_set, easyjwt._get_claim_set())
def test_verify_claim_set_success_without_optional_claims(self): """ Test verifying a valid claim set not containing optional claims. Expected result: `True` """ easyjwt = EasyJWT(self.key) claim_set = easyjwt._get_claim_set() self.assertTrue(easyjwt._verify_claim_set(claim_set))
def test_claim_names_and_claim_set_keys_equal(self): """ Assert that the set of claim names is exactly the same as the set of claim set keys (if empty claims are included). Expected Result: The set of claim names equals the set of claim set keys. """ easyjwt = EasyJWT(self.key) claim_names = easyjwt._get_claim_names() claim_set = easyjwt._get_claim_set(with_empty_claims=True) self.assertSetEqual(claim_names, set(claim_set.keys()))
def test_get_claim_set_lenient_verification(self): """ Test getting the claim set with strict verification disabled. Expected Result: The `_easyjwt_class` claim is not included. """ EasyJWT.strict_verification = False claim_set = dict() easyjwt = EasyJWT(self.key) self.assertDictEqual(claim_set, easyjwt._get_claim_set())
def test_get_claim_set_without_optional_claims_and_without_empty_claims( self): """ Test getting the claim set without getting empty claims if optional claims are not set. Expected Result: A dictionary with the entry for the class is returned. Optional claims are not included. """ claim_set = dict(_easyjwt_class='EasyJWT', ) easyjwt = EasyJWT(self.key) self.assertDictEqual(claim_set, easyjwt._get_claim_set(with_empty_claims=False))
def test_verify_claim_set_failure_class_missing(self): """ Test verifying a claim set with a missing class claim. Expected result: An `UnspecifiedClassError` error is raised. """ # Remove the class claim from the claim set. easyjwt = EasyJWT(self.key) claim_set = easyjwt._get_claim_set() del claim_set['_easyjwt_class'] with self.assertRaises(UnspecifiedClassError): easyjwt._verify_claim_set(claim_set)
def test_verify_claim_set_failure_class_wrong(self): """ Test verifying a claim set with a faulty value in the class claim. Expected result: An `InvalidClassError` error with an explaining message is raised. """ # Manipulate the class claim in the claim set. easyjwt = EasyJWT(self.key) claim_set = easyjwt._get_claim_set() claim_set['_easyjwt_class'] = 'InheritedEasyJWT' with self.assertRaises(InvalidClassError) as exception_cm: easyjwt._verify_claim_set(claim_set) self.assertEqual('Expected class EasyJWT. Got class InheritedEasyJWT', str(exception_cm.exception))
def test_verify_claim_set_failure_claims_unexpected(self): """ Test verifying a claim set with unexpected claims. Expected result: An `InvalidClaimSetError` error with an explaining message is raised. """ easyjwt = EasyJWT(self.key) # Add a claim to the claim set. claim_set = easyjwt._get_claim_set() claim_set['user_id'] = 42 with self.assertRaises(InvalidClaimSetError) as exception_cm: easyjwt._verify_claim_set(claim_set) self.assertEqual('Missing claims: {}. Unexpected claims: {user_id}', str(exception_cm.exception))
def test_verify_claim_set_success_with_optional_claims(self): """ Test verifying a valid claim set containing (valid) optional claims. Expected result: `True` """ easyjwt = EasyJWT(self.key) easyjwt.audience = self.audience easyjwt.expiration_date = self.expiration_date easyjwt.issued_at_date = self.issued_at_date easyjwt.issuer = self.issuer easyjwt.JWT_ID = self.JWT_ID easyjwt.not_before_date = self.not_before_date easyjwt.subject = self.subject claim_set = easyjwt._get_claim_set() self.assertTrue(easyjwt._verify_claim_set(claim_set))
def test_verify_claim_set_success_lenient_verification(self): """ Test verifying a valid claim set without an `_easyjwt_class` claim with strict verification disabled. Expected result: `True` """ EasyJWT.strict_verification = False easyjwt = EasyJWT(self.key) easyjwt.audience = self.audience easyjwt.expiration_date = self.expiration_date easyjwt.issued_at_date = self.issued_at_date easyjwt.issuer = self.issuer easyjwt.JWT_ID = self.JWT_ID easyjwt.not_before_date = self.not_before_date easyjwt.subject = self.subject claim_set = easyjwt._get_claim_set() self.assertTrue(easyjwt._verify_claim_set(claim_set))
def test_get_claim_set_without_optional_claims_but_with_empty_claims(self): """ Test getting the claim set with getting empty claims if optional claims are not set. Expected Result: A dictionary with the entry for the class is returned. Optional claims are included and empty. """ claim_set = dict( _easyjwt_class='EasyJWT', aud=None, exp=None, iat=None, iss=None, jti=None, nbf=None, sub=None, ) easyjwt = EasyJWT(self.key) self.assertDictEqual(claim_set, easyjwt._get_claim_set(with_empty_claims=True))
def test_verify_claim_set_failure_claims_missing(self): """ Test verifying a claim set with missing claims. Expected result: An `InvalidClaimSetError` error with an explaining message is raised. """ # Create a new instance variable in the object by assigning to it. This instance variable will automatically # become a claim. When calling the verify method on this object, this should cause the expected failure if the # claim is not in the created token. easyjwt = EasyJWT(self.key) easyjwt.email = '*****@*****.**' # Now remove the claim from the claim set. claim_set = easyjwt._get_claim_set() del claim_set['email'] with self.assertRaises(InvalidClaimSetError) as exception_cm: easyjwt._verify_claim_set(claim_set) self.assertEqual('Missing claims: {email}. Unexpected claims: {}', str(exception_cm.exception))