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

Improvement/531-no-index-html-warning-in-UI #532

Merged
merged 2 commits 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
4 changes: 2 additions & 2 deletions docat/docat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,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="Documentation uploaded successfully, but no index.html found at root of archive.")

return ApiResponse(message="File successfully uploaded")
return ApiResponse(message="Documentation uploaded successfully")


@app.put("/api/{project}/{version}/tags/{new_tag}", response_model=ApiResponse, status_code=status.HTTP_201_CREATED)
Expand Down
6 changes: 3 additions & 3 deletions docat/tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_successfully_upload(client):
response_data = response.json()

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


Expand All @@ -30,7 +30,7 @@ def test_successfully_override(client_with_claimed_project):
response_data = response.json()

assert response.status_code == 201
assert response_data["message"] == "File successfully uploaded"
assert response_data["message"] == "Documentation uploaded successfully"
assert remove_mock.mock_calls == [call("some-project", "1.0.0", docat.DOCAT_UPLOAD_FOLDER)]


Expand Down Expand Up @@ -182,6 +182,6 @@ def test_upload_issues_warning_missing_index_file(client_with_claimed_project):
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 response_data["message"] == "Documentation uploaded successfully, 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()
45 changes: 22 additions & 23 deletions web/src/pages/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const okFileTypes = [
'application/x-zip-compressed'
]

export default function Upload(): JSX.Element {
export default function Upload (): JSX.Element {
reglim marked this conversation as resolved.
Show resolved Hide resolved
document.title = 'Upload | docat'

const { reload: reloadProjects } = useProjects()
Expand Down Expand Up @@ -97,37 +97,36 @@ export default function Upload(): JSX.Element {
if (!validateFile(file) || file === undefined) return

setIsUploading(true)
try {
const formData = new FormData()
formData.append('file', file)
const formData = new FormData()
formData.append('file', file)

await ProjectRepository.upload(project, version, formData)
const { success, message } = await ProjectRepository.upload(project, version, formData)

// reset the form
setProject('')
setVersion('')
setFile(undefined)
setValidation({})
showMessage({
type: 'success',
content: 'Documentation uploaded successfully',
showMs: 6000
})

// reload the projects
reloadProjects()
} catch (e) {
console.error(e)

const message = (e as { message: string }).message
if (!success) {
console.error(message)
showMessage({
type: 'error',
content: message,
showMs: 6000
})
} finally {
setIsUploading(false)
return
}

// reset the form
setProject('')
setVersion('')
setFile(undefined)
setValidation({})

showMessage({
type: 'success',
content: message,
showMs: 6000
})

reloadProjects()
setIsUploading(false)
})()
}

Expand Down
51 changes: 36 additions & 15 deletions web/src/repositories/ProjectRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,45 @@ function getProjectDocsURL (projectName: string, version: string, docsPath?: str
* @param {string} projectName Name of the project
* @param {string} version Name of the version
* @param {FormData} body Data to upload
* @returns {Promise<{ success: boolean, message: string }>} Success status and (error) message
*/
async function upload (projectName: string, version: string, body: FormData): Promise<void> {
const resp = await fetch(`/api/${projectName}/${version}`,
{
method: 'POST',
body
async function upload (projectName: string, version: string, body: FormData): Promise<{ success: boolean, message: string }> {
try {
const resp = await fetch(`/api/${projectName}/${version}`,
{
method: 'POST',
body
}
)

if (resp.ok) {
const json = await resp.json() as { message: string }
const msg = json.message
return { success: true, message: msg }
}
)

if (resp.ok) return

switch (resp.status) {
case 401:
throw new Error('Failed to upload documentation: Version already exists')
case 504:
throw new Error('Failed to upload documentation: Server unreachable')
default:
throw new Error(`Failed to upload documentation: ${(await resp.json() as { message: string }).message}`)
switch (resp.status) {
case 401:
return {
success: false,
message: 'Failed to upload documentation: Version already exists'
}
case 504:
return {
success: false,
message: 'Failed to upload documentation: Server unreachable'
}
default:
return {
success: false,
message: `Failed to upload documentation: ${(await resp.json() as { message: string }).message}`
}
}
} catch (e) {
return {
success: false,
message: `Failed to upload documentation: ${(e as { message: string }).message}}`
}
}
}

Expand Down
25 changes: 17 additions & 8 deletions web/src/tests/repositories/ProjectRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ describe('upload', () => {
const project = 'test-project'
const version = '1.0.0'

mockFetchData({})
mockFetchData({ message: 'Documentation was uploaded successfully' })

const body = new FormData()
body.append('file', new Blob([''], { type: 'text/plain' }))

await ProjectRepository.upload(project, version, body)
const { success, message } = await ProjectRepository.upload(project, version, body)

expect(global.fetch).toHaveBeenCalledTimes(1)
expect(global.fetch).toHaveBeenCalledWith(`/api/${project}/${version}`,
Expand All @@ -104,6 +104,9 @@ describe('upload', () => {
method: 'POST'
}
)

expect(success).toEqual(true)
expect(message).toEqual('Documentation was uploaded successfully')
})

test('should throw version already exists on 401 status code', async () => {
Expand All @@ -115,8 +118,10 @@ describe('upload', () => {
const body = new FormData()
body.append('file', new Blob([''], { type: 'text/plain' }))

expect(ProjectRepository.upload(project, version, body)
).rejects.toThrow('Failed to upload documentation: Version already exists')
const { success, message } = await ProjectRepository.upload(project, version, body)

expect(success).toEqual(false)
expect(message).toEqual('Failed to upload documentation: Version already exists')
})

test('should throw server unreachable on 504 status code', async () => {
Expand All @@ -128,8 +133,10 @@ describe('upload', () => {
const body = new FormData()
body.append('file', new Blob([''], { type: 'text/plain' }))

expect(ProjectRepository.upload(project, version, body)
).rejects.toThrow('Failed to upload documentation: Server unreachable')
const { success, message } = await ProjectRepository.upload(project, version, body)

expect(success).toEqual(false)
expect(message).toEqual('Failed to upload documentation: Server unreachable')
})

test('should throw error on other status code', async () => {
Expand All @@ -141,8 +148,10 @@ describe('upload', () => {
const body = new FormData()
body.append('file', new Blob([''], { type: 'text/plain' }))

expect(ProjectRepository.upload(project, version, body)
).rejects.toThrow('Failed to upload documentation: Test Error')
const { success, message } = await ProjectRepository.upload(project, version, body)

expect(success).toEqual(false)
expect(message).toEqual('Failed to upload documentation: Test Error')
})
})

Expand Down