Skip to content

Commit

Permalink
Test proxied embed API (#8051)
Browse files Browse the repository at this point in the history
- Changed the url name to match the others and have the same name as the
  proxied one.
- Divide the test so is easy to test the proxied version.
  • Loading branch information
stsewd authored Mar 30, 2021
1 parent 29b6ee3 commit 28436d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
44 changes: 33 additions & 11 deletions readthedocs/embed/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


@pytest.mark.django_db
class TestEmbedAPI:
class BaseTestEmbedAPI:

@pytest.fixture(autouse=True)
def setup_method(self, settings):
Expand All @@ -36,6 +36,10 @@ def setup_method(self, settings):
settings.USE_SUBDOMAIN = True
settings.PUBLIC_DOMAIN = 'readthedocs.io'

def get(self, client, *args, **kwargs):
"""Wrapper around ``client.get`` to be overridden in the proxied api tests."""
return client.get(*args, **kwargs)

def _mock_open(self, content):
@contextmanager
def f(*args, **kwargs):
Expand Down Expand Up @@ -70,9 +74,9 @@ def test_invalid_arguments(self, client):
},
)

api_endpoint = reverse('api_embed')
api_endpoint = reverse('embed_api')
for param in query_params:
r = client.get(api_endpoint, param)
r = self.get(client, api_endpoint, param)
assert r.status_code == status.HTTP_400_BAD_REQUEST

@mock.patch('readthedocs.embed.views.build_media_storage')
Expand Down Expand Up @@ -121,9 +125,9 @@ def test_valid_arguments(self, storage_mock, client):
'section': 'title-one',
},
)
api_endpoint = reverse('api_embed')
api_endpoint = reverse('embed_api')
for param in query_params:
r = client.get(api_endpoint, param)
r = self.get(client, api_endpoint, param)
assert r.status_code == status.HTTP_200_OK

@mock.patch('readthedocs.embed.views.build_media_storage')
Expand All @@ -137,8 +141,9 @@ def test_embed_unknown_section(self, storage_mock, client):
html_file=html_file,
)

response = client.get(
reverse('api_embed'),
response = self.get(
client,
reverse('embed_api'),
{
'project': self.project.slug,
'version': self.version.slug,
Expand Down Expand Up @@ -184,8 +189,9 @@ def test_embed_sphinx(self, storage_mock, section, client):
html_file=html_file,
)

response = client.get(
reverse('api_embed'),
response = self.get(
client,
reverse('embed_api'),
{
'project': self.project.slug,
'version': self.version.slug,
Expand Down Expand Up @@ -233,8 +239,9 @@ def test_embed_mkdocs(self, storage_mock, client):
self.version.documentation_type = MKDOCS
self.version.save()

response = client.get(
reverse('api_embed'),
response = self.get(
client,
reverse('embed_api'),
{
'project': self.project.slug,
'version': self.version.slug,
Expand Down Expand Up @@ -268,3 +275,18 @@ def test_embed_mkdocs(self, storage_mock, client):

assert response.status_code == status.HTTP_200_OK
assert response.data == expected


class TestEmbedAPI(BaseTestEmbedAPI):

pass


@pytest.mark.proxito
class TestProxiedEmbedAPI(BaseTestEmbedAPI):

host = 'project.readthedocs.io'

def get(self, client, *args, **kwargs):
r = client.get(*args, HTTP_HOST=self.host, **kwargs)
return r
2 changes: 1 addition & 1 deletion readthedocs/embed/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@


urlpatterns = [
url(r'', EmbedAPI.as_view(), name='api_embed'),
url(r'', EmbedAPI.as_view(), name='embed_api'),
]

0 comments on commit 28436d4

Please sign in to comment.