Exemplo n.º 1
0
class MiddlewareTests(TestCase):

    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'
        self.old_cache_get = cache.get

    def tearDown(self):
        cache.get = self.old_cache_get

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_no_cname_creation(self):
        self.assertEqual(Domain.objects.count(), 0)
        self.project = get(Project, slug='my_slug')
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST='my.valid.hostname')
        self.middleware.process_request(request)
        self.assertEqual(Domain.objects.count(), 0)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_no_readthedocs_domain(self):
        self.assertEqual(Domain.objects.count(), 0)
        self.project = get(Project, slug='pip')
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST='pip.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(Domain.objects.count(), 0)
Exemplo n.º 2
0
class MiddlewareTests(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'
        self.old_cache_get = cache.get

    def tearDown(self):
        cache.get = self.old_cache_get

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_no_cname_creation(self):
        self.assertEqual(Domain.objects.count(), 0)
        self.project = get(Project, slug='my_slug')
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST='my.valid.hostname')
        self.middleware.process_request(request)
        self.assertEqual(Domain.objects.count(), 0)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_no_readthedocs_domain(self):
        self.assertEqual(Domain.objects.count(), 0)
        self.project = get(Project, slug='pip')
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST='pip.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(Domain.objects.count(), 0)
Exemplo n.º 3
0
    def test_custom_404_page(self, os_view_mock, static_serve_mock):
        os_view_mock.path.exists.return_value = True

        self.public.versions.update(active=True, built=True)

        factory = RequestFactory()
        request = factory.get(
            '/en/latest/notfoundpage.html',
            HTTP_HOST='public.readthedocs.io',
        )

        middleware = SubdomainMiddleware()
        middleware.process_request(request)
        response = server_error_404_subdomain(request)
        self.assertEqual(response.status_code, 404)
    def test_custom_404_page(self, os_view_mock, os_serve_mock):
        os_view_mock.path.exists.return_value = True

        os_serve_mock.path.join.side_effect = os.path.join
        os_serve_mock.path.exists.return_value = True

        self.public.versions.update(active=True, built=True)

        factory = RequestFactory()
        request = factory.get(
            '/en/latest/notfoundpage.html',
            HTTP_HOST='public.readthedocs.io',
        )

        middleware = SubdomainMiddleware()
        middleware.process_request(request)
        response = server_error_404_subdomain(request)
        self.assertEqual(response.status_code, 404)
        self.assertTrue(response['X-Accel-Redirect'].endswith('/public/en/latest/404.html'))
Exemplo n.º 5
0
    def test_custom_404_page(self, os_view_mock, os_serve_mock):
        os_view_mock.path.exists.return_value = True

        os_serve_mock.path.join.side_effect = os.path.join
        os_serve_mock.path.exists.return_value = True

        self.public.versions.update(active=True, built=True)

        factory = RequestFactory()
        request = factory.get(
            '/en/latest/notfoundpage.html',
            HTTP_HOST='public.readthedocs.io',
        )

        middleware = SubdomainMiddleware()
        middleware.process_request(request)
        response = server_error_404_subdomain(request)
        self.assertEqual(response.status_code, 404)
        self.assertTrue(response['X-Accel-Redirect'].endswith(
            '/public/en/latest/404.html'))
Exemplo n.º 6
0
class MiddlewareTests(unittest.TestCase):

    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'

    def test_failey_cname(self):
        request = self.factory.get(self.url, HTTP_HOST = 'my.host.com')
        with self.assertRaises(Http404):
            ret_val = self.middleware.process_request(request)
        self.assertEqual(request.cname, True)

    def test_proper_subdomain(self):
        request = self.factory.get(self.url, HTTP_HOST = 'pip.readthedocs.org')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.subdomain_urls')
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')


    def test_proper_cname(self):
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST = 'my.valid.homename')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'my_slug')

    def test_djangome(self):
        request = self.factory.get(self.url, HTTP_HOST = 'pip.rtfd.org')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.djangome_urls')
        self.assertEqual(request.slug, 'pip')
        self.assertFalse(hasattr(request, 'subdomain'))

    @override_settings(DEBUG=True)
    def test_debug_on(self):
        request = self.factory.get(self.url, HTTP_HOST = 'doesnt.really.matter')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(ret_val, None)
