def test_filtered(self): """Make sure watches cull properly by additional filters.""" # A watch with just the filter we're searching for: registered_user = user(email='*****@*****.**', save=True) exact_watch = watch(event_type=TYPE, user=registered_user, save=True) watch_filter(watch=exact_watch, name='color', value=1).save() # A watch with extra filters: extra_watch = watch(event_type=TYPE, email='*****@*****.**', save=True) watch_filter(watch=extra_watch, name='color', value=1).save() watch_filter(watch=extra_watch, name='flavor', value=2).save() # A watch with no row for the filter we're searching on: watch(event_type=TYPE, email='*****@*****.**').save() # A watch with a mismatching filter--shouldn't be found mismatch_watch = watch(event_type=TYPE, email='*****@*****.**', save=True) watch_filter(watch=mismatch_watch, name='color', value=3).save() self._emails_eq(['*****@*****.**', '*****@*****.**', '*****@*****.**'], FilteredEvent(), color=1) # Search on multiple filters to test joining the filters table twice. # We provide values that match for both filters, as [email protected] # suffices to test exclusion. self._emails_eq(['*****@*****.**', '*****@*****.**', '*****@*****.**'], FilteredEvent(), color=1, flavor=2)
def test_duplicate_tolerance(self): """Assure notify() returns an existing watch if there is a matching one. Also make sure it returns only 1 watch even if there are duplicate matches. """ w1 = watch(event_type=TYPE, email='*****@*****.**', save=True) w2 = watch(event_type=TYPE, email='*****@*****.**', save=True) assert SimpleEvent.notify('*****@*****.**').pk in [w1.pk, w2.pk]
def test_new_user_claim_watches(self, get_current): """Claim user watches upon activation.""" watch(email='*****@*****.**', save=True) get_current.return_value.domain = 'su.mo.com' user = RegistrationProfile.objects.create_inactive_user( 'sumouser1234', 'testpass', '*****@*****.**') key = RegistrationProfile.objects.all()[0].activation_key self.client.get(reverse('users.activate', args=[key]), follow=True) # Watches are claimed. assert user.watch_set.exists()
def test_mock_model(self): """Deleting an instance of MockModel should delete watches. Create instance of MockModel from tidings.tests.mockapp.models, then delete it and watch the cascade go. """ mock_m = MockModel.objects.create() watch(event_type=TYPE, email='*****@*****.**', content_object=mock_m, save=True) MockModel.objects.all().delete() assert not Watch.objects.count(), 'Cascade delete failed.'
def test_unsubscribe_instructions(self): """Make sure unsubscribe_instructions renders and contains the unsubscribe URL.""" w = watch(save=True) template = Template('{% load unsubscribe_instructions %}' '{% unsubscribe_instructions watch %}') assert w.unsubscribe_url() in template.render(Context({'watch': w}))
def test_success(self): """Ensure the watch deletes and view says "yay" when all goes well.""" w = watch(save=True) response = self.client.post( reverse('tidings.unsubscribe', args=[w.pk]) + '?s=' + w.secret) self.assertContains(response, '<h1>Unsubscribed</h1>') eq_(0, Watch.objects.count())
def test_wrong_secret(self): """Assert it complains when an incorrect secret is given.""" w = watch(save=True) for method in [self.client.get, self.client.post]: response = method( reverse('tidings.unsubscribe', args=[w.pk]) + '?s=WRONGwrong') self.assertContains(response, FAILURE_STRING)
def test_simple(self): """Test whether a watch scoped only by event type fires for both anonymous and registered users.""" registered_user = user(email='*****@*****.**', save=True) watch(event_type=TYPE, user=registered_user).save() watch(event_type=TYPE, email='*****@*****.**').save() watch(event_type='something else', email='*****@*****.**').save() self._emails_eq(['*****@*****.**', '*****@*****.**'], SimpleEvent())
def test_duplicates(self): """Don't return duplicate email addresses.""" watch(event_type=TYPE, user=user(email='*****@*****.**', save=True), save=True) watch(event_type=TYPE, email='*****@*****.**').save() watch(event_type=TYPE, email='*****@*****.**').save() eq_(3, Watch.objects.all().count()) # We created what we meant to. self._emails_eq(['*****@*****.**'], SimpleEvent())
def test_merging(self): """Test that duplicate emails across multiple events get merged.""" watch(event_type=TYPE, email='*****@*****.**').save() watch(event_type=TYPE, email='*****@*****.**').save() registered_user = user(email='*****@*****.**', save=True) watch(event_type=ANOTHER_TYPE, user=registered_user).save() self._emails_eq(['*****@*****.**', '*****@*****.**'], EventUnion(SimpleEvent(), AnotherEvent()))
def test_registered_users_favored(self): """When removing duplicates, make sure registered users are kept in favor of anonymous ones having the same email address.""" def make_anonymous_watches(): for x in xrange(3): watch(event_type=TYPE, email='*****@*****.**').save() # Throw some anonymous watches in there in the hope that they would # come out on top if we didn't purposely favor registered users. # Suggestions on how to make this test more reliable are welcome. make_anonymous_watches() # File the registered watch: watch(event_type=TYPE, user=user(first_name='Jed', email='*****@*****.**', save=True)).save() # A few more anonymous watches in case the BTrees flop in the other # direction: make_anonymous_watches() users_and_watches = list(SimpleEvent()._users_watching_by_filter()) u, w = users_and_watches[0] eq_('Jed', u.first_name)
def test_duplicates_case_insensitive(self): """De-duping should compare case-insensitively.""" watch(event_type=TYPE, user=user(email='*****@*****.**', save=True), save=True) watch(event_type=TYPE, email='*****@*****.**').save() watch(event_type=TYPE, email='*****@*****.**').save() eq_(3, Watch.objects.all().count()) # We created what we meant to. addresses = [u.email for u, w in SimpleEvent()._users_watching_by_filter()] eq_(1, len(addresses)) eq_('*****@*****.**', addresses[0].lower())
def test_content_type(self): """Make sure watches filter properly by content type.""" watch_type = ContentType.objects.get_for_model(Watch) content_type_type = ContentType.objects.get_for_model(ContentType) registered_user = user(email='*****@*****.**', save=True) watch(event_type=TYPE, content_type=content_type_type, user=registered_user).save() watch(event_type=TYPE, content_type=content_type_type, email='*****@*****.**').save() watch(event_type=TYPE, content_type=watch_type, email='*****@*****.**').save() self._emails_eq(['*****@*****.**', '*****@*****.**'], ContentTypeEvent())
def test_anonymous_only(self): """Make sure having mixed watches claims right ones.""" # Watch some before registering. watch(email='*****@*****.**', save=True) watch(email='*****@*****.**', save=True) watch(email='*****@*****.**', save=True) # Register. u = user(email='*****@*****.**', save=True) claim_watches(u) # Original anonymous watch is claimed. assert not Watch.objects.filter(email='*****@*****.**').exists() eq_(2, Watch.objects.filter(email=None).count()) eq_(2, Watch.objects.filter(user=u).count()) # No other watches are affected. assert Watch.objects.filter(email='*****@*****.**').exists()
def test_unique_by_email_watch_collection(self): """Make sure _unique_by_email() collects all watches in each cluster.""" w1, w2, w3 = watch(), watch(), watch(email='hi') w4, w5, w6 = watch(), watch(), watch(email='lo') users_and_watches = [ (user(email='hi'), [w1]), (user(email='hi'), [w2]), (user(), [w3]), (user(email='lo'), [w4]), (user(email='lo'), [w5]), (user(), [w6])] result = list(_unique_by_email(users_and_watches)) _, watches = result[0] eq_(set([w1, w2, w3]), set(watches)) # Make sure the watches accumulator gets cleared between clusters: _, watches = result[1] eq_(set([w4, w5, w6]), set(watches))
def test_unique_by_email_user_selection(self): """Test the routine that sorts through users and watches having the same email addresses.""" # Test the best in a cluster coming first, in the middle, and last. # We mark the correct choices with first_name='a'. users_and_watches = [ (user(first_name='a', email='hi'), [watch()]), (user(email='hi'), [watch()]), (user(), [watch(email='hi')]), (user(), [watch(email='mid')]), (user(first_name='a', email='mid'), [watch()]), (user(), [watch(email='mid')]), (user(), [watch(email='lo')]), (user(), [watch(email='lo')]), (user(first_name='a', email='lo'), [watch()]), (user(), [watch(email='none', secret='a')]), (user(), [watch(email='none')])] favorites = list(_unique_by_email(users_and_watches)) eq_(4, len(favorites)) # Test that we chose the correct users, where there are distinguishable # (registered) users to choose from: eq_(['a'] * 3, [u.first_name for u, w in favorites[:3]])
def test_unsubscribe_url(self): """Make sure unsubscribe_url() returns something URL-ish.""" w = watch() url = w.unsubscribe_url() assert url.startswith('http') assert url.endswith('?s=%s' % w.secret)
def test_inactive(self): """Make sure inactive watches don't fire.""" watch(event_type=TYPE, email='*****@*****.**', is_active=False).save() watch(event_type=TYPE, email='*****@*****.**').save() self._emails_eq(['*****@*****.**'], SimpleEvent())
def make_anonymous_watches(): for x in xrange(3): watch(event_type=TYPE, email='*****@*****.**').save()
def test_confirmation(self): """Ensure the confirmation page renders if you feed it valid data.""" w = watch(save=True) response = self.client.get( reverse('tidings.unsubscribe', args=[w.pk]) + '?s=' + w.secret) self.assertContains(response, 'Are you sure you want to unsubscribe?')
def test_watch_lists(self): """Ensure the Union returns every watch a user has.""" w1 = watch(event_type=TYPE, email='*****@*****.**', save=True) w2 = watch(event_type=TYPE, email='*****@*****.**', save=True) u, w = list(EventUnion(SimpleEvent())._users_watching())[0] eq_([w1, w2], sorted(w, key=lambda x: x.id))
def test_fire(self, _mails): """Assert firing the union gets the mails from the first event.""" _mails.return_value = [] watch(event_type=TYPE, email='*****@*****.**').save() EventUnion(SimpleEvent(), AnotherEvent()).fire() assert _mails.called
def _users_watching(self): return [(user(email='*****@*****.**'), [watch()]), (user(email='*****@*****.**'), [watch()])]
def _users_watching(self): return [(user(email='*****@*****.**'), [watch()])]