コード例 #1
0
    def test_expired(self):
        user = Mock()
        id = randrange(1, 100)
        user.id = id
        user.is_admin = False

        secret = faker.sentence()
        expire_time = time.time() - 2 * (60 * 60 * 24) + randrange(1, 100)

        with patch('tahelka.auth.token_generator.current_app') as app:
            with patch(
                'tahelka.auth.token_authenticator.time.time'
            ) as mock_time:
                mock_time.return_value = expire_time
                app.config = {'JWT_SECRET': secret}

                generator = TokenGenerator(user)
                token = generator.generate()

        with patch('tahelka.auth.token_authenticator.current_app') as app:
            app.config = {'JWT_SECRET': secret}
            with patch('tahelka.auth.token_authenticator.g') as g:
                auth_header = f'Bearer {token}'
                authenticator = TokenAuthenticator(auth_header, False)
                with self.assertRaises(Unauthorized):
                    authenticator.authenticate()
コード例 #2
0
    def test_wrong_token(self):
        with patch('tahelka.auth.token_authenticator.current_app') as app:
            with patch('tahelka.auth.token_authenticator.g') as g:
                app.config = {'JWT_SECRET': faker.sentence()}

                auth_header = f'Bearer {faker.sentence()}'
                authenticator = TokenAuthenticator(auth_header, True)
                with self.assertRaises(Unauthorized):
                    authenticator.authenticate()
コード例 #3
0
    def test_happy_non_admin(self):
        user = Mock()
        id = randrange(1, 100)
        user.id = id
        user.is_admin = False

        secret = faker.sentence()

        with patch('tahelka.auth.token_generator.current_app') as app:
            app.config = {'JWT_SECRET': secret}

            generator = TokenGenerator(user)
            token = generator.generate()

        with patch('tahelka.auth.token_authenticator.current_app') as app:
            with patch('tahelka.auth.token_authenticator.g') as g:
                app.config = {'JWT_SECRET': secret}

                auth_header = f'Bearer {token}'
                authenticator = TokenAuthenticator(auth_header, False)
                authenticator.authenticate()
                self.assertEqual(g.user_id, id)