From 07aa09af1f0062772b6b7864474c91eec7cd2eb1 Mon Sep 17 00:00:00 2001 From: Martin Pentrak Date: Wed, 13 Nov 2024 16:38:31 +0100 Subject: [PATCH] admin/api/accounts/{id}/users done Get admin/api/accounts/find Get admin/api/accounts/plan --- tests/integration/conftest.py | 27 ++++++++++- .../test_integration_account_users.py | 46 +++++++++++++++++++ .../integration/test_integration_accounts.py | 12 +++++ threescale_api/defaults.py | 14 +++++- threescale_api/resources.py | 19 ++++++++ 5 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 tests/integration/test_integration_account_users.py diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index c79850a..d16a882 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -13,7 +13,7 @@ Proxy, Backend, Metric, MappingRule, BackendMappingRule, BackendUsage, ActiveDoc, Webhooks, InvoiceState, - ApplicationKey, ApplicationPlans) + ApplicationKey, ApplicationPlans, AccountUser, AccountUsers) load_dotenv() @@ -109,6 +109,11 @@ def update_account_params(): return dict(org_name=name) +@pytest.fixture(scope='module') +def account_get_plan_params(): + return dict(name="Default") + + @pytest.fixture(scope='module') def account_params(): suffix = get_suffix() @@ -123,6 +128,26 @@ def account(account_params, api): cleanup(entity) +@pytest.fixture(scope='module') +def account_user_update_params(): + suffix = get_suffix() + name = f"test-update-{suffix}" + return dict(username=name, email=f"{name}@email.com") + + +@pytest.fixture(scope='module') +def account_user_params(): + suffix = get_suffix() + name = f"test-{suffix}" + return dict(username=name, email=f"{name}@email.com") + + +@pytest.fixture(scope='module') +def account_user(account,account_user_params) -> AccountUser: + user = account.users.create(account_user_params) + yield user + cleanup(user) + @pytest.fixture(scope='module') def application_plan_params() -> dict: suffix = get_suffix() diff --git a/tests/integration/test_integration_account_users.py b/tests/integration/test_integration_account_users.py new file mode 100644 index 0000000..d5792e2 --- /dev/null +++ b/tests/integration/test_integration_account_users.py @@ -0,0 +1,46 @@ +from tests.integration import asserts +from tests.integration.conftest import account + + +def test_account_user_can_be_created(account_user, account_user_params): + asserts.assert_resource(account_user) + asserts.assert_resource_params(account_user, account_user_params) + + +def test_account_users_list(api,account, account_user): + users = account.users.list() + assert len(users) > 1 + + +def test_account_users_get_by_id(account, account_user, account_user_params): + account_user = account.users.read(account_user['id']) + asserts.assert_resource(account_user) + asserts.assert_resource_params(account_user, account_user_params) + + +def test_account_user_can_be_updated(account_user, account_user_update_params): + updated_user = account_user.update(account_user_update_params) + asserts.assert_resource(updated_user) + asserts.assert_resource_params(updated_user, account_user_update_params) + + +def test_account_user_change_status(account_user): + assert account_user['state'] == 'pending' + updated_account_user = account_user.activate() + assert updated_account_user['state'] == 'active' + + updated_account_user = account_user.suspend() + assert updated_account_user['state'] == 'suspended' + + updated_account_user = account_user.un_suspend() + assert updated_account_user['state'] == 'active' + + +def test_account_user_change_role(account_user): + assert account_user['role'] == 'member' + + updated_account_user = account_user.set_as_admin() + assert updated_account_user['role'] == 'admin' + + updated_account_user = account_user.set_as_member() + assert updated_account_user['role'] == 'member' diff --git a/tests/integration/test_integration_accounts.py b/tests/integration/test_integration_accounts.py index 8f96492..e796ed3 100644 --- a/tests/integration/test_integration_accounts.py +++ b/tests/integration/test_integration_accounts.py @@ -24,11 +24,23 @@ def test_account_can_be_read_by_name(api, account, account_params): asserts.assert_resource_params(read, account_params, ['org_name']) +def test_account_can_be_find_by_name(api, account_user, account_params): + account2 = api.accounts.find(dict(username=account_user['username'])) + asserts.assert_resource(account2) + asserts.assert_resource_params(account2, account_params, ['org_name']) + + def test_account_update(api,account, update_account_params): updated_account = account.update(params=update_account_params) asserts.assert_resource(updated_account) asserts.assert_resource_params(updated_account, update_account_params) +def test_get_account_plan(account, account_get_plan_params): + plan = account.get_account_plan() + asserts.assert_resource(plan) + asserts.assert_resource_params(plan,account_get_plan_params, ['name']) + + def test_users_list(api, account): assert len(account.users.list()) >= 1 diff --git a/threescale_api/defaults.py b/threescale_api/defaults.py index 500f1a5..833316b 100644 --- a/threescale_api/defaults.py +++ b/threescale_api/defaults.py @@ -516,6 +516,16 @@ def suspend(self, **kwargs) -> 'DefaultUserResource': """ return self.set_state(state='suspend', **kwargs) + def un_suspend(self, **kwargs) -> 'DefaultUserResource': + """Un suspends the user + Args: + **kwargs: + Optional arguments + Returns(DefaultUserResource): User instance + + """ + return self.set_state(state='unsuspend', **kwargs) + def resume(self, **kwargs): """Resumes the user Args: @@ -541,7 +551,7 @@ def set_as_admin(self, **kwargs): Returns(DefaultUserResource): User instance """ - return self.set_state(state='set_as_admin', **kwargs) + return self.set_state(state='admin', **kwargs) def set_as_member(self, **kwargs): """Demotes the user to s member @@ -550,4 +560,4 @@ def set_as_member(self, **kwargs): Returns(DefaultUserResource): User instance """ - return self.set_state(state='set_as_member', **kwargs) + return self.set_state(state='member', **kwargs) diff --git a/threescale_api/resources.py b/threescale_api/resources.py index b63cd34..1a9f015 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -188,6 +188,19 @@ def create(self, params: dict = None, **kwargs) -> 'Account': """ return self.signup(params=params, **kwargs) + def find(self, params: dict, **kwargs) -> 'Account': + """Find an account + Args: + params(dict): Parameters to used to find account (name, email, etc) + **kwargs: Optional args + Returns(Account): Account instance + """ + log.info("[FIND] Find accout: paramas:%s, kwarfs=%s", params, kwargs) + find_url = self.url + "/find" + response = self.rest.get(path=find_url, json=params, **kwargs) + instance = self._create_instance(response=response) + return instance + def signup(self, params: dict, **kwargs) -> 'Account': """Sign Up for an account Args: @@ -1470,6 +1483,12 @@ def credit_card_delete(self, params: dict = None, **kwargs): response = self.client.rest.delete(url=url, json=params, **kwargs) return response + def get_account_plan(self, params: dict = None, **kwargs): + account_plans = AccountPlans(self, ApplicationPlan) + url = self.url + "/plan" + response = self.client.rest.get(url=url, json=params, **kwargs) + return account_plans._create_instance(response=response) + class UserPermissions(DefaultResource): pass