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_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 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_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_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 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_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_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): 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_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 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_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_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_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_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_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_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_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_posting_valid_data_saves_settings(self, settings_obj): organization = create_fake_organization(id=2, slug='test') request = RequestFactory().post( reverse('notification_settings', args=[organization.slug]), {'notification_level': unicode(NO_NOTIFICATIONS)}) user = UserFactory.build(id=1) 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.assertTrue(settings_obj.save.called)
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_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 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_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)