Exemplo n.º 7
0
class MiddlewareTests(unittest.TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'

    def test_failey_cname(self):
        request = self.factory.get(self.url, HTTP_HOST='my.host.com')
        with self.assertRaises(Http404):
            ret_val = self.middleware.process_request(request)
        self.assertEqual(request.cname, True)

    def test_proper_subdomain(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.readthedocs.org')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.subdomain_urls')
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    def test_proper_cname(self):
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST='my.valid.homename')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'my_slug')

    def test_djangome(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.rtfd.org')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.djangome_urls')
        self.assertEqual(request.slug, 'pip')
        self.assertFalse(hasattr(request, 'subdomain'))

    @override_settings(DEBUG=True)
    def test_debug_on(self):
        request = self.factory.get(self.url, HTTP_HOST='doesnt.really.matter')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(ret_val, None)
Exemplo n.º 8
0
class MiddlewareTests(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'
        self.owner = create_user(username='******', password='******')
        self.pip = get(Project,
                       slug='pip',
                       users=[self.owner],
                       privacy_level='public')

    def test_failey_cname(self):
        request = self.factory.get(self.url, HTTP_HOST='my.host.com')
        with self.assertRaises(Http404):
            self.middleware.process_request(request)
        self.assertEqual(request.cname, True)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_subdomain(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='prod.readthedocs.org')
    def test_subdomain_different_length(self):
        request = self.factory.get(self.url,
                                   HTTP_HOST='pip.prod.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    def test_domain_object(self):
        self.domain = get(Domain, domain='docs.foobar.com', project=self.pip)

        request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.domain_object, True)
        self.assertEqual(request.slug, 'pip')

    def test_domain_object_missing(self):
        self.domain = get(Domain, domain='docs.foobar2.com', project=self.pip)
        request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
        with self.assertRaises(Http404):
            self.middleware.process_request(request)

    def test_proper_cname(self):
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST='my.valid.homename')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'my_slug')

    def test_request_header(self):
        request = self.factory.get(self.url,
                                   HTTP_HOST='some.random.com',
                                   HTTP_X_RTD_SLUG='pip')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_cname_uppercase(self):
        cache.get = lambda x: x.split('.')[0]
        request = self.factory.get(self.url, HTTP_HOST='PIP.RANDOM.COM')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'pip')

    def test_request_header_uppercase(self):
        request = self.factory.get(self.url,
                                   HTTP_HOST='some.random.com',
                                   HTTP_X_RTD_SLUG='PIP')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(USE_SUBDOMAIN=True)
    def test_use_subdomain_on(self):
        request = self.factory.get(self.url, HTTP_HOST='doesnt.really.matter')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(ret_val, None)
Exemplo n.º 9
0
class MiddlewareTests(TestCase):

    urlconf_subdomain = settings.SUBDOMAIN_URLCONF

    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'
        self.owner = create_user(username='******', password='******')
        self.pip = get(Project, slug='pip', users=[self.owner], privacy_level='public')

    def test_failey_cname(self):
        request = self.factory.get(self.url, HTTP_HOST='my.host.com')
        with self.assertRaises(Http404):
            self.middleware.process_request(request)
        self.assertEqual(request.cname, True)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_subdomain(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_restore_urlconf_after_request(self):
        """
        The urlconf attribute for the current thread
        should remain intact after each request,
        When is set to None it means 'use default from settings'.
        """
        set_urlconf(None)
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url)
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

    @override_settings(PRODUCTION_DOMAIN='prod.readthedocs.org')
    def test_subdomain_different_length(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.prod.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    def test_domain_object(self):
        self.domain = get(Domain, domain='docs.foobar.com', project=self.pip)

        request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.domain_object, True)
        self.assertEqual(request.slug, 'pip')

    def test_domain_object_missing(self):
        self.domain = get(Domain, domain='docs.foobar2.com', project=self.pip)
        request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
        with self.assertRaises(Http404):
            self.middleware.process_request(request)

    def test_proper_cname(self):
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST='my.valid.homename')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'my_slug')

    def test_request_header(self):
        request = self.factory.get(self.url, HTTP_HOST='some.random.com', HTTP_X_RTD_SLUG='pip')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_cname_uppercase(self):
        cache.get = lambda x: x.split('.')[0]
        request = self.factory.get(self.url, HTTP_HOST='PIP.RANDOM.COM')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'pip')

    def test_request_header_uppercase(self):
        request = self.factory.get(self.url, HTTP_HOST='some.random.com', HTTP_X_RTD_SLUG='PIP')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(USE_SUBDOMAIN=True)
    # no need to do a real dns query so patch cname_to_slug
    @patch('readthedocs.core.middleware.cname_to_slug', new=lambda x: 'doesnt')
    def test_use_subdomain_on(self):
        request = self.factory.get(self.url, HTTP_HOST='doesnt.really.matter')
        ret_val = self.middleware.process_request(request)
        self.assertIsNone(ret_val, None)
