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_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)
def setUp(self): """ Nadgers haystack SearchView so that: - we don't need a search backend - response is a TemplateResponse, not and HttpResponse (so that in tests we can check context contents) """ import django.template.response import haystack.views # SearchView passes a RequestContext to render_to_response, # but we want to be able to get hold of the request itself # (to pass on to TemplateResponse). class RequestContext(django.template.response.RequestContext): def __init__(self, request): super(RequestContext, self).__init__(request) self.request = request def render_to_response(template, context, context_instance): return django.template.response.TemplateResponse( request=context_instance.request, template=template, context=context) self.context_class = RequestContext minimock.mock("haystack.views.render_to_response", returns_func=render_to_response, tracker=None) minimock.mock("haystack.views.SearchView.get_results", returns_func=self.get_results, tracker=None) self.org = OrganizationFactory()
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_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_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_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_user_type_field_is_present_and_choice_type(self): """ Check that the form contains a boolean for user_type, and that it has the expected initial value. """ request = RequestFactory() organization = OrganizationFactory() form = CustomOrganizationUserAddForm(request, organization) self.assertIn('user_type', form.fields) self.assertTrue(isinstance(form.fields['user_type'], ChoiceField))
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_new_is_editor_field_is_present_and_boolean_type(self): """ Check that the form contains a boolean for is_editor, and that it has the expected initial value. """ request = RequestFactory() organization = OrganizationFactory() form = CustomOrganizationUserAddForm(request, organization) self.assertIn('is_editor', form.fields) self.assertTrue( isinstance(form.fields['is_editor'], BooleanField) ) self.assertTrue(form.fields['is_editor'].initial)
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_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_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_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_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): 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_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_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_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_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_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 create_fake_organization(**kwargs): return OrganizationFactory.build(**kwargs)
def _get_organization(): return OrganizationFactory.build()
def test_new_is_editor_field_is_present_and_boolean_type(self): request = RequestFactory() organization = OrganizationFactory() form = CustomOrganizationUserAddForm(request, organization) self.assertTrue('is_editor' in form.fields) self.assertTrue(isinstance(form.fields['is_editor'], BooleanField))
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_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)