def test_raw_email_view(self): """Advanced users should be able to view raw emails""" basic_user = factories.UserFactory(profile__acct_type='basic') pro_user = factories.UserFactory(profile__acct_type='pro') request = self.request_factory.get(self.url) request.user = basic_user response = self.view(request, self.comm.id) eq_(response.status_code, 302, 'Basic users should be denied access.') request.user = pro_user response = self.view(request, self.comm.id) eq_(response.status_code, 200, 'Advanced users should be allowed access.')
def setUp(self): user = factories.UserFactory() agency = factories.AgencyFactory(status='pending') self.foia = factories.FOIARequestFactory(user=user, agency=agency) self.comm = factories.FOIACommunicationFactory(foia=self.foia, response=True) # tasks that incorporate FOIAs are: # ResponseTask, SnailMailTask, FailedFaxTask, RejectedEmailTask, FlaggedTask, # StatusChangeTask, NewAgencyTask response_task = task.models.ResponseTask.objects.create( communication=self.comm) snail_mail_task = task.models.SnailMailTask.objects.create( category='a', communication=self.comm) failed_fax_task = task.models.FailedFaxTask.objects.create( communication=self.comm) rejected_email_task = task.models.RejectedEmailTask.objects.create( category='d', foia=self.foia) flagged_task = task.models.FlaggedTask.objects.create(user=user, text='Halp', foia=self.foia) status_change_task = task.models.StatusChangeTask.objects.create( user=user, old_status='ack', foia=self.foia) new_agency_task = task.models.NewAgencyTask.objects.create( user=user, agency=agency) self.tasks = [ response_task, snail_mail_task, failed_fax_task, rejected_email_task, flagged_task, status_change_task, new_agency_task ]
def setUp(self): self.user = factories.UserFactory(profile__acct_type='pro') self.user.profile.organization = factories.OrganizationFactory( active=True) self.foia = factories.FOIARequestFactory(user=self.user) self.request_factory = RequestFactory() self.url = self.foia.get_absolute_url()
def test_not_staff_not_ok(self): response = http_get_response(self.url, self.view, factories.UserFactory(), follow=True) eq_(response.status_code, 302) eq_(response.url, '/accounts/login/?next=%s' % self.url)
def setUp(self): self.user = factories.UserFactory() project = factories.ProjectFactory() project.contributors.add(self.user) self.kwargs = {'username': self.user.username} self.url = reverse('project-contributor', kwargs=self.kwargs) self.view = views.ProjectContributorView.as_view()
def test_record_check(self): """When given a check number, a note should be attached to the request.""" user = factories.UserFactory(is_staff=True) check_number = 1 self.task.amount = 100.00 self.task.save() note = self.task.record_check(check_number, user) ok_(isinstance(note, FOIANote), 'The method should return a FOIANote.')
def setUp(self): # We will start with a project that's already been made. self.project = factories.ProjectFactory(private=True, approved=False) self.contributor = factories.UserFactory() self.project.contributors.add(self.contributor) self.kwargs = {'slug': self.project.slug, 'pk': self.project.pk} self.url = reverse('project-publish', kwargs=self.kwargs) self.view = views.ProjectPublishView.as_view()
def setUp(self): self.url = reverse('response-task-list') self.view = ResponseTaskList.as_view() self.user = factories.UserFactory(is_staff=True) task1 = ResponseTaskFactory() task2 = ResponseTaskFactory() task3 = ResponseTaskFactory() self.tasks = [task1, task2, task3]
def test_get_absolute_url(self): text = 'Lorem ipsum' user = factories.UserFactory() foia = factories.FOIARequestFactory() flagged_task = self.task.objects.create(user=user, foia=foia, text=text) _url = reverse('flagged-task', kwargs={'pk': flagged_task.pk}) eq_(flagged_task.get_absolute_url(), _url)
def test_org_invoice_receipt(self, mock_send): """A receipt should be sent after an org subscription payment is made.""" mock_subscription.plan.id = 'org' customer_id = 'test-org' owner = factories.UserFactory(profile__customer_id=customer_id) factories.OrganizationFactory(owner=owner) mock_invoice.customer = customer_id tasks.send_invoice_receipt(mock_invoice.id) mock_send.assert_called_with(fail_silently=False)
def test_for_object(self): """Notifications should be filterable by a single object.""" foia = factories.FOIARequestFactory() _action = new_action(factories.UserFactory(), 'submitted', target=foia) object_notification = factories.NotificationFactory(user=self.user, action=_action) object_notifications = Notification.objects.for_object(foia) ok_(object_notification in object_notifications, 'A notification for the object should be in the set returned.') ok_(self.notification not in object_notifications, 'A notification not including the object should not be in the set returned.')
def setUp(self): self.agency = factories.AgencyFactory() self.url = self.agency.get_absolute_url() self.view = agency.views.detail self.user = factories.UserFactory() self.kwargs = { 'jurisdiction': self.agency.jurisdiction.slug, 'jidx': self.agency.jurisdiction.id, 'slug': self.agency.slug, 'idx': self.agency.id }
def setUp(self): # We will start with a project that's already been made. # We will give that project a single contributor. self.contributor = factories.UserFactory() self.project = factories.ProjectFactory() self.project.contributors.add(self.contributor) self.project.save() self.factory = RequestFactory() self.kwargs = {'slug': self.project.slug, 'pk': self.project.pk} self.url = reverse('project-edit', kwargs=self.kwargs) self.view = views.ProjectEditView.as_view()
def test_tasks_for_foia(self): """ The task manager should return all tasks that explictly or implicitly reference the provided FOIA. """ staff_user = factories.UserFactory(is_staff=True, profile__acct_type='admin') returned_tasks = task.models.Task.objects.filter_by_foia( self.foia, staff_user) eq_( returned_tasks, self.tasks, 'The manager should return all the tasks that incorporate this FOIA.' )
def test_no_permission_to_edit(self): """Users without permission to edit the request should not be able to change the embargo""" user_without_permission = factories.UserFactory( profile__acct_type='pro') assert_false(self.foia.has_perm(user_without_permission, 'change')) data = {'embargo': 'create'} request = self.request_factory.post(self.url, data) request.user = user_without_permission response = self.get_response(request) self.foia.refresh_from_db() eq_(response.status_code, 302) ok_(not self.foia.embargo, 'The embargo should not be set on the request.')
def test_no_permission_to_embargo(self): """Users without permission to embargo the request should not be allowed to do so.""" user_without_permission = factories.UserFactory() self.foia.user = user_without_permission self.foia.save() ok_(self.foia.has_perm(user_without_permission, 'change')) assert_false(self.foia.has_perm(user_without_permission, 'embargo')) data = {'embargo': 'create'} request = self.request_factory.post(self.url, data) request.user = user_without_permission response = self.get_response(request) self.foia.refresh_from_db() eq_(response.status_code, 302) ok_(not self.foia.embargo, 'The embargo should not be set on the request.')
def test_add_contributors(self, mock_notify): """When adding contributors, each new contributor should get an email notification.""" new_contributor = factories.UserFactory() data = { 'title': self.project.title, 'contributors': [self.contributor.pk, new_contributor.pk] } form = forms.ProjectUpdateForm(data, instance=self.project) ok_(form.is_valid(), 'The form should validate. %s' % form.errors) http_post_response(self.url, self.view, data, self.contributor, **self.kwargs) self.project.refresh_from_db() ok_(self.project.has_contributor(new_contributor)) ok_(self.project.has_contributor(self.contributor)) mock_notify.assert_called_once_with(new_contributor, self.project, self.contributor)
def test_flagged_object(self): """A flagged task should be able to return its object.""" text = 'Lorem ipsum' user = factories.UserFactory() foia = factories.FOIARequestFactory() agency = factories.AgencyFactory() jurisdiction = factories.JurisdictionFactory() flagged_foia_task = self.task.objects.create(user=user, foia=foia, text=text) flagged_agency_task = self.task.objects.create(user=user, agency=agency, text=text) flagged_jurisdiction_task = self.task.objects.create( user=user, jurisdiction=jurisdiction, text=text) eq_(flagged_foia_task.flagged_object(), foia) eq_(flagged_agency_task.flagged_object(), agency) eq_(flagged_jurisdiction_task.flagged_object(), jurisdiction)
def test_unembargo(self): """ The embargo should be removable by editors of the request. Any user should be allowed to remove an embargo, even if they cannot apply one. """ user_without_permission = factories.UserFactory() self.foia.user = user_without_permission self.foia.embargo = True self.foia.save() assert_true(self.foia.embargo) assert_true(self.foia.has_perm(user_without_permission, 'change')) assert_false(self.foia.has_perm(user_without_permission, 'embargo')) data = {'embargo': 'delete'} request = self.request_factory.post(self.url, data) request.user = user_without_permission response = self.get_response(request) self.foia.refresh_from_db() eq_(response.status_code, 302) assert_false(self.foia.embargo, 'The embargo should be removed from the request.')
def test_post(self): """Posting a valid ProjectForm should create the project.""" form = forms.ProjectCreateForm({ 'title': 'Cool Project', 'summary': 'Yo my project is cooler than LIFE!', 'image': test_image, 'tags': 'dogs, cats', 'private': True, 'featured': True }) ok_(form.is_valid(), 'The form should validate.') staff_user = factories.UserFactory(is_staff=True) response = http_post_response(self.url, self.view, form.data, staff_user) project = models.Project.objects.last() eq_(response.status_code, 302, 'The response should redirect to the project when it is created.') ok_( staff_user in project.contributors.all(), 'The current user should automatically be added as a contributor.')
def test_cannot_permanent_embargo(self): """Users who cannot set permanent embargoes shouldn't be able to.""" user_without_permission = factories.UserFactory( profile__acct_type='pro') self.foia.user = user_without_permission self.foia.save() assert_true(user_without_permission.profile.can_embargo()) assert_false(user_without_permission.profile.can_embargo_permanently()) assert_true(self.foia.editable_by(user_without_permission)) embargo_form = foia.forms.FOIAEmbargoForm({'permanent_embargo': True}) assert_true(embargo_form.is_valid(), 'Form should validate.') data = {'embargo': 'create'} data.update(embargo_form.data) request = self.request_factory.post(self.url, data) request.user = self.foia.user response = self.get_response(request) self.foia.refresh_from_db() eq_(response.status_code, 302) assert_true(self.foia.embargo, 'An embargo should be set on the request.') assert_false(self.foia.permanent_embargo, 'A permanent embargo should not be set on the request.')
def test_post(self): """Posting data for a crowdfund should create it.""" user = factories.UserFactory(is_staff=True) name = 'Project Crowdfund' description = 'A crowdfund' payment_required = 100 payment_capped = True date_due = date.today() + timedelta(20) data = { 'name': name, 'description': description, 'payment_required': payment_required, 'payment_capped': payment_capped, 'date_due': date_due } request = self.request_factory.post(self.url, data) request.user = user request = mock_middleware(request) response = self.view(request, slug=self.project.slug, pk=self.project.pk) self.project.refresh_from_db() eq_(self.project.crowdfunds.count(), 1, 'A crowdfund should be created and added to the project.') crowdfund = self.project.crowdfunds.first() eq_(crowdfund.name, name) eq_(crowdfund.description, description) expected_payment_required = Decimal(payment_required + payment_required * .15) / 100 eq_( crowdfund.payment_required, expected_payment_required, 'Expected payment of %(expected).2f, actually %(actual).2f' % { 'expected': expected_payment_required, 'actual': crowdfund.payment_required }) eq_(crowdfund.payment_capped, payment_capped) eq_(crowdfund.date_due, date_due) eq_(response.status_code, 302)
def setUp(self): self.user = factories.UserFactory() self.action = new_action(self.user, 'acted') self.notification = factories.NotificationFactory()
def test_staff(self): """Staff users should be able to edit projects.""" staff_user = factories.UserFactory(is_staff=True) response = http_get_response(self.url, self.view, staff_user, **self.kwargs) eq_(response.status_code, 200)
def test_resolve_with_user(self): """Tasks should record the user responsible for the resolution.""" user = factories.UserFactory() self.task.resolve(user) eq_(self.task.resolved_by, user, 'The resolving user should be recorded by the task.')
def setUp(self): self.user = factories.UserFactory(is_staff=True) self.url = reverse('flagged-task-list') self.view = task.views.FlaggedTaskList.as_view() self.task = FlaggedTaskFactory() self.request_factory = RequestFactory()
def test_basic(self): """Basic users should not be able to GET the ProjectCreateView.""" user = factories.UserFactory() response = http_get_response(self.url, self.view, user) eq_(response.status_code, 200, 'Basic users should be able to GET the ProjectCreateView.')
def setUp(self): self.user = factories.UserFactory(is_staff=True) self.url = reverse('projectreview-task-list') self.view = task.views.ProjectReviewTaskList.as_view() self.task = task.factories.ProjectReviewTaskFactory() self.request_factory = RequestFactory()
def test_basic(self): """Basic users should not be able to delete projects.""" user = factories.UserFactory() http_get_response(self.url, self.view, user, **self.kwargs)
def setUp(self): self.user = factories.UserFactory(is_staff=True) self.url = reverse('stale-agency-task-list') self.view = task.views.StaleAgencyTaskList.as_view() self.task = StaleAgencyTaskFactory() self.request_factory = RequestFactory()