Skip to content

Commit

Permalink
jw
Browse files Browse the repository at this point in the history
  • Loading branch information
Filienko committed Aug 14, 2024
1 parent c4de40b commit 7206230
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import unittest
from unittest.mock import patch, MagicMock
from flask import Flask, jsonify, request
from flask import Flask
import jwt
from jwt_proxy.api import validate_jwt, proxy_request # Adjust the import path based on your actual module structure
from jwt_proxy.api import validate_jwt

class TestValidateJWT(unittest.TestCase):

Expand All @@ -17,35 +17,43 @@ def setUp(self):
@self.app.route("/<path:relative_path>", methods=["GET", "POST"])
def validate_jwt_route(relative_path):
return validate_jwt(relative_path)

self.client = self.app.test_client()

@patch('jwt_proxy.api.proxy_request') # Adjust the import path based on where proxy_request is defined
def test_path_whitelist(self, mock_proxy_request):
# Mock response
mock_proxy_request.return_value = jsonify(message="request proxied")
response = self.client.get("/allowed_path")
# Mock response directly without using jsonify
mock_proxy_request.return_value = {"message": "request proxied"}

with self.app.app_context():
response = self.client.get("/allowed_path")

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"message": "request proxied"})

@patch('jwt_proxy.api.proxy_request') # Adjust the import path based on where proxy_request is defined
@patch('jwt.PyJWKClient')
@patch('jwt.decode')
def test_valid_token(self, mock_decode, mock_jwks_client, mock_proxy_request):
mock_proxy_request.return_value = jsonify(message="request proxied")
mock_proxy_request.return_value = {"message": "request proxied"}
mock_jwks_client.return_value.get_signing_key_from_jwt.return_value.key = "test-key"
mock_decode.return_value = {"email": "test@example.com"}

headers = {"Authorization": "Bearer valid-token"}
response = self.client.get("/some_path", headers=headers)

with self.app.app_context():
response = self.client.get("/some_path", headers=headers)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"message": "request proxied"})

@patch('jwt_proxy.api.proxy_request') # Adjust the import path based on where proxy_request is defined
@patch('jwt.PyJWKClient')
@patch('jwt.decode')
def test_missing_token(self, mock_decode, mock_jwks_client, mock_proxy_request):
response = self.client.get("/some_path")
with self.app.app_context():
response = self.client.get("/some_path")

self.assertEqual(response.status_code, 400)
self.assertEqual(response.json, {"message": "token missing"})

Expand All @@ -57,7 +65,10 @@ def test_expired_token(self, mock_decode, mock_jwks_client, mock_proxy_request):
mock_decode.side_effect = jwt.exceptions.ExpiredSignatureError("token expired")

headers = {"Authorization": "Bearer expired-token"}
response = self.client.get("/some_path", headers=headers)

with self.app.app_context():
response = self.client.get("/some_path", headers=headers)

self.assertEqual(response.status_code, 401)
self.assertEqual(response.json, {"message": "token expired"})

Expand Down

0 comments on commit 7206230

Please sign in to comment.