8000 Making the user list operations a no-op userlist not populated by graysonarts · Pull Request #68 · tableau/server-client-python · GitHub
[go: up one dir, main page]

Skip to content

Making the user list operations a no-op userlist not populated #68

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

Merged
merged 1 commit into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from all 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 setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='tableauserverclient',
version='0.1',
version='0.2.dev0',
author='Tableau',
author_email='github@tableau.com',
url='https://github.com/tableau/server-client-python',
Expand Down
41 changes: 28 additions & 13 deletions tableauserverclient/server/endpoint/groups_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .endpoint import Endpoint
from .exceptions import MissingRequiredFieldError
from ...models.exceptions import UnpopulatedPropertyError
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not even sure the import rules for triple . -- is this above or below the .. imports? :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... is the same as ../.. on the command line. I know it's a bit annoying, but we need relative imports.

from .. import RequestFactory, GroupItem, UserItem, PaginationItem
import logging

Expand Down Expand Up @@ -47,7 +48,31 @@ def delete(self, group_id):

# Removes 1 user from 1 group
def remove_user(self, group_item, user_id):
user_set = group_item.users
self._remove_user(group_item, user_id)
try:
user_set = group_item.users
for user in user_set:
if user.id == user_id:
user_set.remove(user)
break
except UnpopulatedPropertyError:
# If we aren't populated, do nothing to the user list
pass
logger.info('Removed user (id: {0}) from group (ID: {1})'.format(user_id, group_item.id))

# Adds 1 user to 1 group
def add_user(self, group_item, user_id):
new_user = self._add_user(group_item, user_id)
try:
user_set = group_item.users
user_set.add(new_user)
group_item._set_users(user_set)
except UnpopulatedPropertyError:
# If we aren't populated, do nothing to the user list
pass
logger.info('Added user (id: {0}) to group (ID: {1})'.format(user_id, group_item.id))

def _remove_user(self, group_item, user_id):
if not group_item.id:
error = "Group item missing ID."
raise MissingRequiredFieldError(error)
Expand All @@ -56,15 +81,8 @@ def remove_user(self, group_item, user_id):
raise ValueError(error)
url = "{0}/{1}/users/{2}".format(self.baseurl, group_item.id, user_id)
self.delete_request(url)
for user in user_set:
if user.id == user_id:
user_set.remove(user)
break
logger.info('Removed user (id: {0}) from group (ID: {1})'.format(user_id, group_item.id))

# Adds 1 user to 1 group
def add_user(self, group_item, user_id):
user_set = group_item.users
def _add_user(self, group_item, user_id):
if not group_item.id:
error = "Group item missing ID."
raise MissingRequiredFieldError(error)
Expand All @@ -74,7 +92,4 @@ def add_user(self, group_item, user_id):
url = "{0}/{1}/users".format(self.baseurl, group_item.id)
add_req = RequestFactory.Group.add_user_req(user_id)
server_response = self.post_request(url, add_req)
new_user = UserItem.from_response(server_response.content).pop()
user_set.add(new_user)
group_item._set_users(user_set)
logger.info('Added user (id: {0}) to group (ID: {1})'.format(user_id, group_item.id))
return UserItem.from_response(server_response.content).pop()
27 changes: 21 additions & 6 deletions test/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,17 @@ def test_add_user(self):
self.assertEqual('ServerAdministrator', user.site_role)

def test_add_user_before_populating(self):
single_group = TSC.GroupItem('test')
self.assertRaises(TSC.UnpopulatedPropertyError, self.server.groups.add_user, single_group,
'5de011f8-5aa9-4d5b-b991-f462c8dd6bb7')
with open(GET_XML, 'rb') as f:
get_xml_response = f.read().decode('utf-8')
with open(ADD_USER, 'rb') as f:
add_user_response = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.get(self.baseurl, text=get_xml_response)
m.post('http://test/api/2.3/sites/dad65087-b08b-4603-af4e-2887b8aafc67/groups/ef8b19c0-43b6-11e6-af50'
'-63f5805dbe3c/users', text=add_user_response)
all_groups, pagination_item = self.server.groups.get()
single_group = all_groups[0]
self.server.groups.add_user(single_group, '5de011f8-5aa9-4d5b-b991-f462c8dd6bb7')

def test_add_user_missing_user_id(self):
with open(POPULATE_USERS, 'rb') as f:
Expand All @@ -120,9 +128,16 @@ def test_add_user_missing_group_id(self):
'5de011f8-5aa9-4d5b-b991-f462c8dd6bb7')

def test_remove_user_before_populating(self):
single_group = TSC.GroupItem('test')
self.assertRaises(TSC.UnpopulatedPropertyError, self.server.groups.remove_user, single_group,
'5de011f8-5aa9-4d5b-b991-f462c8dd6bb7')
with open(GET_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.get(self.baseurl, text=response_xml)
m.delete('http://test/api/2.3/sites/dad65087-b08b-4603-af4e-2887b8aafc67/groups/ef8b19c0-43b6-11e6-af50'
'-63f5805dbe3c/users/5de011f8-5aa9-4d5b-b991-f462c8dd6bb7',
text='ok')
all_groups, pagination_item = self.server.groups.get()
single_group = all_groups[0]
self.server.groups.remove_user(single_group, '5de011f8-5aa9-4d5b-b991-f462c8dd6bb7')

def test_remove_user_missing_user_id(self):
with open(POPULATE_USERS, 'rb') as f:
Expand Down
0