2929
3030class TestE2EBearerAuth :
3131 """End-to-end tests for bearer authentication."""
32-
32+
3333 def setup_method (self ):
3434 """Set up test environment."""
3535 CONF .clear ()
3636 config .parse_args ([])
37-
37+
3838 # Reset the global APP
3939 api .APP = None
4040
@@ -48,7 +48,9 @@ def create_mock_model(self):
4848 "author" : "Test Author" ,
4949 }
5050 mock_model .get_predict_args .return_value = {}
51- mock_model .predict = unittest .mock .AsyncMock (return_value = {"result" : "success" })
51+ mock_model .predict = unittest .mock .AsyncMock (
52+ return_value = {"result" : "success" }
53+ )
5254 mock_model .has_schema = False
5355 mock_model .response_schema = None # Important: set response_schema to None
5456 mock_model .warm = unittest .mock .MagicMock ()
@@ -57,52 +59,56 @@ def create_mock_model(self):
5759 def test_api_access_without_auth (self ):
5860 """Test API access when authentication is disabled."""
5961 CONF .set_override ("auth_bearer_token" , "" )
60-
61- with unittest .mock .patch ('deepaas.model.load_v2_model' ) as mock_load :
62+
63+ with unittest .mock .patch ('deepaas.model.load_v2_model' ):
6264 with unittest .mock .patch .object (model , 'V2_MODEL_NAME' , 'test-model' ):
63- with unittest .mock .patch .object (model , 'V2_MODEL' , self .create_mock_model ()):
65+ with unittest .mock .patch .object (
66+ model , 'V2_MODEL' , self .create_mock_model ()
67+ ):
6468 app = api .get_fastapi_app ()
6569 client = fastapi .testclient .TestClient (app )
66-
70+
6771 # Test root endpoint
6872 response = client .get ("/" )
6973 assert response .status_code == 200
70-
74+
7175 # Test v2 version endpoint
7276 response = client .get ("/v2/" )
7377 assert response .status_code == 200
74-
78+
7579 # Test models endpoint
7680 response = client .get ("/v2/models/" )
7781 assert response .status_code == 200
78-
82+
7983 # Test individual model endpoint
8084 response = client .get ("/v2/models/test-model" )
8185 assert response .status_code == 200
8286
8387 def test_api_access_with_auth_no_token (self ):
8488 """Test API access when authentication is enabled but no token provided."""
8589 CONF .set_override ("auth_bearer_token" , "my-secret-token" )
86-
87- with unittest .mock .patch ('deepaas.model.load_v2_model' ) as mock_load :
90+
91+ with unittest .mock .patch ('deepaas.model.load_v2_model' ):
8892 with unittest .mock .patch .object (model , 'V2_MODEL_NAME' , 'test-model' ):
89- with unittest .mock .patch .object (model , 'V2_MODEL' , self .create_mock_model ()):
93+ with unittest .mock .patch .object (
94+ model , 'V2_MODEL' , self .create_mock_model ()
95+ ):
9096 app = api .get_fastapi_app ()
9197 client = fastapi .testclient .TestClient (app )
92-
98+
9399 # Test root endpoint (should still work - not protected)
94100 response = client .get ("/" )
95101 assert response .status_code == 200
96-
102+
97103 # Test v2 version endpoint (should still work - not protected)
98104 response = client .get ("/v2/" )
99105 assert response .status_code == 200
100-
106+
101107 # Test models endpoint (should require auth)
102108 response = client .get ("/v2/models/" )
103109 assert response .status_code == 401
104110 assert "Bearer token required" in response .json ()["detail" ]
105-
111+
106112 # Test individual model endpoint (should require auth)
107113 response = client .get ("/v2/models/test-model" )
108114 assert response .status_code == 401
@@ -111,20 +117,22 @@ def test_api_access_with_auth_no_token(self):
111117 def test_api_access_with_auth_invalid_token (self ):
112118 """Test API access with invalid bearer token."""
113119 CONF .set_override ("auth_bearer_token" , "my-secret-token" )
114-
115- with unittest .mock .patch ('deepaas.model.load_v2_model' ) as mock_load :
120+
121+ with unittest .mock .patch ('deepaas.model.load_v2_model' ):
116122 with unittest .mock .patch .object (model , 'V2_MODEL_NAME' , 'test-model' ):
117- with unittest .mock .patch .object (model , 'V2_MODEL' , self .create_mock_model ()):
123+ with unittest .mock .patch .object (
124+ model , 'V2_MODEL' , self .create_mock_model ()
125+ ):
118126 app = api .get_fastapi_app ()
119127 client = fastapi .testclient .TestClient (app )
120-
128+
121129 headers = {"Authorization" : "Bearer wrong-token" }
122-
130+
123131 # Test models endpoint with wrong token
124132 response = client .get ("/v2/models/" , headers = headers )
125133 assert response .status_code == 401
126134 assert "Invalid bearer token" in response .json ()["detail" ]
127-
135+
128136 # Test individual model endpoint with wrong token
129137 response = client .get ("/v2/models/test-model" , headers = headers )
130138 assert response .status_code == 401
@@ -133,23 +141,25 @@ def test_api_access_with_auth_invalid_token(self):
133141 def test_api_access_with_auth_valid_token (self ):
134142 """Test API access with valid bearer token."""
135143 CONF .set_override ("auth_bearer_token" , "my-secret-token" )
136-
137- with unittest .mock .patch ('deepaas.model.load_v2_model' ) as mock_load :
144+
145+ with unittest .mock .patch ('deepaas.model.load_v2_model' ):
138146 with unittest .mock .patch .object (model , 'V2_MODEL_NAME' , 'test-model' ):
139- with unittest .mock .patch .object (model , 'V2_MODEL' , self .create_mock_model ()):
147+ with unittest .mock .patch .object (
148+ model , 'V2_MODEL' , self .create_mock_model ()
149+ ):
140150 app = api .get_fastapi_app ()
141151 client = fastapi .testclient .TestClient (app )
142-
152+
143153 headers = {"Authorization" : "Bearer my-secret-token" }
144-
154+
145155 # Test models endpoint with correct token
146156 response = client .get ("/v2/models/" , headers = headers )
147157 assert response .status_code == 200
148158 data = response .json ()
149159 assert "models" in data
150160 assert len (data ["models" ]) == 1
151161 assert data ["models" ][0 ]["name" ] == "Test Model"
152-
162+
153163 # Test individual model endpoint with correct token
154164 response = client .get ("/v2/models/test-model" , headers = headers )
155165 assert response .status_code == 200
@@ -160,21 +170,25 @@ def test_api_access_with_auth_valid_token(self):
160170 def test_predict_endpoint_with_auth (self ):
161171 """Test predict endpoint with authentication."""
162172 CONF .set_override ("auth_bearer_token" , "my-secret-token" )
163-
164- with unittest .mock .patch ('deepaas.model.load_v2_model' ) as mock_load :
173+
174+ with unittest .mock .patch ('deepaas.model.load_v2_model' ):
165175 with unittest .mock .patch .object (model , 'V2_MODEL_NAME' , 'test-model' ):
166- with unittest .mock .patch .object (model , 'V2_MODEL' , self .create_mock_model ()):
176+ with unittest .mock .patch .object (
177+ model , 'V2_MODEL' , self .create_mock_model ()
178+ ):
167179 app = api .get_fastapi_app ()
168180 client = fastapi .testclient .TestClient (app )
169-
181+
170182 # Test predict endpoint without auth
171183 response = client .post ("/v2/models/test-model/predict" )
172184 assert response .status_code == 401
173185 assert "Bearer token required" in response .json ()["detail" ]
174-
186+
175187 # Test predict endpoint with correct auth
176188 headers = {"Authorization" : "Bearer my-secret-token" }
177- response = client .post ("/v2/models/test-model/predict" , headers = headers )
189+ response = client .post (
190+ "/v2/models/test-model/predict" , headers = headers
191+ )
178192 assert response .status_code == 200
179193 data = response .json ()
180- assert "predictions" in data or "result" in data
194+ assert "predictions" in data or "result" in data
0 commit comments