示例#1
0
文件: tests.py 项目: refeed/TWLight
    def test_coordinator_access(self):
        """Coordinators can see someone else's page."""
        factory = RequestFactory()
        request = factory.get(self.url1)
        request.user = self.user_coordinator

        # Define a partner
        partner = PartnerFactory()

        # Editor applies to the partner
        app = ApplicationFactory(
            status=Application.PENDING, editor=self.editor1, partner=partner)
        app.save()

        # Editor details should not be visible to just any coordinator
        try:
            response = views.EditorDetailView.as_view()(request, pk=self.editor1.pk)
            self.fail("Editor details should not be visible to just any coordinator.")
        except PermissionDenied:
            pass

        # Designate the coordinator
        partner.coordinator = request.user
        partner.save()

        # Editor details should be visible to the designated coordinator
        response = views.EditorDetailView.as_view()(request, pk=self.editor1.pk)
        self.assertEqual(response.status_code, 200)
示例#2
0
    def test_waitlisting_partner_calls_email_function(self, mock_email):
        """
        Switching a Partner to WAITLIST status should call the email function
        for apps to that partner with open statuses.
        """
        partner = PartnerFactory(status=Partner.AVAILABLE)
        app = ApplicationFactory(status=Application.PENDING, partner=partner)
        self.assertFalse(mock_email.called)

        partner.status = Partner.WAITLIST
        partner.save()
        self.assertTrue(mock_email.called)
        mock_email.assert_called_with(app)
示例#3
0
    def test_waitlisting_partner_does_not_call_email_function(self, mock_email):
        """
        Switching a Partner to WAITLIST status should NOT call the email
        function for apps to that partner with closed statuses.
        """
        partner = PartnerFactory(status=Partner.AVAILABLE)
        app = ApplicationFactory(status=Application.APPROVED, partner=partner)
        app = ApplicationFactory(status=Application.NOT_APPROVED, partner=partner)
        app = ApplicationFactory(status=Application.SENT, partner=partner)
        self.assertFalse(mock_email.called)

        partner.status = Partner.WAITLIST
        partner.save()
        self.assertFalse(mock_email.called)
