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