Skip to content

Commit c4de40b

Browse files
committed
b
1 parent 7425702 commit c4de40b

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

Diff for: tests/test_validation.py

+20-17
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,37 @@
22
from unittest.mock import patch, MagicMock
33
from flask import Flask, jsonify, request
44
import jwt
5-
6-
# Assume blueprint and validate_jwt function are defined in your application
7-
# For testing purposes, we'll use a simple Flask app
8-
app = Flask(__name__)
9-
app.config["PATH_WHITELIST"] = ["/allowed_path"]
10-
app.config["UPSTREAM_SERVER"] = "http://upstream-server"
11-
app.config["JWKS_URL"] = "http://jwks-url"
12-
5+
from jwt_proxy.api import validate_jwt, proxy_request # Adjust the import path based on your actual module structure
136

147
class TestValidateJWT(unittest.TestCase):
158

169
def setUp(self):
17-
app.testing = True
18-
self.client = app.test_client()
10+
self.app = Flask(__name__)
11+
self.app.config["PATH_WHITELIST"] = ["/allowed_path"]
12+
self.app.config["UPSTREAM_SERVER"] = "http://upstream-server"
13+
self.app.config["JWKS_URL"] = "http://jwks-url"
14+
15+
# Register the route using the validate_jwt function
16+
@self.app.route("/", defaults={"relative_path": ""}, methods=["GET", "POST"])
17+
@self.app.route("/<path:relative_path>", methods=["GET", "POST"])
18+
def validate_jwt_route(relative_path):
19+
return validate_jwt(relative_path)
20+
21+
self.client = self.app.test_client()
1922

20-
@patch('jwt_proxy.api.proxy_request')
23+
@patch('jwt_proxy.api.proxy_request') # Adjust the import path based on where proxy_request is defined
2124
def test_path_whitelist(self, mock_proxy_request):
22-
# Mock response as a Flask Response object directly
23-
mock_proxy_request.return_value = self.client.get('/allowed_path')
25+
# Mock response
26+
mock_proxy_request.return_value = jsonify(message="request proxied")
2427
response = self.client.get("/allowed_path")
2528
self.assertEqual(response.status_code, 200)
2629
self.assertEqual(response.json, {"message": "request proxied"})
2730

28-
@patch('jwt_proxy.api.proxy_request')
31+
@patch('jwt_proxy.api.proxy_request') # Adjust the import path based on where proxy_request is defined
2932
@patch('jwt.PyJWKClient')
3033
@patch('jwt.decode')
3134
def test_valid_token(self, mock_decode, mock_jwks_client, mock_proxy_request):
32-
mock_proxy_request.return_value = self.client.get('/some_path')
35+
mock_proxy_request.return_value = jsonify(message="request proxied")
3336
mock_jwks_client.return_value.get_signing_key_from_jwt.return_value.key = "test-key"
3437
mock_decode.return_value = {"email": "[email protected]"}
3538

@@ -38,15 +41,15 @@ def test_valid_token(self, mock_decode, mock_jwks_client, mock_proxy_request):
3841
self.assertEqual(response.status_code, 200)
3942
self.assertEqual(response.json, {"message": "request proxied"})
4043

41-
@patch('jwt_proxy.api.proxy_request')
44+
@patch('jwt_proxy.api.proxy_request') # Adjust the import path based on where proxy_request is defined
4245
@patch('jwt.PyJWKClient')
4346
@patch('jwt.decode')
4447
def test_missing_token(self, mock_decode, mock_jwks_client, mock_proxy_request):
4548
response = self.client.get("/some_path")
4649
self.assertEqual(response.status_code, 400)
4750
self.assertEqual(response.json, {"message": "token missing"})
4851

49-
@patch('jwt_proxy.api.proxy_request')
52+
@patch('jwt_proxy.api.proxy_request') # Adjust the import path based on where proxy_request is defined
5053
@patch('jwt.PyJWKClient')
5154
@patch('jwt.decode')
5255
def test_expired_token(self, mock_decode, mock_jwks_client, mock_proxy_request):

0 commit comments

Comments
 (0)