コード例 #1
0
    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)
コード例 #2
0
    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()
コード例 #3
0
ファイル: forms_test.py プロジェクト: rrosajp/econsensus
 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))
コード例 #4
0
    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)
コード例 #5
0
 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)
コード例 #6
0
ファイル: alt_view_test.py プロジェクト: yogiblue/econsensus
 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)
コード例 #7
0
ファイル: forms_test.py プロジェクト: rrosajp/econsensus
 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))
コード例 #8
0
ファイル: forms_test.py プロジェクト: rrosajp/econsensus
 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))
コード例 #9
0
ファイル: forms_test.py プロジェクト: yogiblue/econsensus
 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))
コード例 #10
0
ファイル: forms_test.py プロジェクト: yogiblue/econsensus
 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))
コード例 #11
0
    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)
コード例 #12
0
    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))
コード例 #13
0
 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))
コード例 #14
0
    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)
コード例 #15
0
 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))
コード例 #16
0
ファイル: forms_test.py プロジェクト: yogiblue/econsensus
 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))