Пример #1
0
    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)
Пример #2
0
    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))
Пример #3
0
    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))
Пример #4
0
    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))
Пример #5
0
    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))
Пример #6
0
    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))
Пример #7
0
    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))