Skip to content

Commit

Permalink
Register Web Login
Browse files Browse the repository at this point in the history
  • Loading branch information
b0rba committed Mar 13, 2024
1 parent 1db51e2 commit 165ea72
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
27 changes: 27 additions & 0 deletions incognia/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,30 @@ def register_login(self,

except IncogniaHTTPError as e:
raise IncogniaHTTPError(e) from None

def register_web_login(self,
session_token: str,
account_id: str,
external_id: Optional[str] = None,
evaluate: Optional[bool] = None) -> dict:
if not session_token:
raise IncogniaError('session_token is required.')
if not account_id:
raise IncogniaError('account_id is required.')

try:
headers = self.__get_authorization_header()
headers.update(JSON_CONTENT_HEADER)
params = None if evaluate is None else {'eval': evaluate}
body = {
'type': 'login',
'session_token': session_token,
'account_id': account_id,
'external_id': external_id
}
data = encode(body)
return self.__request.post(Endpoints.TRANSACTIONS, headers=headers, params=params,
data=data)

except IncogniaHTTPError as e:
raise IncogniaHTTPError(e) from None
72 changes: 72 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class TestIncogniaAPI(TestCase):
CLIENT_ID: Final[str] = 'ANY_ID'
CLIENT_SECRET: Final[str] = 'ANY_SECRET'
INSTALLATION_ID: Final[str] = 'ANY_INSTALLATION_ID'
SESSION_TOKEN: Final[str] = 'ANY_SESSION_TOKEN'
INVALID_INSTALLATION_ID: Final[str] = 'INVALID_INSTALLATION_ID'
INVALID_SESSION_TOKEN: Final[str] = 'INVALID_SESSION_TOKEN'
ACCOUNT_ID: Final[str] = 'ANY_ACCOUNT_ID'
INVALID_ACCOUNT_ID: Final[str] = 'INVALID_ACCOUNT_ID'
ADDRESS_LINE: Final[str] = 'ANY_ADDRESS_LINE'
Expand Down Expand Up @@ -93,11 +95,21 @@ class TestIncogniaAPI(TestCase):
'installation_id': f'{INSTALLATION_ID}',
'account_id': f'{ACCOUNT_ID}'
})
REGISTER_VALID_WEB_LOGIN_DATA: Final[bytes] = encode({
'type': 'login',
'session_token': f'{SESSION_TOKEN}',
'account_id': f'{ACCOUNT_ID}'
})
REGISTER_INVALID_LOGIN_DATA: Final[bytes] = encode({
'type': 'login',
'installation_id': f'{INVALID_INSTALLATION_ID}',
'account_id': f'{INVALID_ACCOUNT_ID}'
})
REGISTER_INVALID_WEB_LOGIN_DATA: Final[bytes] = encode({
'type': 'login',
'session_token': f'{INVALID_SESSION_TOKEN}',
'account_id': f'{INVALID_ACCOUNT_ID}'
})
DEFAULT_PARAMS: Final[None] = None

@patch.object(BaseRequest, 'post')
Expand Down Expand Up @@ -376,3 +388,63 @@ def test_register_login_when_required_fields_are_invalid_should_raise_an_Incogni
headers=self.AUTH_AND_JSON_CONTENT_HEADERS,
params=self.DEFAULT_PARAMS,
data=self.REGISTER_INVALID_LOGIN_DATA)

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_web_login_when_required_fields_are_valid_should_work(
self, mock_token_manager_get: Mock, mock_base_request_post: Mock):
mock_base_request_post.configure_mock(return_value=self.JSON_RESPONSE)

api = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

request_response = api.register_web_login(self.SESSION_TOKEN, self.ACCOUNT_ID)

mock_token_manager_get.assert_called()
mock_base_request_post.assert_called_with(Endpoints.TRANSACTIONS,
headers=self.AUTH_AND_JSON_CONTENT_HEADERS,
params=self.DEFAULT_PARAMS,
data=self.REGISTER_VALID_WEB_LOGIN_DATA)

self.assertEqual(request_response, self.JSON_RESPONSE)

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_web_login_when_session_token_is_empty_should_raise_an_IncogniaError(
self, mock_token_manager_get: Mock, mock_base_request_post: Mock):
api = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

self.assertRaises(IncogniaError, api.register_web_login, session_token='',
account_id=self.ACCOUNT_ID)

mock_token_manager_get.assert_not_called()
mock_base_request_post.assert_not_called()

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_web_login_when_account_id_is_empty_should_raise_an_IncogniaError(
self, mock_token_manager_get: Mock, mock_base_request_post: Mock):
api = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

self.assertRaises(IncogniaError, api.register_web_login, session_token=self.SESSION_TOKEN,
account_id='')

mock_token_manager_get.assert_not_called()
mock_base_request_post.assert_not_called()

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_web_login_when_required_fields_are_invalid_should_raise_an_IncogniaHTTPError(
self, mock_token_manager_get: Mock, mock_base_request_post: Mock):
mock_base_request_post.configure_mock(side_effect=IncogniaHTTPError)

api = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

self.assertRaises(IncogniaHTTPError, api.register_web_login,
session_token=self.INVALID_SESSION_TOKEN,
account_id=self.INVALID_ACCOUNT_ID)

mock_token_manager_get.assert_called()
mock_base_request_post.assert_called_with(Endpoints.TRANSACTIONS,
headers=self.AUTH_AND_JSON_CONTENT_HEADERS,
params=self.DEFAULT_PARAMS,
data=self.REGISTER_INVALID_WEB_LOGIN_DATA)

0 comments on commit 165ea72

Please sign in to comment.