Rhea
Rhea

Reputation: 401

How to mock an email confidential information in python

I have a method that returns a dictionary and that dictionary contains email specific confidential information like address,recipient, token etc.

For sending an email, I need to access a rest URL and send the data along with confidential information.

I need to mock that method which responsible for sending the mail

  def _get_email_status(self, settings: Settings, data: dict):
    # it's a dictionary
    email_param = settings.get_confidential_data() 
    json_content = Task.build_email_content(
        weekly_build_details, email_param.get(0),
        "subject", email_param.get(1),
        "name", email_param.get(2),
        email_param.get(3)
    # this method contains the rest URL and hit that url
    status = Utilities().send_email(email_param.get(4), 
                                          json_content)
    return status

Wondering how I can write a mock for this class

As per mattyx17 solution,

I have created this,

   test_data = {
                'test': 'test'
            }
            confidential_data = {0: "confidential_details",
                                    1: "confidential_subject",
                                    2: "confidential_name",
                                    3: "confidential3",
                                    4: "confidential4",
                                    5: "confidential5",
                                    6: "confidential6"}
            mock_settings = MagicMock()
            mock_settings.get_confidential_data = MagicMock(return_value=confidential_data)
            mock_settings.keys.return_value.__iter__.return_value = confidential_data.keys()
            mock_settings.__getitem__.side_effect = lambda key: confidential_data[0]
            param = mock_settings.get_confidential_data
            print(param)
            json_content = Task.build_email_content(test_data, dict(mock_settings).get(0),
                                                        dict(mock_settings).get(1), dict(mock_settings).get(2),
                                                        dict(mock_settings).get(3), dict(mock_settings).get(4),
                                                        dict(mock_settings).get(5))
            testUtility = Utilities()
            testUtility.send_email(settings, dict(mock_settings).get(5), json_content)
            self.assertTrue(mock_utility.called)
            print(json_content)


     mock_settings.get_confidential_data.assert_called_once()

But mock_settings.get_confidential_data.assert_called_once() as well as

self.assertTrue(mock_utility.called) are giving error.

Upvotes: 1

Views: 238

Answers (1)

mattyx17
mattyx17

Reputation: 826

You can just mock your settings object and its corresponding method to return whatever you want.

from unittest.mock import MagicMock()    

mock_settings = MagicMock()
confidential_dict = {0: "confidential_details", 
                     1: "confidential_subject",
                     2: "confidential_name",
                     3: "confidential3",
                     4:  "confidential4"}
mock_settings.get_confidential_data = MagicMock(return_value=confidential_dict)

Now when you supply mock settings as an argument to your function you can check that the get_confidential_data method was called (mock_settings.get_confidential_data.assert_called_once()) and check that your other methods/classes were called with the correct info from confidential_dict

Similarly you can mock your Utilities class:

from unittest.mock import patch

@patch("<path-to-class>.Utilities)
def my_test_class(mock_utilities):
   mock_object = MagicMock()
   mock_utilities.return_value = mock_object

Now mock_object is a mocked instance of the Utilities class and you can now mock the send email message

Upvotes: 0

Related Questions