def setUp(self): self.user = UserFactory() self.unread_notification = NotificationFactory(user=self.user, read=False) self.read_notification = NotificationFactory(user=self.user, read=True) self.url = reverse('acct-notifications-unread') self.view = views.UnreadNotificationList.as_view()
def test_when_unread(self, mock_send): """The send method should be called when a user has unread notifications.""" NotificationFactory(user=self.user) tasks.daily_digest() mock_send.assert_called_with( self.user, u'Daily Digest', relativedelta(days=1) )
class TestNotificationList(TestCase): """A user should be able to view lists of their notifications.""" def setUp(self): self.user = UserFactory() self.unread_notification = NotificationFactory(user=self.user, read=False) self.read_notification = NotificationFactory(user=self.user, read=True) self.url = reverse("acct-notifications") self.view = views.NotificationList.as_view() def test_get(self): """The view should provide a list of notifications for the user.""" response = http_get_response(self.url, self.view, self.user) eq_(response.status_code, 200, "The view should return OK.") object_list = response.context_data["object_list"] ok_( self.unread_notification in object_list, "The context should contain the unread notification.", ) ok_( self.read_notification in object_list, "The context should contain the read notification.", ) def test_unauthorized_get(self): """Logged out users trying to access the notifications view should be redirected to the login view.""" response = http_get_response(self.url, self.view) eq_(response.status_code, 302, "The view should redirect.") ok_( reverse("acct-login") in response.url, "Logged out users should be redirected to the login view.", ) def test_mark_all_read(self): """Users should be able to mark all their notifications as read.""" data = {"action": "mark_all_read"} ok_(self.unread_notification.read is not True) http_post_response(self.url, self.view, data, self.user) self.unread_notification.refresh_from_db() ok_( self.unread_notification.read is True, "The unread notification should be marked as read.", )
def test_for_user(self): """Notifications should be filterable by a single user.""" user_notification = NotificationFactory(user=self.user) user_notifications = Notification.objects.for_user(self.user) ok_( user_notification in user_notifications, 'A notification for the user should be in the set returned.' ) ok_( self.notification not in user_notifications, 'A notification for another user should not be in the set returned.' )
def test_for_object(self): """Notifications should be filterable by a single object.""" foia = FOIARequestFactory() _action = new_action(UserFactory(), "submitted", target=foia) object_notification = NotificationFactory(user=self.user, action=_action) object_notifications = Notification.objects.for_object(foia) ok_( object_notification in object_notifications, "A notification for the object should be in the set returned.", ) ok_( self.notification not in object_notifications, "A notification not including the object should not be in the set returned.", )
def test_for_model(self): """Notifications should be filterable by a model type.""" foia = FOIARequestFactory() _action = new_action(UserFactory(), 'submitted', target=foia) object_notification = NotificationFactory( user=self.user, action=_action ) model_notifications = Notification.objects.for_model(foia) ok_( object_notification in model_notifications, 'A notification for the model should be in the set returned.' ) ok_( self.notification not in model_notifications, 'A notification not including the model should not be in the set returned.' )
def test_when_unread(self, mock_send): """The send method should be called when a user has unread notifications.""" NotificationFactory(user=self.user) tasks.daily_digest() mock_send.assert_called_with(self.user.pk, "Daily Digest", "daily")
def setUp(self): self.user = UserFactory() self.action = new_action(self.user, 'acted') self.notification = NotificationFactory()
class TestNotifications(TestCase): """Notifications connect actions to users and contain a read state.""" def setUp(self): self.user = UserFactory() self.action = new_action(self.user, 'acted') self.notification = NotificationFactory() def test_create_notification(self): """Create a notification with a user and an action.""" notification = Notification.objects.create( user=self.user, action=self.action ) ok_(notification, 'Notification object should create without error.') ok_( isinstance(notification, Notification), 'Object should be a Notification.' ) ok_( notification.read is not True, 'Notification sould be unread by default.' ) def test_mark_read(self): """Notifications should be markable as read if unread and unread if read.""" self.notification.mark_read() ok_( self.notification.read is True, 'Notification should be marked as read.' ) self.notification.mark_unread() ok_( self.notification.read is not True, 'Notification should be marked as unread.' ) def test_for_user(self): """Notifications should be filterable by a single user.""" user_notification = NotificationFactory(user=self.user) user_notifications = Notification.objects.for_user(self.user) ok_( user_notification in user_notifications, 'A notification for the user should be in the set returned.' ) ok_( self.notification not in user_notifications, 'A notification for another user should not be in the set returned.' ) def test_for_model(self): """Notifications should be filterable by a model type.""" foia = FOIARequestFactory() _action = new_action(UserFactory(), 'submitted', target=foia) object_notification = NotificationFactory( user=self.user, action=_action ) model_notifications = Notification.objects.for_model(foia) ok_( object_notification in model_notifications, 'A notification for the model should be in the set returned.' ) ok_( self.notification not in model_notifications, 'A notification not including the model should not be in the set returned.' ) def test_for_object(self): """Notifications should be filterable by a single object.""" foia = FOIARequestFactory() _action = new_action(UserFactory(), 'submitted', target=foia) object_notification = NotificationFactory( user=self.user, action=_action ) object_notifications = Notification.objects.for_object(foia) ok_( object_notification in object_notifications, 'A notification for the object should be in the set returned.' ) ok_( self.notification not in object_notifications, 'A notification not including the object should not be in the set returned.' ) def test_get_unread(self): """Notifications should be filterable by their unread status.""" self.notification.mark_unread() ok_( self.notification in Notification.objects.get_unread(), 'Unread notifications should be in the set returned.' ) self.notification.mark_read() ok_( self.notification not in Notification.objects.get_unread(), 'Read notifications should not be in the set returned.' )