def test_one_result_in_correct_org(self): """ Test content of search page context for when a search yields a single result, but only because all but one of the matching decisions are in an organization that we are not looking in. """ decision = DecisionFactory(organization=self.org) other_org = OrganizationFactory() for _ in range(100): other_decision = DecisionFactory(organization=other_org) response = self.do_search_request(q='aardvark') context = response.context_data self.assertEqual(context.get('tab'), 'search') self.assertEqual('aardvark', context.get('query')) self.assertEqual(self.org, context.get('organization')) self.assertTrue(context.get('page')) page = context.get('page') self.assertEqual(1, len(page.object_list)) self.assertFalse(page.has_previous()) self.assertFalse(page.has_next()) self.assertEquals(1, page.number) self.assertEquals(1, page.paginator.num_pages)
class DecisionNotificationTest(TestCase): def setUp(self): user = UserFactory() self.decision = DecisionFactory(author=user, description="Eat Cheese") watcher = UserFactory(email="*****@*****.**") organization = self.decision.organization self.settings = NotificationSettingsFactory( user=watcher, organization=organization, notification_level=FEEDBACK_ADDED_NOTIFICATIONS ) OrganizationUserFactory(user=watcher, organization=organization) def test_edit_triggers_email(self): mail.outbox = [] self.decision.description = "Make Cheese" self.decision.save() self.assertGreater(len(mail.outbox), 0) def test_minor_edit_triggers_no_email(self): mail.outbox = [] self.decision.description = "Eat Cheese!" self.decision.minor_edit = True self.decision.save() self.assertEqual(len(mail.outbox), 0)
def setUp(self): user = UserFactory() self.decision = DecisionFactory(author=user, description="Eat Cheese") watcher = UserFactory(email="*****@*****.**") organization = self.decision.organization self.settings = NotificationSettingsFactory( user=watcher, organization=organization, notification_level=FEEDBACK_ADDED_NOTIFICATIONS) OrganizationUserFactory(user=watcher, organization=organization)
def setUp(self): self.user = UserFactory() self.decision = DecisionFactory() # Ensure value of "now" always increases by amount sufficient # to show up as a change, even if db resolution for datetime # is one second. def now_iter(start): t = start while True: t += datetime.timedelta(hours=1) yield t minimock.mock("timezone.now", returns_iter=now_iter(timezone.now()), tracker=None)
def test_middle_page_with_nondefault_items_per_page(self): """ Test content of search page context for when a search yields results that must be split across three pages. To make things more interesting, we are not using the default number of items per page, and we are asking for the second of the pages. """ for _ in range(75): decision = DecisionFactory(organization=self.org) response = self.do_search_request(q='aardvark', num='25', page='2') context = response.context_data self.assertEqual(context.get('tab'), 'search') self.assertEqual('25', context.get('num')) self.assertEqual('aardvark', context.get('query')) self.assertEqual(self.org, context.get('organization')) page = context.get('page') self.assertEqual(25, len(page.object_list)) self.assertTrue(page.has_previous()) self.assertTrue(page.has_next()) self.assertEquals(2, page.number) self.assertEquals(3, page.paginator.num_pages) self.assertEquals("?q=aardvark&num=25", context.get("queryurl"))
def test_get_organization_returns_organization_for_decision(self): expected_organization = OrganizationFactory.build(id=1) decision = DecisionFactory.build(id=2, organization=expected_organization) settings_handler = ObservationManager() actual_organization = settings_handler._get_organization(decision) self.assertEqual(expected_organization, actual_organization)
def test_include_watchers_adds_watchers_for_decision(self): decision = DecisionFactory.build(id=1) decision = add_watchers(decision) settings_handler = ObservationManager() settings_handler.include_watchers(decision) self.assertSetEqual( set([watcher.user for watcher in decision.watchers.all()]), settings_handler.recipient_list)
def test_include_watchers_not_run_for_minor_edits(self, include_watchers): item = DecisionFactory.build(id=1) item = add_watchers(item) settings_handler = ObservationManager() settings_handler.send_notifications([], item, MINOR_CHANGE, {}, {}, "") self.assertFalse(include_watchers.called)
def setUp(self): mail.outbox = [] self.user = UserFactory(email="*****@*****.**") decision = DecisionFactory(author=self.user) feedbackAuthor = UserFactory(email="*****@*****.**") self.feedback = FeedbackFactory(decision=decision, description="Not so fast", author=feedbackAuthor, editor=feedbackAuthor)
def test_include_watchers_adds_watchers_for_decision(self): decision = DecisionFactory.build(id=1) decision = add_watchers(decision) settings_handler = ObservationManager() settings_handler.include_watchers(decision) self.assertSetEqual( set([watcher.user for watcher in decision.watchers.all()]), settings_handler.recipient_list )
def test_get_organization_returns_organization_for_decision(self): expected_organization = OrganizationFactory.build(id=1) decision = DecisionFactory.build( id=2, organization=expected_organization ) settings_handler = ObservationManager() actual_organization = settings_handler._get_organization(decision) self.assertEqual(expected_organization, actual_organization)
class DecisionNotificationTest(TestCase): def setUp(self): user = UserFactory() self.decision = DecisionFactory(author=user, description="Eat Cheese") watcher = UserFactory(email="*****@*****.**") notification.observe(self.decision, watcher, 'decision_change') def test_edit_triggers_email(self): mail.outbox = [] self.decision.description = "Make Cheese" self.decision.save() self.assertGreater(len(mail.outbox), 0) def test_minor_edit_triggers_no_email(self): mail.outbox = [] self.decision.description = "Eat Cheese!" self.decision.minor_edit = True self.decision.save() self.assertEqual(len(mail.outbox), 0)
def test_status_is_set_in_context_data_and_limits_object_list(self): """ More integrated test, that goes through dispatch, get, and get_context_data method to get the response object and test it. """ decision = DecisionFactory(status=Decision.DECISION_STATUS) org = decision.organization archive = DecisionFactory(organization=org, status=Decision.ARCHIVED_STATUS) kwargs = {'org_slug': org.slug, 'status': archive.status} request = RequestFactory().get('/') request.user = UserFactory.build() request.session = {} response = DecisionList.as_view()(request, **kwargs) self.assertIn('tab', response.context_data.keys()) self.assertEqual(response.context_data['tab'], archive.status) self.assertIn(archive, response.context_data['object_list']) self.assertNotIn(decision, response.context_data['object_list'])
def test_get_organization_returns_organization_for_comment(self): expected_organization = OrganizationFactory.build(id=1) decision = DecisionFactory.build(id=2, organization=expected_organization) feedback = FeedbackFactory.build(id=2, decision=decision) comment = CommentFactory.build() comment.content_object = feedback settings_handler = ObservationManager() actual_organization = settings_handler._get_organization(comment) self.assertEqual(expected_organization, actual_organization)
def test_send_notifications_for_main_items_sends_correct_messages(self): initial_count = len(mail.outbox) number_of_users = 3 organization = OrganizationFactory() decision = DecisionFactory(organization=organization) decision = add_watchers(decision) user1, user2, user3 = UserFactory.create_batch(number_of_users, email="*****@*****.**") NotificationSettingsFactory(user=user1, organization=organization, notification_level=NO_NOTIFICATIONS), NotificationSettingsFactory( user=user2, organization=organization, notification_level=MAIN_ITEMS_NOTIFICATIONS_ONLY), NotificationSettingsFactory( user=user3, organization=organization, notification_level=MINOR_CHANGES_NOTIFICATIONS), settings_handler = ObservationManager() recipients = [user1, user2, user3] settings_handler.send_notifications( recipients, decision, DECISION_NEW, {"observed": decision}, { 'Message-ID': decision.get_message_id(), 'Precedence': 'bulk', 'Auto-Submitted': 'auto-generated' }, "*****@*****.**") final_count = len(mail.outbox) expected_number_messages_sent = len( decision.watchers.all()) + number_of_users - 1 actual_number_messages_sent = final_count - initial_count self.assertEqual(expected_number_messages_sent, actual_number_messages_sent)
def test_include_watchers_adds_watchers_for_comments(self): decision = DecisionFactory.build(id=1) decision = add_watchers(decision) feedback = FeedbackFactory.build(id=2, decision=decision) comment = CommentFactory.build() comment.content_object = feedback settings_handler = ObservationManager() settings_handler.include_watchers(comment) self.assertSetEqual( set([watcher.user for watcher in decision.watchers.all()]), settings_handler.recipient_list)
def setUp(self): user = UserFactory() self.decision = DecisionFactory(author=user, description="Eat Cheese") watcher = UserFactory(email="*****@*****.**") organization = self.decision.organization self.settings = NotificationSettingsFactory( user=watcher, organization=organization, notification_level=FEEDBACK_ADDED_NOTIFICATIONS ) OrganizationUserFactory(user=watcher, organization=organization)
def test_include_watchers_adds_watchers_for_comments(self): decision = DecisionFactory.build(id=1) decision = add_watchers(decision) feedback = FeedbackFactory.build(id=2, decision=decision) comment = CommentFactory.build() comment.content_object = feedback settings_handler = ObservationManager() settings_handler.include_watchers(comment) self.assertSetEqual( set([watcher.user for watcher in decision.watchers.all()]), settings_handler.recipient_list )
def test_get_organization_returns_organization_for_comment(self): expected_organization = OrganizationFactory.build(id=1) decision = DecisionFactory.build( id=2, organization=expected_organization ) feedback = FeedbackFactory.build(id=2, decision=decision) comment = CommentFactory.build() comment.content_object = feedback settings_handler = ObservationManager() actual_organization = settings_handler._get_organization(comment) self.assertEqual(expected_organization, actual_organization)
def test_decision_editor_set_on_update_via_admin(self): user = UserFactory(username='******') decision = DecisionFactory(author=user, editor=user, description="Lorem Ipsum") new_organization = OrganizationFactory() admin_user = self.login_admin_user() self.change_decision_via_admin(decision, new_organization) decision = Decision.objects.get(id=decision.id) self.assertEquals(decision.editor, admin_user)
def test_decision_last_status_set_on_update_via_admin(self): status = Decision.DECISION_STATUS user = UserFactory() decision = DecisionFactory(author=user, editor=user, description="Lorem", status=status) self.assertEquals(decision.last_status, 'new') self.login_admin_user() self.change_decision_via_admin(decision) decision = Decision.objects.get(id=decision.id) self.assertEqual(decision.last_status, status)
def test_is_watched_returns_true_if_user_in_watchers_list(self): user_1 = UserFactory.build(id=1) user_2 = UserFactory.build(id=2) decision = DecisionFactory.build(organization=OrganizationFactory.build()) notice_type = NoticeTypeFactory.build() observed_item_1 = ObservedItemFactory.build(user=user_1, observed_object=decision, notice_type=notice_type) observed_item_2 = ObservedItemFactory.build(user=user_2, observed_object=decision, notice_type=notice_type) mock_item = MagicMock() mock_item.watchers.all = lambda: [observed_item_1, observed_item_2] self.assertTrue(is_watching(user_1, mock_item))
def test_feedback_author_is_assigned_on_feedback_create(self): decision = DecisionFactory() user = UserFactory() request = RequestFactory() request.user = user feedback_create_view = FeedbackCreate() feedback_create_view.request = request feedback_create_view.kwargs = {'parent_pk': decision.id} form = FeedbackForm() form.cleaned_data = {} feedback_create_view.form_valid(form) feedback = decision.feedback_set.get() self.assertEqual(feedback.author, user)
def test_send_notifications_for_main_items_sends_correct_messages(self): initial_count = len(mail.outbox) number_of_users = 3 organization = OrganizationFactory() decision = DecisionFactory( organization=organization ) decision = add_watchers(decision) user1, user2, user3 = UserFactory.create_batch( number_of_users, email="*****@*****.**" ) NotificationSettingsFactory(user=user1, organization=organization, notification_level=NO_NOTIFICATIONS), NotificationSettingsFactory(user=user2, organization=organization, notification_level=MAIN_ITEMS_NOTIFICATIONS_ONLY), NotificationSettingsFactory(user=user3, organization=organization, notification_level=MINOR_CHANGES_NOTIFICATIONS), settings_handler = ObservationManager() recipients = [user1, user2, user3] settings_handler.send_notifications( recipients, decision, DECISION_NEW, {"observed": decision}, { 'Message-ID' : decision.get_message_id(), 'Precedence': 'bulk', 'Auto-Submitted': 'auto-generated' }, "*****@*****.**" ) final_count = len(mail.outbox) expected_number_messages_sent = len(decision.watchers.all()) + number_of_users - 1 actual_number_messages_sent = final_count - initial_count self.assertEqual(expected_number_messages_sent, actual_number_messages_sent)
class DecisionLastModifiedTest(TestCase): """ Tests updating of 'last_modified' date on Decision. """ def setUp(self): self.user = UserFactory() self.decision = DecisionFactory() # Ensure value of "now" always increases by amount sufficient # to show up as a change, even if db resolution for datetime # is one second. def now_iter(start): t = start while True: t += datetime.timedelta(hours=1) yield t minimock.mock("timezone.now", returns_iter=now_iter(timezone.now()), tracker=None) def tearDown(self): minimock.restore() def last_modified(self): """ Gets the last modified date of the test decision. """ return Decision.objects.get(id=self.decision.id).last_modified def test_edit_decision_editor(self): orig_last_modified = self.last_modified() self.decision.editor = UserFactory() self.decision.save() self.assertEquals(orig_last_modified, self.last_modified()) def test_edit_decision_description(self): orig_last_modified = self.last_modified() self.decision.description += "x" self.decision.save() self.assertTrue(orig_last_modified < self.last_modified()) def test_add_feedback_triggers_update(self): orig_last_modified = self.last_modified() feedback = FeedbackFactory(decision=self.decision, author=self.user) self.assertTrue(orig_last_modified < self.last_modified()) def test_add_comment_triggers_update(self): feedback = FeedbackFactory(decision=self.decision, author=self.user) orig_last_modified = self.last_modified() comment = CommentFactory(content_object=feedback, user=self.user) self.assertTrue(orig_last_modified < self.last_modified()) def test_add_watcher_triggers_no_update(self): orig_last_modified = self.last_modified() notification.observe(self.decision, UserFactory(), 'decision_change') self.decision.save() self.assertTrue(orig_last_modified == self.last_modified())
class DecisionLastModifiedTest(TestCase): """ Tests updating of 'last_modified' date on Decision. """ def setUp(self): self.user = UserFactory() self.decision = DecisionFactory() # Ensure value of "now" always increases by amount sufficient # to show up as a change, even if db resolution for datetime # is one second. def now_iter(start): t = start while True: t += datetime.timedelta(hours=1) yield t minimock.mock("timezone.now", returns_iter=now_iter(timezone.now()), tracker=None) def tearDown(self): minimock.restore() def last_modified(self): """ Gets the last modified date of the test decision. """ return Decision.objects.get(id=self.decision.id).last_modified def test_edit_decision_editor(self): orig_last_modified = self.last_modified() self.decision.editor = UserFactory() self.decision.save() self.assertEquals(orig_last_modified, self.last_modified()) def test_edit_decision_description(self): orig_last_modified = self.last_modified() self.decision.description += "x" self.decision.save() self.assertTrue(orig_last_modified < self.last_modified()) def test_add_feedback_triggers_update(self): orig_last_modified = self.last_modified() feedback = FeedbackFactory(decision=self.decision, author=self.user) self.assertTrue(orig_last_modified < self.last_modified()) def test_add_comment_triggers_update(self): feedback = FeedbackFactory(decision=self.decision, author=self.user) orig_last_modified = self.last_modified() comment = CommentFactory(content_object=feedback, user=self.user) self.assertTrue(orig_last_modified < self.last_modified()) def test_add_watcher_triggers_no_update(self): orig_last_modified = self.last_modified() notification.observe(self.decision, UserFactory(), "decision_change") self.decision.save() self.assertTrue(orig_last_modified == self.last_modified())
def setUp(self): mail.outbox = [] self.user = UserFactory(email="*****@*****.**") decision = DecisionFactory(author=self.user) feedbackAuthor = UserFactory(email="*****@*****.**") self.feedback = FeedbackFactory(decision=decision, description="Not so fast", author=feedbackAuthor, editor=feedbackAuthor) organization = decision.organization self.settings = NotificationSettingsFactory( user=self.user, organization=organization, notification_level=FEEDBACK_MAJOR_CHANGES) OrganizationUserFactory(user=self.user, organization=organization)
class DecisionNotificationTest(TestCase): def setUp(self): user = UserFactory() self.decision = DecisionFactory(author=user, description="Eat Cheese") watcher = UserFactory(email="*****@*****.**") organization = self.decision.organization self.settings = NotificationSettingsFactory( user=watcher, organization=organization, notification_level=FEEDBACK_ADDED_NOTIFICATIONS) OrganizationUserFactory(user=watcher, organization=organization) def test_edit_triggers_email(self): mail.outbox = [] self.decision.description = "Make Cheese" self.decision.save() self.assertGreater(len(mail.outbox), 0) def test_minor_edit_triggers_no_email(self): mail.outbox = [] self.decision.description = "Eat Cheese!" self.decision.minor_edit = True self.decision.save() self.assertEqual(len(mail.outbox), 0)
def test_is_watched_returns_true_if_user_in_watchers_list(self): user_1 = UserFactory.build(id=1) user_2 = UserFactory.build(id=2) decision = DecisionFactory.build( organization=OrganizationFactory.build()) notice_type = NoticeTypeFactory.build() observed_item_1 = ObservedItemFactory.build(user=user_1, observed_object=decision, notice_type=notice_type) observed_item_2 = ObservedItemFactory.build(user=user_2, observed_object=decision, notice_type=notice_type) mock_item = MagicMock() mock_item.watchers.all = lambda: [observed_item_1, observed_item_2] self.assertTrue(is_watching(user_1, mock_item))
def test_decision_editor_set_on_update(self): # Create the decision user1 = UserFactory() decision = DecisionFactory(author=user1, editor=user1) # Have a different user update it user2 = UserFactory() request = RequestFactory() request.user = user2 decision_update_view = DecisionUpdate() decision_update_view.request = request decision_update_view.object = decision decision_update_view.get_object = lambda: decision decision_update_view.last_status = 'dummy' form = DecisionForm(instance=decision) form.cleaned_data = {'watch': True} decision_update_view.form_valid(form) self.assertEqual(decision.editor, user2)
def test_active_users_of_org_added_to_watchers_of_new_decision(self): """ See no reason to have this in a view test, this behavior is almost entirely tested already in the model tests in tests/decisions_test.py Suggest, moving this test to there. """ org = OrganizationFactory() active_org_user1 = OrganizationUserFactory(organization=org) active_org_user2 = OrganizationUserFactory(organization=org) inactive_org_user = OrganizationUserFactory( user=UserFactory(is_active=False), organization=org) active_other_org_user = OrganizationUserFactory() decision = DecisionFactory(organization=org) watching_user_ids = decision.watchers.values_list('user_id', flat=True) self.assertIn(active_org_user1.user.id, watching_user_ids) self.assertIn(active_org_user2.user.id, watching_user_ids) self.assertEqual(len(watching_user_ids), 2)
def test_decision_last_status_set_on_update(self): status = Decision.DECISION_STATUS user = UserFactory() decision = DecisionFactory(author = user, editor = user, description = 'Lorem', status = status) self.assertEquals(decision.last_status, 'new') request = RequestFactory().get('/') assign_perm('edit_decisions_feedback', user, decision.organization) request.user = user request.method = 'POST' request.POST = {'status': decision.status, 'description': decision.description} kwargs = {'pk': decision.id} DecisionUpdate.as_view()(request, **kwargs) decision = Decision.objects.get(id=decision.id) self.assertEqual(decision.last_status, status)
def test_user_can_unwatch_a_decision(self): org_user = OrganizationUserFactory() org = org_user.organization user = org_user.user decision = DecisionFactory(organization=org) # Confirm decision has a single watcher self.assertEqual(decision.watchers.count(), 1) # Get the view ready request = RequestFactory() request.user = user decision_update_view = DecisionUpdate() decision_update_view.request = request decision_update_view.object = decision decision_update_view.get_object = lambda: decision decision_update_view.last_status = 'dummy' form = DecisionForm(instance=decision) form.cleaned_data = {'watch': False} # Run the form_valid method to stop observing decision_update_view.form_valid(form) self.assertEqual(decision.watchers.count(), 0)
def test_one_result(self): """ Test content of search page context for when a search yields a single result. """ decision = DecisionFactory(organization=self.org) response = self.do_search_request(q='aardvark') context = response.context_data self.assertEqual(context.get('tab'), 'search') self.assertEqual('aardvark', context.get('query')) self.assertEqual(self.org, context.get('organization')) self.assertTrue(context.get('page')) page = context.get('page') self.assertEqual(1, len(page.object_list)) self.assertFalse(page.has_previous()) self.assertFalse(page.has_next()) self.assertEquals(1, page.number) self.assertEquals(1, page.paginator.num_pages)
def test_more_than_one_page(self): """ Test content of search page context for when a search yields results that must be split across two pages. """ for _ in range(20): decision = DecisionFactory(organization=self.org) response = self.do_search_request(q='aardvark') context = response.context_data self.assertEqual(context.get('tab'), 'search') self.assertEqual('10', context.get('num')) self.assertEqual('aardvark', context.get('query')) self.assertEqual(self.org, context.get('organization')) page = context.get('page') self.assertEqual(10, len(page.object_list)) self.assertFalse(page.has_previous()) self.assertTrue(page.has_next()) self.assertEquals(1, page.number) self.assertEquals(2, page.paginator.num_pages) self.assertEquals("?q=aardvark", context.get("queryurl"))
def test_active_users_of_org_added_to_watchers_of_new_decision(self): """ See no reason to have this in a view test, this behavior is almost entirely tested already in the model tests in tests/decisions_test.py Suggest, moving this test to there. """ org_user_factory = OrganizationUserFactory() org = org_user_factory.organization user_in_org = org_user_factory.user UserFactory() # Create one user not linked to the org for completeness user_inactive = UserFactory(is_active=False) # Link the inactive user with the existing org and confirm it worked OrganizationUserFactory(user=user_inactive, organization=org) user_status_list = [user.user.is_active for user in org.organization_users.all()] assert user_status_list.count(True) == 1 assert user_status_list.count(False) == 1 # The Test decision = DecisionFactory(organization=org) self.assertEqual(decision.watchers.count(), 1, "There should be one watcher.") self.assertEqual(decision.watchers.get().user, user_in_org, "The watcher user should be the active user.")
class DecisionLastModifiedTest(EconsensusFixtureTestCase): """ Tests updating of 'last_modified' date on Decision. """ def setUp(self): self.user = UserFactory() self.decision = DecisionFactory() def last_modified(self): """ Gets the last modified date of the test decision. """ return Decision.objects.get(id=self.decision.id).last_modified def test_edit_decision_editor(self): orig_last_modified = self.last_modified() self.decision.editor = UserFactory() self.decision.save() self.assertEquals(orig_last_modified, self.last_modified()) def test_edit_decision_description(self): orig_last_modified = self.last_modified() self.decision.description += "x" self.decision.save() self.assertTrue(orig_last_modified < self.last_modified()) def test_add_feedback_triggers_update(self): orig_last_modified = self.last_modified() FeedbackFactory(decision=self.decision, author=self.user) self.assertTrue(orig_last_modified < self.last_modified()) def test_add_comment_triggers_update(self): feedback = FeedbackFactory(decision=self.decision, author=self.user) orig_last_modified = self.last_modified() comment = CommentFactory(content_object=feedback, user=self.user) self.send_comment_posted_signal(comment) self.assertTrue(orig_last_modified < self.last_modified()) def test_add_watcher_triggers_no_update(self): orig_last_modified = self.last_modified() notification.observe(self.decision, UserFactory(), DECISION_CHANGE) self.decision.save() self.assertTrue(orig_last_modified == self.last_modified())
def setUp(self): self.user = UserFactory() self.decision = DecisionFactory()
def setUp(self): user = UserFactory() self.decision = DecisionFactory(author=user, description="Eat Cheese") watcher = UserFactory(email="*****@*****.**") notification.observe(self.decision, watcher, 'decision_change')