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

Feature: Add warning if index.html is missing in uploaded archive #530

Merged
merged 1 commit into from
Jun 5, 2023
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
11 changes: 7 additions & 4 deletions docat/docat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def upload(

project_base_path = DOCAT_UPLOAD_FOLDER / project
base_path = project_base_path / version
target_file = base_path / file.filename
target_file = base_path / str(file.filename)

if base_path.is_symlink():
# disallow overwriting of tags (symlinks) with new uploads
Expand All @@ -226,12 +226,12 @@ def upload(

if base_path.exists():
token_status = check_token_for_project(db, docat_api_key, project)
if token_status.valid:
remove_docs(project, version, DOCAT_UPLOAD_FOLDER)
else:
if not token_status.valid:
response.status_code = status.HTTP_401_UNAUTHORIZED
return ApiResponse(message=token_status.reason)

remove_docs(project, version, DOCAT_UPLOAD_FOLDER)

# ensure directory for the uploaded doc exists
base_path.mkdir(parents=True, exist_ok=True)

Expand All @@ -242,6 +242,9 @@ def upload(

extract_archive(target_file, base_path)

if not (base_path / "index.html").exists():
return ApiResponse(message="File successfully uploaded, but no index.html found at root of archive.")

return ApiResponse(message="File successfully uploaded")


Expand Down
18 changes: 18 additions & 0 deletions docat/tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def test_successfully_upload(client):

assert response.status_code == 201
assert response_data["message"] == "File successfully uploaded"
assert (docat.DOCAT_UPLOAD_FOLDER / "some-project" / "1.0.0" / "index.html").exists()


def test_successfully_override(client_with_claimed_project):
Expand Down Expand Up @@ -167,3 +168,20 @@ def test_upload_rejects_forbidden_project_name(client_with_claimed_project):
assert response.json() == {"message": f'Project name "{project_name}" is forbidden, as it conflicts with pages in docat web.'}

assert remove_mock.mock_calls == []


def test_upload_issues_warning_missing_index_file(client_with_claimed_project):
"""
When a project is uploaded without an index.html file,
a warning should be issued, but the upload should succeed.
"""

response = client_with_claimed_project.post(
"/api/some-project/1.0.0", files={"file": ("some-other-file.html", io.BytesIO(b"<h1>Hello World</h1>"), "plain/text")}
)
response_data = response.json()

assert response.status_code == 201
assert response_data["message"] == "File successfully uploaded, but no index.html found at root of archive."
assert (docat.DOCAT_UPLOAD_FOLDER / "some-project" / "1.0.0" / "some-other-file.html").exists()
assert not (docat.DOCAT_UPLOAD_FOLDER / "some-project" / "1.0.0" / "index.html").exists()