示例#4
0
    def handle(self, *args, **options):
        num_partners = options["num"][0]
        tag_list = [
            "science_tag",
            "humanities_tag",
            "social-sciences_tag",
            "history_tag",
            "law_tag",
            "video_tag",
            "multidisciplinary_tag",
        ]

        coordinators = User.objects.filter(groups__name="coordinators")

        for _ in range(num_partners):
            partner = PartnerFactory(
                company_location=random.choice(list(countries)),
                renewals_available=random.choice([True, False]),
                send_instructions=Faker(
                    random.choice(settings.FAKER_LOCALES)
                ).paragraph(nb_sentences=2),
                coordinator=random.choice(coordinators),
                real_name=self.chance(True, False, 40),
                country_of_residence=self.chance(True, False, 20),
                specific_title=self.chance(True, False, 10),
                specific_stream=self.chance(True, False, 10),
                occupation=self.chance(True, False, 10),
                affiliation=self.chance(True, False, 10),
                agreement_with_terms_of_use=self.chance(True, False, 10),
                mutually_exclusive=False,
            )

            # ManyToMany relationships can't be set until the partner object has
            # been created.
            random_languages = random.sample(
                list(Language.objects.all()), random.randint(1, 2)
            )

            for lang in random_languages:
                partner.languages.add(lang)

            new_tags = {}
            partner_tags = []
            for tag in random.sample(tag_list, random.randint(1, 4)):
                partner_tags.append(tag)

            new_tags["tags"] = partner_tags
            partner.new_tags = new_tags

            partner.save()

        all_partners = Partner.even_not_available.all()
        # Set 5 partners to need a registration URL. We do this separately
        # because it requires both the account_email and registration_url
        # fields to be set concurrently.
        for registration_partner in random.sample(list(all_partners), 5):
            registration_partner.account_email = True
            registration_partner.registration_url = Faker(
                random.choice(settings.FAKER_LOCALES)
            ).uri()
            registration_partner.save()

        # While most fields can be set at random, we want to make sure we
        # get partners with certain fields set to particular values.

        # Set 5 random partners to be unavailable
        for unavailable_partner in random.sample(list(all_partners), 5):
            unavailable_partner.status = Partner.NOT_AVAILABLE
            unavailable_partner.save()

        # Set 5% random partners to have excerpt limit in words
        for words in random.sample(list(all_partners), 10):
            words.excerpt_limit = random.randint(100, 250)
            words.save()

        # Set 5% random partners to have excerpt limit in words
        for percentage in random.sample(list(all_partners), 10):
            percentage.excerpt_limit_percentage = random.randint(5, 50)
            percentage.save()

        # Set 1 random partner to have excerpt limits both in words and percentage
        for percentage_words in random.sample(list(all_partners), 1):
            percentage_words.excerpt_limit_percentage = random.randint(5, 50)
            percentage_words.excerpt_limit = random.randint(100, 250)
            percentage_words.save()

        available_partners = all_partners.exclude(status=Partner.NOT_AVAILABLE)

        # Set 10 random available partners to be waitlisted
        for waitlisted_partner in random.sample(list(available_partners), 10):
            waitlisted_partner.status = Partner.WAITLIST
            waitlisted_partner.save()

        # Set 25 random partners to have a long description
        for long_description in random.sample(list(all_partners), 25):
            long_description.description = Faker(
                random.choice(settings.FAKER_LOCALES)
            ).paragraph(nb_sentences=10)
            long_description.save()

        # Set 10 random available partners to be featured
        for featured_partner in random.sample(list(available_partners), 10):
            featured_partner.featured = True
            featured_partner.save()

        # Give any specific_stream flagged partners streams.
        stream_partners = all_partners.filter(specific_stream=True)

        # Random number of accounts available for all partners without streams
        for accounts in all_partners:
            if not accounts.specific_stream:
                accounts.accounts_available = random.randint(10, 550)
                accounts.save()

        # If we happened to not create any partners with streams,
        # create one deliberately.
        if stream_partners.count() == 0:
            stream_partners = random.sample(list(all_partners), 1)
            stream_partners[0].specific_stream = True
            stream_partners[0].save()

        for partner in stream_partners:
            for _ in range(3):
                stream = StreamFactory(
                    partner=partner,
                    name=Faker(random.choice(settings.FAKER_LOCALES)).sentence(
                        nb_words=3
                    )[
                        :-1
                    ],  # [:-1] removes full stop
                    description=Faker(random.choice(settings.FAKER_LOCALES)).paragraph(
                        nb_sentences=2
                    ),
                )

        # Set 15 partners to have somewhere between 1 and 5 video tutorial URLs
        for partner in random.sample(list(all_partners), 15):
            for _ in range(random.randint(1, 5)):
                VideoFactory(
                    partner=partner,
                    tutorial_video_url=Faker(
                        random.choice(settings.FAKER_LOCALES)
                    ).url(),
                )

        # Random number of accounts available for all streams
        all_streams = Stream.objects.all()
        for each_stream in all_streams:
            each_stream.accounts_available = random.randint(10, 100)
            each_stream.save()

        # Generate a few number of suggestions with upvotes
        all_users = User.objects.exclude(is_superuser=True)
        author_user = random.choice(all_users)
        for _ in range(random.randint(3, 10)):
            suggestion = SuggestionFactory(
                description=Faker(random.choice(settings.FAKER_LOCALES)).paragraph(
                    nb_sentences=10
                ),
                author=author_user,
            )
            # Truncate company name to 40 characters so it doesn't error out
            suggestion.suggested_company_name = (
                suggestion.suggested_company_name[:40]
                if len(suggestion.suggested_company_name) > 40
                else suggestion.suggested_company_name
            )
            suggestion.save()
            suggestion.upvoted_users.add(author_user)
            random_users = random.sample(list(all_users), random.randint(1, 10))
            suggestion.upvoted_users.add(*random_users)

        # Set 5 partners use the access code authorization method,
        # and generate a bunch of codes for each.
        for partner in random.sample(list(available_partners), 5):
            partner.authorization_method = Partner.CODES
            partner.save()

            for i in range(25):
                new_access_code = AccessCode()
                new_access_code.code = "".join(
                    random.choice(string.ascii_uppercase + string.digits)
                    for _ in range(10)
                )
                new_access_code.partner = partner
                new_access_code.save()

        # Set 5 partners use the access code authorization method,
        # and generate a bunch of codes for each.
        for partner in random.sample(list(available_partners), 5):
            partner.authorization_method = Partner.CODES
            partner.save()

            for i in range(25):
                new_access_code = AccessCode()
                new_access_code.code = "".join(
                    random.choice(string.ascii_uppercase + string.digits)
                    for _ in range(10)
                )
                new_access_code.partner = partner
                new_access_code.save()