Exemplo n.º 10
0
class MiddlewareTests(TestCase):

    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'
        self.owner = create_user(username='******', password='******')
        self.pip = get(Project, slug='pip', users=[self.owner], privacy_level='public')

    def test_failey_cname(self):
        request = self.factory.get(self.url, HTTP_HOST='my.host.com')
        with self.assertRaises(Http404):
            self.middleware.process_request(request)
        self.assertEqual(request.cname, True)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_subdomain(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.urls.subdomain')
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='prod.readthedocs.org')
    def test_subdomain_different_length(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.prod.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.urls.subdomain')
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    def test_domain_object(self):
        self.domain = get(Domain, domain='docs.foobar.com', project=self.pip)

        request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.urls.subdomain')
        self.assertEqual(request.domain_object, True)
        self.assertEqual(request.slug, 'pip')

    def test_domain_object_missing(self):
        self.domain = get(Domain, domain='docs.foobar2.com', project=self.pip)
        request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
        with self.assertRaises(Http404):
            self.middleware.process_request(request)

    def test_proper_cname(self):
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST='my.valid.homename')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.urls.subdomain')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'my_slug')

    def test_request_header(self):
        request = self.factory.get(self.url, HTTP_HOST='some.random.com', HTTP_X_RTD_SLUG='pip')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.urls.subdomain')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_cname_uppercase(self):
        cache.get = lambda x: x.split('.')[0]
        request = self.factory.get(self.url, HTTP_HOST='PIP.RANDOM.COM')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.urls.subdomain')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'pip')

    def test_request_header_uppercase(self):
        request = self.factory.get(self.url, HTTP_HOST='some.random.com', HTTP_X_RTD_SLUG='PIP')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.urls.subdomain')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(USE_SUBDOMAIN=True)
    def test_use_subdomain_on(self):
        request = self.factory.get(self.url, HTTP_HOST='doesnt.really.matter')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(ret_val, None)
Exemplo n.º 11
0
class MiddlewareTests(TestCase):

    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'

    def test_failey_cname(self):
        request = self.factory.get(self.url, HTTP_HOST='my.host.com')
        with self.assertRaises(Http404):
            self.middleware.process_request(request)
        self.assertEqual(request.cname, True)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_subdomain(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='prod.readthedocs.org')
    def test_subdomain_different_length(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.prod.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    def test_proper_cname(self):
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST='my.valid.homename')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'my_slug')

    def test_request_header(self):
        request = self.factory.get(self.url, HTTP_HOST='some.random.com', HTTP_X_RTD_SLUG='pip')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_cname_uppercase(self):
        cache.get = lambda x: x.split('.')[0]
        request = self.factory.get(self.url, HTTP_HOST='PIP.RANDOM.COM')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'pip')

    def test_request_header_uppercase(self):
        request = self.factory.get(self.url, HTTP_HOST='some.random.com', HTTP_X_RTD_SLUG='PIP')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'readthedocs.core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(DEBUG=True)
    def test_debug_on(self):
        request = self.factory.get(self.url, HTTP_HOST='doesnt.really.matter')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(ret_val, None)
