Skip to content

Commit 1b8b845

Browse files
authored
Add support bot e2e tests (#960)
* Add support bot e2e tests * Make oauth2 tests work regardless of client_id
1 parent fab14bf commit 1b8b845

File tree

5 files changed

+121
-25
lines changed

5 files changed

+121
-25
lines changed

wp_api/src/wp_com/support_bots.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub struct BotMessageSummary {
117117
pub created_at: WpGmtDateTime,
118118
}
119119

120-
#[derive(Debug, PartialEq, Eq, Deserialize, uniffi::Record)]
120+
#[derive(Debug, Default, PartialEq, Eq, Deserialize, uniffi::Record)]
121121
pub struct GetBotConversationParams {
122122
// The number of the page to retrieve, limited to 100.
123123
#[uniffi(default = None)]

wp_com_e2e/src/main.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use anyhow::Result;
22
use async_trait::async_trait;
33
use clap::{Parser, Subcommand};
4-
use std::sync::Arc;
4+
use std::{sync::Arc, time::Duration};
55
use wp_api::{prelude::*, wp_com::client::WpComApiClient};
66

77
mod me_tests;
8-
mod oauth2_tests;
98
mod sites_tests;
9+
mod support_bot_tests;
1010
mod support_eligibility_test;
1111
mod support_tickets_test;
1212

@@ -47,14 +47,21 @@ async fn main() -> Result<(), anyhow::Error> {
4747
token,
4848
allow_writes,
4949
} => {
50+
// Writes to bots can take a while, so we need to increase the timeout
51+
let test_timeout = if allow_writes {
52+
Duration::from_secs(60)
53+
} else {
54+
Duration::from_secs(10)
55+
};
56+
5057
let delegate = WpApiClientDelegate {
5158
auth_provider: WpAuthenticationProvider::static_with_auth(
5259
WpAuthentication::Bearer {
5360
token: token.clone(),
5461
},
5562
)
5663
.into(),
57-
request_executor: Arc::new(ReqwestRequestExecutor::default()),
64+
request_executor: Arc::new(ReqwestRequestExecutor::new(false, test_timeout)),
5865
middleware_pipeline: Arc::new(WpApiMiddlewarePipeline::default()),
5966
app_notifier: Arc::new(EmptyAppNotifier),
6067
};
@@ -81,9 +88,9 @@ async fn run_tests(
8188
token: String,
8289
allow_writes: bool,
8390
) -> Result<(), anyhow::Error> {
84-
me_tests::me_test(client).await?;
85-
oauth2_tests::oauth2_test(client, token.clone()).await?;
91+
me_tests::me_test(client, token.clone()).await?;
8692
sites_tests::sites_test(client).await?;
93+
support_bot_tests::support_bots_test(client, allow_writes).await?;
8794
support_eligibility_test::support_eligibility_test(client).await?;
8895
support_tickets_test::support_tickets_test(client, allow_writes).await?;
8996
Ok(())

wp_com_e2e/src/me_tests.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
use wp_api::wp_com::client::WpComApiClient;
1+
use wp_api::wp_com::{client::WpComApiClient, oauth2::TokenValidationParameters};
22

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

6-
client.me().get().await?;
7-
6+
let user_info = client.me().get().await?.data;
87
println!("✅ Get Current User Info");
98

9+
if let Some(client_id) = user_info.token_client_id {
10+
println!("== OAuth 2 Token Test ==");
11+
client
12+
.oauth2()
13+
.fetch_info(&TokenValidationParameters {
14+
client_id: client_id.to_string(),
15+
token: token.clone(),
16+
})
17+
.await?;
18+
println!("✅ Get OAuth 2 Token Info");
19+
}
20+
1021
Ok(())
1122
}

wp_com_e2e/src/oauth2_tests.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::collections::HashMap;
2+
3+
use wp_api::wp_com::{
4+
client::WpComApiClient,
5+
support_bots::{
6+
AddMessageToBotConversationParams, BotConversation, BotId, ChatId,
7+
CreateBotConversationParams, GetBotConversationParams,
8+
},
9+
};
10+
11+
pub async fn support_bots_test(client: &WpComApiClient, allow_writes: bool) -> anyhow::Result<()> {
12+
let bot_id = BotId("jetpack-chat-mobile".to_string());
13+
println!("== Support Bots Test ==");
14+
15+
let conversations = client
16+
.support_bots()
17+
.get_bot_converation_list(&bot_id)
18+
.await?
19+
.data;
20+
21+
println!(
22+
"✅ Fetch Conversation List: Found {} conversations",
23+
conversations.len()
24+
);
25+
26+
for conversation in conversations {
27+
let chat_id = ChatId(conversation.chat_id);
28+
29+
if let Err(e) = client
30+
.support_bots()
31+
.get_bot_conversation(&bot_id, &chat_id, &GetBotConversationParams::default())
32+
.await
33+
{
34+
println!(
35+
"❌ Fetch Conversation: {} Error: {}",
36+
conversation.chat_id, e
37+
);
38+
return Err(e.into());
39+
} else {
40+
println!("✅ Fetch Conversation: {}", conversation.chat_id);
41+
}
42+
}
43+
44+
if allow_writes {
45+
let new_conversation = create_conversation(client, &bot_id).await?;
46+
let chat_id = ChatId(new_conversation.chat_id);
47+
add_message_to_conversation(client, &bot_id, chat_id).await?;
48+
}
49+
50+
Ok(())
51+
}
52+
53+
async fn create_conversation(
54+
client: &WpComApiClient,
55+
bot_id: &BotId,
56+
) -> anyhow::Result<BotConversation> {
57+
let new_conversation = client
58+
.support_bots()
59+
.create_bot_conversation(
60+
bot_id,
61+
&CreateBotConversationParams {
62+
message: "This is a test – it can be deleted without replying.".to_string(),
63+
user_id: None,
64+
},
65+
)
66+
.await?
67+
.data;
68+
69+
println!("✅ Create Conversation");
70+
71+
Ok(new_conversation)
72+
}
73+
74+
async fn add_message_to_conversation(
75+
client: &WpComApiClient,
76+
bot_id: &BotId,
77+
conversation_id: ChatId,
78+
) -> anyhow::Result<()> {
79+
client
80+
.support_bots()
81+
.add_message_to_bot_conversation(
82+
bot_id,
83+
&conversation_id,
84+
&AddMessageToBotConversationParams {
85+
message: "Test Message".to_string(),
86+
context: HashMap::new(),
87+
},
88+
)
89+
.await?;
90+
91+
println!("✅ Add Message to Conversation");
92+
Ok(())
93+
}

0 commit comments

Comments
 (0)