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
2 changes: 1 addition & 1 deletion wp_api/src/wp_com/support_bots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub struct BotMessageSummary {
pub created_at: WpGmtDateTime,
}

#[derive(Debug, PartialEq, Eq, Deserialize, uniffi::Record)]
#[derive(Debug, Default, PartialEq, Eq, Deserialize, uniffi::Record)]
pub struct GetBotConversationParams {
// The number of the page to retrieve, limited to 100.
#[uniffi(default = None)]
Expand Down
17 changes: 12 additions & 5 deletions wp_com_e2e/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use anyhow::Result;
use async_trait::async_trait;
use clap::{Parser, Subcommand};
use std::sync::Arc;
use std::{sync::Arc, time::Duration};
use wp_api::{prelude::*, wp_com::client::WpComApiClient};

mod me_tests;
mod oauth2_tests;
mod sites_tests;
mod support_bot_tests;
mod support_eligibility_test;
mod support_tickets_test;

Expand Down Expand Up @@ -47,14 +47,21 @@ async fn main() -> Result<(), anyhow::Error> {
token,
allow_writes,
} => {
// Writes to bots can take a while, so we need to increase the timeout
let test_timeout = if allow_writes {
Duration::from_secs(60)
} else {
Duration::from_secs(10)
};

let delegate = WpApiClientDelegate {
auth_provider: WpAuthenticationProvider::static_with_auth(
WpAuthentication::Bearer {
token: token.clone(),
},
)
.into(),
request_executor: Arc::new(ReqwestRequestExecutor::default()),
request_executor: Arc::new(ReqwestRequestExecutor::new(false, test_timeout)),
middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()),
app_notifier: Arc::new(EmptyAppNotifier),
};
Expand All @@ -81,9 +88,9 @@ async fn run_tests(
token: String,
allow_writes: bool,
) -> Result<(), anyhow::Error> {
me_tests::me_test(client).await?;
oauth2_tests::oauth2_test(client, token.clone()).await?;
me_tests::me_test(client, token.clone()).await?;
sites_tests::sites_test(client).await?;
support_bot_tests::support_bots_test(client, allow_writes).await?;
support_eligibility_test::support_eligibility_test(client).await?;
support_tickets_test::support_tickets_test(client, allow_writes).await?;
Ok(())
Expand Down
19 changes: 15 additions & 4 deletions wp_com_e2e/src/me_tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
use wp_api::wp_com::client::WpComApiClient;
use wp_api::wp_com::{client::WpComApiClient, oauth2::TokenValidationParameters};

pub async fn me_test(client: &WpComApiClient) -> anyhow::Result<()> {
pub async fn me_test(client: &WpComApiClient, token: String) -> anyhow::Result<()> {
println!("== Current User Info Test ==");

client.me().get().await?;

let user_info = client.me().get().await?.data;
println!("✅ Get Current User Info");

if let Some(client_id) = user_info.token_client_id {
println!("== OAuth 2 Token Test ==");
client
.oauth2()
.fetch_info(&TokenValidationParameters {
client_id: client_id.to_string(),
token: token.clone(),
})
.await?;
println!("✅ Get OAuth 2 Token Info");
}

Ok(())
}
15 changes: 0 additions & 15 deletions wp_com_e2e/src/oauth2_tests.rs

This file was deleted.

93 changes: 93 additions & 0 deletions wp_com_e2e/src/support_bot_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::collections::HashMap;

use wp_api::wp_com::{
client::WpComApiClient,
support_bots::{
AddMessageToBotConversationParams, BotConversation, BotId, ChatId,
CreateBotConversationParams, GetBotConversationParams,
},
};

pub async fn support_bots_test(client: &WpComApiClient, allow_writes: bool) -> anyhow::Result<()> {
let bot_id = BotId("jetpack-chat-mobile".to_string());
println!("== Support Bots Test ==");

let conversations = client
.support_bots()
.get_bot_converation_list(&bot_id)
.await?
.data;

println!(
"✅ Fetch Conversation List: Found {} conversations",
conversations.len()
);

for conversation in conversations {
let chat_id = ChatId(conversation.chat_id);

if let Err(e) = client
.support_bots()
.get_bot_conversation(&bot_id, &chat_id, &GetBotConversationParams::default())
.await
{
println!(
"❌ Fetch Conversation: {} Error: {}",
conversation.chat_id, e
);
return Err(e.into());
} else {
println!("✅ Fetch Conversation: {}", conversation.chat_id);
}
}

if allow_writes {
let new_conversation = create_conversation(client, &bot_id).await?;
let chat_id = ChatId(new_conversation.chat_id);
add_message_to_conversation(client, &bot_id, chat_id).await?;
}

Ok(())
}

async fn create_conversation(
client: &WpComApiClient,
bot_id: &BotId,
) -> anyhow::Result<BotConversation> {
let new_conversation = client
.support_bots()
.create_bot_conversation(
bot_id,
&CreateBotConversationParams {
message: "This is a test – it can be deleted without replying.".to_string(),
user_id: None,
},
)
.await?
.data;

println!("✅ Create Conversation");

Ok(new_conversation)
}

async fn add_message_to_conversation(
client: &WpComApiClient,
bot_id: &BotId,
conversation_id: ChatId,
) -> anyhow::Result<()> {
client
.support_bots()
.add_message_to_bot_conversation(
bot_id,
&conversation_id,
&AddMessageToBotConversationParams {
message: "Test Message".to_string(),
context: HashMap::new(),
},
)
.await?;

println!("✅ Add Message to Conversation");
Ok(())
}