Exemplo n.º 12
0
class MiddlewareTests(TestCase):

    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'
        self.owner = create_user(username='******', password='******')
        self.pip = get(
            Project,
            slug='pip',
            users=[self.owner],
            privacy_level='public'
        )

    def process_request_auth_middleware(self, request):
        SessionMiddleware().process_request(request)
        AuthenticationMiddleware().process_request(request)

    def test_failey_cname(self):
        self.assertFalse(Domain.objects.filter(domain='my.host.com').exists())
        request = self.factory.get(self.url, HTTP_HOST='my.host.com')
        self.process_request_auth_middleware(request)
        r = self.middleware.process_request(request)
        self.assertEqual(r.status_code, 404)
        self.assertEqual(request.cname, True)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_subdomain(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.urlconf, settings.SUBDOMAIN_URLCONF)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_wrong_subdomain(self):
        http_host = 'xyz-wrong-sub-domain-xyz.readthedocs.org'
        request = self.factory.get(self.url, HTTP_HOST=http_host)
        with self.assertRaises(Http404):
            self.middleware.process_request(request)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_restore_urlconf_after_request(self):
        """
        The urlconf attribute for the current thread
        should remain intact after each request,
        When is set to None it means 'use default from settings'.
        """
        set_urlconf(None)
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url)
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

    @override_settings(PRODUCTION_DOMAIN='prod.readthedocs.org')
    def test_subdomain_different_length(self):
        request = self.factory.get(
            self.url, HTTP_HOST='pip.prod.readthedocs.org'
        )
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, settings.SUBDOMAIN_URLCONF)
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    def test_domain_object(self):
        self.domain = get(Domain, domain='docs.foobar.com', project=self.pip)

        request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, settings.SUBDOMAIN_URLCONF)
        self.assertEqual(request.domain_object, True)
        self.assertEqual(request.slug, 'pip')

    def test_domain_object_missing(self):
        self.domain = get(Domain, domain='docs.foobar2.com', project=self.pip)
        request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
        self.process_request_auth_middleware(request)
        r = self.middleware.process_request(request)
        self.assertEqual(r.status_code, 404)

    def test_request_header(self):
        request = self.factory.get(
            self.url, HTTP_HOST='some.random.com', HTTP_X_RTD_SLUG='pip'
        )
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, settings.SUBDOMAIN_URLCONF)
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_cname_uppercase(self):
        get(Domain, project=self.pip, domain='pip.random.com')
        request = self.factory.get(self.url, HTTP_HOST='PIP.RANDOM.COM')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, settings.SUBDOMAIN_URLCONF)
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'pip')

    def test_request_header_uppercase(self):
        request = self.factory.get(
            self.url, HTTP_HOST='some.random.com', HTTP_X_RTD_SLUG='PIP'
        )
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, settings.SUBDOMAIN_URLCONF)
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    def test_use_subdomain(self):
        domain = 'doesnt.exists.org'
        get(Domain, project=self.pip, domain=domain)
        request = self.factory.get(self.url, HTTP_HOST=domain)
        res = self.middleware.process_request(request)
        self.assertIsNone(res)
        self.assertEqual(request.slug, 'pip')
        self.assertTrue(request.domain_object)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_long_bad_subdomain(self):
        domain = 'www.pip.readthedocs.org'
        request = self.factory.get(self.url, HTTP_HOST=domain)
        self.process_request_auth_middleware(request)
        res = self.middleware.process_request(request)
        self.assertEqual(res.status_code, 400)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_long_subdomain(self):
        domain = 'some.long.readthedocs.org'
        request = self.factory.get(self.url, HTTP_HOST=domain)
        self.process_request_auth_middleware(request)
        res = self.middleware.process_request(request)
        self.assertIsNone(res)
