def test_subscribe_new_email_to_list(self, get_email_hash):
        dm_mailchimp_client = DMMailChimpClient('username', DUMMY_MAILCHIMP_API_KEY, mock.MagicMock())
        with mock.patch.object(
                dm_mailchimp_client._client.lists.members, 'create_or_update', autospec=True) as create_or_update:

            create_or_update.return_value = {"response": "data"}
            with assert_external_service_log_entry():
                res = dm_mailchimp_client.subscribe_new_email_to_list('list_id', '*****@*****.**')

            assert res == {"status": "success", 'error_type': None, 'status_code': 200, "response": "data"}
            create_or_update.assert_called_once_with(
                'list_id',
                "foo",
                {
                    "email_address": "*****@*****.**",
                    "status_if_new": "subscribed"
                }
            )
    def test_get_lists_for_email(self):
        dm_mailchimp_client = DMMailChimpClient('username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp'))
        with mock.patch.object(dm_mailchimp_client._client.lists, 'all', autospec=True) as all_lists:
            all_lists.return_value = {
                "lists": [
                    {"id": "gadz00ks", "name": "Pistachios", "irrelevant": "custard"},
                    {"id": "1886", "name": "Square the circle", "meaningless": 3.1415},
                ],
                "pigeon": "pasty",
            }

            with assert_external_service_log_entry(extra_modules=['mailchimp'], count=1):
                result = dm_mailchimp_client.get_lists_for_email("*****@*****.**")

            assert tuple(result) == (
                {"list_id": "gadz00ks", "name": "Pistachios"},
                {"list_id": "1886", "name": "Square the circle"},
            )

            assert all_lists.call_args_list == [
                mock.call(get_all=True, email="*****@*****.**"),
            ]
 def test_success_does_not_perform_retry(self):
     dm_mailchimp_client = DMMailChimpClient(
         'username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp'), retries=2
     )
     with mock.patch.object(dm_mailchimp_client._client.lists.members, 'all', autospec=True) as all_members:
         all_members.side_effect = [
             {
                 "members": [
                     {"email_address": "*****@*****.**"},
                     {"email_address": "*****@*****.**"},
                 ]
             },
             {
                 "members": []
             },
         ]
         with assert_external_service_log_entry(extra_modules=['mailchimp'], count=2):
             list(dm_mailchimp_client.get_email_addresses_from_list('a_list_id'))
         assert all_members.mock_calls == [
             mock.call('a_list_id', count=100, offset=0),
             mock.call('a_list_id', count=100, offset=100),
         ]
    def test_permanently_remove_email_from_list_failure(self, exception):
        dm_mailchimp_client = DMMailChimpClient('username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp'))
        with mock.patch.object(
            dm_mailchimp_client._client.lists.members,
            'delete_permanent',
            autospec=True,
        ) as del_perm:
            del_perm.side_effect = exception

            with assert_external_service_log_entry(successful_call=False, extra_modules=['mailchimp'], count=1):
                result = dm_mailchimp_client.permanently_remove_email_from_list(
                    "*****@*****.**",
                    "p_kellys_budget",
                )

            assert result is False

            assert del_perm.call_args_list == [
                mock.call(
                    list_id="p_kellys_budget",
                    subscriber_hash="ee5ae5f54bdf3394d48ea4e79e6d0e39",
                ),
            ]
    def test_permanently_remove_email_from_list_success(self):
        dm_mailchimp_client = DMMailChimpClient('username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp'))
        with mock.patch.object(
            dm_mailchimp_client._client.lists.members,
            'delete_permanent',
            autospec=True,
        ) as del_perm:
            del_perm.return_value = {"don't rely": "on me"}

            with assert_external_service_log_entry(extra_modules=['mailchimp'], count=1):
                result = dm_mailchimp_client.permanently_remove_email_from_list(
                    "*****@*****.**",
                    "modern_society",
                )

            assert result is True

            assert del_perm.call_args_list == [
                mock.call(
                    list_id="modern_society",
                    subscriber_hash="ee5ae5f54bdf3394d48ea4e79e6d0e39",
                ),
            ]
    def test_offset_increments_until_no_members(self):
        dm_mailchimp_client = DMMailChimpClient('username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp'))
        with mock.patch.object(dm_mailchimp_client._client.lists.members, 'all', autospec=True) as all_members:
            all_members.side_effect = [
                {"members": [{"email_address": "*****@*****.**"}]},
                {"members": [{"email_address": "*****@*****.**"}]},
                {"members": [{"email_address": "*****@*****.**"}]},
                {"members": [{"email_address": "*****@*****.**"}]},
                {"members": [{"email_address": "*****@*****.**"}]},
                {"members": [{"email_address": "*****@*****.**"}]},
                {"members": [{"email_address": "*****@*****.**"}]},
                {"members": []},
            ]

            res = dm_mailchimp_client.get_email_addresses_from_list('a_list_id')

            with assert_external_service_log_entry(extra_modules=['mailchimp'], count=8):
                assert list(res) == [
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                ]

                assert all_members.call_args_list == [
                    mock.call('a_list_id', count=100, offset=0),
                    mock.call('a_list_id', count=100, offset=100),
                    mock.call('a_list_id', count=100, offset=200),
                    mock.call('a_list_id', count=100, offset=300),
                    mock.call('a_list_id', count=100, offset=400),
                    mock.call('a_list_id', count=100, offset=500),
                    mock.call('a_list_id', count=100, offset=600),
                    mock.call('a_list_id', count=100, offset=700),
                ]
    def test_get_all_notifications_logs_for_external_service_calls(self, dm_notify_client):
        with mock.patch(self.client_class_str + '.' + 'get_all_notifications') as get_all_notifications_mock:
            get_all_notifications_mock.return_value = {"notifications": []}

            with assert_external_service_log_entry(service='Notify', description='get_all_notifications'):
                dm_notify_client.get_all_notifications()