Skip to content

Commit

Permalink
add tests for staticfiles and uploaded files middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
livioribeiro committed Jun 4, 2024
1 parent a11fe5b commit 58ba602
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/selva/web/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ async def _initialize_middleware(self):
for cls in reversed(middleware):
if not hasattr(cls, selva.di.decorator.DI_ATTRIBUTE_SERVICE):
selva.di.decorator.service(cls)

if not self.di.has(cls):
self.di.register(cls)

mid = await self.di.get(cls)
Expand Down
4 changes: 2 additions & 2 deletions tests/ext/data/memcached/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from http import HTTPStatus

import pytest
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport

from selva.configuration.defaults import default_settings
from selva.configuration.settings import Settings
Expand Down Expand Up @@ -34,7 +34,7 @@ async def test_application(application: str, database: str):

await app._lifespan_startup()

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/")

await app._lifespan_shutdown()
Expand Down
4 changes: 2 additions & 2 deletions tests/ext/data/redis/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from http import HTTPStatus

import pytest
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport

from selva.configuration.defaults import default_settings
from selva.configuration.settings import Settings
Expand Down Expand Up @@ -34,7 +34,7 @@ async def test_application(application: str, database: str):

await app._lifespan_startup()

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/")

await app._lifespan_shutdown()
Expand Down
4 changes: 2 additions & 2 deletions tests/ext/data/sqlalchemy/test_application.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from http import HTTPStatus

import pytest
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport

from selva.configuration.defaults import default_settings
from selva.configuration.settings import Settings
Expand Down Expand Up @@ -34,7 +34,7 @@ async def test_application(application: str, database: str):

await app._lifespan_startup()

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/")

assert response.status_code == HTTPStatus.OK, response.text
3 changes: 2 additions & 1 deletion tests/ext/templates/jinja/test_jinja_render.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from pathlib import Path

from selva.configuration.defaults import default_settings
Expand All @@ -18,7 +19,7 @@ async def test_render_template():


async def test_render_str():
settings = Settings(default_settings)
settings = Settings(deepcopy(default_settings))
template = JinjaTemplate(settings)
template.initialize()
result = await template.render_str("{{ variable }}", {"variable": "Jinja"})
Expand Down
12 changes: 6 additions & 6 deletions tests/ext/templates/jinja/test_jinja_response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path

from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport

from selva.configuration.defaults import default_settings
from selva.configuration.settings import Settings
Expand All @@ -21,7 +21,7 @@ async def test_render():
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/render")

assert response.status_code == 200
Expand All @@ -34,7 +34,7 @@ async def test_stream():
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/stream")

assert response.status_code == 200
Expand All @@ -46,7 +46,7 @@ async def test_define_content_type():
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/define_content_type")

assert response.status_code == 200
Expand All @@ -57,7 +57,7 @@ async def test_override_content_type():
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/override_content_type")

assert response.status_code == 200
Expand All @@ -68,7 +68,7 @@ async def test_content_type_from_response():
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/content_type_from_response")

assert response.status_code == 200
Expand Down
6 changes: 3 additions & 3 deletions tests/web/application/test_application.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from http import HTTPStatus

from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport

from selva.configuration.defaults import default_settings
from selva.configuration.settings import Settings
Expand All @@ -13,7 +13,7 @@ async def test_application():
)
app = Selva(settings)

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/")
assert response.text == "Ok"

Expand All @@ -24,6 +24,6 @@ async def test_not_found():
)
app = Selva(settings)

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/not-found")
assert response.status_code == HTTPStatus.NOT_FOUND
6 changes: 3 additions & 3 deletions tests/web/application/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from asgikit.requests import Request
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport

from selva.configuration import Settings
from selva.configuration.defaults import default_settings
Expand Down Expand Up @@ -30,9 +30,9 @@ async def test_middleware():
}
)
app = Selva(settings)
app.di.register(MyMiddleware)
# app.di.register(MyMiddleware)
await app._lifespan_startup()

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/")
assert response.text == "Middleware Ok"
1 change: 0 additions & 1 deletion tests/web/application/test_service_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ def initialize(self):
app.di.register(Service)

await app._lifespan_startup()

assert Service.startup_called
4 changes: 2 additions & 2 deletions tests/web/exception_handler/test_exception_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport

from selva.configuration import Settings
from selva.configuration.defaults import default_settings
Expand All @@ -18,6 +18,6 @@ async def test_exception_handler():
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(app=app)
client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/")
assert response.json() == {"exception": MyException.__name__}
File renamed without changes.
Empty file.
1 change: 1 addition & 0 deletions tests/web/middleware/files/resources/assets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body { display: none }
1 change: 1 addition & 0 deletions tests/web/middleware/files/resources/media/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message": "lorem ipsum"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet.
109 changes: 109 additions & 0 deletions tests/web/middleware/files/test_staticfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
from http import HTTPStatus
from pathlib import Path

