use crate::http::client::{self};
use crate::notification::Slack;
use crate::queue::delivery::Refused;
use crate::slack::Message;
use serde_json::json;
use crate::config::Env;
use laravel_notifications::slack::channel::*;
#[tokio::test]
async fn only_lasting_refusals_are_undeliverable() {
crate::Application::configure()
.replace_env(Env::from_iter([("SLACK_BOT_TOKEN", "xoxb-1")]))
.create()
.scope(async || {
let refusal = |error: &str| client::response().json(json!({ "ok": false, "error": error }));
for (response, undeliverable, reason) in [
(
refusal("channel_not_found"),
true,
"Slack refused the message: channel_not_found",
),
(refusal("invalid_auth"), true, "Slack refused the message: invalid_auth"),
(
refusal("ratelimited"),
false,
"Slack couldn't post the message: ratelimited",
),
(
refusal("internal_error"),
false,
"Slack couldn't post the message: internal_error",
),
(
client::response().status(429),
false,
"Slack refused the message with HTTP 429",
),
(
client::response().status(503),
false,
"Slack refused the message with HTTP 503",
),
] {
let _http = client::fake().with("slack.com/*", response);
let error = Slack::send(Route::channel("C1"), Message::new("Hi")).await.unwrap_err();
let found = error.chain().any(|source| source.is::<Refused>());
assert_eq!(found, undeliverable, "{error}");
assert_eq!(error.to_string(), reason);
}
for (status, undeliverable) in [(404, true), (400, true), (429, false), (500, false)] {
let _http = client::fake().with("hooks.slack.com/*", client::response().status(status));
let webhook = Route::webhook("https://hooks.slack.com/services/T0/B0/X");
let error = Slack::send(webhook, Message::new("Hi")).await.unwrap_err();
let found = error.chain().any(|source| source.is::<Refused>());
assert_eq!(found, undeliverable, "HTTP {status}: {error}");
}
})
.await;
}