コード例 #1
0
    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])
コード例 #2
0
    def authenticate(self):
        """Send login request and update User instance, login headers, and token expiration"""

        # Temporarily remove auth from Swimlane session for auth request to avoid recursive loop during login request
        self._swimlane._session.auth = None
        resp = self._swimlane.request(
            'post',
            'user/login',
            json={
                'userName': self._username,
                'password': self._password
            },
        )
        self._swimlane._session.auth = self

        # Get JWT from response content
        json_content = resp.json()
        token = json_content.pop('token', None)

        # Grab token expiration
        token_data = jwt.decode(token, verify=False)
        token_expiration = pendulum.from_timestamp(token_data['exp'])

        headers = {'Authorization': 'Bearer {}'.format(token)}

        # Create User instance for authenticating user from login response data
        user = User(self._swimlane, _user_raw_from_login_content(json_content))

        self._login_headers = headers
        self.user = user
        self._token_expiration = token_expiration
コード例 #3
0
    def lock(self):
        """
        Lock the record to the Current User.


        Notes:

        Warnings:

        Args:

        """
        self.validate()
        response = self._swimlane.request(
            'post', 'app/{}/record/{}/lock'.format(self.app.id,
                                                   self.id)).json()
        self.locked = True
        self.locking_user = User(self._swimlane, response['lockingUser'])
        self.locked_date = response['lockedDate']
コード例 #4
0
    def __call__(self, request):
        """Attach necessary headers to all requests"""

        headers = {'Private-Token': self._access_token}

        request.headers.update(headers)

        # Only make the call to user/authorize to get the user's profile if we haven't retrieved it
        # already
        if self.user is not None:
            return request

        # Temporarily remove auth from Swimlane session for auth request to avoid recursive loop during the request
        self._swimlane._session.auth = None
        resp = self._swimlane.request('get', 'user/authorize', headers=headers)
        self._swimlane._session.auth = self

        json_content = resp.json()
        self.user = User(self._swimlane,
                         _user_raw_from_login_content(json_content))

        return request
コード例 #5
0
 def _parse_raw_element(self, raw_element):
     return User(self._swimlane, raw_element)
コード例 #6
0
def test_type_checks(mock_swimlane, raw):
    """Use User class to test $type validation"""
    with pytest.raises(TypeError):
        User(mock_swimlane, raw)