class TestAuthBlueprint(unittest.TestCase): def setUp(self): self.auth = Auth(app) def test_encode_auth_token(self): token = self.auth.encode_auth_token() self.assertIsInstance(token, bytes) def test_decode_auth_token_success(self): token = self.auth.encode_auth_token() self.assertTrue( self.auth.decode_auth_token(token) == self.auth.HARDCODED_USER_ID) @mock.patch('auth.auth.Auth.decode_auth_token') def test_decode_auth_token_invalid(self, mock_auth_decode): invalid_token = b'junktokenisinvalid' mock_auth_decode.side_effect = jwt.InvalidTokenError with self.assertRaises(jwt.InvalidTokenError): result = self.auth.decode_auth_token(invalid_token) self.assertEqual(result, self.auth.invalid_token) @mock.patch('auth.auth.Auth.decode_auth_token') def test_decode_auth_token_expired(self, mock_auth_decode): expired_token = b'junktokenisexpired' mock_auth_decode.side_effect = jwt.ExpiredSignatureError with self.assertRaises(jwt.ExpiredSignatureError): result = self.auth.decode_auth_token(expired_token) self.assertEqual(result, self.auth.expired_token)
def auth_token(): """Endpoint to retrieve an Authorization token. Returns: JSON -- Containing 'token' key with corresponding Authorization token. """ try: auth = Auth(app) token = auth.encode_auth_token() return make_response({"token": token.decode('utf-8')}), status.HTTP_200_OK except Exception as e: return e