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 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_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_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_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 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 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_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 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_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_multi_org_redirect_on_login_with_orglist_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=/', {'username': user.username, 'password': '******'}, follow = True) expected_url = reverse('organization_list') self.assertRedirects(response, expected_url)
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_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 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 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_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_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 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_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_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_organisation_user_delete_view_is_accessible_to_admin(self): org_user_delete_view = CustomOrganizationUserDelete() org_user = OrganizationUserFactory(is_admin=True) 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=org.pk, user_pk=user_2.pk) self.assertEqual( reverse('organization_user_list', args=[org.pk]), response['Location'])
def test_slug_generated_from_name(self): """ An unique slug should be generated for each Organization from its name. """ org_name = "This is my org's name!!" expected_slug = slugify(org_name) org1 = OrganizationFactory(name=org_name) self.assertEqual(org1.slug, expected_slug) user = UserFactory() request = RequestFactory() request.user = user form = CustomOrganizationAddForm(request) form.cleaned_data = {'name': org_name} org2 = form.save() self.assertNotEqual(org2.slug, org1.slug) self.assertTrue(org2.slug.startswith(org1.slug))
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_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_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))