|
2 | 2 |
|
3 | 3 | from __future__ import absolute_import, unicode_literals, division, print_function
|
4 | 4 | import mock
|
| 5 | +from mock import patch, mock_open |
5 | 6 | import django_dynamic_fixture as fixture
|
6 | 7 |
|
7 | 8 | from django.contrib.auth.models import User
|
8 | 9 | from django.test import TestCase
|
9 | 10 | from django.test.utils import override_settings
|
10 | 11 | from django.http import Http404
|
11 | 12 | from django.conf import settings
|
| 13 | +from django.urls import reverse |
12 | 14 |
|
13 | 15 | from readthedocs.rtd_tests.base import RequestFactoryTestMixin
|
14 | 16 | from readthedocs.projects import constants
|
@@ -77,6 +79,28 @@ def test_private_files_not_found(self):
|
77 | 79 | self.assertTrue('private_web_root' in str(exc.exception))
|
78 | 80 | self.assertTrue('public_web_root' not in str(exc.exception))
|
79 | 81 |
|
| 82 | + @override_settings( |
| 83 | + PYTHON_MEDIA=False, |
| 84 | + USE_SUBDOMAIN=True, |
| 85 | + PUBLIC_DOMAIN='readthedocs.io', |
| 86 | + ROOT_URLCONF=settings.SUBDOMAIN_URLCONF, |
| 87 | + ) |
| 88 | + def test_robots_txt(self): |
| 89 | + self.public.versions.update(active=True, built=True) |
| 90 | + response = self.client.get( |
| 91 | + reverse('robots_txt'), |
| 92 | + HTTP_HOST='private.readthedocs.io', |
| 93 | + ) |
| 94 | + self.assertEqual(response.status_code, 404) |
| 95 | + |
| 96 | + self.client.force_login(self.eric) |
| 97 | + response = self.client.get( |
| 98 | + reverse('robots_txt'), |
| 99 | + HTTP_HOST='private.readthedocs.io', |
| 100 | + ) |
| 101 | + # Private projects/versions always return 404 for robots.txt |
| 102 | + self.assertEqual(response.status_code, 404) |
| 103 | + |
80 | 104 |
|
81 | 105 | @override_settings(SERVE_DOCS=[constants.PRIVATE, constants.PUBLIC])
|
82 | 106 | class TestPublicDocs(BaseDocServing):
|
@@ -110,3 +134,40 @@ def test_both_files_not_found(self):
|
110 | 134 | _serve_symlink_docs(request, project=self.private, filename='/en/latest/usage.html', privacy_level='public')
|
111 | 135 | self.assertTrue('private_web_root' not in str(exc.exception))
|
112 | 136 | self.assertTrue('public_web_root' in str(exc.exception))
|
| 137 | + |
| 138 | + @override_settings( |
| 139 | + PYTHON_MEDIA=False, |
| 140 | + USE_SUBDOMAIN=True, |
| 141 | + PUBLIC_DOMAIN='readthedocs.io', |
| 142 | + ROOT_URLCONF=settings.SUBDOMAIN_URLCONF, |
| 143 | + ) |
| 144 | + def test_default_robots_txt(self): |
| 145 | + self.public.versions.update(active=True, built=True) |
| 146 | + response = self.client.get( |
| 147 | + reverse('robots_txt'), |
| 148 | + HTTP_HOST='public.readthedocs.io', |
| 149 | + ) |
| 150 | + self.assertEqual(response.status_code, 200) |
| 151 | + self.assertEqual(response.content, b'User-agent: *\nAllow: /\n') |
| 152 | + |
| 153 | + @override_settings( |
| 154 | + PYTHON_MEDIA=False, |
| 155 | + USE_SUBDOMAIN=True, |
| 156 | + PUBLIC_DOMAIN='readthedocs.io', |
| 157 | + ROOT_URLCONF=settings.SUBDOMAIN_URLCONF, |
| 158 | + ) |
| 159 | + @patch( |
| 160 | + 'builtins.open', |
| 161 | + new_callable=mock_open, |
| 162 | + read_data='My own robots.txt', |
| 163 | + ) |
| 164 | + @patch('readthedocs.core.views.serve.os') |
| 165 | + def test_custom_robots_txt(self, os_mock, open_mock): |
| 166 | + os_mock.path.exists.return_value = True |
| 167 | + self.public.versions.update(active=True, built=True) |
| 168 | + response = self.client.get( |
| 169 | + reverse('robots_txt'), |
| 170 | + HTTP_HOST='public.readthedocs.io', |
| 171 | + ) |
| 172 | + self.assertEqual(response.status_code, 200) |
| 173 | + self.assertEqual(response.content, b'My own robots.txt') |
0 commit comments