Beispiel #1
0
    def test_average_accounts_per_user(self):
        """
        Test that the 'Average accounts per user' statistic returns the correct
        number.
        """
        # Create 3 users
        user1 = UserFactory()
        user2 = UserFactory()
        _ = UserFactory()

        # Three partners
        partner1 = PartnerFactory()
        partner2 = PartnerFactory()
        partner3 = PartnerFactory()

        # Four Authorizations
        auth1 = Authorization(user=user1, partner=partner1)
        auth1.save()
        auth2 = Authorization(user=user2, partner=partner1)
        auth2.save()
        auth3 = Authorization(user=user2, partner=partner2)
        auth3.save()
        auth4 = Authorization(user=user2, partner=partner3)
        auth4.save()

        request = self.factory.get(self.dashboard_url)
        request.user = self.user

        response = views.DashboardView.as_view()(request)
        average_authorizations = response.context_data[
            "average_authorizations"]

        # We expect 2 authorizations (one user has 1, one has 3, average is 2)
        self.assertEqual(average_authorizations, 2)
Beispiel #2
0
    def test_authorization_authorizer_fails_validation(self):
        """
        Attempting to create an authorization with a non-coordinator
        and non-staff user should raise a ValidationError.
        """
        user = UserFactory()
        user2 = UserFactory()

        auth = Authorization(user=user, authorizer=user2)
        with self.assertRaises(ValidationError):
            auth.save()
Beispiel #3
0
    def test_command_output(self):
        # Needs a superuser first.
        user = UserFactory()

        call_command('user_example_data', '200')
        call_command('resources_example_data', '50')
        call_command('applications_example_data', '300')
Beispiel #4
0
    def setUpClass(cls):
        super(GraphsTestCase, cls).setUpClass()
        cls.factory = RequestFactory()

        Request(path="/admin/", ip="127.0.0.1").save()
        Request(path="/admin/", ip="127.0.0.1").save()
        Request(path="/admin/login/", ip="127.0.0.1").save()

        staff_user = User.objects.create_user(username="******", password="******")
        staff_user.is_staff = True
        staff_user.save()
        cls.staff_user = staff_user

        cls.coordinator = EditorFactory(user__email="*****@*****.**").user
        coordinators = get_coordinators()
        coordinators.user_set.add(cls.coordinator)

        user = UserFactory()
        cls.user = user

        cls.partner = PartnerFactory()

        cls.app = ApplicationFactory(partner=cls.partner)
        cls.app.status = Application.APPROVED
        cls.app.save()

        cls.dashboard_url = reverse("dashboard")
Beispiel #5
0
    def test_comment_email_sending_3(self):
        """
        After the editor and coordinator post a comment, an additional
        coordinator posts a comment. One email is sent to the first coordinator,
        and a distinct email is sent to the editor.
        """
        app, request = self._set_up_email_test_objects()
        request.user = UserFactory()
        self.assertEqual(len(mail.outbox), 0)

        _ = self._create_comment(app, self.coordinator1)
        _ = self._create_comment(app, self.editor)
        comment3 = self._create_comment(app, self.coordinator2)
        comment_was_posted.send(sender=Comment,
                                comment=comment3,
                                request=request)

        self.assertEqual(len(mail.outbox), 2)

        # Either order of email sending is fine.
        try:
            self.assertEqual(mail.outbox[0].to, [self.coordinator1.email])
            self.assertEqual(mail.outbox[1].to, [self.editor.email])
        except AssertionError:
            self.assertEqual(mail.outbox[1].to, [self.coordinator1.email])
            self.assertEqual(mail.outbox[0].to, [self.editor.email])
Beispiel #6
0
    def test_command_output(self):
        # Needs a superuser first.
        user = UserFactory()

        call_command("user_example_data", "200")
        call_command("resources_example_data", "50")
        call_command("applications_example_data", "300")
Beispiel #7
0
    def test_comment_email_sending_4(self):
        """
        A comment made on an application that's any further along the process
        than PENDING (i.e. a coordinator has taken some action on it) should
        fire an email to the coordinator who took the last action on it.
        """
        app, request = self._set_up_email_test_objects()
        request.user = UserFactory()
        self.assertEqual(len(mail.outbox), 0)

        # Create a coordinator with a test client session
        coordinator = EditorCraftRoom(self, Terms=True, Coordinator=True)

        self.partner.coordinator = coordinator.user
        self.partner.save()

        # Approve the application
        url = reverse("applications:evaluate", kwargs={"pk": app.pk})
        response = self.client.post(url,
                                    data={"status": Application.QUESTION},
                                    follow=True)

        comment4 = self._create_comment(app, self.editor)
        comment_was_posted.send(sender=Comment,
                                comment=comment4,
                                request=request)

        self.assertEqual(len(mail.outbox), 1)

        self.assertEqual(mail.outbox[0].to, [coordinator.user.email])
Beispiel #8
0
    def test_coordinators_or_self_2(self):
        """
        PartnerCoordinatorOrSelf should allow superusers.
        """
        user = UserFactory(is_superuser=True)

        req = RequestFactory()
        req.user = user

        test = TestPartnerCoordinatorOrSelf()

        test.dispatch(req)
Beispiel #9
0
    def test_editors_only_1(self):
        """
        EditorsOnly allows editors.
        """
        user = UserFactory()
        _ = EditorFactory(user=user)

        req = RequestFactory()
        req.user = user

        test = TestEditorsOnly()
        test.dispatch(req)
Beispiel #10
0
    def test_coordinators_only_2(self):
        """
        CoordinatorsOnly should allow superusers.
        """
        user = UserFactory(is_superuser=True)

        req = RequestFactory()
        req.user = user

        test = TestCoordinatorsOnly()

        test.dispatch(req)
Beispiel #11
0
    def test_self_only_1(self):
        """
        SelfOnly allows users who are also the object returned by get_object.
        """
        user = UserFactory()

        req = RequestFactory()
        req.user = user

        test = TestSelfOnly(obj=user)

        test.dispatch(req)
Beispiel #12
0
    def test_self_only_3(self):
        """
        SelfOnly disallows users who fail the above criteria.
        """
        # We'll need to force the usernames to be different so that the
        # underlying objects will end up different, apparently.
        user = UserFactory(username="******")

        req = RequestFactory()
        req.user = user

        test = TestSelfOnly(obj=None)

        with self.assertRaises(PermissionDenied):
            test.dispatch(req)

        user2 = UserFactory(username="******")
        test = TestSelfOnly(obj=user2)

        with self.assertRaises(PermissionDenied):
            test.dispatch(req)
Beispiel #13
0
    def test_coordinators_only_1(self):
        """
        CoordinatorsOnly should allow coordinators.
        """
        user = UserFactory()
        coordinators.user_set.add(user)

        req = RequestFactory()
        req.user = user

        test = TestCoordinatorsOnly()

        test.dispatch(req)
Beispiel #14
0
    def test_coordinators_or_self_3(self):
        """
        CoordinatorOrSelf should users who are the same as the view's user,
        if view.get_object returns a user.
        """
        user = UserFactory()

        req = RequestFactory()
        req.user = user

        test = TestCoordinatorOrSelf(obj=user)

        test.dispatch(req)
Beispiel #15
0
    def test_self_only_2(self):
        """
        SelfOnly allows users who own the object returned by get_object.
        """
        user = UserFactory()
        editor = EditorFactory(user=user)

        req = RequestFactory()
        req.user = user

        test = TestSelfOnly(obj=editor)

        test.dispatch(req)
Beispiel #16
0
    def test_email_required_2(self):
        """
        EmailRequired allows superusers (even without email)
        """
        user = UserFactory(email="", is_superuser=True)

        req = RequestFactory()
        req.user = user
        req.path = "/"

        test = TestEmailRequired()

        test.dispatch(req)
Beispiel #17
0
    def test_tou_required_2(self):
        """
        ToURequired allows superusers.
        """
        user = UserFactory(is_superuser=True)

        req = RequestFactory()
        req.user = user
        req.path = "/"

        test = TestToURequired()

        test.dispatch(req)
Beispiel #18
0
    def test_editors_only_3(self):
        """
        EditorsOnly does not allow non-superusers who aren't editors.
        """
        user = UserFactory(is_superuser=False)
        self.assertFalse(hasattr(user, "editor"))

        req = RequestFactory()
        req.user = user

        test = TestEditorsOnly()
        with self.assertRaises(PermissionDenied):
            test.dispatch(req)
Beispiel #19
0
    def test_coordinators_or_self_1(self):
        """
        CoordinatorsOrSelf should allow coordinators.
        """
        user = UserFactory()
        coordinators.user_set.add(user)

        req = RequestFactory()
        req.user = user

        test = TestCoordinatorsOrSelf()

        # Should not raise error.
        test.dispatch(req)
Beispiel #20
0
    def test_authorization_authorizer_validation(self):
        """
        When an Authorization is created, we validate that
        the authorizer field is set to a user with an expected
        group.
        """
        user = UserFactory()
        coordinator_editor = EditorCraftRoom(self, Terms=True, Coordinator=True)

        auth = Authorization(user=user, authorizer=coordinator_editor.user)
        try:
            auth.save()
        except ValidationError:
            self.fail("Authorization authorizer validation failed.")
Beispiel #21
0
    def test_coordinators_or_self_4(self):
        """
        CoordinatorOrSelf should users who own the object returned by the
        view's get_object.
        """
        user = UserFactory()
        editor = EditorFactory(user=user)

        req = RequestFactory()
        req.user = user

        test = TestCoordinatorOrSelf(obj=editor)

        test.dispatch(req)
Beispiel #22
0
    def test_coordinators_or_self_5(self):
        """
        CoordinatorOrSelf should not allow users who fail all of the above
        criteria.
        """
        user = UserFactory(is_superuser=False)

        req = RequestFactory()
        req.user = user

        test = TestCoordinatorOrSelf(obj=None)

        with self.assertRaises(PermissionDenied):
            test.dispatch(req)
