Skip to content

Commit

Permalink
admin/api/accounts/{id}/users done
Browse files Browse the repository at this point in the history
Get admin/api/accounts/find
Get admin/api/accounts/plan
  • Loading branch information
Gituser010 committed Jan 13, 2025
1 parent d0c6bf7 commit 3f47e69
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 3 deletions.
25 changes: 24 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Proxy, Backend, Metric, MappingRule,
BackendMappingRule, BackendUsage,
ActiveDoc, Webhooks, InvoiceState,
ApplicationKey, ApplicationPlans)
ApplicationKey, ApplicationPlans, AccountUser, AccountUsers)

load_dotenv()

Expand Down Expand Up @@ -108,6 +108,9 @@ def update_account_params():
name = f"updated-{suffix}"
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():
Expand All @@ -123,6 +126,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()
Expand Down
42 changes: 42 additions & 0 deletions tests/integration/test_integration_account_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from tests.integration import asserts

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_user):
users = api.accounts.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'
12 changes: 12 additions & 0 deletions tests/integration/test_integration_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_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
14 changes: 12 additions & 2 deletions threescale_api/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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)
19 changes: 19 additions & 0 deletions threescale_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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_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
Expand Down

0 comments on commit 3f47e69

Please sign in to comment.