def get(self, arg, value): """Retrieve single user record by id or username Warnings: User display names are not unique. If using `display_name`, method will fail if multiple Users are returned with the same display name Keyword Args: id (str): Full User ID display_name (str): User display name Returns: User: User instance matching provided inputs Raises: TypeError: Unexpected or more than one keyword argument provided ValueError: No matching user found based on provided inputs, or multiple Users with same display name """ if arg == 'id': response = self._swimlane.request('get', 'user/{}'.format(value)) try: user_data = response.json() except ValueError: raise ValueError( 'Unable to find user with ID "{}"'.format(value)) return User(self._swimlane, user_data) else: response = self._swimlane.request( 'get', 'user/search?query={}'.format(quote_plus(value))) matched_users = response.json() # Display name not unique, fail if multiple users share the same target display name target_matches = [] for user_data in matched_users: user_display_name = user_data.get('displayName') if user_display_name == value: target_matches.append(user_data) # No matches if not target_matches: raise ValueError( 'Unable to find user with display name "{}"'.format(value)) # Multiple matches if len(target_matches) > 1: raise ValueError( 'Multiple users returned with display name "{}". Matching user IDs: {}' .format( value, ', '.join( ['"{}"'.format(r['id']) for r in target_matches]))) return User(self._swimlane, target_matches[0])
def authenticate(self, username, password, verify_ssl=True): """Send login request and return User instance and login headers""" # Explicitly provide verify_ssl argument, appears to not consistently be acknowledged across versions during # initial setup for auth resp = self._swimlane.request('post', 'user/login', json={ 'userName': username, 'password': password, 'domain': '' }, verify=verify_ssl) json_content = resp.json() # Check for token in response content token = json_content.pop('token', None) if token is None: # Legacy cookie authentication (2.13-) headers = { 'Cookie': ';'.join(["%s=%s" % cookie for cookie in resp.cookies.items()]) } else: # JWT auth (2.14+) headers = {'Authorization': 'Bearer {}'.format(token)} # User user = User(self._swimlane, _user_raw_from_login_content(json_content)) return user, headers
def list(self): """Retrieve all users Returns: :class:`list` of :class:`~swimlane.core.resources.usergroup.User`: List of all Users """ response = self._swimlane.request('get', "user") return [ User(self._swimlane, raw_user_data) for raw_user_data in response.json().get('users', []) ]
def test_type_checks(mock_swimlane, raw): """Use User class to test $type validation""" with pytest.raises(TypeError): User(mock_swimlane, raw)
def test_find_by_name_does_not_exist(self, mock_client): mock_client.get.return_value = [] users = User.find(name='Some Other User') self.assertEqual(list(users), [])
def test_find_by_name(self, mock_client): mock_client.get.return_value = MOCK_USERS users = list(User.find(name='Mock User')) self.assertEqual(len(users), 1) self.assertIsInstance(users[0], User)
def test_find_by_id(self, mock_client): mock_client.get.return_value = MOCK_USER user = User.find(user_id='123') self.assertIsInstance(user, User) self.assertEqual(user.id, '123')
def test_get_user_selection(self): user = User(MOCK_USER) user_selection = User.get_user_selection(user) self.assertEqual(user_selection['id'], '123') self.assertEqual(user_selection['name'], 'Mock User')
def test_init(self): user = User(MOCK_USER) for key, value in MOCK_USER.items(): self.assertEqual(getattr(user, key), value)
import mock import unittest from swimlane.core.resources import Report, User from swimlane.core.search.filtering import create_filter, EQ MOCK_REPORT = { 'id': '123', 'name': 'Mock Report' } MOCK_USER = User(fields={ 'id': '123', 'name': 'Mock User' }) class ReportTestCase(unittest.TestCase): def test_init(self): report = Report(MOCK_REPORT) for key, value in MOCK_REPORT.items(): self.assertEqual(getattr(report, key), value) @mock.patch('swimlane.core.resources.report.Client', autospec=True) def test_insert(self, mock_client): report = Report(MOCK_REPORT) report.insert() mock_client.post.assert_called_once_with(report, 'reports') @mock.patch('swimlane.core.resources.report.Client', autospec=True) def test_update(self, mock_client):