File diff 94c56bb468cb → daf93dc32b26
conservancy/tests.py
Show inline comments
 
import datetime
 

	
 
from django.http import Http404
 
from django.test import RequestFactory, TestCase
 
import pytest
 
from pytest_django.asserts import assertContains, assertTemplateUsed
 

	
 
from . import views
 
from conservancy.fundgoal.models import FundraisingGoal
 

	
 

	
 
class ContentTest(TestCase):
 
    def setUp(self):
 
        self.factory = RequestFactory()
 
        FundraisingGoal.objects.create(
 
            fundraiser_code_name='cy2023-end-year-match',
 
            fundraiser_goal_amount=0,
 
            fundraiser_so_far_amount=0,
 
            fundraiser_donation_count=0,
 
            fundraiser_donation_count_disclose_threshold=0,
 
            fundraiser_endtime=datetime.datetime(2000, 1, 1)
 
        )
 

	
 
    def test_about_page_served(self):
 
        request = self.factory.get('/about/')
 
        with self.assertTemplateUsed('about/index.html'):
 
            response = views.index(request).render()
 
        self.assertContains(response, 'Conservancy is a nonprofit organization')
 

	
 
    def test_annual_report_file_served(self):
 
        request = self.factory.get('/docs/conservancy_annual-report_fy-2011.pdf')
 
        response = views.index(request)
 
        self.assertEqual(response.headers['Content-Type'], 'application/pdf')
 

	
 
    def test_path_traversal_404s(self):
 
        request = self.factory.get('/about/../../settings.py')
 
        with self.assertRaises(Http404):
 
            views.index(request)
 
def create_fundraising_goal():
 
    FundraisingGoal.objects.create(
 
        fundraiser_code_name='cy2023-end-year-match',
 
        fundraiser_goal_amount=0,
 
        fundraiser_so_far_amount=0,
 
        fundraiser_donation_count=0,
 
        fundraiser_donation_count_disclose_threshold=0,
 
        fundraiser_endtime=datetime.datetime(2000, 1, 1)
 
    )
 

	
 

	
 
@pytest.mark.django_db
 
def test_about_page_served(rf):
 
    create_fundraising_goal()
 
    request = rf.get('/about/')
 
    with assertTemplateUsed('about/index.html'):
 
        response = views.index(request).render()
 
    assertContains(response, 'Conservancy is a nonprofit organization')
 

	
 

	
 
def test_annual_report_file_served(rf):
 
    request = rf.get('/docs/conservancy_annual-report_fy-2011.pdf')
 
    response = views.index(request)
 
    assert response.headers['Content-Type'] == 'application/pdf'
 

	
 

	
 
def test_path_traversal_404s(rf):
 
    request = rf.get('/about/../../settings.py')
 
    with pytest.raises(Http404):
 
        views.index(request)