def test_post_update_composer(self): """Test submitting a composer""" composer = FOIAComposerFactory( status='started', user__profile__num_requests=4, ) agency = AgencyFactory() data = { 'title': 'Title', 'requested_docs': 'ABC', 'agencies': agency.pk, 'action': 'submit', } request = self.request_factory.post( reverse('foia-draft', kwargs={ 'idx': composer.pk }), data, ) request.user = composer.user request = mock_middleware(request) response = UpdateComposer.as_view()(request, idx=composer.pk) eq_(response.status_code, 302) composer.refresh_from_db() ok_(composer.status, 'submitted')
def test_return_requests(self): """Test return requests""" organization = OrganizationFactory(num_requests=100) composer = FOIAComposerFactory( status='submitted', num_org_requests=1, num_monthly_requests=2, num_reg_requests=3, user__profile__num_requests=5, user__profile__monthly_requests=10, user__profile__organization=organization, ) composer._return_requests({ 'regular': 2, 'monthly': 0, 'org': 1, }) composer.user.profile.refresh_from_db() composer.user.profile.organization.refresh_from_db() eq_(composer.num_reg_requests, 1) eq_(composer.num_monthly_requests, 2) eq_(composer.num_org_requests, 0) eq_(composer.user.profile.num_requests, 7) eq_(composer.user.profile.monthly_requests, 10) eq_(composer.user.profile.organization.num_requests, 101)
def test_calc_return_requests(self): """Test calculating the return requests""" composer = FOIAComposerFactory( status='submitted', agencies=AgencyFactory.create_batch(6), num_org_requests=1, num_monthly_requests=2, num_reg_requests=3, user__profile__num_requests=5, user__profile__monthly_requests=10, user__profile__organization=OrganizationFactory(num_requests=100), ) values = [ (7, 4, 2, 1), (6, 3, 2, 1), (5, 3, 2, 0), (4, 3, 1, 0), (3, 3, 0, 0), (2, 2, 0, 0), (1, 1, 0, 0), ] for total, reg, monthly, org in values: eq_( composer._calc_return_requests(total), { 'regular': reg, 'monthly': monthly, 'org': org, }, )
def test_get_update_composer_revoke(self): """Get the update composer form for a recently submitted composer""" composer = FOIAComposerFactory(status="submitted", delayed_id="123", datetime_submitted=timezone.now()) request = self.request_factory.get( reverse("foia-draft", kwargs={"idx": composer.pk})) request.user = composer.user request = mock_middleware(request) response = UpdateComposer.as_view()(request, idx=composer.pk) eq_(response.status_code, 200) composer.refresh_from_db() eq_(composer.status, "started")
def test_agency_merge(self): """Test agency merging""" good_agency = AgencyFactory(status="approved", email=None, fax=None) bad_agency = AgencyFactory(status="approved", email=None, fax=None) appeal_agency = AgencyFactory(appeal_agency=bad_agency) foia = FOIARequestFactory(agency=bad_agency) composer = FOIAComposerFactory(agencies=[bad_agency]) user = UserFactory() email = EmailAddressFactory() AgencyEmailFactory(agency=good_agency, email=email, email_type="to") AgencyEmailFactory(agency=bad_agency, email=email, email_type="cc") fax1 = PhoneNumberFactory() fax2 = PhoneNumberFactory() AgencyPhoneFactory(agency=good_agency, phone=fax1, request_type="primary") AgencyPhoneFactory(agency=bad_agency, phone=fax2, request_type="primary") good_agency.merge(bad_agency, user) bad_agency.refresh_from_db() appeal_agency.refresh_from_db() foia.refresh_from_db() composer.refresh_from_db() eq_(bad_agency.status, "rejected") eq_(foia.agency, good_agency) eq_(composer.agencies.first(), good_agency) eq_(appeal_agency.appeal_agency, good_agency) # email that already exists is not copied over eq_(good_agency.emails.count(), 1) eq_(good_agency.agencyemail_set.first().email_type, "to") # phone number that doesnt exist is copied over eq_(good_agency.phones.count(), 2) # existing phone number is unaffected ok_( good_agency.agencyphone_set.filter( phone=fax1, request_type="primary" ).exists() ) # its type is set to none when copied over ok_( good_agency.agencyphone_set.filter(phone=fax2, request_type="none").exists() ) assert_in(good_agency.name, bad_agency.notes) assert_in(str(good_agency.pk), bad_agency.notes) assert_in(user.username, bad_agency.notes)
def test_calc_return_requests(self): """Test calculating the return requests""" composer = FOIAComposerFactory( status="submitted", agencies=AgencyFactory.create_batch(6), num_monthly_requests=2, num_reg_requests=3, ) values = [(6, 4, 2), (5, 3, 2), (4, 3, 1), (3, 3, 0), (2, 2, 0), (1, 1, 0)] for total, reg, monthly in values: eq_( composer._calc_return_requests(total), {"regular": reg, "monthly": monthly}, )
def test_autosave_good(self): """Test a succesful autosave""" composer = FOIAComposerFactory(status="started") request = self.request_factory.post( reverse("foia-autosave", kwargs={"idx": composer.pk}), { "title": "New Title", "requested_docs": "ABC" }, ) request.user = composer.user request = mock_middleware(request) response = autosave(request, idx=composer.pk) eq_(response.status_code, 200) composer.refresh_from_db() eq_(composer.title, "New Title") eq_(composer.requested_docs, "ABC")
def test_autosave_good(self): """Test a succesful autosave""" composer = FOIAComposerFactory(status='started') request = self.request_factory.post( reverse('foia-autosave', kwargs={'idx': composer.pk}), { 'title': 'New Title', 'requested_docs': 'ABC', }, ) request.user = composer.user request = mock_middleware(request) response = autosave(request, idx=composer.pk) eq_(response.status_code, 200) composer.refresh_from_db() eq_(composer.title, 'New Title') eq_(composer.requested_docs, 'ABC')
def test_get_viewable_draft(self): """Test get viewable for a draft composer""" FOIAComposerFactory(status='started') assert_true(FOIAComposer.objects.get_viewable(self.staff).exists()) assert_false(FOIAComposer.objects.get_viewable(self.user).exists()) assert_false(FOIAComposer.objects.get_viewable(self.anon).exists())
def test_get_update_composer_bad(self): """Try to update a composer that can no longer be updated""" composer = FOIAComposerFactory(status='filed') request = self.request_factory.get( reverse('foia-draft', kwargs={'idx': composer.pk})) request.user = composer.user request = mock_middleware(request) response = UpdateComposer.as_view()(request, idx=composer.pk) eq_(response.status_code, 302)
def test_get_update_composer(self): """Get the update composer form""" composer = FOIAComposerFactory() request = self.request_factory.get( reverse('foia-draft', kwargs={'idx': composer.pk})) request.user = composer.user request = mock_middleware(request) response = UpdateComposer.as_view()(request, idx=composer.pk) eq_(response.status_code, 200)
def test_post_update_composer(self): """Test submitting a composer""" composer = FOIAComposerFactory(status="started") agency = AgencyFactory() data = { "title": "Title", "requested_docs": "ABC", "agencies": agency.pk, "action": "submit", "stripe_pk": "STRIPE_PK", } request = self.request_factory.post( reverse("foia-draft", kwargs={"idx": composer.pk}), data) request.user = composer.user request = mock_middleware(request) response = UpdateComposer.as_view()(request, idx=composer.pk) eq_(response.status_code, 302) composer.refresh_from_db() ok_(composer.status, "submitted")
def test_post_delete_update_composer(self): """Test deleting a composer""" composer = FOIAComposerFactory(status="started") data = {"action": "delete"} request = self.request_factory.post( reverse("foia-draft", kwargs={"idx": composer.pk}), data) request.user = composer.user request = mock_middleware(request) response = UpdateComposer.as_view()(request, idx=composer.pk) eq_(response.status_code, 302) assert_false(FOIAComposer.objects.filter(pk=composer.pk).exists())
def setUp(self): self.agencies = AgencyFactory.create_batch(6) self.organization = OrganizationFactory(num_requests=100) self.composer = FOIAComposerFactory( status='submitted', agencies=self.agencies, num_org_requests=1, num_monthly_requests=2, num_reg_requests=3, user__profile__num_requests=5, user__profile__monthly_requests=10, user__profile__organization=self.organization, ) self.task = MultiRequestTask.objects.create(composer=self.composer)
def test_autosave_bad(self): """Test a failed autosave""" composer = FOIAComposerFactory(status="started") request = self.request_factory.post( reverse("foia-autosave", kwargs={"idx": composer.pk}), { "agencies": "foobar", "requested_docs": "ABC" }, ) request.user = composer.user request = mock_middleware(request) response = autosave(request, idx=composer.pk) eq_(response.status_code, 400)
def test_composer_detail_draft_bad(self): """Composer detail view redirects to update page if draft""" composer = FOIAComposerFactory(status="started") request = self.request_factory.get( reverse( "foia-composer-detail", kwargs={ "slug": composer.slug, "idx": composer.pk }, )) request.user = UserFactory() request = mock_middleware(request) ComposerDetail.as_view()(request, slug=composer.slug, idx=composer.pk)
def setUp(self): self.agencies = AgencyFactory.create_batch(6) self.composer = FOIAComposerFactory( status="submitted", agencies=self.agencies, num_monthly_requests=2, num_reg_requests=3, ) self.task = MultiRequestTask.objects.create(composer=self.composer) self.mocker = requests_mock.Mocker() mock_squarelet(self.mocker) self.mocker.start() self.addCleanup(self.mocker.stop)
def test_autosave_bad(self): """Test a failed autosave""" composer = FOIAComposerFactory(status='started') request = self.request_factory.post( reverse('foia-autosave', kwargs={'idx': composer.pk}), { 'agencies': 'foobar', 'requested_docs': 'ABC', }, ) request.user = composer.user request = mock_middleware(request) response = autosave(request, idx=composer.pk) eq_(response.status_code, 400)
def test_public_views(self): """Test public views while not logged in""" # give the user a composer so they have a public profile FOIAComposerFactory(user=self.user, status="submitted") # account overview page response = http_get_response(reverse("accounts"), views.AccountsView.as_view()) eq_(response.status_code, 302) # profile page request_factory = RequestFactory() request = request_factory.get(self.user.profile.get_absolute_url()) request = mock_middleware(request) request.user = AnonymousUser() response = views.ProfileView.as_view()(request, username=self.user.username) eq_(response.status_code, 200)
def test_composer_detail_draft_bad(self): """Composer detail view redirects to update page if draft""" composer = FOIAComposerFactory(status='started') request = self.request_factory.get( reverse('foia-composer-detail', kwargs={ 'slug': composer.slug, 'idx': composer.pk })) request.user = UserFactory() request = mock_middleware(request) ComposerDetail.as_view()( request, slug=composer.slug, idx=composer.pk, )
def test_composer_detail_draft(self): """Composer detail view redirects to update page if draft""" composer = FOIAComposerFactory(status='started') request = self.request_factory.get( reverse('foia-composer-detail', kwargs={ 'slug': composer.slug, 'idx': composer.pk })) request.user = composer.user request = mock_middleware(request) response = ComposerDetail.as_view()( request, slug=composer.slug, idx=composer.pk, ) eq_(response.status_code, 302) eq_(response.url, reverse('foia-draft', kwargs={'idx': composer.pk}))
def test_post_delete_update_composer(self): """Test deleting a composer""" composer = FOIAComposerFactory( status='started', user__profile__num_requests=4, ) data = { 'action': 'delete', } request = self.request_factory.post( reverse('foia-draft', kwargs={'idx': composer.pk}), data, ) request.user = composer.user request = mock_middleware(request) response = UpdateComposer.as_view()(request, idx=composer.pk) eq_(response.status_code, 302) assert_false(FOIAComposer.objects.filter(pk=composer.pk).exists())
def test_return_requests(self): """Test return requests""" composer = FOIAComposerFactory( status="submitted", num_monthly_requests=2, num_reg_requests=3 ) composer.organization.number_requests = 100 composer.organization.monthly_requests = 50 composer.organization.save() composer._return_requests({"regular": 2, "monthly": 1}) composer.refresh_from_db() eq_(composer.num_reg_requests, 1) eq_(composer.num_monthly_requests, 1) eq_(composer.organization.number_requests, 102) eq_(composer.organization.monthly_requests, 51)
def test_request_lifecycle_no_email(self, mock_request): """Test a request going through the full cycle as if we had to physically mail it """ # pylint: disable=too-many-statements # pylint: disable=protected-access mock_squarelet(mock_request) mail.outbox = [] user = UserFactory(membership__organization__number_requests=1) agency = AgencyFactory(email=None, fax=None) cal = agency.jurisdiction.get_calendar() with freeze_time("2010-02-01"): nose.tools.eq_(len(mail.outbox), 0) ## create and submit request composer = FOIAComposerFactory( user=user, organization=user.profile.organization, title="Test with no email", agencies=[agency], ) composer.submit() foia = FOIARequest.objects.get(composer=composer) comm = foia.communications.last() self.run_commit_hooks() # check that a snail mail task was created nose.tools.ok_( SnailMailTask.objects.filter(communication=comm, category="n").exists()) ## two days pass, then the admin mails in the request with freeze_time("2010-02-03"): foia.status = "processed" foia.update_dates() foia.save() # make sure dates were set correctly nose.tools.eq_(foia.composer.datetime_submitted, datetime(2010, 2, 1, tzinfo=pytz.utc)) nose.tools.eq_( foia.date_due, cal.business_days_from(date(2010, 2, 1), agency.jurisdiction.days), ) nose.tools.eq_( foia.date_followup, max( foia.date_due, foia.communications.last().datetime.date() + timedelta(foia._followup_days()), ), ) nose.tools.ok_(foia.days_until_due is None) # no more mail should have been sent nose.tools.eq_(len(mail.outbox), 0) old_date_due = foia.date_due ## after 5 days agency replies with a fix needed with freeze_time("2010-02-08"): comm = FOIACommunication.objects.create( foia=foia, from_user=agency.get_user(), to_user=user, datetime=timezone.now(), response=True, communication="Test communication", ) foia.status = "fix" foia.save() foia.update(comm.anchor()) # make sure dates were set correctly nose.tools.eq_(foia.composer.datetime_submitted, datetime(2010, 2, 1, tzinfo=pytz.utc)) nose.tools.ok_(foia.date_due is None) nose.tools.ok_(foia.date_followup is None) nose.tools.eq_( foia.days_until_due, cal.business_days_between(date(2010, 2, 8), old_date_due), ) old_days_until_due = foia.days_until_due ## after 10 days the user submits the fix and the admin submits it right away with freeze_time("2010-02-18"): comm = FOIACommunication.objects.create( foia=foia, from_user=user, to_user=agency.get_user(), datetime=timezone.now(), response=False, communication="Test communication", ) foia.status = "submitted" foia.save() foia.submit() self.run_commit_hooks() # check that another snail mail task is created nose.tools.ok_( SnailMailTask.objects.filter(communication=comm, category="u").exists()) foia.status = "processed" foia.update_dates() foia.save() # make sure dates were set correctly nose.tools.eq_(foia.composer.datetime_submitted, datetime(2010, 2, 1, tzinfo=pytz.utc)) nose.tools.eq_( foia.date_due, cal.business_days_from(date.today(), old_days_until_due)) nose.tools.eq_( foia.date_followup, max( foia.date_due, foia.communications.last().datetime.date() + timedelta(foia._followup_days()), ), ) nose.tools.ok_(foia.days_until_due is None) old_date_due = foia.date_due ## after 4 days agency replies with the documents with freeze_time("2010-02-22"): comm = FOIACommunication.objects.create( foia=foia, from_user=agency.get_user(), to_user=user, datetime=timezone.now(), response=True, communication="Test communication", ) foia.status = "done" foia.save() foia.update(comm.anchor()) # make sure dates were set correctly nose.tools.eq_(foia.composer.datetime_submitted, datetime(2010, 2, 1, tzinfo=pytz.utc)) nose.tools.eq_(foia.date_due, old_date_due) nose.tools.ok_(foia.date_followup is None) nose.tools.ok_(foia.days_until_due is None)