Skip to content
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
28 changes: 9 additions & 19 deletions atlassian/confluence/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,26 +1437,16 @@ def attach_content(
raise

if existing_attachment:
# Update existing attachment on the specific attachment ID
# Update existing attachment on the specific attachment ID. Server/Data Center
# only accepts POST on the data path below the child, whatever api_version is set.
attachment_id = existing_attachment["id"]
if self.api_version == "1.0":
# older API versions use POST on data path below the child
update_path = f"{path}/{attachment_id}/data"
response = self.post(
path=update_path,
data=data,
headers=headers,
files={"file": (name, content, content_type)},
)
else:
# newer API versions use PUT on a path derived from the attachment ID directly
update_path = f"rest/api/content/{attachment_id}"
response = self.put(
path=update_path,
data=data,
headers=headers,
files={"file": (name, content, content_type)},
)
update_path = f"{path}/{attachment_id}/data"
response = self.post(
path=update_path,
data=data,
headers=headers,
files={"file": (name, content, content_type)},
)
else:
# Create new attachment using POST
response = self.post(
Expand Down
17 changes: 17 additions & 0 deletions tests/confluence/test_confluence_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ def test_attach_content_normalizes_path_like_attachment_names(self, mock_post, m

assert mock_post.call_args.kwargs["files"] == {"file": ("diagram.png", content, "image/png")}

@pytest.mark.parametrize("api_version", [None, "latest", "1.0"], ids=["default", "latest", "1.0"])
@patch.object(ConfluenceServer, "put")
@patch.object(ConfluenceServer, "get")
@patch.object(ConfluenceServer, "post")
def test_attach_content_updates_existing_attachment_with_post(self, mock_post, mock_get, mock_put, api_version):
kwargs = {} if api_version is None else {"api_version": api_version}
confluence = ConfluenceServer(url="https://test.confluence.com", username="test", password="test", **kwargs)
content = io.BytesIO(b"new image")
mock_get.return_value = {"results": [{"id": "att-1", "title": "diagram.png"}]}
mock_post.return_value = {"id": "att-1"}

confluence.attach_content(content, "diagram.png", "image/png", page_id="123")

mock_put.assert_not_called()
assert mock_post.call_args.kwargs["path"] == "rest/api/content/123/child/attachment/att-1/data"
assert mock_post.call_args.kwargs["files"] == {"file": ("diagram.png", content, "image/png")}

def test_attach_content_rejects_empty_attachment_name(self, confluence_server):
with pytest.raises(ApiValueError, match="must contain a filename"):
confluence_server.attach_content(io.BytesIO(b"content"), "/", page_id="123")
Expand Down
Loading