Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adminUsers "add_user" built #58

Merged
merged 8 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyzscaler/zia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .url_filters import URLFilteringAPI
from .users import UserManagementAPI
from .vips import DataCenterVIPSAPI
from .admin_role_management import AdminAndRoleManagementAPI
from .admin_and_role_management import AdminAndRoleManagementAPI


class ZIA(APISession):
Expand Down
72 changes: 72 additions & 0 deletions pyzscaler/zia/admin_and_role_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from restfly.endpoint import APIEndpoint

from pyzscaler.utils import Iterator, snake_to_camel


class AdminAndRoleManagementAPI(APIEndpoint):
def list_users(self, **kwargs):
"""
Returns a list of admin users.

Keyword Args:
**include_auditor_users (bool, optional):
Include or exclude auditor user information in the list.
**include_admin_users (bool, optional):
Include or exclude admin user information in the list. (default: True)
**search (str, optional):
The search string used to partially match against an admin/auditor user's Login ID or Name.
**page (int, optional):
Specifies the page offset.
**page_size (int, optional):
Specifies the page size. The default size is 100, but the maximum size is 1000.

Returns:
:obj:`list`: The admin_users resource record.

Examples:
>>> users = zia.admin_and_role_management.list_users(search='login_name')

"""
return list(Iterator(self._api, "adminUsers", **kwargs))

def add_user(self, **kwargs):
"""
Creates a ZIA Admin User.

Args:
**kwargs:
login_name: str
user_name: str
email: str
role: dict
admin_scope: dict
type: str
scope_entitites: list[dict]
is_non_editable: bool
disabled: bool
is_auditor: bool
password: bool
is_password_login_allowed: bool
is_security_report_comm_enabled: bool
is_service_update_comm_enabled: bool
is_product_update_comm_enabled: bool
is_password_expired: bool
is_exec_mobile_app_enabled: bool
exec_mobile_app_tokens: list[dict]

Returns: dict of user's account

Examples:
>>> admin_user = zia.admin_and_role_management.add_user(
... login_name='username',
... user_name:'Jim Bob',
... email='jim@domain.com'
...)

"""
# Add parameters to payload
payload = {}
for key, value in kwargs.items():
payload[snake_to_camel(key)] = value

return self._post("adminUsers", json=payload)
30 changes: 0 additions & 30 deletions pyzscaler/zia/admin_role_management.py

This file was deleted.

161 changes: 161 additions & 0 deletions tests/zia/test_admin_and_role_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# FIXME: This whole file isn't working as I don't know how, yet, to write a test. Basically I've copy/pasted test_users.py.
import pytest
import responses
from responses import matchers

@pytest.fixture(name="users")
def fixture_users():
return [
{
"id": 1,
"name": "Test User A",
"email": "testusera@example.com",
"groups": {"id": 1, "name": "test"},
"department": {"id": 1, "name": "test_department"},
"comments": "Test",
"adminUser": False,
"isNonEditable": False,
"disabled": False,
"deleted": False,
},
{
"id": 2,
"name": "Test User B",
"email": "testuserb@example.com",
"groups": {"id": 1, "name": "test"},
"department": {"id": 1, "name": "test_department"},
"adminUser": True,
"isNonEditable": False,
"disabled": True,
"deleted": False,
},
]


@responses.activate
def test_users_add_user(zia, users):
responses.add(
method="POST",
url="https://zsapi.zscaler.net/api/v1/users",
json=users[0],
status=200,
match=[
matchers.json_params_matcher(
{
"name": "Test User A",
"email": "testusera@example.com",
"groups": {"id": "1"},
"department": {"id": "1"},
"comments": "Test",
}
)
],
)

resp = zia.users.add_user(
name="Test User A",
email="testusera@example.com",
groups={"id": "1"},
department={"id": "1"},
comments="Test",
)

assert isinstance(resp, dict)
assert resp.id == 1
assert resp.admin_user is False
assert resp.comments == "Test"


@responses.activate
def test_list_users_with_one_page(zia, paginated_items):
items = paginated_items(200)

responses.add(
responses.GET,
url="https://zsapi.zscaler.net/api/v1/users",
json=items[0:100],
status=200,
)
responses.add(
responses.GET,
url="https://zsapi.zscaler.net/api/v1/users",
json=items[100:200],
status=200,
)

resp = zia.users.list_users(max_pages=1, page_size=100)

assert isinstance(resp, list)
assert resp[50].id == 50
assert len(resp) == 100


@responses.activate
def test_list_users_with_two_pages(zia, paginated_items):
items = paginated_items(200)

responses.add(
responses.GET,
url="https://zsapi.zscaler.net/api/v1/users",
json=items[0:100],
status=200,
)
responses.add(
responses.GET,
url="https://zsapi.zscaler.net/api/v1/users",
json=items[100:200],
status=200,
)

resp = zia.users.list_users(max_pages=2, page_size=100)

assert isinstance(resp, list)
assert resp[50].id == 50
assert resp[150].id == 150
assert len(resp) == 200


@responses.activate
def test_list_users_with_max_items_1(zia, paginated_items):
items = paginated_items(200)

responses.add(
responses.GET,
url="https://zsapi.zscaler.net/api/v1/users",
json=items[0:100],
status=200,
)
responses.add(
responses.GET,
url="https://zsapi.zscaler.net/api/v1/users",
json=items[100:200],
status=200,
)

resp = zia.users.list_users(max_items=1)

assert isinstance(resp, list)
assert len(resp) == 1


@responses.activate
def test_list_users_with_max_items_150(zia, paginated_items):
items = paginated_items(200)

responses.add(
responses.GET,
url="https://zsapi.zscaler.net/api/v1/users",
json=items[0:100],
status=200,
)
responses.add(
responses.GET,
url="https://zsapi.zscaler.net/api/v1/users",
json=items[100:200],
status=200,
)

resp = zia.users.list_users(max_items=150)

assert isinstance(resp, list)
assert len(resp) == 150