Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/services/dataset_manager/dataset_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def list_datasets(self, page, page_size):
return res_to_dict
elif response.status_code == 404:
SrvErrorHandler.customized_handle(ECustomizedError.USER_DISABLED, True)
elif response.status_code == 401:
SrvErrorHandler.customized_handle(ECustomizedError.INVALID_TOKEN, if_exit=True)
else:
SrvErrorHandler.default_handle(response.content, True)
except Exception as e:
Expand Down
2 changes: 2 additions & 0 deletions app/services/project_manager/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def list_projects(self, page, page_size, order, order_by):
return res_to_dict
elif response.status_code == 404:
SrvErrorHandler.customized_handle(ECustomizedError.USER_DISABLED, True)
elif response.status_code == 401:
SrvErrorHandler.customized_handle(ECustomizedError.INVALID_TOKEN, if_exit=True)
else:
SrvErrorHandler.default_handle(response.content, True)
except Exception:
Expand Down
3 changes: 3 additions & 0 deletions app/services/user_authentication/token_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from app.configs.user_config import UserConfig
from app.models.enums import LoginMethod
from app.models.service_meta_class import MetaService
from app.services.output_manager.error_handler import ECustomizedError
from app.services.output_manager.error_handler import SrvErrorHandler
from app.services.user_authentication.user_login_logout import exchange_api_key

Expand Down Expand Up @@ -93,6 +94,8 @@ def refresh(self, azp: str) -> None:
response = requests.post(url, data=payload, headers=headers)
if response.status_code == 200:
self.update_token(response.json()['access_token'], response.json()['refresh_token'])
elif response.status_code == 401:
SrvErrorHandler.customized_handle(ECustomizedError.INVALID_TOKEN, if_exit=True)
else:
SrvErrorHandler.default_handle(response.content)

Expand Down
2 changes: 2 additions & 0 deletions app/utils/aggregated.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def search_item(project_code, zone, folder_relative_path, item_type, container_t
SrvErrorHandler.customized_handle(ECustomizedError.PERMISSION_DENIED, project_code)
elif res.status_code == 404:
pass
elif res.status_code == 401:
SrvErrorHandler.customized_handle(ECustomizedError.INVALID_TOKEN, if_exit=True)
elif res.status_code != 200:
SrvErrorHandler.default_handle(res.text, True)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "app"
version = "2.9.1"
version = "2.9.2"
description = "This service is designed to support pilot platform"
authors = ["Indoc Systems"]

Expand Down
20 changes: 20 additions & 0 deletions tests/app/services/user_authentication/test_token_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
# Contact Indoc Systems for any questions regarding the use of this source code.

import jwt
import pytest

from app.configs.app_config import AppConfig
from app.configs.user_config import UserConfig
from app.services.user_authentication.token_manager import SrvTokenManager
from tests.conftest import decoded_token


class TestSrvTokenManager:
Expand Down Expand Up @@ -38,3 +41,20 @@ def test_refresh_api_key_calls_keycloak_and_stores_access_token_in_config(self,

assert manager.config.access_token == access_token
assert manager.config.refresh_token == ''

def test_refresh_failed_with_invalid_token(self, requests_mock, mocker, settings, capsys):
manager = SrvTokenManager()
mocker.patch(
'app.services.user_authentication.token_manager.SrvTokenManager.decode_access_token',
return_value=decoded_token(),
)

requests_mock.post(
AppConfig.Connections.url_keycloak_token,
status_code=401,
)

with pytest.raises(SystemExit):
manager.refresh('test_azp')
out, _ = capsys.readouterr()
assert out.rstrip() == 'Your login session has expired. Please try again or log in again.'
2 changes: 1 addition & 1 deletion tests/app/utils/test_aggregated.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ def test_search_file_error_handling_with_401(requests_mock, mocker, capsys):
with pytest.raises(SystemExit):
search_item(test_project_code, 'zone', 'folder_relative_path', 'file', 'project')
out, _ = capsys.readouterr()
assert out.rstrip() == 'Authentication failed.'
assert out.rstrip() == 'Your login session has expired. Please try again or log in again.'