1
1
import unittest
2
2
from unittest .mock import patch , MagicMock
3
- from flask import Flask , jsonify , request
3
+ from flask import Flask
4
4
import jwt
5
- from jwt_proxy .api import validate_jwt , proxy_request # Adjust the import path based on your actual module structure
5
+ from jwt_proxy .api import validate_jwt
6
6
7
7
class TestValidateJWT (unittest .TestCase ):
8
8
@@ -17,35 +17,43 @@ def setUp(self):
17
17
@self .app .route ("/<path:relative_path>" , methods = ["GET" , "POST" ])
18
18
def validate_jwt_route (relative_path ):
19
19
return validate_jwt (relative_path )
20
-
20
+
21
21
self .client = self .app .test_client ()
22
22
23
23
@patch ('jwt_proxy.api.proxy_request' ) # Adjust the import path based on where proxy_request is defined
24
24
def test_path_whitelist (self , mock_proxy_request ):
25
- # Mock response
26
- mock_proxy_request .return_value = jsonify (message = "request proxied" )
27
- response = self .client .get ("/allowed_path" )
25
+ # Mock response directly without using jsonify
26
+ mock_proxy_request .return_value = {"message" : "request proxied" }
27
+
28
+ with self .app .app_context ():
29
+ response = self .client .get ("/allowed_path" )
30
+
28
31
self .assertEqual (response .status_code , 200 )
29
32
self .assertEqual (response .json , {"message" : "request proxied" })
30
33
31
34
@patch ('jwt_proxy.api.proxy_request' ) # Adjust the import path based on where proxy_request is defined
32
35
@patch ('jwt.PyJWKClient' )
33
36
@patch ('jwt.decode' )
34
37
def test_valid_token (self , mock_decode , mock_jwks_client , mock_proxy_request ):
35
- mock_proxy_request .return_value = jsonify ( message = " request proxied")
38
+ mock_proxy_request .return_value = { " message" : " request proxied"}
36
39
mock_jwks_client .return_value .get_signing_key_from_jwt .return_value .key = "test-key"
37
40
mock_decode .
return_value = {
"email" :
"[email protected] " }
38
41
39
42
headers = {"Authorization" : "Bearer valid-token" }
40
- response = self .client .get ("/some_path" , headers = headers )
43
+
44
+ with self .app .app_context ():
45
+ response = self .client .get ("/some_path" , headers = headers )
46
+
41
47
self .assertEqual (response .status_code , 200 )
42
48
self .assertEqual (response .json , {"message" : "request proxied" })
43
49
44
50
@patch ('jwt_proxy.api.proxy_request' ) # Adjust the import path based on where proxy_request is defined
45
51
@patch ('jwt.PyJWKClient' )
46
52
@patch ('jwt.decode' )
47
53
def test_missing_token (self , mock_decode , mock_jwks_client , mock_proxy_request ):
48
- response = self .client .get ("/some_path" )
54
+ with self .app .app_context ():
55
+ response = self .client .get ("/some_path" )
56
+
49
57
self .assertEqual (response .status_code , 400 )
50
58
self .assertEqual (response .json , {"message" : "token missing" })
51
59
@@ -57,7 +65,10 @@ def test_expired_token(self, mock_decode, mock_jwks_client, mock_proxy_request):
57
65
mock_decode .side_effect = jwt .exceptions .ExpiredSignatureError ("token expired" )
58
66
59
67
headers = {"Authorization" : "Bearer expired-token" }
60
- response = self .client .get ("/some_path" , headers = headers )
68
+
69
+ with self .app .app_context ():
70
+ response = self .client .get ("/some_path" , headers = headers )
71
+
61
72
self .assertEqual (response .status_code , 401 )
62
73
self .assertEqual (response .json , {"message" : "token expired" })
63
74
0 commit comments