From d3dbd7b50784634351d5aa6ecc2ff1f00bb5f082 Mon Sep 17 00:00:00 2001 From: mjoshionemind Date: Sat, 23 Dec 2023 17:06:23 +0530 Subject: [PATCH 1/2] added users.account --- docs/users.rst | 5 + jelastic/api/users/__init__.py | 306 ++++++++++++++++++++++++++++++ tests/unit/test_api/test_users.py | 256 +++++++++++++++++++++++++ 3 files changed, 567 insertions(+) create mode 100644 tests/unit/test_api/test_users.py diff --git a/docs/users.rst b/docs/users.rst index 243a5bbe..8f318028 100644 --- a/docs/users.rst +++ b/docs/users.rst @@ -5,3 +5,8 @@ Users :members: :undoc-members: :show-inheritance: + +.. automodule:: jelastic.api.users._Account + :members: + :undoc-members: + :show-inheritance: diff --git a/jelastic/api/users/__init__.py b/jelastic/api/users/__init__.py index 7da62d38..4961022e 100644 --- a/jelastic/api/users/__init__.py +++ b/jelastic/api/users/__init__.py @@ -4,4 +4,310 @@ class Users(ClientAbstract): + """ + The methods of this service provide user account information (such as SSH keys, quotas, etc.) and manage account-related settings. + + + >>> from jelastic import Jelastic + >>> jelastic = Jelastic('https://jca.xapp.cloudmydc.com', token='d6f4e314a5b5fefd164995169f28ae32d987704f') + >>> jelastic.users + + Ref: https://docs.jelastic.com/api/private/#!/api/users + """ + _endpoint1 = "users" + + @property + def Account(self) -> "_Account": + """ + >>> from jelastic import Jelastic + >>> jelastic = Jelastic('https://jca.xapp.cloudmydc.com', token='d6f4e314a5b5fefd164995169f28ae32d987704f') + >>> jelastic.users.Account + + Ref: https://docs.jelastic.com/api/private/#!/api/users.Account + """ + return _Account(session=self._session, token=self._token, debug=self._debug) + + +class _Account(Users): + """ + >>> from jelastic import Jelastic + >>> jelastic = Jelastic('https://jca.xapp.cloudmydc.com', token='d6f4e314a5b5fefd164995169f28ae32d987704f') + >>> jelastic.users.Account + + Ref: https://docs.jelastic.com/api/private/#!/api/users.Account + """ + + _endpoint2 = "account" + + def AddAccount( + self, + email: str, + password: str, + name: list[str] = None, + notify: list[bool] = None, + reseller_id: list[int] = None, + ): + """ + param email: unique email address of the new account. + param password: password for the new account. + param name: an account's name. + param notify: defines whether to send a confirmation letter to the new user email upon success (true) or not (false). + param reseller_id: unique identifier of the target reseller platform. + """ + return self._get( + "AddAccount", + params={ + "email": email, + "password": password, + "name": name, + "notify": notify, + "resellerId": reseller_id, + }, + delimiter=",", + ) + + def AddSSHKey( + self, + title: str, + ssh_key: str, + is_private: bool, + ): + """ + Response which contains new key data and operation result. + + param title: title of the ssh key. + param ssh_key: value of the ssh key. + """ + return self._get( + "AddSSHKey", + params={ + "title": title, + "sshKey": ssh_key, + "isPrivate": is_private, + }, + ) + + def ChangeEmail( + self, + email: str, + redirect_url: list[str] = None, + ): + """ + param email: new account email address (provided by the user). + param redirect_url: a link to display in user email message. + """ + return self._get( + "ChangeEmail", + params={ + "email": email, + "redirectUrl": redirect_url, + }, + delimiter=",", + ) + + def ChangeName( + self, + name: str, + ): + """ + param name: new account name (provided by the user). + """ + return self._get( + "ChangeName", + params={ + "name": name, + }, + ) + + def ChangePassword( + self, + old_password: str, + new_password: str, + invalidate_sessions: list[str] = None, + ): + """ + param old_password: user's current password (as specified by the user). + param new_password: user's new password (as specified by the user). + param invalidate_sessions: defines whether to invalidate all active user sessions except the current one (true) or not (false, by default). + """ + return self._get( + "ChangePassword", + params={ + "oldPassword": old_password, + "newPassword": new_password, + "invalidateSessions": invalidate_sessions, + }, + delimiter=",", + ) + + def CheckUser( + self, + login: str, + ): + return self._get( + "CheckUser", + params={ + "login": login, + }, + ) + + def DeleteSSHKey( + self, + id: int, + ): + """ + param id: unique identifier of the ssh key. + """ + return self._get( + "DeleteSSHKey", + params={ + "id": id, + }, + ) + + def Disable2FA( + self, + password: list[str] = None, + ): + return self._get( + "Disable2FA", + params={ + "password": password, + }, + delimiter=",", + ) + + def Enable2FA( + self, + code: str, + password: list[str] = None, + ): + return self._get( + "Enable2FA", + params={ + "code": code, + "password": password, + }, + delimiter=",", + ) + + def Get2FABackupCodes( + self, + password: list[str] = None, + ): + return self._get( + "Get2FABackupCodes", + params={ + "password": password, + }, + delimiter=",", + ) + + def Get2FAConfig( + self, + password: list[str] = None, + ): + return self._get( + "Get2FAConfig", + params={ + "password": password, + }, + delimiter=",", + ) + + def GetSSHKeys( + self, + is_private: list[str] = None, + ): + return self._get( + "GetSSHKeys", + params={ + "isPrivate": is_private, + }, + delimiter=",", + ) + + def GetUserInfo( + self, + ): + return self._get("GetUserInfo", params={}) + + def GetUserInfoInner( + self, + login: str, + ): + return self._get( + "GetUserInfoInner", + params={ + "login": login, + }, + ) + + def RecoverPassword(self, email: str, lang: list[str] = None): + return self._get( + "RecoverPassword", + params={ + "email": email, + "lang": lang, + }, + delimiter=",", + ) + + def Regenerate2FABackupCodes( + self, + password: list[str] = None, + ): + return self._get( + "Regenerate2FABackupCodes", + params={ + "password": password, + }, + delimiter=",", + ) + + def SetAsTenantHost( + self, + uid: int, + force_change: bool, + ): + """ + param uid: unique identifier of the target user. + param force_change: defines whether to change tenant host if one already exists (true) or not (false). + """ + return self._get( + "SetAsTenantHost", + params={ + "uid": uid, + "forceChange": force_change, + }, + ) + + def SetPassword( + self, + auth_key: str, + invalidate_sessions: list[bool] = None, + ): + """ + param auth_key: authentication key to confirm the operation. + param invalidate_sessions: defines whether to invalidate all active user sessions except the current one (true) or not (false, by default). + """ + return self._get( + "SetPassword", + params={ + "authKey": auth_key, + "invalidateSessions": invalidate_sessions, + }, + delimiter=",", + ) + + def SetUserData( + self, + data: str, + ): + return self._get( + "SetUserData", + params={ + "data": data, + }, + ) diff --git a/tests/unit/test_api/test_users.py b/tests/unit/test_api/test_users.py new file mode 100644 index 00000000..cf192cb6 --- /dev/null +++ b/tests/unit/test_api/test_users.py @@ -0,0 +1,256 @@ +import pytest +from unittest.mock import patch, Mock +from jelastic.api import Users + +success_response = {"error": "", "reason": 0, "result": 0, "source": "billing"} + + +@pytest.fixture +def client(): + with patch("jelastic.api.abstract.ClientAbstract._get") as mock_get: + users = Users(session=Mock(), token="token") + users._get = mock_get + yield users + + +def test_add_account(client): + client._get.return_value = success_response + response = client.Account.AddAccount( + "email", "password", ["name1", "name2"], [True, True], [1, 1] + ) + client._get.assert_called_with( + "AddAccount", + params={ + "email": "email", + "password": "password", + "name": ["name1", "name2"], + "notify": [True, True], + "resellerId": [1, 1], + }, + delimiter=",", + ) + + assert response == success_response + + +def test_add_ssh_key(client): + client._get.return_value = success_response + response = client.Account.AddSSHKey( + "title", + "ssh_key", + True, + ) + client._get.assert_called_with( + "AddSSHKey", + params={ + "title": "title", + "sshKey": "ssh_key", + "isPrivate": True, + }, + ) + assert response == success_response + + +def test_change_email(client): + client._get.return_value = success_response + response = client.Account.ChangeEmail("email", ["redirect_url1", "redirect_url2"]) + client._get.assert_called_with( + "ChangeEmail", + params={ + "email": "email", + "redirectUrl": ["redirect_url1", "redirect_url2"], + }, + delimiter=",", + ) + assert response == success_response + + +def test_change_name(client): + client._get.return_value = success_response + response = client.Account.ChangeName("name") + client._get.assert_called_with( + "ChangeName", + params={ + "name": "name", + }, + ) + assert response == success_response + + +def test_change_password(client): + client._get.return_value = success_response + response = client.Account.ChangePassword( + "old_password", "new_password", ["invalidate_sessions1", "invalidate_sessions2"] + ) + client._get.assert_called_with( + "ChangePassword", + params={ + "oldPassword": "old_password", + "newPassword": "new_password", + "invalidateSessions": ["invalidate_sessions1", "invalidate_sessions2"], + }, + delimiter=",", + ) + assert response == success_response + + +def test_check_user(client): + client._get.return_value = success_response + response = client.Account.CheckUser("login") + client._get.assert_called_with( + "CheckUser", + params={ + "login": "login", + }, + ) + assert response == success_response + + +def test_delete_ssh_key(client): + client._get.return_value = success_response + response = client.Account.DeleteSSHKey(1) + client._get.assert_called_with( + "DeleteSSHKey", + params={ + "id": 1, + }, + ) + assert response == success_response + + +def test_disable_2_fa(client): + client._get.return_value = success_response + response = client.Account.Disable2FA(["password1", "password2"]) + client._get.assert_called_with( + "Disable2FA", + params={"password": ["password1", "password2"]}, + delimiter=",", + ) + assert response == success_response + + +def test_enable_2_fa(client): + client._get.return_value = success_response + response = client.Account.Enable2FA("code", ["password1", "password2"]) + client._get.assert_called_with( + "Enable2FA", + params={"code": "code", "password": ["password1", "password2"]}, + delimiter=",", + ) + assert response == success_response + + +def test_get_2_fa_backup_codes(client): + client._get.return_value = success_response + response = client.Account.Get2FABackupCodes(["password1", "password2"]) + client._get.assert_called_with( + "Get2FABackupCodes", + params={"password": ["password1", "password2"]}, + delimiter=",", + ) + assert response == success_response + + +def test_get_2_fa_config(client): + client._get.return_value = success_response + response = client.Account.Get2FAConfig(["password1", "password2"]) + client._get.assert_called_with( + "Get2FAConfig", + params={"password": ["password1", "password2"]}, + delimiter=",", + ) + assert response == success_response + + +def test_get_ssh_keys(client): + client._get.return_value = success_response + response = client.Account.GetSSHKeys(["is_private1", "is_private2"]) + client._get.assert_called_with( + "GetSSHKeys", + params={"isPrivate": ["is_private1", "is_private2"]}, + delimiter=",", + ) + assert response == success_response + + +def test_get_user_info(client): + client._get.return_value = success_response + response = client.Account.GetUserInfo() + client._get.assert_called_with( + "GetUserInfo", + params={}, + ) + assert response == success_response + + +def test_check_user_info_inner(client): + client._get.return_value = success_response + response = client.Account.GetUserInfoInner("login") + client._get.assert_called_with( + "GetUserInfoInner", + params={ + "login": "login", + }, + ) + assert response == success_response + + +def test_recover_password(client): + client._get.return_value = success_response + response = client.Account.RecoverPassword("email", ["lang1", "lang2"]) + client._get.assert_called_with( + "RecoverPassword", + params={"email": "email", "lang": ["lang1", "lang2"]}, + delimiter=",", + ) + assert response == success_response + + +def test_regenerate_2_fa_backup_codes(client): + client._get.return_value = success_response + response = client.Account.Regenerate2FABackupCodes(["password1", "password2"]) + client._get.assert_called_with( + "Regenerate2FABackupCodes", + params={"password": ["password1", "password2"]}, + delimiter=",", + ) + assert response == success_response + + +def test_set_as_tenant_host(client): + client._get.return_value = success_response + response = client.Account.SetAsTenantHost(1, True) + client._get.assert_called_with( + "SetAsTenantHost", + params={ + "uid": 1, + "forceChange": True, + }, + ) + assert response == success_response + + +def test_set_password(client): + client._get.return_value = success_response + response = client.Account.SetPassword("auth_key", [True, True]) + client._get.assert_called_with( + "SetPassword", + params={ + "authKey": "auth_key", + "invalidateSessions": [True, True], + }, + delimiter=",", + ) + assert response == success_response + + +def test_set_user_data(client): + client._get.return_value = success_response + response = client.Account.SetUserData("data") + client._get.assert_called_with( + "SetUserData", + params={ + "data": "data", + }, + ) + assert response == success_response From 89e0ef72432645610a8ff1b974171caed5487189 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Sun, 24 Dec 2023 22:54:30 +0530 Subject: [PATCH 2/2] fixed merge --- jelastic/api/users/__init__.py | 2 +- tests/unit/test_api/{ => test_users}/test_users.py | 14 +------------- 2 files changed, 2 insertions(+), 14 deletions(-) rename tests/unit/test_api/{ => test_users}/test_users.py (94%) diff --git a/jelastic/api/users/__init__.py b/jelastic/api/users/__init__.py index 733547ca..2bffbfc4 100644 --- a/jelastic/api/users/__init__.py +++ b/jelastic/api/users/__init__.py @@ -26,7 +26,6 @@ def Account(self) -> "_Account": """ return _Account(session=self._session, token=self._token, debug=self._debug) - @property def Authentication(self) -> "_Authentication": """ @@ -84,6 +83,7 @@ def SSO(self) -> "_SSO": """ return _SSO(session=self._session, token=self._token, debug=self._debug) + class _Account(Users): """ >>> from jelastic import Jelastic diff --git a/tests/unit/test_api/test_users.py b/tests/unit/test_api/test_users/test_users.py similarity index 94% rename from tests/unit/test_api/test_users.py rename to tests/unit/test_api/test_users/test_users.py index cf192cb6..0765ddf0 100644 --- a/tests/unit/test_api/test_users.py +++ b/tests/unit/test_api/test_users/test_users.py @@ -1,16 +1,4 @@ -import pytest -from unittest.mock import patch, Mock -from jelastic.api import Users - -success_response = {"error": "", "reason": 0, "result": 0, "source": "billing"} - - -@pytest.fixture -def client(): - with patch("jelastic.api.abstract.ClientAbstract._get") as mock_get: - users = Users(session=Mock(), token="token") - users._get = mock_get - yield users +from . import * def test_add_account(client):