Switch content tests to pytest
This commit is contained in:
		
							parent
							
								
									94c56bb468
								
							
						
					
					
						commit
						daf93dc32b
					
				
					 4 changed files with 42 additions and 33 deletions
				
			
		
							
								
								
									
										13
									
								
								README.md
									
										
									
									
									
								
							
							
						
						
									
										13
									
								
								README.md
									
										
									
									
									
								
							|  | @ -42,10 +42,15 @@ You'll need a copy of `conservancy/djangocommonsettings.py`, a file that not | |||
| committed to the repository that has database settings and other | ||||
| environment-specific config. | ||||
| 
 | ||||
| To run the tests, install `pytest-django` and run pytest: | ||||
| 
 | ||||
|     python3 -m pip install pytest-django | ||||
|     python3 -m pytest | ||||
| 
 | ||||
| Then run: | ||||
| 
 | ||||
|     python manage.py migrate | ||||
|     python manage.py runserver | ||||
|     python3 manage.py migrate | ||||
|     python3 manage.py runserver | ||||
| 
 | ||||
| There is also a Dockerfile available if that's more convenient. See that file | ||||
| for details. | ||||
|  | @ -60,5 +65,5 @@ SystemD timer. See `systemd/conservancy-www-update.timer` for details. | |||
| The `migrate` and `collectstatic` commands are not run automatically. You may | ||||
| need to run these if modifying the database schema or adding/moving static files: | ||||
| 
 | ||||
|     sudo -u www-data /var/www/venv-website/bin/python manage.py migrate | ||||
|     sudo -u www-data /var/www/venv-website/bin/python manage.py collectstatic --link | ||||
|     sudo -u www-data /var/www/venv-website/bin/python3 manage.py migrate | ||||
|     sudo -u www-data /var/www/venv-website/bin/python3 manage.py collectstatic --link | ||||
|  |  | |||
|  | @ -6,9 +6,9 @@ set -x  # Show output | |||
| set -e  # Abort on failure | ||||
| cd /var/www/website | ||||
| sudo -u www-data git pull | ||||
| sudo -u www-data /var/www/venv-website/bin/python manage.py check | ||||
| sudo -u www-data /var/www/venv-website/bin/python manage.py migrate | ||||
| sudo -u www-data /var/www/venv-website/bin/python manage.py collectstatic -v0 --noinput --link | ||||
| sudo -u www-data /var/www/venv-website/bin/python3 manage.py check | ||||
| sudo -u www-data /var/www/venv-website/bin/python3 manage.py migrate | ||||
| sudo -u www-data /var/www/venv-website/bin/python3 manage.py collectstatic -v0 --noinput --link | ||||
| sudo systemctl restart apache2 | ||||
| curl --silent --head https://sfconservancy.org | grep --perl-regexp "^HTTP/.+ 200" | ||||
| EOF | ||||
|  |  | |||
|  | @ -1,36 +1,40 @@ | |||
| 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 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) | ||||
|     ) | ||||
| 
 | ||||
|     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') | ||||
| @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_path_traversal_404s(self): | ||||
|         request = self.factory.get('/about/../../settings.py') | ||||
|         with self.assertRaises(Http404): | ||||
|             views.index(request) | ||||
| 
 | ||||
| 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) | ||||
|  |  | |||
|  | @ -2,4 +2,4 @@ | |||
| Django==3.2.19 | ||||
| beautifulsoup4==4.11.2 | ||||
| html5lib==1.1 | ||||
| django_countries==7.3.2 | ||||
| django-countries==7.3.2 | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue