|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | """ |
3 | | - * TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available. |
4 | | - * Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. |
5 | | - * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at http://opensource.org/licenses/MIT |
7 | | - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
8 | | - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
9 | | - * specific language governing permissions and limitations under the License. |
| 3 | +* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available. |
| 4 | +* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | +* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at http://opensource.org/licenses/MIT |
| 7 | +* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 8 | +* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 | +* specific language governing permissions and limitations under the License. |
10 | 10 | """ |
| 11 | + |
11 | 12 | import pytest |
12 | 13 | from django.contrib.auth.models import AnonymousUser |
13 | 14 | from django.core.cache import caches |
|
16 | 17 | from apigw_manager.apigw import authentication, providers |
17 | 18 | from apigw_manager.apigw.providers import CachePublicKeyProvider, DefaultJWTProvider, SettingsPublicKeyProvider |
18 | 19 |
|
| 20 | + |
19 | 21 | @pytest.fixture() |
20 | 22 | def mock_response(mocker): |
21 | 23 | return mocker.MagicMock() |
@@ -70,9 +72,9 @@ def jwt_request(fake_gateway_name, jwt_decoded, mock_request): |
70 | 72 |
|
71 | 73 | @pytest.fixture() |
72 | 74 | def invalid_apigw_request(mock_request): |
73 | | - mock_request.META[ |
74 | | - "HTTP_X_BKAPI_JWT" |
75 | | - ] = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoibHljIn0.iHy-g0R-q3sVnO16gTHV0FAIViEuKMGCtNLNVYSJX5c" |
| 75 | + mock_request.META["HTTP_X_BKAPI_JWT"] = ( |
| 76 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoibHljIn0.iHy-g0R-q3sVnO16gTHV0FAIViEuKMGCtNLNVYSJX5c" |
| 77 | + ) |
76 | 78 |
|
77 | 79 | return mock_request |
78 | 80 |
|
@@ -243,16 +245,38 @@ def _setup_backend(self): |
243 | 245 |
|
244 | 246 | def test_authenticate_user(self, mock_request): |
245 | 247 | user = self.backend.authenticate( |
246 | | - mock_request, gateway_name="test", bk_username="admin", tenant_id="system", verified=True |
| 248 | + mock_request, |
| 249 | + gateway_name="test", |
| 250 | + bk_username="admin", |
| 251 | + verified=True, |
| 252 | + tenant_id="system", |
| 253 | + ) |
| 254 | + assert not isinstance(user, AnonymousUser) |
| 255 | + assert user.username == "admin" |
| 256 | + assert user.is_authenticated is True |
| 257 | + assert user.tenant_id == "system" |
| 258 | + |
| 259 | + def test_authenticate_user_no_tenant_id(self, mock_request): |
| 260 | + user = self.backend.authenticate( |
| 261 | + mock_request, |
| 262 | + gateway_name="test", |
| 263 | + bk_username="admin", |
| 264 | + verified=True, |
247 | 265 | ) |
248 | 266 | assert not isinstance(user, AnonymousUser) |
249 | 267 | assert user.username == "admin" |
250 | 268 | assert user.is_authenticated is True |
| 269 | + assert user.tenant_id == "" |
251 | 270 |
|
252 | 271 | def test_authenticate_anonymous_user(self, mock_request): |
253 | 272 | user = self.backend.authenticate( |
254 | | - mock_request, gateway_name="test", bk_username="admin", tenant_id="system", verified=False |
| 273 | + mock_request, |
| 274 | + gateway_name="test", |
| 275 | + bk_username="admin", |
| 276 | + verified=False, |
| 277 | + tenant_id="system", |
255 | 278 | ) |
256 | 279 | assert isinstance(user, AnonymousUser) |
257 | 280 | assert user.username == "admin" |
258 | 281 | assert user.is_authenticated is False |
| 282 | + assert user.tenant_id == "" |
0 commit comments