def test_get_user_name_for_notification(self): user = UserFactory() user.username = "******" self.assertEquals("bobbins", publicweb_filters.get_user_name_for_notification(user)) user.first_name = "Robert" user.last_name = "Bins" self.assertEquals("Robert Bins", publicweb_filters.get_user_name_for_notification(user))
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 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_get_user_name_for_notification(self): user = UserFactory() user.username = "******" self.assertEquals( "bobbins", publicweb_filters.get_user_name_for_notification(user)) user.first_name = "Robert" user.last_name = "Bins" self.assertEquals( "Robert Bins", publicweb_filters.get_user_name_for_notification(user))
def test_multi_org_redirect_on_login(self): user = UserFactory() org_user1 = OrganizationUserFactory(user=user) org_user2 = OrganizationUserFactory(user=user) user.set_password('test') user.save() response = self.client.post(reverse('remember_me_login'), {'username': user.username, 'password': '******'}, follow = True) expected_url = reverse('organization_list') self.assertRedirects(response, expected_url)
def test_multi_org_redirect_on_login_with_other_next_query(self): user = UserFactory() org_user1 = OrganizationUserFactory(user=user) org_user2 = OrganizationUserFactory(user=user) user.set_password('test') user.save() response = self.client.post(reverse('auth_login')+'?next=/organizations/add/', {'username': user.username, 'password': '******'}, follow = True) expected_url = reverse('organization_add') self.assertRedirects(response, expected_url)
def login_admin_user(self): admin_user = UserFactory( username = '******', is_staff = True, is_superuser = True) admin_user.set_password('test') admin_user.save() self.assertEqual( self.client.login(username=admin_user.username, password='******'), True, "Failed to log in admin user" ) return admin_user
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 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)
def test_is_editor_is_true_adds_permission_to_instance(self): org = OrganizationFactory() user = UserFactory(email='*****@*****.**') # Check that permission is False self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org)) # Need to pass {'is_admin': True} for clean_is_admin to validate request = RequestFactory() request.user = UserFactory.build() form = CustomOrganizationUserAddForm(request, org) form.cleaned_data = {'is_editor': True, 'is_admin': True, 'email': user.email} form.save() # Now it should be True self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org))
def test_is_editor_is_false_removes_permission_from_instance(self): org = OrganizationFactory() user = UserFactory(email='*****@*****.**') assign(GUARDIAN_PERMISSION, user, org) # Confirm permission is True self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org)) request = RequestFactory() request.user = UserFactory.build() form = CustomOrganizationUserAddForm(request, org) form.cleaned_data = {'is_editor': False, 'is_admin': True, 'email': user.email} form.save() # Now it should be False self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org))
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_save_assigns_the_edit_decisions_feedback_permission_to_user(self): """ Tests that the permission has been assigned to the user. Also implicitly tests that the request.user is the user that is assigned to the organization. """ # Note need to set is_active=True otherwise has_perm will be False user = UserFactory(is_active=True, email='*****@*****.**') request = RequestFactory() # With the new save method, the requesting user is the one who gets the # new organiations permissions. request.user = user form = CustomOrganizationAddForm(request) form.cleaned_data = {'name': 'Test'} org = form.save() self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org))
def test_user_type_editor_adds_permission_to_instance(self): """ When user_type is editor, the correct permission is set for that user for that organization. """ org = OrganizationFactory() user = UserFactory(email='*****@*****.**') # Check the user doesn't have the permission self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org)) request = RequestFactory() request.user = UserFactory.build() form = CustomOrganizationUserAddForm(request, org) form.cleaned_data = {'user_type': 'editor', 'email': user.email} form.save() # Now they should have the permission self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org))
def test_user_type_viewer_removes_permission_from_instance(self): """ When user_type is set to viewer, the permission is removed. """ org = OrganizationFactory() user = UserFactory(email='*****@*****.**') assign_perm(GUARDIAN_PERMISSION, user, org) # Confirm the user has the permission self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org)) request = RequestFactory() request.user = UserFactory.build() form = CustomOrganizationUserAddForm(request, org) form.cleaned_data = {'user_type': 'viewer', 'email': user.email} form.save() # Now they shouldn't have the permission self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org))
def test_minor_edit_by_non_author_triggers_email(self): mail.outbox = [] self.feedback.description = "Not so quick" self.feedback.minor_edit = True self.feedback.editor = UserFactory(email="*****@*****.**") self.feedback.save() self.assertGreater(len(mail.outbox), 0)
def test_save_assigns_the_edit_decisions_feedback_permission_to_user(self): """ Tests that the permission has been assigned to the user. Also implicitly tests that the request.user is the user that is assigned to the organization. """ # Note: need to set is_active=True otherwise has_perm will be False user = UserFactory(is_active=True, email='*****@*****.**') request = RequestFactory() # With the new save method, the requesting user is the one who gets the # new organization's permissions. request.user = user form = CustomOrganizationAddForm(request) form.cleaned_data = {'name': 'Test'} org = form.save() self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org))
def test_get_settings_returns_settings_for_user(self): settings_handler = ObservationManager() user = UserFactory.build(id=1) settings = settings_handler.get_settings( user=user, organization=OrganizationFactory.build() ) self.assertEqual(user, settings.user)
def do_search_request(self, **params): kwargs = {'org_slug': self.org.slug} request = RequestFactory().get('/', params) request.user = UserFactory.build() request.session = {} view = DecisionSearchView() view.context_class = self.context_class return view(request, **kwargs)
def test_is_editor_is_true_adds_permission_to_instance(self): org = OrganizationFactory() user = UserFactory(email='*****@*****.**') # Check that permission is False self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org)) # Need to pass {'is_admin': True} for clean_is_admin to validate request = RequestFactory() request.user = UserFactory.build() form = CustomOrganizationUserAddForm(request, org) form.cleaned_data = { 'is_editor': True, 'is_admin': True, 'email': user.email } form.save() # Now it should be True self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org))
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_get_author_name(self): feedback = Feedback(author=None) self.assertEqual(feedback.get_author_name(), "An Anonymous Contributor") user = UserFactory() feedback = FeedbackFactory(author=user) self.assertEqual(feedback.get_author_name(), user.username)
def test_user_type_editor_adds_permission_to_instance(self): """ When user_type is editor, the correct permission is set for that user for that organization. """ org = OrganizationFactory() user = UserFactory(email='*****@*****.**') # Check the user doesn't have the permission self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org)) request = RequestFactory() request.user = UserFactory.build() form = CustomOrganizationUserAddForm(request, org) form.cleaned_data = {'user_type' : 'editor', 'email': user.email} form.save() # Now they should have the permission self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org))
def test_get_settings_returns_settings_for_organization(self): settings_handler = ObservationManager() organization = OrganizationFactory.build(id=2) settings = settings_handler.get_settings( user=UserFactory.build(), organization=organization ) self.assertEqual(organization, settings.organization)
def test_is_editor_is_false_removes_permission_from_instance(self): org = OrganizationFactory() user = UserFactory(email='*****@*****.**') assign(GUARDIAN_PERMISSION, user, org) # Confirm permission is True self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org)) request = RequestFactory() request.user = UserFactory.build() form = CustomOrganizationUserAddForm(request, org) form.cleaned_data = { 'is_editor': False, 'is_admin': True, 'email': user.email } form.save() # Now it should be False self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org))
def setUp(self): self.user = UserFactory(username='******', email='*****@*****.**', first_name='Bob', last_name='Dylan', is_staff=True) self.request = RequestFactory().get('blah') self.request.user = self.user self.view = YourDetails()
def test_get_settings_notification_level_deault_is_main_items_only(self): settings_handler = ObservationManager() settings = settings_handler.get_settings( user=UserFactory.build(), organization=OrganizationFactory.build() ) self.assertEqual( FEEDBACK_MAJOR_CHANGES, settings.notification_level )
def test_slug_generated_from_name(self): user = UserFactory() request = RequestFactory() request.user = user form = CustomOrganizationAddForm(request) org_name = "This is my org's name!!" form.cleaned_data = {'name': org_name} org = form.save() expected_slug = slugify(org_name) self.assertEqual(org.slug, expected_slug)
def add_watchers(decision): users = UserFactory.create_batch(size=3, email="*****@*****.**") for user in users: notice_type = NoticeTypeFactory() decision.watchers.add( ObservedItemFactory(added=datetime.now(utc), user=user, observed_object=decision, notice_type=notice_type)) return decision
def test_notification_settings_view_uses_notification_settings_form(self): user = UserFactory.build(id=1) organization = create_fake_organization(id=2, slug="test") request = RequestFactory().get("/") request.user = user context = UserNotificationSettings.as_view()(request, org_slug=organization.slug).context_data self.assertIsInstance(context["form"], NotificationSettingsForm)
def test_is_editor_is_true_adds_permission_to_instance(self): """ When is_editor is ticked, the correct permission is set for that user for that organization. """ org = OrganizationFactory() user = UserFactory(email='*****@*****.**') # Check the user doesn't have the permission self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org)) request = RequestFactory() request.user = UserFactory.build() form = CustomOrganizationUserAddForm(request, org) # Need to pass {'is_admin': True} for clean_is_admin to validate form.cleaned_data = {'is_editor': True, 'is_admin': True, 'email': user.email} form.save() # Now they should have the permission self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org))
def test_get_user_name_from_comment(self): comment = Comment(user=None, user_name='') self.assertEqual(publicweb_filters.get_user_name_from_comment(comment), "An Anonymous Contributor") comment.user_name = "Harry" self.assertEqual(publicweb_filters.get_user_name_from_comment(comment), "Harry") user = UserFactory() comment.user = user self.assertEqual(publicweb_filters.get_user_name_from_comment(comment), user.username)
def test_notification_settings_view_uses_notification_settings_form(self): user = UserFactory.build(id=1) organization = create_fake_organization(id=2, slug='test') request = RequestFactory().get('/') request.user = user context = UserNotificationSettings.as_view()( request, org_slug=organization.slug).context_data self.assertIsInstance(context['form'], NotificationSettingsForm)
def test_is_editor_is_false_removes_permission_from_instance(self): """ When is_editor gets unticked, the permission is removed. Also implicitly tests is_editor form field's required property because for a BooleanField, False is empty. """ org = OrganizationFactory() user = UserFactory(email='*****@*****.**') assign(GUARDIAN_PERMISSION, user, org) # Confirm the user has the permission self.assertTrue(user.has_perm(GUARDIAN_PERMISSION, org)) request = RequestFactory() request.user = UserFactory.build() form = CustomOrganizationUserAddForm(request, org) form.cleaned_data = {'is_editor': False, 'is_admin': True, 'email': user.email} form.save() # Now they shouldn't have the permission self.assertFalse(user.has_perm(GUARDIAN_PERMISSION, org))
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_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_request_user_is_new_organizations_admin(self): """ We are overriding django-organizations' default behavior to find and create users based on the requested email and are making the requesting user the default admin of a new organization. """ user = UserFactory() request = RequestFactory() request.user = user form = CustomOrganizationAddForm(request) form.cleaned_data = {'name': 'Test'} org = form.save() self.assertTrue(org.is_admin(user))
def add_watchers(decision): users = UserFactory.create_batch(size=3, email="*****@*****.**") for user in users: notice_type = NoticeTypeFactory() decision.watchers.add( ObservedItemFactory( added=datetime.now(utc), user=user, observed_object=decision, notice_type=notice_type ) ) return decision
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_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.")
def add_watchers(decision): users = UserFactory.build_batch(size=3) watchers = [] for index, user in enumerate(users): user.id = index notice_type = NoticeTypeFactory.build(id=index) decision.watchers.add( ObservedItemFactory.build(id=index, added=datetime.now(utc), user=user, observed_object=decision, notice_type=notice_type)) # decision.watchers = watchers return decision
def test_organisation_user_delete_view_doesnt_let_user_delete_others(self): org_user_delete_view = CustomOrganizationUserDelete() org_user = OrganizationUserFactory() org = org_user.organization user_1 = org_user.user user_2 = UserFactory() assign_perm(GUARDIAN_PERMISSION, user_1, org) request = RequestFactory().post("/", {'submit': "Delete"}) request.user = user_1 org_user_delete_view.get_object = lambda: org_user response = org_user_delete_view.dispatch( request, organization_pk=unicode(org.pk), user_pk=unicode(user_2.pk)) self.assertIsInstance(response, HttpResponseForbidden)
def test_status_set_in_get_and_get_context_data(self): """ More integrated test, that goes through dispatch, get, and get_context_data method to get the response object. """ org = OrganizationFactory() kwargs = {'org_slug': org.slug, 'status': Decision.PROPOSAL_STATUS} request = RequestFactory().get('/') request.user = UserFactory.build() request.session = {} dl = DecisionList.as_view() response = dl(request, **kwargs) self.assertEqual(response.context_data['tab'], Decision.PROPOSAL_STATUS)
def login_admin_user(self): admin_user = UserFactory(username='******', is_staff=True, is_superuser=True) admin_user.set_password('test') admin_user.save() self.assertEqual( self.client.login(username=admin_user.username, password='******'), True, "Failed to log in admin user") return admin_user
def add_watchers(decision): users = UserFactory.build_batch(size=3) watchers = [] for index, user in enumerate(users): user.id = index notice_type = NoticeTypeFactory.build(id=index) decision.watchers.add( ObservedItemFactory.build( id=index, added=datetime.now(utc), user=user, observed_object=decision, notice_type=notice_type ) ) # decision.watchers = watchers return decision
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_posting_invalid_data_returns_form_with_errors(self, settings_obj): user = UserFactory.build(id=1) organization = create_fake_organization(id=2, slug="test") request = RequestFactory().post(reverse("notification_settings", args=[organization.id])) request.user = user # This patch depends on the UsertNotificationSettings.model patch # It needs to return the object created by that patch, which is passed # in as a parameter. # The only way I've found to handle the dependency is to do this patch # here with patch( "publicweb.views.UserNotificationSettings.model.objects", get=lambda organization, user: settings_obj ): response = UserNotificationSettings.as_view()(request, org_slug=organization.slug) self.assertIn("form", response.context_data) self.assertTrue(response.context_data["form"].errors)
def test_cancel_doesnt_save_settings(self, settings_obj): user = UserFactory.build(id=1) organization = create_fake_organization(id=2, slug="test") request = RequestFactory().post( reverse("notification_settings", args=[organization.id]), {"notification_level": unicode(NO_NOTIFICATIONS), "submit": "Cancel"}, ) request.user = user # This patch depends on the UsertNotificationSettings.model patch # It needs to return the object created by that patch, which is passed # in as a parameter. # The only way I've found to handle the dependency is to do this patch # here with patch( "publicweb.views.UserNotificationSettings.model.objects", get=lambda organization, user: settings_obj ): UserNotificationSettings.as_view()(request, org_slug=organization.slug) self.assertFalse(settings_obj.save.called)
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)