<commit_before>from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from django.test import override_settings
from django.urls import reverse

from mock import patch

from contacts.models import Contact
from phone_numbers.models import PhoneNumber
from sms.models import SmsMessage
from voice.models import Call

from deterrence.models import Deterrent
from deterrence.models import DeterrenceCampaign
from deterrence.models import DeterrenceMessage

import deterrence.tasks


@override_settings(TWILIO_PHONE_NUMBER="+18881112222",
                   TWILIO_ACCOUNT_SID='ACxxxx',
                   TWILIO_AUTH_TOKEN='<PASSWORD>',
                   GARFIELD_NUMBER_OF_DETERRENTS=1,
                   GARFIELD_DETERRENT_INTERVAL=300)
class DeterrenceCampaignTestCase(TestCase):
    @patch('deterrence.tasks.check_campaign_for_contact.apply_async')
    @patch('contacts.tasks.lookup_contact.apply_async')
    def setUp(self, mock_lookup, mock_check_campaign):
        self.contact_a = \
            Contact.objects.create(phone_number="+15556667777",
                                   whitepages_first_name="John")
        self.contact_b = Contact.objects.create(phone_number="+15556667778")
        self.contact_c = Contact.objects.create(phone_number="+15556667779")

        self.phone_number = PhoneNumber.objects.create(sid="PNxxx",
                                                       account_sid="ACxxx",
                                                       service_sid="SExxx",
                                                       url="http://exmple.com",
                                                       e164="+15558675309",
                                                       formatted="(555) "
                                                                 "867-5309",
                                                       friendly_name="Stuff.",
                                                       number_type="ADV",
                                                       country_code="1")

        self.det_number = PhoneNumber.objects.create(sid="PNyyy",
                                                     account_sid="ACxxx",
                                                     service_sid="SExxx",
                                                     url="http://exmple.com",
                                                     e164="+15558675310",
                                                     formatted="(555) "
                                                               "867-5310",
                                                     friendly_name="Stuff.",
                                                     number_type="DET",
                                                     country_code="1")

        self.contact_a.related_phone_numbers.add(self.phone_number)
        self.contact_b.related_phone_numbers.add(self.phone_number)
        self.contact_c.related_phone_numbers.add(self.phone_number)

        image = SimpleUploadedFile(name="example_image.png",
                                   content=open("./deterrence/tests/assets/"
                                                "example_image.png",
                                                "rb").read(),
                                   content_type="image/png")

        self.deterrent = Deterrent.objects.create(image=image,
                                                  body="A message from "
                                                       "Garfield.",
                                                  personalize=True)
        self.deterrence_campaign = \
            DeterrenceCampaign.objects \
            .create(related_deterrent=self.deterrent)

        self.deterrence_campaign.related_contacts.add(self.contact_a)
        self.deterrence_campaign.related_contacts.add(self.contact_b)
        self.deterrence_campaign.related_contacts.add(self.contact_c)

        self.message = {"From": "+15556667777",
                        "To": "+15558675309",
                        "Body": "Test."}

    @patch('deterrence.tasks.send_deterrence.apply_async')
    def test_send_deterrence_campaign(self, mock_send):
        deterrence.tasks.send_deterrence_campaign("http://example.com")

        self.assertEquals(3,
                          mock_send.call_count)

        campaign = DeterrenceCampaign.objects.latest('date_created')
        self.assertFalse(campaign.date_sent is None)

    @patch('deterrence.tasks.send_deterrence.apply_async')
    def test_send_deterrence_do_not_deter(self, mock_send):
        self.contact_a.do_not_deter = True
        self.contact_a.save()

        deterrence.tasks.send_deterrence_campaign("http://example.com")

        self.assertEquals(2, mock_send.call_count)
        self.assertFalse(self.phone_number.contact_set.all()[0].deterred)

        campaign = DeterrenceCampaign.objects.latest('date_created')
        self.assertFalse(campaign.date_sent is None)

    @patch('deterrence.tasks.send_deterrence.apply_async')
    def test_send_deterrence_arrested(self, mock_send):
        self.contact_a.arrested = True
        self.contact_a.save()

        deterrence.tasks.send_deterrence_campaign("http://example.com")

        self.assertEquals(2, mock_send.call_count)
        self.assertFalse(self.phone_number.contact_set.all()[0].deterred)

        campaign = DeterrenceCampaign.objects.latest('date_created')
        self.assertFalse(campaign.date_sent is None)

    @patch('deterrence.tasks.send_deterrence.apply_async')
    def test_send_deterrence_recruiter(self, mock_send):
        self.contact_a.recruiter = True
        self.contact_a.save()

        deterrence.tasks.send_deterrence_campaign("http://example.com")

        self.assertEquals(2, mock_send.call_count)
        self.assertFalse(self.phone_number.contact_set.all()[0].deterred)

        campaign = DeterrenceCampaign.objects.latest('date_created')
        self.assertFalse(campaign.date_sent is None)


