Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a robots.txt file #180

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Release notes


## 3.2.6

* Allow user to GET their own user data
* Add a robots.txt file and allow indexing only of documentation


## 3.2.5

* Set default access token expiration time to 30 minutes
Expand Down
23 changes: 20 additions & 3 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import timedelta
import json
import os
from textwrap import dedent
from typing import Annotated, Awaitable, Callable, Union

from fastapi import Depends, FastAPI, HTTPException, status, Header, Request, Response
Expand Down Expand Up @@ -118,9 +119,7 @@ async def check_user_agent(
async def docs_redirect(
accept: Union[str, None] = Header(default="text/html")
) -> RedirectResponse:
"""
Redireciona para a documentação da API.
"""
"""Redireciona para a documentação da API."""

if accept == "application/json":
location = "/openapi.json"
Expand All @@ -132,6 +131,24 @@ async def docs_redirect(
)


@app.get("/robots.txt", include_in_schema=False)
async def robots_txt() -> Response:
"""Retorna um arquivo robots.txt para orientar crawlers e permitir
indexação somente dos caminhos de documentação.
"""
return Response(
dedent(
"""
User-agent: *
Allow: /docs$
Allow: /redoc$
Disallow: /
"""
).lstrip(),
media_type="text/plain",
)


# ## AUTH --------------------------------------------------


Expand Down
15 changes: 15 additions & 0 deletions tests/endpoints_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,18 @@ def test_docs_csp_header(client: Client, path: str):

assert response.status_code == status.HTTP_200_OK
assert response.headers.get("Content-Security-Policy", None) is not None


def test_robots_txt(client: Client):
"""Testa a presença do arquivo robots.txt

Args:
client (Client): fixture do cliente http.
"""
response = client.get("/robots.txt", headers={"Accept": "text/plain"})
content = response.text

assert response.status_code == status.HTTP_200_OK
assert "Allow: /docs$" in content
assert "Allow: /redoc$" in content
assert "Disallow: /\n" in content