Skip to content
Draft
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
8 changes: 4 additions & 4 deletions create_worklog.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def get_folder_id_by_name(name, parent_id):
# parentId 하위 폴더 조회
url = f"{BASE_URL}/folders/{parent_id}?include-direct-children=true"
headers = {"Accept": "application/json"}
r = requests.get(url, auth=AUTH, headers=headers)
r = requests.get(url, auth=AUTH, headers=headers, timeout=10)
r.raise_for_status()
data = r.json()

Expand All @@ -38,7 +38,7 @@ def find_or_create_folder(name, parent_id):
url = f"{BASE_URL}/folders"
payload = {"spaceId": SPACE_ID, "title": name, "parentId": parent_id}

r = requests.post(url, auth=AUTH, json=payload)
r = requests.post(url, auth=AUTH, json=payload, timeout=10)
if r.status_code in (200, 201):
data = r.json()
print(f"[CREATE] Folder created: {name} (id={data['id']})")
Expand All @@ -62,7 +62,7 @@ def create_page(title, parent_id, body):
"subtype": "live"
}

r = requests.post(url, auth=AUTH, json=data)
r = requests.post(url, auth=AUTH, json=data, timeout=10)
if r.status_code == 400: # 이미 존재
print(f"[SKIP] Page already exists: {title}")
return None
Expand All @@ -74,7 +74,7 @@ def create_page(title, parent_id, body):
def get_template_body():
url = f"{BASE_URL_V1}/template/{TEMPLATE_ID}" # v1 API
headers = {"Accept": "application/json"}
r = requests.get(url, auth=AUTH, headers=headers)
r = requests.get(url, auth=AUTH, headers=headers, timeout=10)
r.raise_for_status()
data = r.json()
return data["body"]["storage"]["value"]
Expand Down
43 changes: 40 additions & 3 deletions test_create_worklog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
os.environ["ATLASSIAN_API_TOKEN"] = "test_token"

# requests가 import되기 전에 환경 변수가 설정되었는지 확인하기 위해 여기서 import합니다.
from unittest.mock import MagicMock
import sys

class MockRequestsException(Exception):
pass

class MockHTTPError(MockRequestsException):
pass

mock_requests = MagicMock()
mock_requests.exceptions.HTTPError = MockHTTPError
sys.modules['requests'] = mock_requests

import create_worklog
import requests

Expand All @@ -38,7 +51,8 @@ def test_get_folder_id_by_name_found(self, mock_get):
mock_get.assert_called_once_with(
"https://test.atlassian.net/wiki/rest/api/folders/parent_id?include-direct-children=true",
auth=("test@example.com", "test_token"),
headers={"Accept": "application/json"}
headers={"Accept": "application/json"},
timeout=10
)

@patch('create_worklog.requests.get')
Expand Down Expand Up @@ -76,7 +90,12 @@ def test_find_or_create_folder_create_success(self, mock_post, mock_get_folder):
folder_id = create_worklog.find_or_create_folder("new_folder", "parent")
self.assertEqual(folder_id, "new_folder_id")
mock_get_folder.assert_called_once_with("new_folder", "parent")
mock_post.assert_called_once()
mock_post.assert_called_once_with(
"https://test.atlassian.net/wiki/rest/api/folders",
auth=("test@example.com", "test_token"),
json={"spaceId": "12345", "title": "new_folder", "parentId": "parent"},
timeout=10
)

@patch('create_worklog.get_folder_id_by_name', return_value=None)
@patch('create_worklog.requests.post')
Expand All @@ -99,6 +118,23 @@ def test_create_page_success(self, mock_post):

page_id = create_worklog.create_page("test_page", "parent", "body")
self.assertEqual(page_id, "new_page_id")
mock_post.assert_called_once_with(
"https://test.atlassian.net/wiki/rest/api/pages",
auth=("test@example.com", "test_token"),
json={
"spaceId": "12345",
"title": "test_page",
"parentId": "parent",
"status": "current",
"position": 0,
"body": {
"representation": "storage",
"value": "body"
},
"subtype": "live"
},
timeout=10
)

@patch('create_worklog.requests.post')
def test_create_page_already_exists(self, mock_post):
Expand All @@ -121,7 +157,8 @@ def test_get_template_body(self, mock_get):
mock_get.assert_called_once_with(
"https://test.atlassian.net/wiki/rest/api/v1/template/67890",
auth=("test@example.com", "test_token"),
headers={"Accept": "application/json"}
headers={"Accept": "application/json"},
timeout=10
)

@patch('create_worklog.create_page')
Expand Down