Skip to content

Commit

Permalink
Fix url parameters setting for showing
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjentix committed Oct 12, 2024
1 parent b1bbc20 commit dea6611
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 57 deletions.
11 changes: 7 additions & 4 deletions telegram_gate/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ impl State {
}

#[must_use]
pub fn delete_confirmation(allow_not_deleted_messages: bool) -> Self {
Self::DeleteConfirmation(delete_confirmation::DeleteConfirmation::test(
Self::create_displayed_resource_data(allow_not_deleted_messages),
))
pub async fn delete_confirmation(allow_not_deleted_messages: bool) -> Self {
Self::DeleteConfirmation(
delete_confirmation::DeleteConfirmation::test(Self::create_displayed_resource_data(
allow_not_deleted_messages,
))
.await,
)
}

fn create_displayed_resource_data(
Expand Down
21 changes: 11 additions & 10 deletions telegram_gate/src/state/delete_confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ pub struct DeleteConfirmation {
impl DeleteConfirmation {
/// Create a new [`DeleteConfirmation`] state for tests.
#[cfg(test)]
pub fn test(displayed_resource_data: Arc<RwLock<DisplayedResourceData>>) -> Self {
pub async fn test(displayed_resource_data: Arc<RwLock<DisplayedResourceData>>) -> Self {
let resource_name = displayed_resource_data.read().await.resource_name.clone();
Self {
record: grpc::Record {
resource: Some(grpc::Resource {
name: "Test".to_owned(),
name: resource_name,
}),
encrypted_payload: b"unused".to_vec(),
salt: b"unused".to_vec(),
Expand Down Expand Up @@ -172,14 +173,14 @@ pub mod tests {

#[test]
pub async fn help_success() {
let delete_confirmation = State::delete_confirmation(true);
let delete_confirmation = State::delete_confirmation(true).await;

test_help_success(delete_confirmation).await
}

#[test]
pub async fn start_failure() {
let delete_confirmation = State::delete_confirmation(true);
let delete_confirmation = State::delete_confirmation(true).await;
let start = Command::start();

test_unavailable_command(delete_confirmation, start).await
Expand All @@ -193,31 +194,31 @@ pub mod tests {

#[test]
pub async fn web_app_failure() {
let delete_confirmation = State::delete_confirmation(true);
let delete_confirmation = State::delete_confirmation(true).await;
let web_app = MessageBox::web_app("data".to_owned(), "button_text".to_owned());

test_unexpected_message(delete_confirmation, web_app).await
}

#[test]
pub async fn list_failure() {
let delete_confirmation = State::delete_confirmation(true);
let delete_confirmation = State::delete_confirmation(true).await;
let list = MessageBox::list();

test_unexpected_message(delete_confirmation, list).await
}

#[test]
pub async fn add_failure() {
let delete_confirmation = State::delete_confirmation(true);
let delete_confirmation = State::delete_confirmation(true).await;
let add = MessageBox::add();

test_unexpected_message(delete_confirmation, add).await
}

#[test]
pub async fn arbitrary_failure() {
let delete_confirmation = State::delete_confirmation(true);
let delete_confirmation = State::delete_confirmation(true).await;
let arbitrary = MessageBox::arbitrary("Test arbitrary message");

test_unexpected_message(delete_confirmation, arbitrary).await
Expand Down Expand Up @@ -293,15 +294,15 @@ pub mod tests {

#[test]
pub async fn show_failure() {
let delete_confirmation = State::delete_confirmation(true);
let delete_confirmation = State::delete_confirmation(true).await;
let show_button = ButtonBox::show();

test_unexpected_button(delete_confirmation, show_button).await;
}

#[test]
pub async fn delete_failure() {
let delete_confirmation = State::delete_confirmation(true);
let delete_confirmation = State::delete_confirmation(true).await;
let delete_button = ButtonBox::delete();

test_unexpected_button(delete_confirmation, delete_button).await;
Expand Down
9 changes: 5 additions & 4 deletions telegram_gate/src/state/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,15 @@ pub mod tests {
const CANCEL_MESSAGE_ID: i32 = 201;
const RESOURCE_MESSAGE_ID: i32 = 202;

let delete_confirmation = State::DeleteConfirmation(DeleteConfirmation::test(
Arc::new(RwLock::new(DisplayedResourceData::new(
let delete_confirmation = State::DeleteConfirmation(
DeleteConfirmation::test(Arc::new(RwLock::new(DisplayedResourceData::new(
teloxide::types::MessageId(REQUEST_MESSAGE_ID),
teloxide::types::MessageId(CANCEL_MESSAGE_ID),
teloxide::types::MessageId(RESOURCE_MESSAGE_ID),
"test.resource.com".to_owned(),
))),
));
))))
.await,
);

let yes_button = ButtonBox::yes();

Expand Down
60 changes: 25 additions & 35 deletions telegram_gate/src/state/resource_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,31 +165,28 @@ impl ResourceActions {
let resource_name_param = record
.resource
.as_ref()
.map(|resource| format!("resource_name={}&", resource.name));

let mut url = context
.web_app_url()
.clone()
.join("/show")
.expect("Failed to join Web App url with `/show`");
url.set_query(
resource_name_param
.map(|param| format!("resource_name={param}"))
.as_deref(),
);
url.set_query(Some(&format!("payload={payload}")));
url.set_query(Some(&format!("salt={salt}")));
.map(|resource| format!("resource_name={}&", resource.name))
.unwrap_or_default();

teloxide::types::InlineKeyboardMarkup::new([[
let keyboard = teloxide::types::InlineKeyboardMarkup::new([[
teloxide::types::InlineKeyboardButton::callback(
button::kind::Delete.to_string(),
button::kind::Delete.to_string(),
),
teloxide::types::InlineKeyboardButton::web_app(
button::kind::Show.to_string(),
teloxide::types::WebAppInfo { url },
teloxide::types::WebAppInfo {
url: context
.web_app_url()
.clone()
.join(&format!(
"/show?{resource_name_param}payload={payload}&salt={salt}",
))
.expect("Failed to join Web App url with `/show`"),
},
),
]])
]]);
keyboard
}
}

Expand Down Expand Up @@ -328,13 +325,9 @@ pub mod tests {
teloxide::types::InlineKeyboardButton::web_app(
"👀 Show",
teloxide::types::WebAppInfo {
url: {
let mut url = web_app_test_url().join("/show").unwrap();
url.set_query(Some("resource_name=test.resource.com"));
url.set_query(Some("payload=dW51c2Vk"));
url.set_query(Some("salt=dW51c2Vk"));
url
},
url: web_app_test_url()
.join("/show?resource_name=test.resource.com&payload=dW51c2Vk&salt=dW51c2Vk")
.unwrap(),
},
),
]]))
Expand Down Expand Up @@ -455,14 +448,15 @@ pub mod tests {
pub async fn from_delete_confirmation_by_no_success() {
let resource_message_id = teloxide::types::MessageId(602);

let delete_confirmation = State::DeleteConfirmation(DeleteConfirmation::test(
Arc::new(RwLock::new(DisplayedResourceData::new(
let delete_confirmation = State::DeleteConfirmation(
DeleteConfirmation::test(Arc::new(RwLock::new(DisplayedResourceData::new(
teloxide::types::MessageId(600),
teloxide::types::MessageId(602),
resource_message_id,
"test.resource.com".to_owned(),
))),
));
))))
.await,
);
let no_button = ButtonBox::no();

let mut mock_context = Context::default();
Expand Down Expand Up @@ -490,13 +484,9 @@ pub mod tests {
teloxide::types::InlineKeyboardButton::web_app(
"👀 Show",
teloxide::types::WebAppInfo {
url: {
let mut url = web_app_test_url().join("/show").unwrap();
url.set_query(Some("resource_name=test.resource.com"));
url.set_query(Some("payload=dW51c2Vk"));
url.set_query(Some("salt=dW51c2Vk"));
url
},
url: web_app_test_url()
.join("/show?resource_name=test.resource.com&payload=dW51c2Vk&salt=dW51c2Vk")
.unwrap(),
},
),
]]))
Expand Down
9 changes: 5 additions & 4 deletions telegram_gate/src/state/resources_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,15 @@ pub mod tests {
let displayed_cancel_message_id = teloxide::types::MessageId(CANCEL_MESSAGE_ID);
let displayed_resource_message_id = teloxide::types::MessageId(RESOURCE_MESSAGE_ID);

let delete_confirmation = State::DeleteConfirmation(DeleteConfirmation::test(
Arc::new(RwLock::new(DisplayedResourceData::new(
let delete_confirmation = State::DeleteConfirmation(
DeleteConfirmation::test(Arc::new(RwLock::new(DisplayedResourceData::new(
resource_request_message_id,
displayed_cancel_message_id,
displayed_resource_message_id,
"test.resource.com".to_owned(),
))),
));
))))
.await,
);

let cancel = Command::Cancel(crate::command::Cancel);

Expand Down

0 comments on commit dea6611

Please sign in to comment.