-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Security Logs methods support (#149)
- Loading branch information
1 parent
5b2c756
commit 627bcb6
Showing
7 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pdoc__ = {"tests": False} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from enum import Enum | ||
|
||
|
||
class SecurityLogEvent(Enum): | ||
LOGIN = "login" | ||
PASSWORD_SET = "password.set" | ||
PASSWORD_CHANGE = "password.change" | ||
EMAIL_CHANGE = "email.change" | ||
LOGIN_CHANGE = "login.change" | ||
PERSONAL_TOKEN_ISSUED = "personal_token.issued" | ||
PERSONAL_TOKEN_REVOKED = "personal_token.revoked" | ||
MFA_ENABLED = "mfa.enabled" | ||
MFA_DISABLED = "mfa.disabled" | ||
SESSION_REVOKE = "session.revoke" | ||
SESSION_REVOKE_ALL = "session.revoke_all" | ||
SSO_CONNECT = "sso.connect" | ||
SSO_DISCONNECT = "sso.disconnect" | ||
USER_REMOVE = "user.remove" | ||
APPLICATION_CONNECTED = "application.connected" | ||
APPLICATION_DISCONNECTED = "application.disconnected" | ||
WEBAUTHN_CREATED = "webauthn.created" | ||
WEBAUTHN_DELETED = "webauthn.deleted" | ||
TRUSTED_DEVICE_REMOVE = "trusted_device.remove" | ||
TRUSTED_DEVICE_REMOVE_ALL = "trusted_device.remove_all" | ||
DEVICE_VERIFICATION_ENABLED = "device_verification.enabled" | ||
DEVICE_VERIFICATION_DISABLED = "device_verification.disabled" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from crowdin_api.api_resources.abstract.resources import BaseResource | ||
from crowdin_api.api_resources.security_logs.enums import SecurityLogEvent | ||
|
||
|
||
class SecurityLogsResource(BaseResource): | ||
""" | ||
Resource for Security Logs | ||
Link to documentaion: | ||
https://developer.crowdin.com/api/v2/#tag/Security-Logs | ||
Link to documentation for enterprise: | ||
https://developer.crowdin.com/enterprise/api/v2/#tag/Security-Logs | ||
""" | ||
|
||
def get_user_security_logs_path( | ||
self, userId: int, securityLogId: Optional[int] = None | ||
): | ||
if securityLogId is not None: | ||
return f"/users/{userId}/security-logs/{securityLogId}" | ||
return f"/users/{userId}/security-logs" | ||
|
||
def list_user_security_logs( | ||
self, | ||
userId: int, | ||
event: Optional[SecurityLogEvent] = None, | ||
createdAfter: Optional[datetime] = None, | ||
createdBefore: Optional[datetime] = None, | ||
ipAddress: Optional[str] = None, | ||
limit: Optional[int] = None, | ||
offset: Optional[int] = None, | ||
page: Optional[int] = None, | ||
): | ||
""" | ||
List User Security Logs | ||
Link to documentaion: | ||
https://developer.crowdin.com/api/v2/#operation/api.users.security-logs.getMany | ||
""" | ||
|
||
params = { | ||
"event": event, | ||
"createdAfter": createdAfter, | ||
"createdBefore": createdBefore, | ||
"ipAddress": ipAddress, | ||
} | ||
params.update(self.get_page_params(page=page, offset=offset, limit=limit)) | ||
|
||
return self._get_entire_data( | ||
method="get", | ||
path=self.get_user_security_logs_path(userId=userId), | ||
params=params, | ||
) | ||
|
||
def get_user_security_log(self, userId: int, securityLogId: int): | ||
""" | ||
Get User Security Log | ||
Link to documentation: | ||
https://developer.crowdin.com/api/v2/#operation/api.users.security-logs.get | ||
""" | ||
|
||
return self.requester.request( | ||
method="get", | ||
path=self.get_user_security_logs_path( | ||
userId=userId, securityLogId=securityLogId | ||
), | ||
) | ||
|
||
def get_organization_security_logs_path(self, securityLogId: Optional[int] = None): | ||
if securityLogId is not None: | ||
return f"/security-logs/{securityLogId}" | ||
return "/security-logs" | ||
|
||
def list_organization_security_logs( | ||
self, | ||
event: Optional[SecurityLogEvent] = None, | ||
createdAfter: Optional[datetime] = None, | ||
createdBefore: Optional[datetime] = None, | ||
ipAddress: Optional[str] = None, | ||
userId: Optional[int] = None, | ||
limit: Optional[int] = None, | ||
offset: Optional[int] = None, | ||
page: Optional[int] = None, | ||
): | ||
""" | ||
List Organization Security Logs | ||
Link to documentation: | ||
https://developer.crowdin.com/enterprise/api/v2/#operation/api.security-logs.getMany | ||
""" | ||
|
||
params = { | ||
"event": event, | ||
"createdAfter": createdAfter, | ||
"createdBefore": createdBefore, | ||
"ipAddress": ipAddress, | ||
"userId": userId, | ||
} | ||
params.update(self.get_page_params(page=page, offset=offset, limit=limit)) | ||
|
||
return self._get_entire_data( | ||
method="get", | ||
path=self.get_organization_security_logs_path(), | ||
params=params, | ||
) | ||
|
||
def get_organization_security_log(self, securityLogId: int): | ||
""" | ||
Get Organization Security Log | ||
Link to documentaion: | ||
https://developer.crowdin.com/enterprise/api/v2/#operation/api.security-logs.get | ||
""" | ||
|
||
return self.requester.request( | ||
method="get", | ||
path=self.get_organization_security_logs_path(securityLogId=securityLogId), | ||
) |
165 changes: 165 additions & 0 deletions
165
crowdin_api/api_resources/security_logs/tests/test_security_logs_resource.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
from datetime import datetime | ||
from unittest import mock | ||
|
||
import pytest | ||
from crowdin_api.api_resources.security_logs.enums import SecurityLogEvent | ||
from crowdin_api.api_resources.security_logs.resource import SecurityLogsResource | ||
from crowdin_api.requester import APIRequester | ||
|
||
|
||
class TestSecurityLogsResource: | ||
resource_class = SecurityLogsResource | ||
|
||
def get_resource(self, base_absolut_url): | ||
return self.resource_class(requester=APIRequester(base_url=base_absolut_url)) | ||
|
||
@pytest.mark.parametrize( | ||
"incoming_data, path", | ||
( | ||
({"userId": 1}, "/users/1/security-logs"), | ||
({"userId": 1, "securityLogId": 2}, "/users/1/security-logs/2"), | ||
), | ||
) | ||
def test_get_user_security_logs_path(self, incoming_data, path, base_absolut_url): | ||
resource = self.get_resource(base_absolut_url) | ||
assert resource.get_user_security_logs_path(**incoming_data) == path | ||
|
||
@pytest.mark.parametrize( | ||
"in_params, request_params", | ||
( | ||
( | ||
{ | ||
"event": SecurityLogEvent.LOGIN, | ||
"createdAfter": datetime(year=2024, month=1, day=1, hour=12), | ||
"createdBefore": datetime(year=2024, month=1, day=30, hour=12), | ||
"ipAddress": "127.0.0.1", | ||
"offset": 0, | ||
"limit": 10, | ||
}, | ||
{ | ||
"event": SecurityLogEvent.LOGIN, | ||
"createdAfter": datetime(year=2024, month=1, day=1, hour=12), | ||
"createdBefore": datetime(year=2024, month=1, day=30, hour=12), | ||
"ipAddress": "127.0.0.1", | ||
"offset": 0, | ||
"limit": 10, | ||
}, | ||
), | ||
( | ||
{ | ||
"offset": 0, | ||
"limit": 10, | ||
}, | ||
{ | ||
"event": None, | ||
"createdAfter": None, | ||
"createdBefore": None, | ||
"ipAddress": None, | ||
"offset": 0, | ||
"limit": 10, | ||
}, | ||
), | ||
), | ||
) | ||
@mock.patch("crowdin_api.requester.APIRequester.request") | ||
def test_list_user_security_logs( | ||
self, m_request, in_params, request_params, base_absolut_url | ||
): | ||
m_request.return_value = "response" | ||
|
||
resource = self.get_resource(base_absolut_url) | ||
assert resource.list_user_security_logs(userId=1, **in_params) == "response" | ||
m_request.assert_called_once_with( | ||
method="get", | ||
path=resource.get_user_security_logs_path(userId=1), | ||
params=request_params, | ||
) | ||
|
||
@mock.patch("crowdin_api.requester.APIRequester.request") | ||
def test_get_user_security_log(self, m_request, base_absolut_url): | ||
m_request.return_value = "response" | ||
|
||
resource = self.get_resource(base_absolut_url) | ||
assert resource.get_user_security_log(userId=1, securityLogId=2) == "response" | ||
m_request.assert_called_once_with( | ||
method="get", | ||
path=resource.get_user_security_logs_path(userId=1, securityLogId=2), | ||
) | ||
|
||
@pytest.mark.parametrize( | ||
"incoming_data, path", | ||
( | ||
({}, "/security-logs"), | ||
({"securityLogId": 1}, "/security-logs/1"), | ||
), | ||
) | ||
def test_get_organization_security_logs_path( | ||
self, incoming_data, path, base_absolut_url | ||
): | ||
resource = self.get_resource(base_absolut_url) | ||
assert resource.get_organization_security_logs_path(**incoming_data) == path | ||
|
||
@pytest.mark.parametrize( | ||
"in_params, request_params", | ||
( | ||
( | ||
{ | ||
"event": SecurityLogEvent.LOGIN, | ||
"createdAfter": datetime(year=2024, month=1, day=1, hour=12), | ||
"createdBefore": datetime(year=2024, month=1, day=30, hour=12), | ||
"ipAddress": "127.0.0.1", | ||
"userId": 1, | ||
"offset": 0, | ||
"limit": 10, | ||
}, | ||
{ | ||
"event": SecurityLogEvent.LOGIN, | ||
"createdAfter": datetime(year=2024, month=1, day=1, hour=12), | ||
"createdBefore": datetime(year=2024, month=1, day=30, hour=12), | ||
"ipAddress": "127.0.0.1", | ||
"userId": 1, | ||
"offset": 0, | ||
"limit": 10, | ||
}, | ||
), | ||
( | ||
{ | ||
"offset": 0, | ||
"limit": 10, | ||
}, | ||
{ | ||
"event": None, | ||
"createdAfter": None, | ||
"createdBefore": None, | ||
"ipAddress": None, | ||
"userId": None, | ||
"offset": 0, | ||
"limit": 10, | ||
}, | ||
), | ||
), | ||
) | ||
@mock.patch("crowdin_api.requester.APIRequester.request") | ||
def test_list_organization_security_logs( | ||
self, m_request, in_params, request_params, base_absolut_url | ||
): | ||
m_request.return_value = "response" | ||
|
||
resource = self.get_resource(base_absolut_url) | ||
assert resource.list_organization_security_logs(**in_params) == "response" | ||
m_request.assert_called_once_with( | ||
method="get", | ||
path=resource.get_organization_security_logs_path(), | ||
params=request_params, | ||
) | ||
|
||
@mock.patch("crowdin_api.requester.APIRequester.request") | ||
def test_get_organization_security_log(self, m_request, base_absolut_url): | ||
m_request.return_value = "response" | ||
|
||
resource = self.get_resource(base_absolut_url) | ||
assert resource.get_organization_security_log(securityLogId=1) == "response" | ||
m_request.assert_called_once_with( | ||
method="get", | ||
path=resource.get_organization_security_logs_path(securityLogId=1), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters