Search code examples
pythondjangounit-testingmockingdjango-unittest

How do I Mock a method that makes multiple POST and GET request all requiring different response data?


I have looked at How to mock REST API and I have read the answers but I still can't seem to get my head around how I would go about dealing with a method that executes multiple GET and POST requests. Here is some of my code below.

I have a class, UserAliasGroups(). Its __init__() method executes requests.post() to login into the external REST API. I have in my unit test this code to handling the mocking of the login and it works as expected.

    @mock.patch('aliases.user_alias_groups.requests.get')
    @mock.patch('aliases.user_alias_groups.requests.post')
    def test_user_alias_groups_class(self, mock_post, mock_get):
        init_response = {
            'HID-SessionData': 'token==',
            'errmsg': '',
            'success': True
        }
        mock_response = Mock()
        mock_response.json.return_value = init_response
        mock_response.status_code = status.HTTP_201_CREATED
        mock_post.return_value = mock_response

        uag = UserAliasGroups(auth_user='TEST_USER.gen',
                              auth_pass='FakePass',
                              groups_api_url='https://example.com')
        self.assertEqual(uag.headers, {'HID-SessionData': 'token=='})

I also have defined several methods like obtain_request_id(), has_group_been_deleted(), does_group_already_exists() and others. I also define a method called create_user_alias_group() that calls obtain_request_id(), has_group_been_deleted(), does_group_already_exists() and others.

I also have code in my unit test to mock a GET request to the REST API to test my has_group_been_deleted() method that looks like this:

        has_group_been_deleted_response = {
            'error_code': 404, 
            'error_message': 'A group with this ID does not exist'
        }
        mock_response = Mock()
        mock_response.json.return_value = has_group_been_deleted_response
        mock_response.status_code = status.HTTP_404_NOT_FOUND
        mock_get.return_value = mock_response

Now I can get to my question. Below is the pertinent part of my code.

class UserAliasGroups:

    def __init__(
            self,
            auth_user=settings.GENERIC_USER,
            auth_pass=settings.GENERIC_PASS,
            groups_api_url=settings.GROUPS_API_URL
    ):
        """ __init__() does the login to groups. """
        self.auth_user = auth_user
        self.auth_pass = auth_pass
        self.headers = None
        self.groups_api_url = groups_api_url
        # Initializes a session with the REST API service. Each login session times out after 5 minutes of inactivity.
        self.login_url = f'{self.groups_api_url}/api/login'

        response = requests.post(self.login_url, json={}, headers={'Content-type': 'application/json'},
                                 auth=(auth_user, auth_pass))
        if response.status_code is not 201:
            try:
                json = response.json()
            except:
                json = "Could not decode json."
            raise self.UserAliasGroupsException(f"Error: User {self.auth_user}, failed to login into "
                                                f"{self.login_url} {json}")

        response_json = response.json()
        self.headers = {'HID-SessionData': response_json['HID-SessionData']}

    def obtain_request_id(self, request_reason):
        payload = {'request_reason': request_reason}
        url = f'{self.groups_api_url}/api/v1/session/requests'
        response = requests.post(url=url, json=payload, headers=self.headers)
        if response.status_code is not status.HTTP_200_OK:
            try:
                json = response.json()
            except:
                json = "Could not decode json."
            msg = f'obtain_request_id() Error url={url} {response.status_code} {json}.'
            raise self.UserAliasGroupsException(msg)
        request_id = response.json().get('request_id')
        return request_id

    def has_group_been_deleted(self, group_name):
        url = f'{self.groups_api_url}/api/v1/groups/{group_name}/attributes/RESATTR_GROUP_DELETED_ON'
        response = requests.get(url=url, headers=self.headers)
        return response.status_code == status.HTTP_200_OK


    def does_group_already_exists(self, group_name):
        url = f'{self.groups_api_url}/api/v1/groups/{group_name}'
        response = requests.get(url=url, headers=self.headers)
        if response.status_code is status.HTTP_200_OK:
            # check if the group has been "deleted".
            return not self.has_group_been_deleted(group_name=group_name)
        return False

    def create_user_alias_group(
            self,
            ... long list of params omitted for brevity ...     
    ):

       if check_exists:
            # Check if group already exists or not.
            if self.does_group_already_exists(group_name):
                msg = f'Cannot create group {group_name}. Group already exists.'
                raise self.UserAliasGroupsException(msg)
       ... more code omitted for brevity ...

My question is how do I write my unit test to deal with multiple calls to requests.post() and request.get() all resulting in different responses in my create_user_alias_group() method?

I want to call create_user_alias_group() in my unit test so I have to figure out how to mock multiple requests.get() and requests.post() calls.

Do I have use multiple decorators like this:

@mock.patch('aliases.user_alias_groups.obtain_request_id.requests.post')
@mock.patch('aliases.user_alias_groups.does_group_already_exists.requests.get')
@mock.patch('aliases.user_alias_groups.has_group_been_deleted.requests.get')
def test_user_alias_groups_class(self, mock_post, mock_get):
    ...

?

Thanks for looking my long question :)


Solution

  • You can use mock.side_effect which takes an iterable. Then different calls will return different values:

    mock = Mock()
    mock.side_effect = ['a', 'b', 'c']
    

    This way the first call to mock returns "a", then the next one "b" and so on. (In your case, you'll set mock_get.side_effect).