Skip to content
Draft
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
18 changes: 12 additions & 6 deletions pyoverkiz/auth/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
from collections.abc import Mapping
from typing import Any, cast

import boto3
from aiohttp import ClientSession, FormData
from botocore.client import BaseClient
from botocore.config import Config
from botocore.exceptions import ClientError
from warrant_lite import WarrantLite

from pyoverkiz.auth.base import AuthContext, AuthStrategy
from pyoverkiz.auth.credentials import (
Expand Down Expand Up @@ -257,9 +252,20 @@ class NexityAuthStrategy(SessionLoginStrategy):

async def login(self) -> None:
"""Perform login using Nexity username and password."""
try:
import boto3
from botocore.config import Config
from botocore.exceptions import ClientError
from warrant_lite import WarrantLite
except ImportError as error:
raise NexityServiceException(
"Nexity authentication requires optional dependencies. Install with the "
"Nexity auth extras."
) from error

loop = asyncio.get_running_loop()

def _client() -> BaseClient:
def _client() -> Any:
return boto3.client(
"cognito-idp", config=Config(region_name=NEXITY_COGNITO_REGION)
)
Expand Down
16 changes: 8 additions & 8 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,12 @@ async def test_login_maps_invalid_credentials_client_error(self):
)
warrant_instance = MagicMock()
warrant_instance.authenticate_user.side_effect = bad_credentials_error
boto3_mock = MagicMock()
boto3_mock.client.return_value = MagicMock()

with (
patch("pyoverkiz.auth.strategies.boto3.client", return_value=MagicMock()),
patch(
"pyoverkiz.auth.strategies.WarrantLite", return_value=warrant_instance
),
patch("boto3.client", boto3_mock.client),
patch("warrant_lite.WarrantLite", return_value=warrant_instance),
pytest.raises(NexityBadCredentialsException),
):
strategy = NexityAuthStrategy(
Expand All @@ -542,12 +542,12 @@ async def test_login_propagates_non_auth_client_error(self):
)
warrant_instance = MagicMock()
warrant_instance.authenticate_user.side_effect = service_error
boto3_mock = MagicMock()
boto3_mock.client.return_value = MagicMock()

with (
patch("pyoverkiz.auth.strategies.boto3.client", return_value=MagicMock()),
patch(
"pyoverkiz.auth.strategies.WarrantLite", return_value=warrant_instance
),
patch("boto3.client", boto3_mock.client),
patch("warrant_lite.WarrantLite", return_value=warrant_instance),
pytest.raises(ClientError, match="InternalErrorException"),
):
strategy = NexityAuthStrategy(
Expand Down