2
2
from unittest .mock import patch , MagicMock
3
3
from flask import Flask , jsonify , request
4
4
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
13
6
14
7
class TestValidateJWT (unittest .TestCase ):
15
8
16
9
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 ()
19
22
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
21
24
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" )
24
27
response = self .client .get ("/allowed_path" )
25
28
self .assertEqual (response .status_code , 200 )
26
29
self .assertEqual (response .json , {"message" : "request proxied" })
27
30
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
29
32
@patch ('jwt.PyJWKClient' )
30
33
@patch ('jwt.decode' )
31
34
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" )
33
36
mock_jwks_client .return_value .get_signing_key_from_jwt .return_value .key = "test-key"
34
37
mock_decode .
return_value = {
"email" :
"[email protected] " }
35
38
@@ -38,15 +41,15 @@ def test_valid_token(self, mock_decode, mock_jwks_client, mock_proxy_request):
38
41
self .assertEqual (response .status_code , 200 )
39
42
self .assertEqual (response .json , {"message" : "request proxied" })
40
43
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
42
45
@patch ('jwt.PyJWKClient' )
43
46
@patch ('jwt.decode' )
44
47
def test_missing_token (self , mock_decode , mock_jwks_client , mock_proxy_request ):
45
48
response = self .client .get ("/some_path" )
46
49
self .assertEqual (response .status_code , 400 )
47
50
self .assertEqual (response .json , {"message" : "token missing" })
48
51
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
50
53
@patch ('jwt.PyJWKClient' )
51
54
@patch ('jwt.decode' )
52
55
def test_expired_token (self , mock_decode , mock_jwks_client , mock_proxy_request ):
0 commit comments