diff --git a/tests/endpoints/test_pages.py b/tests/endpoints/test_pages.py index c5b333f..e12c209 100644 --- a/tests/endpoints/test_pages.py +++ b/tests/endpoints/test_pages.py @@ -72,8 +72,8 @@ class TestPagesEndpoint: def test_list_basic(self, pages_endpoint, sample_page_data): """Test basic page listing.""" - # Mock the GraphQL response - mock_response = {"data": {"pages": [sample_page_data]}} + # Mock the GraphQL response structure that matches Wiki.js schema + mock_response = {"data": {"pages": {"list": [sample_page_data]}}} pages_endpoint._post = Mock(return_value=mock_response) # Call list method @@ -93,7 +93,7 @@ class TestPagesEndpoint: def test_list_with_parameters(self, pages_endpoint, sample_page_data): """Test page listing with filter parameters.""" - mock_response = {"data": {"pages": [sample_page_data]}} + mock_response = {"data": {"pages": {"list": [sample_page_data]}}} pages_endpoint._post = Mock(return_value=mock_response) # Call with parameters @@ -157,7 +157,7 @@ class TestPagesEndpoint: def test_get_success(self, pages_endpoint, sample_page_data): """Test getting a page by ID.""" - mock_response = {"data": {"page": sample_page_data}} + mock_response = {"data": {"pages": {"single": sample_page_data}}} pages_endpoint._post = Mock(return_value=mock_response) # Call method @@ -221,7 +221,16 @@ class TestPagesEndpoint: def test_create_success(self, pages_endpoint, sample_page_create, sample_page_data): """Test creating a new page.""" - mock_response = {"data": {"createPage": sample_page_data}} + mock_response = { + "data": { + "pages": { + "create": { + "responseResult": {"succeeded": True}, + "page": sample_page_data + } + } + } + } pages_endpoint._post = Mock(return_value=mock_response) # Call method @@ -246,7 +255,16 @@ class TestPagesEndpoint: def test_create_with_dict(self, pages_endpoint, sample_page_data): """Test creating a page with dict data.""" - mock_response = {"data": {"createPage": sample_page_data}} + mock_response = { + "data": { + "pages": { + "create": { + "responseResult": {"succeeded": True}, + "page": sample_page_data + } + } + } + } pages_endpoint._post = Mock(return_value=mock_response) page_dict = { @@ -357,7 +375,7 @@ class TestPagesEndpoint: def test_search_success(self, pages_endpoint, sample_page_data): """Test searching pages.""" - mock_response = {"data": {"pages": [sample_page_data]}} + mock_response = {"data": {"pages": {"list": [sample_page_data]}}} pages_endpoint._post = Mock(return_value=mock_response) # Call method @@ -385,7 +403,7 @@ class TestPagesEndpoint: def test_get_by_tags_match_all(self, pages_endpoint, sample_page_data): """Test getting pages by tags (match all).""" - mock_response = {"data": {"pages": [sample_page_data]}} + mock_response = {"data": {"pages": {"list": [sample_page_data]}}} pages_endpoint._post = Mock(return_value=mock_response) # Call method @@ -450,7 +468,7 @@ class TestPagesEndpoint: # Mock Page constructor to raise an exception mock_page_class.side_effect = ValueError("Parsing error") - mock_response = {"data": {"pages": [sample_page_data]}} + mock_response = {"data": {"pages": {"list": [sample_page_data]}}} pages_endpoint._post = Mock(return_value=mock_response) with pytest.raises(APIError, match="Failed to parse page data"): @@ -458,7 +476,7 @@ class TestPagesEndpoint: def test_graphql_query_structure(self, pages_endpoint, sample_page_data): """Test that GraphQL queries have correct structure.""" - mock_response = {"data": {"pages": [sample_page_data]}} + mock_response = {"data": {"pages": {"list": [sample_page_data]}}} pages_endpoint._post = Mock(return_value=mock_response) # Call list method @@ -469,8 +487,8 @@ class TestPagesEndpoint: query_data = call_args[1]["json_data"] assert "query" in query_data - assert "variables" in query_data - assert "pages(" in query_data["query"] + # variables key may or may not be present depending on whether parameters were passed + assert "list(" in query_data["query"] # Updated to match new GraphQL structure assert "id" in query_data["query"] assert "title" in query_data["query"] assert "content" in query_data["query"] diff --git a/tests/test_integration.py b/tests/test_integration.py index 03fdd4d..4d6f4da 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -30,20 +30,22 @@ class TestWikiJSClientIntegration: mock_response.ok = True mock_response.json.return_value = { "data": { - "pages": [ - { - "id": 1, - "title": "Test Page", - "path": "test", - "content": "Content", - "isPublished": True, - "isPrivate": False, - "tags": [], - "locale": "en", - "createdAt": "2023-01-01T00:00:00Z", - "updatedAt": "2023-01-01T00:00:00Z", - } - ] + "pages": { + "list": [ + { + "id": 1, + "title": "Test Page", + "path": "test", + "content": "Content", + "isPublished": True, + "isPrivate": False, + "tags": [], + "locale": "en", + "createdAt": "2023-01-01T00:00:00Z", + "updatedAt": "2023-01-01T00:00:00Z", + } + ] + } } } mock_session.request.return_value = mock_response diff --git a/wikijs/client.py b/wikijs/client.py index 531e557..144c1bb 100644 --- a/wikijs/client.py +++ b/wikijs/client.py @@ -145,7 +145,7 @@ class WikiJSClient: params: Optional[Dict[str, Any]] = None, json_data: Optional[Dict[str, Any]] = None, **kwargs: Any, - ) -> Dict[str, Any]: + ) -> Any: """Make HTTP request to Wiki.js API. Args: @@ -195,7 +195,7 @@ class WikiJSClient: except requests.exceptions.RequestException as e: raise APIError(f"Request failed: {str(e)}") from e - def _handle_response(self, response: requests.Response) -> Dict[str, Any]: + def _handle_response(self, response: requests.Response) -> Any: """Handle HTTP response and extract data. Args: diff --git a/wikijs/endpoints/base.py b/wikijs/endpoints/base.py index 0887ff7..5fd6296 100644 --- a/wikijs/endpoints/base.py +++ b/wikijs/endpoints/base.py @@ -31,7 +31,7 @@ class BaseEndpoint: params: Optional[Dict[str, Any]] = None, json_data: Optional[Dict[str, Any]] = None, **kwargs: Any, - ) -> Dict[str, Any]: + ) -> Any: """Make HTTP request through the client. Args: @@ -54,7 +54,7 @@ class BaseEndpoint: def _get( self, endpoint: str, params: Optional[Dict[str, Any]] = None, **kwargs: Any - ) -> Dict[str, Any]: + ) -> Any: """Make GET request. Args: @@ -73,7 +73,7 @@ class BaseEndpoint: json_data: Optional[Dict[str, Any]] = None, params: Optional[Dict[str, Any]] = None, **kwargs: Any, - ) -> Dict[str, Any]: + ) -> Any: """Make POST request. Args: @@ -95,7 +95,7 @@ class BaseEndpoint: json_data: Optional[Dict[str, Any]] = None, params: Optional[Dict[str, Any]] = None, **kwargs: Any, - ) -> Dict[str, Any]: + ) -> Any: """Make PUT request. Args: @@ -113,7 +113,7 @@ class BaseEndpoint: def _delete( self, endpoint: str, params: Optional[Dict[str, Any]] = None, **kwargs: Any - ) -> Dict[str, Any]: + ) -> Any: """Make DELETE request. Args: diff --git a/wikijs/endpoints/pages.py b/wikijs/endpoints/pages.py index 1fa9940..306f618 100644 --- a/wikijs/endpoints/pages.py +++ b/wikijs/endpoints/pages.py @@ -84,15 +84,24 @@ class PagesEndpoint(BaseEndpoint): if order_direction not in ["ASC", "DESC"]: raise ValidationError("order_direction must be ASC or DESC") - # Build GraphQL query using actual Wiki.js schema + # Build GraphQL query with variables using actual Wiki.js schema query = """ - query { + query($limit: Int, $offset: Int, $search: String, $tags: [String], $locale: String, $authorId: Int, $orderBy: String, $orderDirection: String) { pages { - list { + list(limit: $limit, offset: $offset, search: $search, tags: $tags, locale: $locale, authorId: $authorId, orderBy: $orderBy, orderDirection: $orderDirection) { id title path + content + description isPublished + isPrivate + tags + locale + authorId + authorName + authorEmail + editor createdAt updatedAt } @@ -100,8 +109,31 @@ class PagesEndpoint(BaseEndpoint): } """ - # Make request (no variables needed for simple list query) - response = self._post("/graphql", json_data={"query": query}) + # Build variables object + variables = {} + if limit is not None: + variables["limit"] = limit + if offset is not None: + variables["offset"] = offset + if search is not None: + variables["search"] = search + if tags is not None: + variables["tags"] = tags + if locale is not None: + variables["locale"] = locale + if author_id is not None: + variables["authorId"] = author_id + if order_by is not None: + variables["orderBy"] = order_by + if order_direction is not None: + variables["orderDirection"] = order_direction + + # Make request with query and variables + json_data = {"query": query} + if variables: + json_data["variables"] = variables + + response = self._post("/graphql", json_data=json_data) # Parse response if "errors" in response: @@ -277,9 +309,29 @@ class PagesEndpoint(BaseEndpoint): # Build GraphQL mutation using actual Wiki.js schema mutation = """ - mutation($content: String!, $description: String!, $editor: String!, $isPublished: Boolean!, $isPrivate: Boolean!, $locale: String!, $path: String!, $tags: [String]!, $title: String!) { + mutation( + $content: String!, + $description: String!, + $editor: String!, + $isPublished: Boolean!, + $isPrivate: Boolean!, + $locale: String!, + $path: String!, + $tags: [String]!, + $title: String! + ) { pages { - create(content: $content, description: $description, editor: $editor, isPublished: $isPublished, isPrivate: $isPrivate, locale: $locale, path: $path, tags: $tags, title: $title) { + create( + content: $content, + description: $description, + editor: $editor, + isPublished: $isPublished, + isPrivate: $isPrivate, + locale: $locale, + path: $path, + tags: $tags, + title: $title + ) { responseResult { succeeded errorCode @@ -381,7 +433,15 @@ class PagesEndpoint(BaseEndpoint): # Build GraphQL mutation mutation = """ - mutation($id: Int!, $title: String, $content: String, $description: String, $isPublished: Boolean, $isPrivate: Boolean, $tags: [String]) { + mutation( + $id: Int!, + $title: String, + $content: String, + $description: String, + $isPublished: Boolean, + $isPrivate: Boolean, + $tags: [String] + ) { updatePage( id: $id, title: $title, @@ -602,7 +662,7 @@ class PagesEndpoint(BaseEndpoint): # Handle tags - convert from Wiki.js format if "tags" in page_data: if isinstance(page_data["tags"], list): - # Handle both formats: ["tag1", "tag2"] or [{"tag": "tag1"}, {"tag": "tag2"}] + # Handle both formats: ["tag1", "tag2"] or [{"tag": "tag1"}] tags = [] for tag in page_data["tags"]: if isinstance(tag, dict) and "tag" in tag: