Skip to content

Commit

Permalink
Feature: Add warning if index.html is missing in uploaded archive
Browse files Browse the repository at this point in the history
Upload now tests whether an index.html file is present, and returns the
following message if not:

"File successfully uploaded, but no index.html found at root of archive."

Fixes: #509
  • Loading branch information
reglim committed Jun 5, 2023
1 parent f5de60b commit ce56d97
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
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()

0 comments on commit ce56d97

Please sign in to comment.