-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e5a17c
commit 561c939
Showing
11 changed files
with
26 additions
and
541 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,9 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
|
||
import flask | ||
|
||
from api.config import Configuration | ||
from api.controller.circulation_manager import CirculationManagerController | ||
from core.model import ConfigurationSetting | ||
|
||
|
||
class StaticFileController(CirculationManagerController): | ||
def static_file(self, directory, filename): | ||
max_age = ConfigurationSetting.sitewide( | ||
self._db, Configuration.STATIC_FILE_CACHE_TIME | ||
).int_value | ||
return flask.send_from_directory(directory, filename, max_age=max_age) | ||
|
||
def image(self, filename): | ||
directory = os.path.join( | ||
os.path.abspath(os.path.dirname(__file__)), | ||
"..", | ||
"..", | ||
"resources", | ||
"images", | ||
) | ||
return self.static_file(directory, filename) | ||
class StaticFileController: | ||
@staticmethod | ||
def static_file(directory, filename): | ||
return flask.send_from_directory(directory, filename) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
from werkzeug.exceptions import NotFound | ||
|
||
from api.config import Configuration | ||
from core.model import ConfigurationSetting | ||
from tests.fixtures.api_controller import CirculationControllerFixture | ||
from api.controller.static_file import StaticFileController | ||
from tests.fixtures.api_images_files import ImageFilesFixture | ||
from tests.fixtures.flask import FlaskAppFixture | ||
|
||
|
||
class TestStaticFileController: | ||
def test_static_file( | ||
self, | ||
circulation_fixture: CirculationControllerFixture, | ||
api_image_files_fixture: ImageFilesFixture, | ||
flask_app_fixture: FlaskAppFixture, | ||
): | ||
files = api_image_files_fixture | ||
cache_timeout = ConfigurationSetting.sitewide( | ||
circulation_fixture.db.session, Configuration.STATIC_FILE_CACHE_TIME | ||
) | ||
cache_timeout.value = 10 | ||
|
||
expected_content = files.sample_data("blue.jpg") | ||
with circulation_fixture.app.test_request_context("/"): | ||
response = circulation_fixture.app.manager.static_files.static_file( | ||
files.directory, "blue.jpg" | ||
) | ||
with flask_app_fixture.test_request_context(): | ||
response = StaticFileController.static_file(files.directory, "blue.jpg") | ||
|
||
assert 200 == response.status_code | ||
assert "public, max-age=10" == response.headers.get("Cache-Control") | ||
assert expected_content == response.response.file.read() | ||
assert response.status_code == 200 | ||
assert response.headers.get("Cache-Control") == "no-cache" | ||
assert response.response.file.read() == expected_content | ||
|
||
with circulation_fixture.app.test_request_context("/"): | ||
with flask_app_fixture.test_request_context(): | ||
pytest.raises( | ||
NotFound, | ||
circulation_fixture.app.manager.static_files.static_file, | ||
StaticFileController.static_file, | ||
files.directory, | ||
"missing.png", | ||
) | ||
|
||
def test_image( | ||
self, | ||
circulation_fixture: CirculationControllerFixture, | ||
resources_files_fixture: ImageFilesFixture, | ||
): | ||
files = resources_files_fixture | ||
|
||
filename = "FirstBookLoginButton280.png" | ||
expected_content = files.sample_data(filename) | ||
|
||
with circulation_fixture.app.test_request_context("/"): | ||
response = circulation_fixture.app.manager.static_files.image(filename) | ||
|
||
assert 200 == response.status_code | ||
assert expected_content == response.response.file.read() |
Oops, something went wrong.