class DeterrenceTestCase(TestCase):
    @patch('deterrence.tasks.check_campaign_for_contact.apply_async')
    @patch('contacts.tasks.lookup_contact.apply_async')
    def setUp(self, mock_lookup, mock_check_campaign):
        self.contact_a = Contact.objects.create(phone_number="+15556667777")
        self.contact_b = Contact.objects.create(phone_number="+15556667778")
        self.contact_c = Contact.objects.create(phone_number="+15556667779")

        self.phone_number = PhoneNumber.objects.create(sid="PNxxx",
                                                       account_sid="ACxxx",
                                                       service_sid="SExxx",
                                                       url="http://exmple.com",
                                                       e164="+15558675309",
                                                       formatted="(555) "
                                                                 "867-5309",
                                                       friendly_name="Stuff.",
                                                       number_type="ADV",
                                                       country_code="1")

        self.det_number = PhoneNumber.objects.create(sid="PNyyy",
                                                     account_sid="ACxxx",
                                                     service_sid="SExxx",
                                                     url="http://exmple.com",
                                                     e164="+15558675310",
                                                     formatted="(555) "
                                                               "867-5310",
                                                     friendly_name="Stuff.",
                                                     number_type="DET",
                                                     country_code="1")

        self.contact_a.related_phone_numbers.add(self.phone_number)
        self.contact_b.related_phone_numbers.add(self.phone_number)
        self.contact_c.related_phone_numbers.add(self.phone_number)

        image = SimpleUploadedFile(name="example_image.png",
                                   content=open("./deterrence/tests/assets/"
                                                "example_image.png",
                                                "rb").read(),
                                   content_type="image/png")

        self.deterrent = Deterrent.objects.create(image=image,
                                                  body="A message from "
                                                       "Garfield.",
                                                  personalize=True)
        self.deterrence_campaign = \
            DeterrenceCampaign.objects \
            .create(related_deterrent=self.deterrent)

        self.deterrence_campaign.related_contacts.add(self.contact_a)
        self.deterrence_campaign.related_contacts.add(self.contact_b)
        self.deterrence_campaign.related_contacts.add(self.contact_c)

        self.message = {"From": "+15556667777",
                        "To": "+15558675309",
                        "Body": "Test."}

    @patch('deterrence.tasks.send_sms_message')
    def test_send_deterrence(self, mock_send):
        mock_send.side_effect = [{'Sid': 'MMxxx',
                                  'Body': 'A message from Garfield.',
                                  'Status': 'queued'}]

        deterrence.tasks.send_deterrence("http://example.com",
                                         self.deterrence_campaign.id,
                                         self.contact_a.id)

        mock_send.assert_called_once_with(
            from_="+15558675310",
            to="+15556667777",
            body="A message from Garfield.",
            media_url="http://example.com/"
                      "{0}".format(self.deterrent.image.url),
            status_callback="http://example.com"
                            "{0}".format(reverse('deterrence:deterrence'
                                                 '_message_status_callback')))
        self.assertEquals(1,
                          len(DeterrenceMessage.objects.all()))
        contact = Contact.objects.get(pk=self.contact_a.id)
        self.assertTrue(contact.deterred)
        self.assertEquals(1,
                          contact.deterrents_received)

    @patch('deterrence.tasks.send_sms_message')
    def test_send_deterrence_first_name(self, mock_send):
        mock_send.side_effect = [{'Sid': 'MMxxx',
                                  'Body': 'John, a message from Garfield.',
                                  'Status': 'queued'}]

        self.contact_a.whitepages_first_name = "John"
        self.contact_a.save()

        deterrence.tasks.send_deterrence("http://example.com",
                                         self.deterrence_campaign.id,
                                         self.contact_a.id)

        mock_send.assert_called_once_with(
            from_="+15558675310",
            to="+15556667777",
            body="John, a message from Garfield.",
            media_url="http://example.com/"
                      "{0}".format(self.deterrent.image.url),
            status_callback="http://example.com"
                            "{0}".format(reverse('deterrence:deterrence'
                                                 '_message_status_callback')))
        self.assertEquals(1,
                          len(DeterrenceMessage.objects.all()))
        contact = Contact.objects.get(pk=self.contact_a.id)
        self.assertTrue(contact.deterred)

    @patch('deterrence.tasks.send_sms_message')
    def test_send_deterrence_no_personalize(self, mock_send):
        mock_send.side_effect = [{'Sid': 'MMxxx',
                                  'Body': 'A message from Garfield.',
                                  'Status': 'queued'}]
<commit_msg>Fix tests for MessageSid change in .<commit_after>from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from django.test import override_settings
from django.urls import reverse

from mock import patch