Exemplo n.º 13
0
class MiddlewareTests(TestCase):

    urlconf_subdomain = settings.SUBDOMAIN_URLCONF

    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'
        self.owner = create_user(username='******', password='******')
        self.pip = get(
            Project,
            slug='pip',
            users=[self.owner],
            privacy_level='public'
        )

    def process_request_auth_middleware(self, request):
        SessionMiddleware().process_request(request)
        AuthenticationMiddleware().process_request(request)

    def test_failey_cname(self):
        self.assertFalse(Domain.objects.filter(domain='my.host.com').exists())
        request = self.factory.get(self.url, HTTP_HOST='my.host.com')
        self.process_request_auth_middleware(request)
        r = self.middleware.process_request(request)
        self.assertEqual(r.status_code, 404)
        self.assertEqual(request.cname, True)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_subdomain(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_wrong_subdomain(self):
        http_host = 'xyz-wrong-sub-domain-xyz.readthedocs.org'
        request = self.factory.get(self.url, HTTP_HOST=http_host)
        with self.assertRaises(Http404):
            self.middleware.process_request(request)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_restore_urlconf_after_request(self):
        """
        The urlconf attribute for the current thread
        should remain intact after each request,
        When is set to None it means 'use default from settings'.
        """
        set_urlconf(None)
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url)
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

        self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
        urlconf = get_urlconf()
        self.assertIsNone(urlconf)

    @override_settings(PRODUCTION_DOMAIN='prod.readthedocs.org')
    def test_subdomain_different_length(self):
        request = self.factory.get(
            self.url, HTTP_HOST='pip.prod.readthedocs.org'
        )
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    def test_domain_object(self):
        self.domain = get(Domain, domain='docs.foobar.com', project=self.pip)

        request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.domain_object, True)
        self.assertEqual(request.slug, 'pip')

    def test_domain_object_missing(self):
        self.domain = get(Domain, domain='docs.foobar2.com', project=self.pip)
        request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
        self.process_request_auth_middleware(request)
        r = self.middleware.process_request(request)
        self.assertEqual(r.status_code, 404)

    def test_request_header(self):
        request = self.factory.get(
            self.url, HTTP_HOST='some.random.com', HTTP_X_RTD_SLUG='pip'
        )
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_cname_uppercase(self):
        get(Domain, project=self.pip, domain='pip.random.com')
        request = self.factory.get(self.url, HTTP_HOST='PIP.RANDOM.COM')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'pip')

    def test_request_header_uppercase(self):
        request = self.factory.get(
            self.url, HTTP_HOST='some.random.com', HTTP_X_RTD_SLUG='PIP'
        )
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, self.urlconf_subdomain)
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    def test_use_subdomain(self):
        domain = 'doesnt.exists.org'
        get(Domain, project=self.pip, domain=domain)
        request = self.factory.get(self.url, HTTP_HOST=domain)
        res = self.middleware.process_request(request)
        self.assertIsNone(res)
        self.assertEqual(request.slug, 'pip')
        self.assertTrue(request.domain_object)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_long_bad_subdomain(self):
        domain = 'www.pip.readthedocs.org'
        request = self.factory.get(self.url, HTTP_HOST=domain)
        self.process_request_auth_middleware(request)
        res = self.middleware.process_request(request)
        self.assertEqual(res.status_code, 400)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_long_subdomain(self):
        domain = 'some.long.readthedocs.org'
        request = self.factory.get(self.url, HTTP_HOST=domain)
        self.process_request_auth_middleware(request)
        res = self.middleware.process_request(request)
        self.assertIsNone(res)