Beispiel #23
0
    def test_comment_email_sending_5(self):
        """
        A comment from the applying editor made on an application that
        has had no actions taken on it and no existing comments should
        not fire an email to anyone.
        """
        app, request = self._set_up_email_test_objects()
        request.user = UserFactory()
        self.assertEqual(len(mail.outbox), 0)

        comment5 = self._create_comment(app, self.editor)
        comment_was_posted.send(sender=Comment, comment=comment5, request=request)

        self.assertEqual(len(mail.outbox), 0)
Beispiel #24
0
    def test_coordinators_only_3(self):
        """
        CoordinatorsOnly should disallow anyone not fitting the above two
        criteria.
        """
        user = UserFactory(is_superuser=False)

        req = RequestFactory()
        req.user = user

        test = TestCoordinatorsOnly()

        with self.assertRaises(PermissionDenied):
            test.dispatch(req)
Beispiel #25
0
    def handle(self, *args, **options):
        num_editors = options['num'][0]
        fake = Faker()

        existing_users = User.objects.all()

        # Superuser the only user, per twlight_vagrant README instructions.
        if existing_users.count() == 0:
            raise CommandError('No users present to Superuser. '
                               'Please login first.')
        elif existing_users.count() > 1:
            raise CommandError('More than one user present. '
                               'Please ensure that only one user is present.')
        else:
            user = existing_users[0]
            user.is_superuser = True
            user.is_staff = True
            user.save()

        for _ in range(num_editors):
            user = UserFactory(email=fake.word() + "@example.com")
            editor = EditorFactory(
                user=user,
                real_name=fake.name(),
                country_of_residence=fake.country(),
                occupation=fake.job(),
                affiliation=fake.company(),
                wp_editcount=random.randint(50, 2000),
                wp_registered=fake.date_time_between(start_date="-10y",
                                                     end_date="now",
                                                     tzinfo=None),
                contributions=fake.paragraph(nb_sentences=4))

        # All users who aren't the superuser
        all_users = User.objects.exclude(is_superuser=True)

        # Flag wp_valid correctly if user is valid
        for user in all_users:
            date_valid = datetime.today().date() - timedelta(
                days=182) >= user.editor.wp_registered

            if user.editor.wp_editcount > 500 and date_valid:
                user.editor.wp_valid = True
                user.editor.save()

        # Make 5 random users coordinators
        coordinators = get_coordinators()
        for user in random.sample(all_users, 5):
            user.groups.add(coordinators)
            user.save()

        # Set 5 random non-coordinator users to have restricted data processing
        restricted = get_restricted()
        non_coordinators = all_users.exclude(groups__name='coordinators')
        for user in random.sample(non_coordinators, 5):
            user.groups.add(restricted)
            user.save()
Beispiel #26
0
    def test_comment_email_sending_1(self):
        """
        A coordinator posts a comment to an Editor's application and an email
        is send to that Editor. An email is not sent to the coordinator.
        """
        app, request = self._set_up_email_test_objects()
        request.user = UserFactory()

        self.assertEqual(len(mail.outbox), 0)

        comment1 = self._create_comment(app, self.coordinator1)
        comment_was_posted.send(sender=Comment, comment=comment1, request=request)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, [self.editor.email])
Beispiel #27
0
    def test_email_required_2(self):
        """
        EmailRequired disallows users who fail the above criteria.
        """
        user = UserFactory(email="", is_superuser=False)

        req = RequestFactory()
        req.user = user
        req.path = "/"

        test = TestEmailRequired()

        resp = test.dispatch(req)
        # This test doesn't deny permission; it asks people to add their email.
        self.assertTrue(isinstance(resp, HttpResponseRedirect))
Beispiel #28
0
    def test_email_required_1(self):
        """
        EmailRequired allows users who have an email on file.
        """
        user = UserFactory(email="*****@*****.**")

        req = RequestFactory()
        req.user = user
        # EmailRequired expects the request to have a path that it will use to
        # construct a next parameter. Doesn't matter what it is, but it needs
        # to exist.
        req.path = "/"

        test = TestEmailRequired()

        test.dispatch(req)
Beispiel #29
0
    def test_user_language_csv(self):
        """
        Test that the CSVUserLanguage csv download works
        """
        for language in ["en", "fr", "fr", "de"]:
            user = UserFactory()
            user.userprofile.lang = language
            user.userprofile.save()

        request = self.factory.get(reverse("csv:user_language"))
        request.user = self.user

        response = views.CSVUserLanguage.as_view()(request)

        expected_data = [["en", "1"], ["fr", "2"], ["de", "1"]]

        self._verify_equal(response, expected_data)
Beispiel #30
0
    def test_user_language_csv(self):
        """
        Test that the CSVUserLanguage csv download works
        """
        for language in ['en', 'fr', 'fr', 'de']:
            user = UserFactory()
            user.userprofile.lang = language
            user.userprofile.save()

        request = self.factory.get(reverse('csv:user_language'))
        request.user = self.user

        response = views.CSVUserLanguage.as_view()(request)

        expected_data = [['en', '1'], ['fr', '2'], ['de', '1']]

        self._verify_equal(response, expected_data)