Fix tests
This commit is contained in:
@@ -154,27 +154,68 @@ class NextcloudClient:
|
||||
"category": note.get("category"),
|
||||
"modified": note.get("modified"),
|
||||
}
|
||||
)
|
||||
return search_results
|
||||
)
|
||||
raise e
|
||||
|
||||
def delete_webdav_resource(self, *, path: str):
|
||||
"""Delete a resource (file or directory) via WebDAV DELETE."""
|
||||
# Ensure path ends with a slash if it's a directory
|
||||
if not path.endswith('/'):
|
||||
# This is a heuristic; a more robust solution would check resource type first
|
||||
# but for the specific case of deleting the attachment directory, this is acceptable.
|
||||
path_with_slash = f"{path}/"
|
||||
else:
|
||||
path_with_slash = path
|
||||
|
||||
webdav_path = f"{self._get_webdav_base_path()}/{path_with_slash.lstrip('/')}"
|
||||
logger.info("Deleting WebDAV resource: %s", webdav_path)
|
||||
|
||||
headers = {"OCS-APIRequest": "true"}
|
||||
try:
|
||||
response = self._client.delete(webdav_path, headers=headers)
|
||||
response.raise_for_status() # Raises for 4xx/5xx status codes
|
||||
logger.info("Successfully deleted WebDAV resource '%s' (Status: %s)", webdav_path, response.status_code)
|
||||
# DELETE typically returns 204 No Content on success
|
||||
return {"status_code": response.status_code}
|
||||
|
||||
except HTTPStatusError as e:
|
||||
logger.error(
|
||||
"HTTP error deleting WebDAV resource '%s': %s",
|
||||
webdav_path,
|
||||
e,
|
||||
)
|
||||
# It's expected to get a 404 if the resource doesn't exist, which is fine.
|
||||
# We only re-raise if it's not a 404.
|
||||
if e.response.status_code != 404:
|
||||
raise e
|
||||
else:
|
||||
logger.info("Resource '%s' not found, no deletion needed.", webdav_path)
|
||||
return {"status_code": 404} # Indicate resource was not found
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Unexpected error deleting WebDAV resource '%s': %s",
|
||||
webdav_path,
|
||||
e,
|
||||
)
|
||||
raise e
|
||||
|
||||
def notes_delete_note(self, *, note_id: int):
|
||||
# First delete the note through the Notes API
|
||||
response = self._client.delete(f"/apps/notes/api/v1/notes/{note_id}")
|
||||
response.raise_for_status()
|
||||
json_response = response.json()
|
||||
|
||||
# Note that we don't try to delete the attachments directory via WebDAV
|
||||
# This is because Nextcloud Notes app does not delete the attachments
|
||||
# when a note is deleted - this is the expected behavior of the app
|
||||
# and not a bug. Attachments remain orphaned in the system.
|
||||
|
||||
# If we did try to delete them, it would fail with a 401 Unauthorized
|
||||
# or CSRF check error because WebDAV requires a different authentication
|
||||
# method for DELETE operations than for GET/PUT operations.
|
||||
|
||||
# The test_note_attachment_cleanup.py test explicitly verifies this behavior
|
||||
# to ensure our understanding is correct.
|
||||
|
||||
|
||||
# Now, attempt to delete the associated attachments directory via WebDAV
|
||||
# Add a trailing slash as suggested
|
||||
attachment_dir_path = f"Notes/.attachments.{note_id}/"
|
||||
logger.info(f"Attempting to delete attachment directory for note {note_id} via WebDAV: {attachment_dir_path}")
|
||||
try:
|
||||
self.delete_webdav_resource(path=attachment_dir_path)
|
||||
logger.info(f"Successfully attempted to delete attachment directory for note {note_id}.")
|
||||
except Exception as e:
|
||||
# Log the error but don't re-raise, as note deletion itself was successful
|
||||
logger.error(f"Failed to delete attachment directory for note {note_id}: {e}")
|
||||
|
||||
return json_response
|
||||
|
||||
# Removed incorrect get_note_attachment method that used Notes API
|
||||
@@ -200,14 +241,24 @@ class NextcloudClient:
|
||||
if not mime_type:
|
||||
mime_type = "application/octet-stream" # Default if guessing fails
|
||||
|
||||
headers = {"Content-Type": mime_type}
|
||||
headers = {"Content-Type": mime_type, "OCS-APIRequest": "true"}
|
||||
try:
|
||||
# First check if we can access WebDAV at all with current credentials
|
||||
# by checking the Notes directory
|
||||
notes_dir_path = f"{webdav_base}/Notes"
|
||||
logger.info("Testing WebDAV access to Notes directory: %s", notes_dir_path)
|
||||
|
||||
# Log details of the auth being used by the client for this specific request
|
||||
if self._client.auth:
|
||||
auth_header = self._client.auth.auth_flow(self._client.build_request("GET", notes_dir_path)).__next__().headers.get("Authorization")
|
||||
logger.info("Authorization header for PROPFIND (Notes dir): %s", auth_header if auth_header else "Not present or generated by auth flow")
|
||||
else:
|
||||
logger.info("No httpx.Auth object configured on the client for PROPFIND (Notes dir).")
|
||||
|
||||
propfind_headers = {"Depth": "0", "OCS-APIRequest": "true"}
|
||||
logger.info("Headers for PROPFIND (Notes dir): %s", propfind_headers)
|
||||
notes_dir_response = self._client.request("PROPFIND", notes_dir_path,
|
||||
headers={"Depth": "0"})
|
||||
headers=propfind_headers)
|
||||
|
||||
if notes_dir_response.status_code == 401:
|
||||
logger.error("WebDAV authentication failed for Notes directory. Please verify WebDAV permissions.")
|
||||
@@ -226,7 +277,9 @@ class NextcloudClient:
|
||||
# Ensure the parent directory exists using MKCOL
|
||||
parent_dir_path = f"{webdav_base}/Notes/.attachments.{note_id}"
|
||||
logger.info("Creating attachments directory: %s", parent_dir_path)
|
||||
mkcol_response = self._client.request("MKCOL", parent_dir_path)
|
||||
mkcol_headers = {"OCS-APIRequest": "true"}
|
||||
logger.info("Headers for MKCOL (Attachments dir): %s", mkcol_headers)
|
||||
mkcol_response = self._client.request("MKCOL", parent_dir_path, headers=mkcol_headers)
|
||||
# MKCOL should return 201 Created or 405 Method Not Allowed (if exists)
|
||||
# We can ignore 405, but raise for other errors
|
||||
if mkcol_response.status_code not in [201, 405]:
|
||||
|
||||
Reference in New Issue
Block a user