示例#5
0
class ApplicationCommentTest(TestCase):
    def setUp(self):
        super(ApplicationCommentTest, self).setUp()
        self.editor = EditorFactory(user__email="*****@*****.**").user

        coordinators = get_coordinators()

        self.coordinator1 = EditorFactory(user__email="*****@*****.**",
                                          user__username="******").user
        self.coordinator2 = EditorFactory(user__email="*****@*****.**",
                                          user__username="******").user
        coordinators.user_set.add(self.coordinator1)
        coordinators.user_set.add(self.coordinator2)

        self.partner = PartnerFactory()

    def _create_comment(self, app, user):
        CT = ContentType.objects.get_for_model

        comm = Comment.objects.create(
            content_type=CT(Application),
            object_pk=app.pk,
            user=user,
            user_name=user.username,
            comment="Content!",
            site=Site.objects.get_current(),
        )
        comm.save()

        return comm

    def _set_up_email_test_objects(self):
        app = ApplicationFactory(editor=self.editor.editor,
                                 partner=self.partner)

        factory = RequestFactory()
        request = factory.post(get_form_target())
        return app, request

    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])

    def test_comment_email_sending_2(self):
        """
        After a coordinator posts a comment, the Editor posts an additional
        comment. An email is sent to the coordinator who posted the earlier
        comment. An email is not 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)
        comment2 = self._create_comment(app, self.editor)

        comment_was_posted.send(sender=Comment,
                                comment=comment2,
                                request=request)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, [self.coordinator1.email])

    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])

    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])

    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)
示例#6
0
文件: tests.py 项目: rv-raman/TWLight
class UserRenewalNoticeTest(TestCase):
    def setUp(self):
        super(UserRenewalNoticeTest, self).setUp()
        editor = EditorFactory(user__email="*****@*****.**")
        self.user = editor.user

        self.coordinator = EditorFactory().user
        coordinators = get_coordinators()
        coordinators.user_set.add(self.coordinator)

        self.partner = PartnerFactory()

        self.authorization = Authorization()
        self.authorization.user = self.user
        self.authorization.authorizer = self.coordinator
        self.authorization.date_expires = datetime.today() + timedelta(weeks=1)
        self.authorization.save()
        self.authorization.partners.add(self.partner)

    def test_single_user_renewal_notice(self):
        """
        Given one authorization that expires in two weeks, ensure
        that our email task sends an email to that user.
        """
        call_command("user_renewal_notice")

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, [self.user.email])

    def test_user_renewal_notice_disabled(self):
        """
        Users have the option to disable renewal notices. If users have
        disabled emails, we shouldn't send them one.
        """
        self.user.userprofile.send_renewal_notices = False
        self.user.userprofile.save()

        call_command("user_renewal_notice")

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

    def test_user_renewal_notice_doesnt_duplicate(self):
        """
        If we run the command a second time, the same user shouldn't receive
        a second email.
        """
        call_command("user_renewal_notice")
        self.assertEqual(len(mail.outbox), 1)

        call_command("user_renewal_notice")
        self.assertEqual(len(mail.outbox), 1)

    def test_user_renewal_notice_past_date(self):
        """
        If the authorization expired before today, the user shouldn't
        receive a notice.
        """
        self.authorization.date_expires = datetime.today() - timedelta(weeks=1)
        self.authorization.save()
        call_command("user_renewal_notice")

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

    def test_user_renewal_notice_future_date(self):
        """
        If the authorization doesn't expire for months, the user
        shouldn't receive a notice.
        """
        self.authorization.date_expires = datetime.today() + timedelta(weeks=8)
        self.authorization.save()
        call_command("user_renewal_notice")

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

    def test_user_renewal_notice_future_date_1(self):
        """
        If we have multiple authorizations to send emails for, let's make
        sure we send distinct emails to the right places.
        """
        editor2 = EditorFactory(user__email="*****@*****.**")

        authorization2 = Authorization()
        authorization2.user = editor2.user
        authorization2.authorizer = self.coordinator
        authorization2.date_expires = datetime.today() + timedelta(weeks=1)
        authorization2.save()
        authorization2.partners.add(self.partner)

        call_command("user_renewal_notice")

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

        # Make sure that the two emails went to the two expected
        # email addresses.
        # This looks a little complicated because mail.outbox[0].to is a
        # (one element) list, and we need to compare sets to ensure we've
        # got 1 of each email.
        self.assertEqual(
            {mail.outbox[0].to[0], mail.outbox[1].to[0]},
            {"*****@*****.**", "*****@*****.**"},
        )

    def test_user_renewal_notice_after_renewal(self):
        """
        If a user renews their authorization, we want to remind
        them again when it runs out.
        """
        call_command("user_renewal_notice")
        self.assertEqual(len(mail.outbox), 1)
        self.authorization.refresh_from_db()
        self.assertTrue(self.authorization.reminder_email_sent)

        # We already have an authorization, so let's setup up
        # an application that 'corresponds' to it.
        application = ApplicationFactory(
            editor=self.user.editor,
            sent_by=self.coordinator,
            partner=self.partner,
            status=Application.SENT,
            requested_access_duration=1,
        )
        application.save()

        # File a renewal, approve it, and send it.
        self.partner.renewals_available = True
        self.partner.save()
        renewed_app = application.renew()
        renewed_app.status = application.APPROVED
        renewed_app.save()
        renewed_app.status = application.SENT
        renewed_app.sent_by = self.coordinator
        renewed_app.save()

        # Sending this renewal notice will have sent the user
        # an email, so we expect 2 emails now.
        self.assertEqual(len(mail.outbox), 2)

        # We've correctly marked reminder_email_sent as False
        self.authorization.refresh_from_db()
        self.assertFalse(self.authorization.reminder_email_sent)

        # And calling the command should send a third email.
        call_command("user_renewal_notice")
        self.assertEqual(len(mail.outbox), 3)
示例#7
0
    def handle(self, *args, **options):
        num_partners = options['num'][0]
        tag_list = ["science", "humanities", "social science", "history",
                    "law", "video", "multidisciplinary"]
        fake = Faker()

        coordinators = User.objects.filter(groups__name='coordinators')

        for _ in range(num_partners):
            partner = PartnerFactory(
                company_location = random.choice(list(countries)),
                renewals_available = random.choice([True, False]),
                short_description = fake.paragraph(nb_sentences=4),
                send_instructions = fake.paragraph(nb_sentences=2),
                coordinator = self.chance(
                    random.choice(coordinators), None, 20),
                real_name = self.chance(True, False, 40),
                country_of_residence = self.chance(True, False, 20),
                specific_title = self.chance(True, False, 10),
                specific_stream = self.chance(True, False, 10),
                occupation = self.chance(True, False, 10),
                affiliation = self.chance(True, False, 10),
                agreement_with_terms_of_use = self.chance(True, False, 10),
                mutually_exclusive = False
                )

            # ManyToMany relationships can't be set until the partner object has
            # been created.
            random_languages = random.sample(Language.objects.all(),
                    random.randint(1,2)
                    )

            for lang in random_languages:
                partner.languages.add(lang)

            partner.save()

        all_partners = Partner.even_not_available.all()
        for partner in all_partners:
            for tag in random.sample(tag_list, random.randint(1,4)):
                partner.tags.add(tag)

        # Set 5 partners to need a registration URL. We do this separately
        # because it requires both the account_email and registration_url
        # fields to be set concurrently.
        for registration_partner in random.sample(all_partners, 5):
            registration_partner.account_email = True
            registration_partner.registration_url = fake.uri()
            registration_partner.save()

        # While most fields can be set at random, we want to make sure we
        # get partners with certain fields set to particular values.

        # Set 5 random partners to be unavailable
        for unavailable_partner in random.sample(all_partners, 5):
            unavailable_partner.status = Partner.NOT_AVAILABLE
            unavailable_partner.save()
            
        # Set 5% random partners to have excerpt limit in words
        for words in random.sample(all_partners, 10):
            words.excerpt_limit = random.randint(100, 250)
            words.save()
            
        # Set 5% random partners to have excerpt limit in words
        for percentage in random.sample(all_partners, 10):
            percentage.excerpt_limit_percentage = random.randint(5, 50)
            percentage.save()
            
        # Set 1 random partner to have excerpt limits both in words and percentage
        for percentage_words in random.sample(all_partners, 1):
            percentage_words.excerpt_limit_percentage = random.randint(5, 50)
            percentage_words.excerpt_limit = random.randint(100, 250)
            percentage_words.save()
            
        available_partners = all_partners.exclude(status= Partner.NOT_AVAILABLE)

        # Set 10 random available partners to be waitlisted
        for waitlisted_partner in random.sample(available_partners, 10):
            waitlisted_partner.status = Partner.WAITLIST
            waitlisted_partner.save()

        # Set 25 random partners to have a long description
        for long_description in random.sample(all_partners, 25):
            long_description.description = fake.paragraph(nb_sentences = 10)
            long_description.save()

        # Set 10 random available partners to be featured
        for featured_partner in random.sample(available_partners, 10):
            featured_partner.featured = True
            featured_partner.save()

        # Give any specific_stream flagged partners streams.
        stream_partners = all_partners.filter(specific_stream=True)
        
        # Random number of accounts available for all partners without streams
        for accounts in all_partners:
            if not accounts.specific_stream:
                accounts.accounts_available = random.randint(10, 550)
                accounts.save()
            
        # If we happened to not create any partners with streams,
        # create one deliberately.
        if stream_partners.count() == 0:
            stream_partners = random.sample(all_partners, 1)
            stream_partners[0].specific_stream = True
            stream_partners[0].save()
        
        for partner in stream_partners:
            for _ in range(3):
                stream = StreamFactory(
                    partner= partner,
                    name= fake.sentence(nb_words= 3)[:-1], # [:-1] removes full stop
                    description= fake.paragraph(nb_sentences=2)
                    )
        
        # Set 15 partners to have somewhere between 1 and 5 video tutorial URLs
        for partner in random.sample(all_partners, 15):
            for _ in range(random.randint(1, 5)):
                VideoFactory(
                    partner = partner,
                    tutorial_video_url = fake.url()
                    )
                    
        # Random number of accounts available for all streams
        all_streams = Stream.objects.all()
        for each_stream in all_streams:
            each_stream.accounts_available = random.randint(10, 100)
            each_stream.save()
示例#8
0
class AuthorizationBaseTestCase(TestCase):
    """
    Setup class for Authorization Object tests.
    Could possibly achieve the same effect via a new factory class.
    """
    def setUp(self):
        super(AuthorizationBaseTestCase, self).setUp()

        self.partner1 = PartnerFactory(authorization_method=Partner.EMAIL,
                                       status=Partner.AVAILABLE)
        self.partner2 = PartnerFactory(authorization_method=Partner.PROXY,
                                       status=Partner.AVAILABLE)
        self.partner3 = PartnerFactory(authorization_method=Partner.CODES,
                                       status=Partner.AVAILABLE)
        self.partner4 = PartnerFactory(authorization_method=Partner.EMAIL,
                                       status=Partner.AVAILABLE)

        self.editor1 = EditorFactory()
        self.editor1.user.email = fake.email()
        self.editor1.user.save()
        self.editor2 = EditorFactory()
        self.editor3 = EditorFactory()
        # Editor 4 is a coordinator with a session.
        self.editor4 = EditorCraftRoom(self, Terms=True, Coordinator=True)
        # Editor 4 is the designated coordinator for all partners.
        self.partner1.coordinator = self.editor4.user
        self.partner1.account_length = timedelta(days=180)
        self.partner1.target_url = 'http://test.localdomain'
        self.partner1.save()
        self.partner2.coordinator = self.editor4.user
        self.partner2.save()
        self.partner3.coordinator = self.editor4.user
        self.partner3.save()
        self.partner4.coordinator = self.editor4.user
        self.partner4.save()

        # Editor 5 is a coordinator without a session and with no designated partners.
        self.editor5 = EditorFactory()
        coordinators.user_set.add(self.editor5.user)

        # Create applications.
        self.app1 = ApplicationFactory(editor=self.editor1,
                                       partner=self.partner1,
                                       status=Application.PENDING)
        self.app2 = ApplicationFactory(editor=self.editor2,
                                       partner=self.partner1,
                                       status=Application.PENDING)
        self.app3 = ApplicationFactory(editor=self.editor3,
                                       partner=self.partner1,
                                       status=Application.PENDING)
        self.app4 = ApplicationFactory(editor=self.editor1,
                                       partner=self.partner2,
                                       status=Application.PENDING)
        self.app5 = ApplicationFactory(editor=self.editor2,
                                       partner=self.partner2,
                                       status=Application.PENDING)
        self.app6 = ApplicationFactory(editor=self.editor3,
                                       partner=self.partner2,
                                       status=Application.PENDING)
        self.app7 = ApplicationFactory(editor=self.editor1,
                                       partner=self.partner3,
                                       status=Application.PENDING)
        self.app8 = ApplicationFactory(editor=self.editor1,
                                       partner=self.partner4,
                                       status=Application.PENDING)
        self.app9 = ApplicationFactory(editor=self.editor2,
                                       partner=self.partner3,
                                       status=Application.PENDING)

        # Editor 4 will update status on applications to partners 1 and 2.
        # Send the application
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app1.pk}),
            data={"status": Application.SENT},
            follow=True,
        )
        self.app1.refresh_from_db()
        self.auth_app1 = Authorization.objects.get(
            authorizer=self.editor4.user,
            authorized_user=self.editor1.user,
            partner=self.partner1,
        )

        # Approve the application
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app2.pk}),
            data={"status": Application.APPROVED},
            follow=True,
        )
        self.app2.refresh_from_db()
        self.auth_app2 = Authorization(
            authorizer=self.editor4.user,
            authorized_user=self.editor2.user,
            partner=self.partner1,
        )

        # Send the application
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app3.pk}),
            data={"status": Application.SENT},
            follow=True,
        )
        self.app3.refresh_from_db()
        self.auth_app3 = Authorization.objects.get(
            authorizer=self.editor4.user,
            authorized_user=self.editor3.user,
            partner=self.partner1,
        )

        # Send the application
        # PROXY authorization methods don't set .SENT on the evaluate page;
        # .APPROVED will automatically update them to .SENT
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app4.pk}),
            data={"status": Application.APPROVED},
            follow=True,
        )
        self.app4.refresh_from_db()
        self.auth_app4 = Authorization.objects.get(
            # https://phabricator.wikimedia.org/T233508
            # authorizer=self.editor4.user,
            authorized_user=self.editor1.user,
            partner=self.partner2,
        )

        # Send the application
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app5.pk}),
            data={"status": Application.APPROVED},
            follow=True,
        )
        self.app5.refresh_from_db()
        self.auth_app5 = Authorization.objects.get(
            # https://phabricator.wikimedia.org/T233508
            # authorizer=self.editor4.user,
            authorized_user=self.editor2.user,
            partner=self.partner2,
        )

        # Set up an access code to distribute
        self.access_code = AccessCode(code="ABCD-EFGH-IJKL",
                                      partner=self.partner3)
        self.access_code.save()

        self.message_patcher = patch(
            "TWLight.applications.views.messages.add_message")
        self.message_patcher.start()

    def tearDown(self):
        super(AuthorizationBaseTestCase, self).tearDown()
        self.partner1.delete()
        self.partner2.delete()
        self.partner3.delete()
        self.partner4.delete()
        self.access_code.delete()
        self.editor1.delete()
        self.editor2.delete()
        self.editor3.delete()
        self.editor4.delete()
        self.app1.delete()
        self.app2.delete()
        self.app3.delete()
        self.app4.delete()
        self.app5.delete()
        self.app6.delete()
        self.app7.delete()
        self.app8.delete()
        self.app9.delete()
示例#9
0
class AuthorizationBaseTestCase(TestCase):
    """
    Setup class for Authorization Object tests.
    Could possibly achieve the same effect via a new factory class.
    """
    def setUp(self):
        super(AuthorizationBaseTestCase, self).setUp()

        self.partner1 = PartnerFactory(authorization_method=Partner.EMAIL,
                                       status=Partner.AVAILABLE)
        self.partner2 = PartnerFactory(
            authorization_method=Partner.PROXY,
            status=Partner.AVAILABLE,
            requested_access_duration=True,
        )
        self.partner3 = PartnerFactory(authorization_method=Partner.CODES,
                                       status=Partner.AVAILABLE)
        self.partner4 = PartnerFactory(authorization_method=Partner.EMAIL,
                                       status=Partner.AVAILABLE)
        self.partner5 = PartnerFactory(
            authorization_method=Partner.EMAIL,
            status=Partner.AVAILABLE,
            specific_stream=True,
        )
        self.partner5_stream1 = StreamFactory(
            partner=self.partner5, authorization_method=Partner.EMAIL)
        self.partner5_stream2 = StreamFactory(
            partner=self.partner5, authorization_method=Partner.EMAIL)

        self.editor1 = EditorFactory()
        self.editor1.user.email = Faker(random.choice(
            settings.FAKER_LOCALES)).email()
        self.editor1.user.save()
        self.editor2 = EditorFactory()
        self.editor3 = EditorFactory()
        # Editor 4 is a coordinator with a session.
        self.editor4 = EditorCraftRoom(self, Terms=True, Coordinator=True)
        # Editor 4 is the designated coordinator for all partners.
        self.partner1.coordinator = self.editor4.user
        self.partner1.account_length = timedelta(days=180)
        self.partner1.target_url = "http://test.localdomain"
        self.partner1.save()
        self.partner2.coordinator = self.editor4.user
        self.partner2.save()
        self.partner3.coordinator = self.editor4.user
        self.partner3.save()
        self.partner4.coordinator = self.editor4.user
        self.partner4.save()
        self.partner5.coordinator = self.editor4.user
        self.partner5.save()

        # Editor 5 is a coordinator without a session and with no designated partners.
        self.editor5 = EditorFactory()
        coordinators.user_set.add(self.editor5.user)

        # Create applications.
        self.app1 = ApplicationFactory(editor=self.editor1,
                                       partner=self.partner1,
                                       status=Application.PENDING)
        self.app2 = ApplicationFactory(editor=self.editor2,
                                       partner=self.partner1,
                                       status=Application.PENDING)
        self.app3 = ApplicationFactory(editor=self.editor3,
                                       partner=self.partner1,
                                       status=Application.PENDING)
        self.app4 = ApplicationFactory(editor=self.editor1,
                                       partner=self.partner2,
                                       status=Application.PENDING)
        self.app5 = ApplicationFactory(editor=self.editor2,
                                       partner=self.partner2,
                                       status=Application.PENDING)
        self.app6 = ApplicationFactory(editor=self.editor3,
                                       partner=self.partner2,
                                       status=Application.PENDING)
        self.app7 = ApplicationFactory(editor=self.editor1,
                                       partner=self.partner3,
                                       status=Application.PENDING)
        self.app8 = ApplicationFactory(editor=self.editor1,
                                       partner=self.partner4,
                                       status=Application.PENDING)
        self.app9 = ApplicationFactory(editor=self.editor2,
                                       partner=self.partner3,
                                       status=Application.PENDING)
        self.app10 = ApplicationFactory(
            editor=self.editor1,
            partner=self.partner5,
            specific_stream=self.partner5_stream1,
            status=Application.PENDING,
        )
        self.app11 = ApplicationFactory(
            editor=self.editor1,
            partner=self.partner5,
            specific_stream=self.partner5_stream2,
            status=Application.PENDING,
        )

        # Editor 4 will update status on applications to partners 1, 2, and 5.
        # Send the application
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app1.pk}),
            data={"status": Application.SENT},
            follow=True,
        )
        self.app1.refresh_from_db()
        self.auth_app1 = Authorization.objects.get(
            authorizer=self.editor4.user,
            user=self.editor1.user,
            partners=self.partner1)
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app10.pk}),
            data={"status": Application.SENT},
            follow=True,
        )
        self.app10.refresh_from_db()
        self.auth_app10 = Authorization.objects.get(
            authorizer=self.editor4.user,
            user=self.editor1.user,
            partners=self.partner5,
            stream=self.partner5_stream1,
        )
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app11.pk}),
            data={"status": Application.SENT},
            follow=True,
        )
        self.app11.refresh_from_db()
        self.auth_app11 = Authorization.objects.get(
            authorizer=self.editor4.user,
            user=self.editor1.user,
            partners=self.partner5,
            stream=self.partner5_stream2,
        )

        # Send the application
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app2.pk}),
            data={"status": Application.SENT},
            follow=True,
        )
        self.app2.refresh_from_db()
        self.auth_app2 = Authorization.objects.get(
            authorizer=self.editor4.user,
            user=self.editor2.user,
            partners=self.partner1)

        # Send the application
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app3.pk}),
            data={"status": Application.SENT},
            follow=True,
        )
        self.app3.refresh_from_db()
        self.auth_app3 = Authorization.objects.get(
            authorizer=self.editor4.user,
            user=self.editor3.user,
            partners=self.partner1)

        # PROXY authorization methods don't set .SENT on the evaluate page;
        # .APPROVED will automatically update them to .SENT

        # This app was created with a factory, which doesn't create a revision.
        # Let's update the status so that we have one.
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app4.pk}),
            data={"status": Application.QUESTION},
            follow=True,
        )
        # Approve the application
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app4.pk}),
            data={"status": Application.APPROVED},
            follow=True,
        )

        self.app4.refresh_from_db()
        self.auth_app4 = Authorization.objects.get(
            authorizer=self.editor4.user,
            user=self.editor1.user,
            partners=self.partner2)

        # This app was created with a factory, which doesn't create a revision.
        # Let's update the status so that we have one.
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app5.pk}),
            data={"status": Application.QUESTION},
            follow=True,
        )
        # Approve the application
        self.client.post(
            reverse("applications:evaluate", kwargs={"pk": self.app5.pk}),
            data={"status": Application.APPROVED},
            follow=True,
        )
        self.app5.refresh_from_db()
        self.auth_app5 = Authorization.objects.get(
            authorizer=self.editor4.user,
            user=self.editor2.user,
            partners=self.partner2)

        # Set up an access code to distribute
        self.access_code = AccessCode(code="ABCD-EFGH-IJKL",
                                      partner=self.partner3)
        self.access_code.save()

        self.message_patcher = patch(
            "TWLight.applications.views.messages.add_message")
        self.message_patcher.start()

    def tearDown(self):
        super(AuthorizationBaseTestCase, self).tearDown()
        self.partner1.delete()
        self.partner2.delete()
        self.partner3.delete()
        self.partner4.delete()
        self.partner5.delete()
        self.access_code.delete()
        self.editor1.delete()
        self.editor2.delete()
        self.editor3.delete()
        self.editor4.delete()
        self.app1.delete()
        self.app2.delete()
        self.app3.delete()
        self.app4.delete()
        self.app5.delete()
        self.app6.delete()
        self.app7.delete()
        self.app8.delete()
        self.app9.delete()
        self.app10.delete()
        self.app11.delete()