예제 #1
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()
예제 #2
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()