import pytest
from httpx import ASGITransport, AsyncClient

from selva.configuration import Settings
from selva.configuration.defaults import default_settings
from selva.web.application import Selva
from selva.web.middleware.files import StaticFilesMiddleware

MIDDLEWARE = [f"{StaticFilesMiddleware.__module__}.{StaticFilesMiddleware.__name__}"]


@pytest.fixture(autouse=True)
def chdir_fixture(monkeypatch):
monkeypatch.chdir(Path(__file__).parent)


async def test_static_file():
settings = Settings(
default_settings
| {
"application": f"{__package__}.application",
"middleware": MIDDLEWARE,
}
)
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/static/lorem-ipsum.txt")

assert response.status_code == HTTPStatus.OK
assert "text/plain" in response.headers["Content-Type"]
assert response.text == "Lorem ipsum dolor sit amet."


async def test_static_file_mapping():
settings = Settings(
default_settings
| {
"application": f"{__package__}.application",
"middleware": MIDDLEWARE,
"staticfiles": default_settings["staticfiles"]
| {
"mappings": {
"text-file": "lorem-ipsum.txt",
},
},
}
)
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/text-file")

assert response.request.url.path == "/text-file"
assert response.status_code == HTTPStatus.OK
assert "text/plain" in response.headers["Content-Type"]
assert response.text == "Lorem ipsum dolor sit amet."


async def test_static_files_path():
settings = Settings(
default_settings
| {
"application": f"{__package__}.application",
"middleware": MIDDLEWARE,
"staticfiles": default_settings["staticfiles"]
| {
"path": "assets",
},
}
)
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/static/lorem-ipsum.txt")
assert response.status_code == HTTPStatus.NOT_FOUND

response = await client.get("http://localhost:8000/assets/lorem-ipsum.txt")
assert response.status_code == HTTPStatus.OK
assert "text/plain" in response.headers["Content-Type"]
assert response.text == "Lorem ipsum dolor sit amet."


async def test_static_files_root():
settings = Settings(
default_settings
| {
"application": f"{__package__}.application",
"middleware": MIDDLEWARE,
"staticfiles": default_settings["staticfiles"]
| {
"root": Path(__file__).parent / "resources" / "assets",
},
}
)
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/static/style.css")
assert response.status_code == HTTPStatus.OK
assert "text/css" in response.headers["Content-Type"]
assert response.text == "body { display: none }"
83 changes: 83 additions & 0 deletions tests/web/middleware/files/test_uploadedfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from http import HTTPStatus
from pathlib import Path

import pytest
from httpx import ASGITransport, AsyncClient

from selva.configuration import Settings
from selva.configuration.defaults import default_settings
from selva.web.application import Selva
from selva.web.middleware.files import UploadedFilesMiddleware

MIDDLEWARE = [f"{UploadedFilesMiddleware.__module__}.{UploadedFilesMiddleware.__name__}"]


@pytest.fixture(autouse=True)
def chdir_fixture(monkeypatch):
monkeypatch.chdir(Path(__file__).parent)


async def test_uploaded_file():
settings = Settings(
default_settings
| {
"application": f"{__package__}.application",
"middleware": MIDDLEWARE,
}
)
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/uploads/lorem-ipsum.txt")

assert response.status_code == HTTPStatus.OK
assert "text/plain" in response.headers["Content-Type"]
assert response.text == "Lorem ipsum dolor sit amet."


async def test_uploaded_files_path():
settings = Settings(
default_settings
| {
"application": f"{__package__}.application",
"middleware": MIDDLEWARE,
"uploadedfiles": default_settings["uploadedfiles"]
| {
"path": "media",
},
}
)
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/uploads/lorem-ipsum.txt")
assert response.status_code == HTTPStatus.NOT_FOUND

response = await client.get("http://localhost:8000/media/lorem-ipsum.txt")
assert response.status_code == HTTPStatus.OK
assert "text/plain" in response.headers["Content-Type"]
assert response.text == "Lorem ipsum dolor sit amet."


async def test_uploaded_files_root():
settings = Settings(
default_settings
| {
"application": f"{__package__}.application",
"middleware": MIDDLEWARE,
"uploadedfiles": default_settings["uploadedfiles"]
| {
"root": Path(__file__).parent / "resources" / "media",
},
}
)
app = Selva(settings)
await app._lifespan_startup()

client = AsyncClient(transport=ASGITransport(app=app))
response = await client.get("http://localhost:8000/uploads/data.json")
assert response.status_code == HTTPStatus.OK
assert "application/json" in response.headers["Content-Type"]
assert response.text == '{"message": "lorem ipsum"}'

0 comments on commit 58ba602

Please sign in to comment.