def test_simulate(self, caplog, simulate): """ Test contact archiving simulate flag """ caplog.set_level(logging.INFO, logger='datahub.company.tasks.contact') date = timezone.now() - relativedelta(days=10) with freeze_time(date): company1 = CompanyFactory() company2 = CompanyFactory(archived=True) contact1 = ContactFactory(company=company1) contact2 = ContactFactory(company=company2) task_result = automatic_contact_archive.apply_async( kwargs={'simulate': simulate}) contact1.refresh_from_db() contact2.refresh_from_db() if simulate: assert caplog.messages == [ f'[SIMULATION] Automatically archived contact: {contact2.id}', ] else: assert task_result.successful() assert contact1.archived is False assert contact2.archived is True assert caplog.messages == [ f'Automatically archived contact: {contact2.id}' ]
def test_does_not_allow_file_with_bad_utf8_after_header(self, monkeypatch): """ Test that the form rejects a CSV file with invalid UTF-8 after its header. As reading and decoding happens in chunks, we patch the the function to validate the columns in the form because it can decode text that is close to the header. (That means in reality, this check will only be triggered for invalid Unicode sequences that are relatively deep in the file.) """ monkeypatch.setattr( 'datahub.company.admin.contact.LoadEmailMarketingOptOutsForm._validate_columns', Mock(), ) creation_time = datetime(2011, 2, 1, 14, 0, 10, tzinfo=utc) with freeze_time(creation_time): contact = ContactFactory( email='test1@datahub', accepts_dit_email_marketing=True, ) csv_body = b""""email\r test1@datahub\r \xc3\x28 """ file = io.BytesIO( b''.join((BOM_UTF8, csv_body)), ) file.name = 'test.csv' url = reverse( admin_urlname(Contact._meta, 'load-email-marketing-opt-outs'), ) with freeze_time('2014-05-03 19:00:16'): response = self.client.post( url, data={ 'email_list': file, }, ) assert response.status_code == status.HTTP_200_OK messages = list(response.context['messages']) assert len(messages) == 1 assert messages[0].level == django_messages.ERROR assert messages[0].message == ( 'There was an error decoding the text in the file provided. No records have been ' 'modified.' ) # Changes should have been rolled back contact.refresh_from_db() assert contact.accepts_dit_email_marketing is True assert contact.modified_on == creation_time
def test_archive_with_updates(self): """ Test contact archiving with updates on correct contacts """ date = timezone.now() - relativedelta(days=10) with freeze_time(date): company1 = CompanyFactory() company2 = CompanyFactory(archived=True) contact1 = ContactFactory(company=company1) contact2 = ContactFactory(company=company2) contact3 = ContactFactory(company=company2) for c in [contact1, contact2, contact3]: assert c.archived is False assert c.archived_reason is None assert c.archived_on is None # run task twice expecting same result for _ in range(2): task_result = automatic_contact_archive.apply_async( kwargs={'limit': 200}) assert task_result.successful() contact1.refresh_from_db() contact2.refresh_from_db() contact3.refresh_from_db() assert contact1.archived is False assert contact2.archived is True assert contact3.archived is True assert contact1.archived_reason is None assert contact2.archived_reason is not None assert contact3.archived_reason is not None assert contact1.archived_on is None assert contact2.archived_on == date assert contact3.archived_on == date # run again at later time expecting no changes task_result = automatic_contact_archive.apply_async( kwargs={'limit': 200}) assert task_result.successful() contact1.refresh_from_db() contact2.refresh_from_db() contact3.refresh_from_db() assert contact1.archived is False assert contact2.archived is True assert contact3.archived is True assert contact1.archived_reason is None assert contact2.archived_reason is not None assert contact3.archived_reason is not None assert contact1.archived_on is None assert contact2.archived_on == date assert contact3.archived_on == date