Skip to content

Commit

Permalink
Add /tags related errors and their integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzkocer committed Dec 26, 2024
1 parent 90afa36 commit ab93f92
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
4 changes: 4 additions & 0 deletions wp_api/src/api_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down
1 change: 1 addition & 0 deletions wp_api_integration_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
76 changes: 76 additions & 0 deletions wp_api_integration_tests/tests/test_tags_err.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit ab93f92

Please sign in to comment.