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()
def test_authorization_authorizer_validation_staff(self): """ The authorizer can be a staff member but not a coordinator. """ user = UserFactory() user2 = UserFactory() user2.is_staff = True user2.save() auth = Authorization(user=user, authorizer=user2) try: auth.save() except ValidationError: self.fail("Authorization authorizer validation failed.")
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.")
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.partner = self.partner authorization2.date_expires = datetime.today() + timedelta(weeks=1) authorization2.save() 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 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.partner = self.partner self.authorization.date_expires = datetime.today() + timedelta(weeks=2) self.authorization.save()
def test_user_delete_authorizations(self): """ Verify that deleted user authorizations are expired and contain no user links """ delete_url = reverse("users:delete_data", kwargs={"pk": self.user_editor.pk}) # Need a password so we can login self.user_editor.set_password("editor") self.user_editor.save() self.client = Client() session = self.client.session self.client.login(username=self.username1, password="******") partner = PartnerFactory() user_auth = Authorization( user=self.user_editor, partner=partner, date_authorized=date.today(), date_expires=date.today() + timedelta(days=30), ) user_auth.save() submit = self.client.post(delete_url) user_auth.refresh_from_db() self.assertEqual(user_auth.date_expires, date.today() - timedelta(days=1))
def post_revision_commit(sender, instance, **kwargs): if reversion.is_active() and (instance.status == 2 or instance.status == 4) and not instance.imported: authorized_user = instance.user authorizer = reversion.get_user() authorization = Authorization() if instance.specific_stream: authorization.stream = instance.specific_stream authorization.authorized_user = authorized_user authorization.authorizer = authorizer authorization.partner = instance.partner authorization.save()
def setUp(self): super(ProjectPage2021LaunchTest, self).setUp() editor = EditorFactory(user__email="*****@*****.**") self.user = editor.user # The user logged in: request = RequestFactory().get("/login") signals.user_logged_in.send(sender=self.user.__class__, request=request, user=self.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 setUpTestData(cls): super().setUpTestData() editor = EditorFactory(user__email="*****@*****.**") cls.user = editor.user cls.coordinator = EditorFactory().user coordinators = get_coordinators() coordinators.user_set.add(cls.coordinator) cls.partner = PartnerFactory() cls.authorization = Authorization() cls.authorization.user = cls.user cls.authorization.authorizer = cls.coordinator cls.authorization.date_expires = datetime.today() + timedelta(weeks=1) cls.authorization.save() cls.authorization.partners.add(cls.partner)
def setUpTestData(cls): super().setUpTestData() editor = EditorFactory(user__email="*****@*****.**") cls.user = editor.user # The user logged in: request = RequestFactory().get("/login") signals.user_logged_in.send(sender=cls.user.__class__, request=request, user=cls.user) cls.coordinator = EditorFactory().user coordinators = get_coordinators() coordinators.user_set.add(cls.coordinator) cls.partner = PartnerFactory() cls.authorization = Authorization() cls.authorization.user = cls.user cls.authorization.authorizer = cls.coordinator cls.authorization.date_expires = datetime.today() + timedelta(weeks=1) cls.authorization.save() cls.authorization.partners.add(cls.partner)
def test_authorization_authorizer_can_be_updated(self): """ After successfully creating a valid Authorization, we should be able to remove the authorizer from the expected user groups and still save the object. """ user = UserFactory() coordinator_editor = EditorCraftRoom(self, Terms=True, Coordinator=True) auth = Authorization(user=user, authorizer=coordinator_editor.user) auth.save() coordinators = get_coordinators() coordinators.user_set.remove(coordinator_editor.user) try: auth.save() except ValidationError: self.fail("Authorization authorizer validation failed.")
class ProjectPage2021LaunchTest(TestCase): def setUp(self): super(ProjectPage2021LaunchTest, self).setUp() editor = EditorFactory(user__email="*****@*****.**") self.user = editor.user # The user logged in: request = RequestFactory().get("/login") signals.user_logged_in.send(sender=self.user.__class__, request=request, user=self.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_project_page_2021_launch_email_1(self): """ With one user, calling the project page 2021 launch command should send a single email, to that user. """ call_command("project_page_2021_launch") self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.user.email]) def test_project_page_2021_launch_email_2(self): """ Adding an inactive user shouldn't send another email. """ _ = EditorFactory(user__email="*****@*****.**") call_command("project_page_2021_launch") self.assertEqual(len(mail.outbox), 1) def test_project_page_2021_launch_email_3(self): """ The project page 2021 launch command should record the email was sent """ self.assertFalse( self.user.userprofile.project_page_2021_notification_sent) call_command("project_page_2021_launch") self.user.userprofile.refresh_from_db() self.assertTrue( self.user.userprofile.project_page_2021_notification_sent) def test_project_page_2021_launch_email_4(self): """ The project page 2021 launch command should not send to a user we recorded as having received the email already. """ self.user.userprofile.project_page_2021_notification_sent = True self.user.userprofile.save() call_command("project_page_2021_launch") self.assertEqual(len(mail.outbox), 0)
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, partner=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, partner=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, partner=self.partner5, stream=self.partner5_stream2, ) # 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, 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, user=self.editor3.user, partner=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, partner=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, 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()
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.partner = self.partner self.authorization.date_expires = datetime.today() + timedelta(weeks=2) self.authorization.save() 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.partner = self.partner authorization2.date_expires = datetime.today() + timedelta(weeks=1) authorization2.save() 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]}, {"*****@*****.**", "*****@*****.**"}, )
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)
def post_revision_commit(sender, instance, **kwargs): # For some authorization methods, we can skip the manual Approved->Sent # step and just immediately take an Approved application and give it # a finalised status. skip_approved = (instance.status == Application.APPROVED and instance.is_instantly_finalized()) if skip_approved: instance.status = Application.SENT instance.save() # Renewals are for applications that are approved/sent. # Having a parent for NOT_APPROVED apps hinders us from # correctly renewing the parent. So, we unset the parent # if the status is NOT_APPROVED and the app already has # a parent. if instance.status == Application.NOT_APPROVED and instance.parent: instance.parent = None instance.save() # Authorize editor to access resource after an application is saved as sent. if instance.status == Application.SENT: # Check if an authorization already exists. if instance.specific_stream: existing_authorization = Authorization.objects.filter( user=instance.user, partner=instance.partner, stream=instance.specific_stream, ) else: existing_authorization = Authorization.objects.filter( user=instance.user, partner=instance.partner) authorized_user = instance.user authorizer = instance.sent_by # In the case that there is no existing authorization, create a new one if existing_authorization.count() == 0: authorization = Authorization() # If an authorization already existed (such as in the case of a # renewal), we'll simply update that one. elif existing_authorization.count() == 1: authorization = existing_authorization[0] else: logger.error("Found more than one authorization object for " "{user} - {partner}".format(user=instance.user, partner=instance.partner)) return if instance.specific_stream: authorization.stream = instance.specific_stream authorization.user = authorized_user authorization.authorizer = authorizer authorization.partner = instance.partner # If this is a proxy partner, and the requested_access_duration # field is set to false, set (or reset) the expiry date # to one year from now if (instance.partner.authorization_method == Partner.PROXY and instance.requested_access_duration is None): one_year_from_now = date.today() + timedelta(days=365) authorization.date_expires = one_year_from_now # If this is a proxy partner, and the requested_access_duration # field is set to true, set (or reset) the expiry date # to 1, 3, 6 or 12 months from today based on user input elif (instance.partner.authorization_method == Partner.PROXY and instance.partner.requested_access_duration is True): custom_expiry_date = date.today() + relativedelta( months=instance.requested_access_duration) authorization.date_expires = custom_expiry_date # Alternatively, if this partner has a specified account_length, # we'll use that to set the expiry. elif instance.partner.account_length: # account_length should be a timedelta authorization.date_expires = date.today( ) + instance.partner.account_length authorization.save()
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, authorizer=self.coordinator) auth1.save() auth1.partners.add(partner1) auth2 = Authorization(user=user2, authorizer=self.coordinator) auth2.save() auth2.partners.add(partner1) auth3 = Authorization(user=user2, authorizer=self.coordinator) auth3.save() auth3.partners.add(partner2) auth4 = Authorization(user=user2, authorizer=self.coordinator) auth4.save() auth4.partners.add(partner3) 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)
def handle(self, **options): # Get sent applications with an available partner and editor. We're sorting by ascending date to back-fill # authorizations from oldest to newest. sent_applications = Application.objects.filter( status=Application.SENT, editor__isnull=False, partner__isnull=False, partner__status=0).order_by('date_created', 'editor', 'partner') for application in sent_applications: # Check if an authorization already exists. if application.specific_stream: existing_authorization = Authorization.objects.filter( user=application.user, partner=application.partner, stream=application.specific_stream) else: existing_authorization = Authorization.objects.filter( user=application.user, partner=application.partner) # In the case that there is no existing authorization, create a new one if existing_authorization.count() == 0: authorization = Authorization() # You can't set the date_authorized on creation, but you can modify it afterwards. So save immediately. authorization.save() # We set the authorization date to the date the application was closed. authorization.date_authorized = application.date_closed if application.specific_stream: authorization.stream = application.specific_stream authorization.user = application.user authorization.authorizer = application.sent_by authorization.partner = application.partner # If this is a proxy partner, and the requested_access_duration # field is set to false, set (or reset) the expiry date # to one year from authorization. if application.partner.authorization_method == Partner.PROXY and application.requested_access_duration is None: one_year_from_auth = authorization.date_authorized + timedelta( days=365) authorization.date_expires = one_year_from_auth # If this is a proxy partner, and the requested_access_duration # field is set to true, set (or reset) the expiry date # to 1, 3, 6 or 12 months from authorization based on user input elif application.partner.authorization_method == Partner.PROXY and application.partner.requested_access_duration is True: custom_expiry_date = authorization.date_authorized + relativedelta( months=+application.requested_access_duration) authorization.date_expires = custom_expiry_date # Alternatively, if this partner has a specified account_length, # we'll use that to set the expiry. elif application.partner.account_length: # account_length should be a timedelta authorization.date_expires = authorization.date_authorized + application.partner.account_length authorization.save() logger.info("authorization created: {authorization}".format( authorization=authorization))
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()