From ab93f92a12e8c05f62fed4295c5c5f838d705b94 Mon Sep 17 00:00:00 2001 From: Oguz Kocer Date: Thu, 26 Dec 2024 14:58:18 -0500 Subject: [PATCH] Add `/tags` related errors and their integration tests --- .../kotlin/TagsEndpointTest.kt | 11 +++ wp_api/src/api_error.rs | 4 + wp_api_integration_tests/src/lib.rs | 1 + .../tests/test_tags_err.rs | 76 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 wp_api_integration_tests/tests/test_tags_err.rs diff --git a/native/kotlin/api/kotlin/src/integrationTest/kotlin/TagsEndpointTest.kt b/native/kotlin/api/kotlin/src/integrationTest/kotlin/TagsEndpointTest.kt index 2d243db3..8af1095d 100644 --- a/native/kotlin/api/kotlin/src/integrationTest/kotlin/TagsEndpointTest.kt +++ b/native/kotlin/api/kotlin/src/integrationTest/kotlin/TagsEndpointTest.kt @@ -2,10 +2,12 @@ package rs.wordpress.api.kotlin import kotlinx.coroutines.test.runTest import org.junit.jupiter.api.Test +import uniffi.wp_api.PostListParams import uniffi.wp_api.SparseTagFieldWithEditContext import uniffi.wp_api.TagCreateParams import uniffi.wp_api.TagListParams import uniffi.wp_api.TagUpdateParams +import uniffi.wp_api.WpErrorCode import uniffi.wp_api.wpAuthenticationFromUsernameAndPassword import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -104,4 +106,13 @@ class TagsEndpointTest { assertEquals("new_slug", updatedTag.slug) restoreTestServer() } + + @Test + fun testErrorTermInvalid() = runTest { + val result = + client.request { requestBuilder -> + requestBuilder.tags().retrieveWithEditContext(9999999) + } + assert(result.wpErrorCode() is WpErrorCode.TermInvalid) + } } diff --git a/wp_api/src/api_error.rs b/wp_api/src/api_error.rs index f37e2995..e9bfea4c 100644 --- a/wp_api/src/api_error.rs +++ b/wp_api/src/api_error.rs @@ -147,6 +147,8 @@ pub enum WpErrorCode { CannotRead, #[serde(rename = "rest_cannot_read_post")] CannotReadPost, + #[serde(rename = "rest_cannot_update")] + CannotUpdate, #[serde(rename = "rest_cannot_view")] CannotView, #[serde(rename = "rest_cannot_view_plugin")] @@ -213,6 +215,8 @@ pub enum WpErrorCode { PostInvalidPageNumber, #[serde(rename = "rest_taxonomy_invalid")] TaxonomyInvalid, + #[serde(rename = "rest_term_invalid")] + TermInvalid, #[serde(rename = "rest_type_invalid")] TypeInvalid, #[serde(rename = "rest_not_logged_in")] diff --git a/wp_api_integration_tests/src/lib.rs b/wp_api_integration_tests/src/lib.rs index d924a8be..1c8ccd60 100644 --- a/wp_api_integration_tests/src/lib.rs +++ b/wp_api_integration_tests/src/lib.rs @@ -62,6 +62,7 @@ pub const MEDIA_TEST_FILE_PATH: &str = "../test_media.jpg"; pub const MEDIA_TEST_FILE_CONTENT_TYPE: &str = "image/jpeg"; pub const CATEGORY_ID_1: CategoryId = CategoryId(1); pub const TAG_ID_100: TagId = TagId(100); +pub const TAG_ID_INVALID: TagId = TagId(99999999); pub const POST_TEMPLATE_SINGLE_WITH_SIDEBAR: &str = "single-with-sidebar"; pub fn api_client() -> WpApiClient { diff --git a/wp_api_integration_tests/tests/test_tags_err.rs b/wp_api_integration_tests/tests/test_tags_err.rs new file mode 100644 index 00000000..e008659f --- /dev/null +++ b/wp_api_integration_tests/tests/test_tags_err.rs @@ -0,0 +1,76 @@ +use serial_test::parallel; +use wp_api::{ + tags::{TagCreateParams, TagListParams, TagUpdateParams}, + WpErrorCode, +}; +use wp_api_integration_tests::{ + api_client, api_client_as_subscriber, AssertWpError, POST_ID_INVALID, TAG_ID_100, + TAG_ID_INVALID, +}; + +#[tokio::test] +#[parallel] +async fn create_err_cannot_create() { + api_client_as_subscriber() + .tags() + .create(&TagCreateParams { + name: "foo".to_string(), + description: None, + slug: None, + }) + .await + .assert_wp_error(WpErrorCode::CannotCreate); +} + +#[tokio::test] +#[parallel] +async fn delete_err_cannot_delete() { + api_client_as_subscriber() + .tags() + .delete(&TAG_ID_100) + .await + .assert_wp_error(WpErrorCode::CannotDelete); +} + +#[tokio::test] +#[parallel] +async fn list_err_forbidden_context() { + api_client_as_subscriber() + .tags() + .list_with_edit_context(&TagListParams::default()) + .await + .assert_wp_error(WpErrorCode::ForbiddenContext); +} + +#[tokio::test] +#[parallel] +async fn list_err_post_invalid_id() { + api_client() + .tags() + .list_with_edit_context(&TagListParams { + post: Some(POST_ID_INVALID), + ..Default::default() + }) + .await + .assert_wp_error(WpErrorCode::PostInvalidId); +} + +#[tokio::test] +#[parallel] +async fn retrieve_err_term_invalid() { + api_client() + .tags() + .retrieve_with_edit_context(&TAG_ID_INVALID) + .await + .assert_wp_error(WpErrorCode::TermInvalid); +} + +#[tokio::test] +#[parallel] +async fn update_err_cannot_update() { + api_client_as_subscriber() + .tags() + .update(&TAG_ID_100, &TagUpdateParams::default()) + .await + .assert_wp_error(WpErrorCode::CannotUpdate); +}