Exemplo n.º 14
0
class MiddlewareTests(unittest.TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = '/'

    def test_failey_cname(self):
        request = self.factory.get(self.url, HTTP_HOST='my.host.com')
        with self.assertRaises(Http404):
            self.middleware.process_request(request)
        self.assertEqual(request.cname, True)

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_subdomain(self):
        request = self.factory.get(self.url, HTTP_HOST='pip.readthedocs.org')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.subdomain_urls')
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, 'pip')

    def test_proper_cname(self):
        cache.get = lambda x: 'my_slug'
        request = self.factory.get(self.url, HTTP_HOST='my.valid.homename')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'my_slug')

    def test_request_header(self):
        request = self.factory.get(self.url,
                                   HTTP_HOST='some.random.com',
                                   HTTP_X_RTD_SLUG='pip')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(PRODUCTION_DOMAIN='readthedocs.org')
    def test_proper_cname_uppercase(self):
        cache.get = lambda x: x.split('.')[0]
        request = self.factory.get(self.url, HTTP_HOST='PIP.RANDOM.COM')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, 'pip')

    def test_request_header_uppercase(self):
        request = self.factory.get(self.url,
                                   HTTP_HOST='some.random.com',
                                   HTTP_X_RTD_SLUG='PIP')
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, 'core.subdomain_urls')
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, 'pip')

    @override_settings(DEBUG=True)
    def test_debug_on(self):
        request = self.factory.get(self.url, HTTP_HOST='doesnt.really.matter')
        ret_val = self.middleware.process_request(request)
        self.assertEqual(ret_val, None)
Exemplo n.º 15
0
class MiddlewareTests(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = SubdomainMiddleware()
        self.url = "/"
        self.owner = create_user(username="******", password="******")
        self.pip = get(Project, slug="pip", users=[self.owner], privacy_level="public")

    def test_failey_cname(self):
        request = self.factory.get(self.url, HTTP_HOST="my.host.com")
        with self.assertRaises(Http404):
            self.middleware.process_request(request)
        self.assertEqual(request.cname, True)

    @override_settings(PRODUCTION_DOMAIN="readthedocs.org")
    def test_proper_subdomain(self):
        request = self.factory.get(self.url, HTTP_HOST="pip.readthedocs.org")
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, "readthedocs.core.urls.subdomain")
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, "pip")

    @override_settings(PRODUCTION_DOMAIN="prod.readthedocs.org")
    def test_subdomain_different_length(self):
        request = self.factory.get(self.url, HTTP_HOST="pip.prod.readthedocs.org")
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, "readthedocs.core.urls.subdomain")
        self.assertEqual(request.subdomain, True)
        self.assertEqual(request.slug, "pip")

    def test_domain_object(self):
        self.domain = get(Domain, domain="docs.foobar.com", project=self.pip)

        request = self.factory.get(self.url, HTTP_HOST="docs.foobar.com")
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, "readthedocs.core.urls.subdomain")
        self.assertEqual(request.domain_object, True)
        self.assertEqual(request.slug, "pip")

    def test_domain_object_missing(self):
        self.domain = get(Domain, domain="docs.foobar2.com", project=self.pip)
        request = self.factory.get(self.url, HTTP_HOST="docs.foobar.com")
        with self.assertRaises(Http404):
            self.middleware.process_request(request)

    def test_proper_cname(self):
        cache.get = lambda x: "my_slug"
        request = self.factory.get(self.url, HTTP_HOST="my.valid.homename")
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, "readthedocs.core.urls.subdomain")
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, "my_slug")

    def test_request_header(self):
        request = self.factory.get(self.url, HTTP_HOST="some.random.com", HTTP_X_RTD_SLUG="pip")
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, "readthedocs.core.urls.subdomain")
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, "pip")

    @override_settings(PRODUCTION_DOMAIN="readthedocs.org")
    def test_proper_cname_uppercase(self):
        cache.get = lambda x: x.split(".")[0]
        request = self.factory.get(self.url, HTTP_HOST="PIP.RANDOM.COM")
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, "readthedocs.core.urls.subdomain")
        self.assertEqual(request.cname, True)
        self.assertEqual(request.slug, "pip")

    def test_request_header_uppercase(self):
        request = self.factory.get(self.url, HTTP_HOST="some.random.com", HTTP_X_RTD_SLUG="PIP")
        self.middleware.process_request(request)
        self.assertEqual(request.urlconf, "readthedocs.core.urls.subdomain")
        self.assertEqual(request.cname, True)
        self.assertEqual(request.rtdheader, True)
        self.assertEqual(request.slug, "pip")

    @override_settings(USE_SUBDOMAIN=True)
    def test_use_subdomain_on(self):
        request = self.factory.get(self.url, HTTP_HOST="doesnt.really.matter")
        ret_val = self.middleware.process_request(request)
        self.assertEqual(ret_val, None)