Skip to content

Commit

Permalink
added challenge integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
randommm committed Mar 9, 2024
1 parent 0c0bd89 commit 83b8228
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ opt-level = 3

[dev-dependencies]
axum-test = "14"
http = "1"
61 changes: 56 additions & 5 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use axum_test::{TestServer, TestServerConfig};
use hmac::{Hmac, Mac};
use http::{HeaderName, HeaderValue};
use rust_slackbot_llm::routes::create_routes;
use serde_json::Value;
use serde_json::{json, Value};
use sha2::Sha256;
use std::{fmt::Write, time::SystemTime};

async fn new_test_app() -> TestServer {
let app = create_routes().await.unwrap();
let config = TestServerConfig::builder()
.expect_success_by_default()
.mock_transport()
.build();
let config = TestServerConfig::builder().mock_transport().build();

TestServer::new_with_config(app, config).unwrap()
}
Expand All @@ -22,3 +23,53 @@ async fn test_index() {
let expected_response: Value = "Welcome to the Rust Slackbot LLM API!".into();
response.assert_json(&expected_response);
}

#[tokio::test]
async fn test_challenge() {
let slack_signing_secret = "some_signing_secret";
std::env::set_var("SLACK_SIGNING_SECRET", slack_signing_secret);

let server = new_test_app().await;
let now = std::time::SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as f64;
let now = now.to_string();
let payload = json!({
"token": "some_token",
"challenge": "some_challenge",
"type": "url_verification",
});

let body = payload.to_string();
let body = "v0:".to_owned() + now.as_str() + ":" + body.as_str();

let mut mac = Hmac::<Sha256>::new_from_slice(slack_signing_secret.as_bytes())
.expect("HMAC can take key of any size");
mac.update(body.as_bytes());
let mac_result = mac.finalize();
let slack_signature = mac_result.into_bytes();
let slack_signature = slack_signature.iter().fold(String::new(), |mut output, b| {
let _ = write!(output, "{b:02x}");
output
});
let slack_signature = "v0=".to_owned() + slack_signature.as_str();

let response = server
.post("/v1/slack_events")
.json(&payload)
.add_header(
HeaderName::from_static("x-slack-request-timestamp"),
HeaderValue::from_str(now.as_str()).unwrap(),
)
.add_header(
HeaderName::from_static("x-slack-signature"),
HeaderValue::from_str(slack_signature.as_str()).unwrap(),
)
.await;

response.assert_json(&json!({
"challenge": "some_challenge",
}));
assert_eq!(response.status_code(), 200);
}

0 comments on commit 83b8228

Please sign in to comment.