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)
def test_app_distribution_by_partner_csv(self): """ Test that the CSVAppDistribution csv download works """ for app_status in [ Application.PENDING, Application.APPROVED, Application.QUESTION ]: app = ApplicationFactory(partner=self.partner) app.status = app_status app.save() # Let's create an application for a different partner too, which # shouldn't be counted. app = ApplicationFactory() app.status = Application.APPROVED app.save() request = self.factory.get( reverse('csv:app_distribution_by_partner', kwargs={'pk': self.partner.pk})) request.user = self.user response = views.CSVAppDistribution.as_view()(request, pk=self.partner.pk) expected_data = [['Pending', '1'], ['Approved', '2'], ['Under discussion', '1'], ['Sent to partner', '0'], ['Not approved', '0'], ['Invalid', '0']] self._verify_equal(response, expected_data)
def test_app_distribution_csv(self): """ Test that the CSVAppDistribution csv download works """ # Create some applications with different statuses for app_status in [ Application.PENDING, Application.APPROVED, Application.QUESTION, ]: app = ApplicationFactory() app.status = app_status app.save() request = self.factory.get(reverse("csv:app_distribution")) request.user = self.user response = views.CSVAppDistribution.as_view()(request) expected_data = [ ["Pending", "1"], ["Approved", "2"], ["Under discussion", "1"], ["Sent to partner", "0"], ["Not approved", "0"], ["Invalid", "0"], ] self._verify_equal(response, expected_data)
def test_reapproval_does_not_call_email_function(self, mock_email): """ Saving an Application with APPROVED status, when it already had an APPROVED status, should not re-send the email. """ app = ApplicationFactory(status=Application.PENDING) app.status = Application.APPROVED app.save() app.save() self.assertEqual(mock_email.call_count, 1)
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 test_updating_existing_authorization(self): """ In the case that an authorization already exists for a user, and they apply for renewal, their authorization object should be updated with any new information (e.g. authorizer). """ # Revalidate starting authorizer. self.assertEqual(self.auth_app1.authorizer, self.editor4.user) # Create a new application to the same partner (in reality this # is most likely to be a renewal) app1_renewal = ApplicationFactory( editor=self.app1.user.editor, partner=self.app1.partner ) app1_renewal.status = Application.APPROVED app1_renewal.save() # Assign a new coordinator to this partner app1_renewal.partner.coordinator = self.editor5.user app1_renewal.partner.save() # And mark this one as sent, but by a different user. request = RequestFactory().post( reverse( "applications:send_partner", kwargs={"pk": app1_renewal.partner.pk} ), data={"applications": [app1_renewal.pk]}, ) request.user = self.editor5.user _ = TWLight.applications.views.SendReadyApplicationsView.as_view()( request, pk=app1_renewal.partner.pk ) auth_app1_renewal = Authorization.objects.get( user=self.app1.user, authorizer=self.editor5.user, partners=self.app1.partner, ) self.assertTrue(auth_app1_renewal)
def test_app_distribution_csv(self): """ Test that the CSVAppDistribution csv download works """ # Create some applications with different statuses for app_status in [ Application.PENDING, Application.APPROVED, Application.QUESTION ]: app = ApplicationFactory() app.status = app_status app.save() request = self.factory.get(reverse('csv:app_distribution')) request.user = self.user response = views.CSVAppDistribution.as_view()(request) expected_data = [['Pending', '1'], ['Approved', '2'], ['Under discussion', '1'], ['Sent to partner', '0'], ['Not approved', '0'], ['Invalid', '0']] self._verify_equal(response, expected_data)
def test_rerejection_does_not_call_email_function(self, mock_email): app = ApplicationFactory(status=Application.PENDING) app.status = Application.NOT_APPROVED app.save() app.save() self.assertEqual(mock_email.call_count, 1)
def test_rejection_calls_email_function(self, mock_email): app = ApplicationFactory(status=Application.PENDING) app.status = Application.NOT_APPROVED app.save() self.assertTrue(mock_email.called)
def handle(self, *args, **options): num_applications = options['num'][0] fake = Faker() available_partners = Partner.objects.all() # Don't fire any applications from the superuser. all_users = User.objects.exclude(is_superuser=True) import_date = datetime.datetime(2017, 7, 17, 0, 0, 0) for _ in range(num_applications): random_user = random.choice(all_users) random_partner = random.choice(available_partners) app = ApplicationFactory( editor = random_user.editor, partner = random_partner, hidden = self.chance(True, False, 10) ) # Make sure partner-specific information is filled. if random_partner.specific_stream: app.specific_stream = random.choice( Stream.objects.filter(partner=random_partner)) if random_partner.specific_title: app.specific_title = fake.sentence(nb_words= 3) if random_partner.agreement_with_terms_of_use: app.agreement_with_terms_of_use = True if random_partner.account_email: app.account_email = fake.email() # Imported applications have very specific information, and were # all imported on the same date. imported = self.chance(True, False, 50) if imported: app.status = Application.SENT app.date_created = import_date app.date_closed = import_date app.rationale = "Imported on 2017-07-17" app.comments = "Imported on 2017-07-17" app.imported = True else: app.status = random.choice(Application.STATUS_CHOICES)[0] app.date_created = fake.date_time_between( start_date = random_user.editor.wp_registered, end_date = "now", tzinfo=None) app.rationale = fake.paragraph(nb_sentences=3) app.comments = fake.paragraph(nb_sentences=2) # For closed applications, assign date_closed and date_open if app.status in Application.FINAL_STATUS_LIST: if not imported: potential_end_date = app.date_created + relativedelta(years=1) if potential_end_date > datetime.datetime.now(): end_date = "now" else: end_date = potential_end_date app.date_closed = fake.date_time_between( start_date = app.date_created, end_date = end_date, tzinfo=None) app.days_open = (app.date_closed - app.date_created).days if app.status == Application.SENT: # Assign sent_by if this is a non-imported sent applications if not imported: app.sent_by = random_partner.coordinator # If this partner has access codes, assign a code to # this sent application. if random_partner.authorization_method == Partner.CODES: this_partner_access_codes = AccessCode.objects.filter( partner=random_partner, authorization__isnull=True) app_code = random.choice(this_partner_access_codes) #app_code.application = app app_code.save() app.save() # Renew a selection of sent apps. all_apps = Application.objects.filter(status=Application.SENT) num_to_renew = int(all_apps.count()*0.5) for app_to_renew in random.sample(all_apps, num_to_renew): app_to_renew.renew()
def handle(self, *args, **options): num_applications = options["num"][0] available_partners = Partner.objects.all() # Don't fire any applications from the superuser. all_editors = Editor.objects.exclude(user__is_superuser=True) import_date = datetime.datetime(2017, 7, 17, 0, 0, 0) # We want to flag applications as SENT via a client later, so let's only # automatically give applications non-Sent statuses. valid_choices = [ Application.PENDING, Application.QUESTION, Application.APPROVED, Application.NOT_APPROVED, Application.INVALID, ] for _ in range(num_applications): random_editor = random.choice(all_editors) # Limit to partners this user hasn't already applied to. not_applied_partners = available_partners.exclude( applications__editor=random_editor) if not_applied_partners: random_partner = random.choice(not_applied_partners) app = ApplicationFactory( editor=random_editor, partner=random_partner, hidden=self.chance(True, False, 10), ) # Make sure partner-specific information is filled. if random_partner.specific_stream: app.specific_stream = random.choice( Stream.objects.filter(partner=random_partner)) if random_partner.specific_title: app.specific_title = Faker( random.choice( settings.FAKER_LOCALES)).sentence(nb_words=3) if random_partner.agreement_with_terms_of_use: app.agreement_with_terms_of_use = True if random_partner.account_email: app.account_email = Faker( random.choice(settings.FAKER_LOCALES)).email() # Imported applications have very specific information, and were # all imported on the same date. imported = self.chance(True, False, 50) if imported: app.status = Application.SENT app.date_created = import_date app.date_closed = import_date app.rationale = "Imported on 2017-07-17" app.comments = "Imported on 2017-07-17" app.imported = True else: app.status = random.choice(valid_choices) # Figure out earliest valid date for this app if random_editor.wp_registered < import_date.date(): start_date = import_date else: start_date = random_editor.wp_registered app.date_created = Faker( random.choice( settings.FAKER_LOCALES)).date_time_between( start_date=start_date, end_date="now", tzinfo=None) app.rationale = Faker(random.choice( settings.FAKER_LOCALES)).paragraph(nb_sentences=3) app.comments = Faker(random.choice( settings.FAKER_LOCALES)).paragraph(nb_sentences=2) # For closed applications, assign date_closed and date_open if app.status in Application.FINAL_STATUS_LIST: if not imported: potential_end_date = app.date_created + relativedelta( years=1) if potential_end_date > datetime.datetime.now(): end_date = "now" else: end_date = potential_end_date app.date_closed = Faker( random.choice( settings.FAKER_LOCALES)).date_time_between( start_date=app.date_created, end_date=end_date, tzinfo=None) app.days_open = (app.date_closed - app.date_created).days # Make sure we always set sent_by if app.status == Application.SENT and not app.sent_by: app.sent_by = twl_team app.save() # Let's mark all Approved applications that were approved more # than 3 weeks ago as Sent. old_approved_apps = Application.objects.filter( status=Application.APPROVED, date_created__lte=datetime.datetime.now() - relativedelta(weeks=3), ) # We need to be able to handle messages message_patcher = patch( "TWLight.applications.views.messages.add_message") message_patcher.start() for approved_app in old_approved_apps: client = Client(SERVER_NAME="twlight.vagrant.localdomain") coordinator = logged_in_example_coordinator( client, approved_app.partner.coordinator) url = reverse("applications:send_partner", kwargs={"pk": approved_app.partner.pk}) this_partner_access_codes = AccessCode.objects.filter( partner=approved_app.partner, authorization__isnull=True) if approved_app.partner.authorization_method == Partner.EMAIL: request = RequestFactory().post( url, data={"applications": [approved_app.pk]}) # If this partner has access codes, assign a code to # this sent application. elif (this_partner_access_codes and approved_app.partner.authorization_method == Partner.CODES): access_code = random.choice(this_partner_access_codes) request = RequestFactory().post( url, data={ "accesscode": [ "{app_pk}_{code}".format(app_pk=approved_app.pk, code=access_code.code) ] }, ) request.user = coordinator response = SendReadyApplicationsView.as_view()( request, pk=approved_app.partner.pk) # Renew a selection of sent apps. all_apps = Application.objects.filter(status=Application.SENT) num_to_renew = int(all_apps.count() * 0.5) for app_to_renew in random.sample(list(all_apps), num_to_renew): app_to_renew.renew() for application in Application.objects.filter( status=Application.PENDING, parent__isnull=False): parent_application = Application.objects.get( pk=application.parent.pk) app_date = parent_application.date_closed renewal_date = Faker(random.choice( settings.FAKER_LOCALES)).date_time_between(start_date=app_date, end_date="now", tzinfo=None) application.date_created = renewal_date application.save()
def handle(self, *args, **options): num_applications = options['num'][0] fake = Faker() available_partners = Partner.objects.all() # Don't fire any applications from the superuser. all_users = User.objects.exclude(is_superuser=True) for _ in range(num_applications): random_user = random.choice(all_users) random_partner = random.choice(available_partners) app = ApplicationFactory( editor = random_user.editor, partner = random_partner, hidden = self.chance(True, False, 10) ) # Make sure partner-specific information is filled. if random_partner.specific_stream: app.specific_stream = random.choice( Stream.objects.filter(partner=random_partner)) if random_partner.specific_title: app.specific_title = fake.sentence(nb_words= 3) if random_partner.agreement_with_terms_of_use: app.agreement_with_terms_of_use = True if random_partner.account_email: app.account_email = fake.email() # Imported applications have very specific information, and were # all imported on the same date. imported = self.chance(True, False, 50) import_date = datetime.datetime(2017, 7, 17, 0, 0, 0) if imported: app.status = Application.SENT app.date_created = import_date app.date_closed = import_date app.rationale = "Imported on 2017-07-17" app.comments = "Imported on 2017-07-17" else: app.status = random.choice(Application.STATUS_CHOICES)[0] app.date_created = fake.date_time_between( start_date = random_user.editor.wp_registered, end_date = "now", tzinfo=None) app.rationale = fake.paragraph(nb_sentences=3) app.comments = fake.paragraph(nb_sentences=2) # Assign sent_by if this is a non-imported sent applications if app.status == Application.SENT: if not imported: app.sent_by = random_partner.coordinator app.date_closed = fake.date_time_between( start_date = app.date_created, end_date = "now", tzinfo=None) else: app.date_closed = import_date app.save() # Renew a selection of apps. all_apps = Application.objects.all() num_to_renew = int(num_applications*0.3) for app_to_renew in random.sample(all_apps, num_to_renew): app_to_renew.renew()