from contacts.models import Contact
from phone_numbers.models import PhoneNumber
from sms.models import SmsMessage
from voice.models import Call

from deterrence.models import Deterrent
from deterrence.models import DeterrenceCampaign
from deterrence.models import DeterrenceMessage

import deterrence.tasks


@override_settings(TWILIO_PHONE_NUMBER="+18881112222",
                   TWILIO_ACCOUNT_SID='ACxxxx',
                   TWILIO_AUTH_TOKEN='<PASSWORD>',
                   GARFIELD_NUMBER_OF_DETERRENTS=1,
                   GARFIELD_DETERRENT_INTERVAL=300)
class DeterrenceCampaignTestCase(TestCase):
    @patch('deterrence.tasks.check_campaign_for_contact.apply_async')
    @patch('contacts.tasks.lookup_contact.apply_async')
    def setUp(self, mock_lookup, mock_check_campaign):
        self.contact_a = \
            Contact.objects.create(phone_number="+15556667777",
                                   whitepages_first_name="John")
        self.contact_b = Contact.objects.create(phone_number="+15556667778")
        self.contact_c = Contact.objects.create(phone_number="+15556667779")

        self.phone_number = PhoneNumber.objects.create(sid="PNxxx",
                                                       account_sid="ACxxx",
                                                       service_sid="SExxx",
                                                       url="http://exmple.com",
                                                       e164="+15558675309",
                                                       formatted="(555) "
                                                                 "867-5309",
                                                       friendly_name="Stuff.",
                                                       number_type="ADV",
                                                       country_code="1")

        self.det_number = PhoneNumber.objects.create(sid="PNyyy",
                                                     account_sid="ACxxx",
                                                     service_sid="SExxx",
                                                     url="http://exmple.com",
                                                     e164="+15558675310",
                                                     formatted="(555) "
                                                               "867-5310",
                                                     friendly_name="Stuff.",
                                                     number_type="DET",
                                                     country_code="1")

        self.contact_a.related_phone_numbers.add(self.phone_number)
        self.contact_b.related_phone_numbers.add(self.phone_number)
        self.contact_c.related_phone_numbers.add(self.phone_number)

        image = SimpleUploadedFile(name="example_image.png",
                                   content=open("./deterrence/tests/assets/"
                                                "example_image.png",
                                                "rb").read(),
                                   content_type="image/png")

        self.deterrent = Deterrent.objects.create(image=image,
                                                  body="A message from "
                                                       "Garfield.",
                                                  personalize=True)
        self.deterrence_campaign = \
            DeterrenceCampaign.objects \
            .create(related_deterrent=self.deterrent)

        self.deterrence_campaign.related_contacts.add(self.contact_a)
        self.deterrence_campaign.related_contacts.add(self.contact_b)
        self.deterrence_campaign.related_contacts.add(self.contact_c)

        self.message = {"From": "+15556667777",
                        "To": "+15558675309",
                        "Body": "Test."}

    @patch('deterrence.tasks.send_deterrence.apply_async')
    def test_send_deterrence_campaign(self, mock_send):
        deterrence.tasks.send_deterrence_campaign("http://example.com")

        self.assertEquals(3,
                          mock_send.call_count)

        campaign = DeterrenceCampaign.objects.latest('date_created')
        self.assertFalse(campaign.date_sent is None)

    @patch('deterrence.tasks.send_deterrence.apply_async')
    def test_send_deterrence_do_not_deter(self, mock_send):
        self.contact_a.do_not_deter = True
        self.contact_a.save()

        deterrence.tasks.send_deterrence_campaign("http://example.com")

        self.assertEquals(2, mock_send.call_count)
        self.assertFalse(self.phone_number.contact_set.all()[0].deterred)

        campaign = DeterrenceCampaign.objects.latest('date_created')
        self.assertFalse(campaign.date_sent is None)

    @patch('deterrence.tasks.send_deterrence.apply_async')
    def test_send_deterrence_arrested(self, mock_send):
        self.contact_a.arrested = True
        self.contact_a.save()

        deterrence.tasks.send_deterrence_campaign("http://example.com")

        self.assertEquals(2, mock_send.call_count)
        self.assertFalse(self.phone_number.contact_set.all()[0].deterred)

        campaign = DeterrenceCampaign.objects.latest('date_created')
        self.assertFalse(campaign.date_sent is None)

    @patch('deterrence.tasks.send_deterrence.apply_async')
    def test_send_deterrence_recruiter(self, mock_send):
        self.contact_a.recruiter = True
        self.contact_a.save()

        deterrence.tasks.send_deterrence_campaign("http://example.com")

        self.assertEquals(2, mock_send.call_count)
        self.assertFalse(self.phone_number.contact_set.all()[0].deterred)

        campaign = DeterrenceCampaign.objects.latest('date_created')
        self.assertFalse(campaign.date_sent is None)


