def tracking_pixel(email, notification_id=None): """Returns a mailing tracking pixel""" data = { # Email Address 'e': email, # Unique Key 'k': generate_code(), # Current Time (Without microseconds, in ISOFormat) 't': now().replace(microsecond=0).isoformat(), } # Add Notification ID (if available) if notification_id: data['n'] = notification_id encoded_data, verification_hash = url_representation_encode(data) gif_url = reverse('email_open', kwargs={ 'encoded_data': encoded_data, 'request_hash': verification_hash }) # Add our local tracking pixel local_tracking = ('<img src="{origin}{location}" width="1" height="1"' ' border="0">').format(origin=settings.ORIGIN, location=gif_url) return local_tracking
def tracking_pixel(email, notification_id=None): """Returns a mailing tracking pixel""" data = { # Email Address 'e': email, # Unique Key 'k': generate_code(), # Current Time (Without microseconds, in ISOFormat) 't': now().replace(microsecond=0).isoformat(), } # Add Notification ID (if available) if notification_id: data['n'] = notification_id encoded_data, verification_hash = url_representation_encode(data) gif_url = reverse( 'email_open', kwargs={ 'encoded_data': encoded_data, 'request_hash': verification_hash } ) # Add our local tracking pixel local_tracking = ('<img src="{origin}{location}" width="1" height="1"' ' border="0">').format(origin=settings.ORIGIN, location=gif_url) return local_tracking
def test_missing_data_returns_404(self): """Test that not providing all required fields raises Http404""" data = { 'e': '*****@*****.**' } representation, verification_hash = url_representation_encode(data) with self.assertRaises(Http404): self.view(self.request, encoded_data=representation, request_hash=verification_hash)
def test_roundtrip(self): """Test that an identical dictionary can be encoded and decoded""" test_dict = { 'special_char': '%##!@#9813&&&&', 'email': '*****@*****.**' } code, verification_hash = utils.url_representation_encode(test_dict) # Decode the data, verify we can reverse the process decoded_data, decoded_hash = utils.url_representation_decode(code) self.assertEqual(decoded_hash, verification_hash) self.assertDictEqual(decoded_data, test_dict)
def test_url_representation_encode(self): """Test the url_representation_encode functionality""" # As we're passing in a dictionary we cannot guarantee that the order # of the URL string will be consistent, so we'll have to test for both encoded_options = [ base64.urlsafe_b64encode('first=Stanley&last=Smith').strip('='), base64.urlsafe_b64encode('last=Smith&first=Stanley').strip('=') ] data = {'first': 'Stanley', 'last': 'Smith'} code, verification_hash = utils.url_representation_encode(data) self.assertTrue(any([option == code for option in encoded_options])) # Ensure that the verification hash is a 10 character string self.assertEqual(len(verification_hash), 10) self.assertIsInstance(verification_hash, str)
def test_url_representation_encode(self): """Test the url_representation_encode functionality""" # As we're passing in a dictionary we cannot guarantee that the order # of the URL string will be consistent, so we'll have to test for both encoded_options = [ base64.urlsafe_b64encode('first=Stanley&last=Smith').strip('='), base64.urlsafe_b64encode('last=Smith&first=Stanley').strip('=') ] data = { 'first': 'Stanley', 'last': 'Smith' } code, verification_hash = utils.url_representation_encode(data) self.assertTrue(any([option == code for option in encoded_options])) # Ensure that the verification hash is a 10 character string self.assertEqual(len(verification_hash), 10) self.assertIsInstance(verification_hash, str)