def test_open(self): """Test an open webhook""" # pylint: disable=too-many-locals recipient = "*****@*****.**" comm = FOIACommunicationFactory( foia__email=EmailAddress.objects.fetch(recipient) ) email = comm.emails.first() event = "opened" city = "Boston" region = "MA" country = "US" client_type = "browser" client_name = "Chrome" client_os = "Linux" device_type = "desktop" user_agent = ( "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 " "(KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31" ) ip_address = "50.56.129.169" data = { "event": event, "email_id": email.pk, "recipient": recipient, "city": city, "region": region, "country": country, "client-type": client_type, "client-name": client_name, "client-os": client_os, "device-type": device_type, "user-agent": user_agent, "ip": ip_address, } self.sign(data) request = self.factory.post(reverse("mailgun-opened"), data) opened(request) # pylint: disable=no-value-for-parameter comm.refresh_from_db() nose.tools.ok_( EmailOpen.objects.filter( email=email, datetime=datetime(2017, 1, 2, 17, tzinfo=pytz.utc), recipient=EmailAddress.objects.fetch(recipient), city=city, region=region, country=country, client_type=client_type, client_name=client_name, client_os=client_os, device_type=device_type, user_agent=user_agent[:255], ip_address=ip_address, ).exists() )
def test_open(self): """Test an open webhook""" # pylint: disable=too-many-locals recipient = '*****@*****.**' comm = FOIACommunicationFactory( foia__email=EmailAddress.objects.fetch(recipient), ) email = comm.emails.first() event = 'opened' city = 'Boston' region = 'MA' country = 'US' client_type = 'browser' client_name = 'Chrome' client_os = 'Linux' device_type = 'desktop' user_agent = ( 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 ' '(KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31' ) ip_address = '50.56.129.169' data = { 'event': event, 'email_id': email.pk, 'recipient': recipient, 'city': city, 'region': region, 'country': country, 'client-type': client_type, 'client-name': client_name, 'client-os': client_os, 'device-type': device_type, 'user-agent': user_agent, 'ip': ip_address, } self.sign(data) request = self.factory.post(reverse('mailgun-opened'), data) opened(request) # pylint: disable=no-value-for-parameter comm.refresh_from_db() nose.tools.ok_( EmailOpen.objects.filter( email=email, datetime=datetime(2017, 1, 2, 17, tzinfo=pytz.utc), recipient=EmailAddress.objects.fetch(recipient), city=city, region=region, country=country, client_type=client_type, client_name=client_name, client_os=client_os, device_type=device_type, user_agent=user_agent[:255], ip_address=ip_address, ).exists() )
def test_delivered(self): """Test a delivered webhook""" comm = FOIACommunicationFactory() email = comm.emails.first() data = {"event": "delivered", "email_id": email.pk} self.sign(data) request = self.factory.post(reverse("mailgun-delivered"), data) delivered(request) # pylint: disable=no-value-for-parameter comm.refresh_from_db() nose.tools.eq_( comm.emails.first().confirmed_datetime, datetime(2017, 1, 2, 17, tzinfo=pytz.utc), )
def test_bounce(self): """Test a bounce webhook""" recipient = '*****@*****.**' comm = FOIACommunicationFactory( foia__email=EmailAddress.objects.fetch(recipient), foia__agency__fax=None, ) email = comm.emails.first() event = 'bounced' code = 550 error = ( "5.1.1 The email account that you tried to reach " "does not exist. Please try 5.1.1 double-checking " "the recipient's email address for typos or 5.1.1 " "unnecessary spaces. Learn more at 5.1.1 " "http://support.example.com/mail/bin/answer.py" ) data = { 'event': event, 'email_id': email.pk, 'code': code, 'error': error, 'recipient': recipient, } self.sign(data) request = self.factory.post(reverse('mailgun-bounces'), data) bounces(request) # pylint: disable=no-value-for-parameter comm.refresh_from_db() nose.tools.ok_( EmailError.objects.filter( email=email, datetime=datetime(2017, 1, 2, 17, tzinfo=pytz.utc), recipient=EmailAddress.objects.fetch(recipient), code=code, error=error, event=event, ).exists() )
class TestCommunicationMove(test.TestCase): """Tests the move method""" def setUp(self): self.foia1 = FOIARequestFactory() self.foia2 = FOIARequestFactory() self.comm = FOIACommunicationFactory(foia=self.foia1) self.file = FOIAFileFactory(comm=self.comm) eq_(self.comm.files.count(), 1) self.user = UserFactory() @patch('muckrock.foia.tasks.upload_document_cloud.apply_async') def test_move_single_comm(self, mock_upload): """Should change the request associated with the communication.""" moved_comms = self.comm.move([self.foia2.pk], self.user) eq_(len(moved_comms), 1, 'Move function should only return one item') moved_comm = moved_comms[0] eq_( moved_comm, self.comm, 'Communication returned should be the same as the one acted on.' ) eq_( moved_comm.foia.id, self.foia2.id, 'Should change the FOIA associated with the communication.' ) moved_files = moved_comm.files.all() moved_file = moved_files[0] logging.debug( 'File foia: %d; Expected: %d', moved_file.comm.foia.id, self.foia2.id ) eq_( moved_file.comm, self.comm, 'Should not have changed the communication associated with the file.' ) # a move log should be generated ok_( CommunicationMoveLog.objects.filter( communication=moved_comm, foia=self.foia1, user=self.user, ).exists() ) mock_upload.assert_called() @patch('muckrock.foia.tasks.upload_document_cloud.apply_async') def test_move_multi_comms(self, mock_upload): """Should move the comm to the first request, then clone it to the rest.""" comm_count = FOIACommunication.objects.count() comms = self.comm.move([self.foia1.id, self.foia2.id], self.user) # + 1 communications created self.comm.refresh_from_db() eq_( self.comm.foia.id, self.foia1.id, 'The communication should be moved to the first listed request.' ) eq_( FOIACommunication.objects.count(), comm_count + 1, 'A clone should be made for each additional request in the list.' ) eq_( len(comms), 2, 'Two communications should be returned, since two were operated on.' ) eq_( self.comm.pk, comms[0].pk, 'The first communication in the list should be this one.' ) ok_( comms[1].pk is not self.comm.pk, 'The second communication should be a new one, since it was cloned.' ) # each comm should have a move log generated for it for comm in comms: ok_( CommunicationMoveLog.objects.filter( communication=comm, foia=self.foia1, user=self.user, ).exists() ) mock_upload.assert_called() @raises(ValueError) def test_move_invalid_foia(self): """Should raise an error if trying to call move on invalid request pks.""" original_request = self.comm.foia.id self.comm.move('abc', self.user) self.comm.refresh_from_db() eq_( self.comm.foia.id, original_request, 'If something goes wrong, the move should not complete.' ) @raises(ValueError) def test_move_empty_list(self): """Should raise an error if trying to call move on an empty list.""" original_request = self.comm.foia.id self.comm.move([], self.user) self.comm.refresh_from_db() eq_( self.comm.foia.id, original_request, 'If something goes wrong, the move should not complete.' ) def test_move_missing_ffile(self): """ The move operation should not crash when FOIAFile has a null ffile field. """ self.file.ffile = None self.file.save() ok_(not self.comm.files.all()[0].ffile) self.comm.move([self.foia2.pk], self.user)