class DeterrenceTestCase(TestCase):
    @patch('deterrence.tasks.check_campaign_for_contact.apply_async')
    @patch('contacts.tasks.lookup_contact.apply_async')
    def setUp(self, mock_lookup, mock_check_campaign):
        self.contact_a = Contact.objects.create(phone_number="+15556667777")
        self.contact_b = Contact.objects.create(phone_number="+15556667778")
        self.contact_c = Contact.objects.create(phone_number="+15556667779")

        self.phone_number = PhoneNumber.objects.create(sid="PNxxx",
                                                       account_sid="ACxxx",
                                                       service_sid="SExxx",
                                                       url="http://exmple.com",
                                                       e164="+15558675309",
                                                       formatted="(555) "
                                                                 "867-5309",
                                                       friendly_name="Stuff.",
                                                       number_type="ADV",
                                                       country_code="1")

        self.det_number = PhoneNumber.objects.create(sid="PNyyy",
                                                     account_sid="ACxxx",
                                                     service_sid="SExxx",
                                                     url="http://exmple.com",
                                                     e164="+15558675310",
                                                     formatted="(555) "
                                                               "867-5310",
                                                     friendly_name="Stuff.",
                                                     number_type="DET",
                                                     country_code="1")

        self.contact_a.related_phone_numbers.add(self.phone_number)
        self.contact_b.related_phone_numbers.add(self.phone_number)
        self.contact_c.related_phone_numbers.add(self.phone_number)

        image = SimpleUploadedFile(name="example_image.png",
                                   content=open("./deterrence/tests/assets/"
                                                "example_image.png",
                                                "rb").read(),
                                   content_type="image/png")

        self.deterrent = Deterrent.objects.create(image=image,
                                                  body="A message from "
                                                       "Garfield.",
                                                  personalize=True)
        self.deterrence_campaign = \
            DeterrenceCampaign.objects \
            .create(related_deterrent=self.deterrent)

        self.deterrence_campaign.related_contacts.add(self.contact_a)
        self.deterrence_campaign.related_contacts.add(self.contact_b)
        self.deterrence_campaign.related_contacts.add(self.contact_c)

        self.message = {"From": "+15556667777",
                        "To": "+15558675309",
                        "Body": "Test."}

    @patch('deterrence.tasks.send_sms_message')
    def test_send_deterrence(self, mock_send):
        mock_send.side_effect = [{'MessageSid': 'MMxxx',
                                  'Body': 'A message from Garfield.',
                                  'Status': 'queued'}]

        deterrence.tasks.send_deterrence("http://example.com",
                                         self.deterrence_campaign.id,
                                         self.contact_a.id)

        mock_send.assert_called_once_with(
            from_="+15558675310",
            to="+15556667777",
            body="A message from Garfield.",
            media_url="http://example.com/"
                      "{0}".format(self.deterrent.image.url),
            status_callback="http://example.com"
                            "{0}".format(reverse('deterrence:deterrence'
                                                 '_message_status_callback')))
        self.assertEquals(1,
                          len(DeterrenceMessage.objects.all()))
        contact = Contact.objects.get(pk=self.contact_a.id)
        self.assertTrue(contact.deterred)
        self.assertEquals(1,
                          contact.deterrents_received)

    @patch('deterrence.tasks.send_sms_message')
    def test_send_deterrence_first_name(self, mock_send):
        mock_send.side_effect = [{'MessageSid': 'MMxxx',
                                  'Body': 'John, a message from Garfield.',
                                  'Status': 'queued'}]

        self.contact_a.whitepages_first_name = "John"
        self.contact_a.save()

        deterrence.tasks.send_deterrence("http://example.com",
                                         self.deterrence_campaign.id,
                                         self.contact_a.id)

        mock_send.assert_called_once_with(
            from_="+15558675310",
            to="+15556667777",
            body="John, a message from Garfield.",
            media_url="http://example.com/"
                      "{0}".format(self.deterrent.image.url),
            status_callback="http://example.com"
                            "{0}".format(reverse('deterrence:deterrence'
                                                 '_message_status_callback')))
        self.assertEquals(1,
                          len(DeterrenceMessage.objects.all()))
        contact = Contact.objects.get(pk=self.contact_a.id)
        self.assertTrue(contact.deterred)

    @patch('deterrence.tasks.send_sms_message')
    def test_send_deterrence_no_personalize(self, mock_send):
        mock_send.side_effect = [{'MessageSid': 'MMxxx',
                                  'Body': 'A message from Garfield.',
                                  'Status': 'queued'}]

{'fraction_non_alphanumeric': 0.09296796796796797, 'fraction_numerical': 0.02360694027360694, 'mean_word_length': 1.3557673413244253, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 230, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3818034', 'n_tokens_mistral': 6142, 'n_tokens_neox': 5705, 'n_words': 676}