Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin::on_call: Add basic send() tests #3121

Closed
wants to merge 1 commit into from
Closed
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
79 changes: 78 additions & 1 deletion 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 @@ -92,6 +92,7 @@ conduit-test = "0.9.0-alpha.4"
diesel_migrations = { version = "1.3.0", features = ["postgres"] }
hyper-tls = "0.5"
lazy_static = "1.0"
mockito = "0.28"
tokio = "1"
tower-service = "0.3.0"

Expand Down
80 changes: 79 additions & 1 deletion src/admin/on_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ impl Event {
/// If the variant is `Trigger`, this will page whoever is on call
/// (potentially waking them up at 3 AM).
pub fn send(self) -> Result<()> {
#[cfg(not(test))]
let base_url = "https://events.pagerduty.com";
#[cfg(test)]
let base_url = mockito::server_url();

let api_token = dotenv::var("PAGERDUTY_API_TOKEN")?;
let service_key = dotenv::var("PAGERDUTY_INTEGRATION_KEY")?;

let url = format!("{}/generic/2010-04-15/create_event.json", base_url);
let response = Client::new()
.post("https://events.pagerduty.com/generic/2010-04-15/create_event.json")
.post(&url)
.header(header::ACCEPT, "application/vnd.pagerduty+json;version=2")
.header(header::AUTHORIZATION, format!("Token token={}", api_token))
.json(&FullEvent {
Expand Down Expand Up @@ -66,3 +72,75 @@ struct InvalidEvent {
message: String,
errors: Vec<String>,
}

#[cfg(test)]
mod tests {
use super::Event;
use mockito::{mock, Matcher};
use std::env;

#[test]
fn test_send() {
// set environment variables for this test
env::set_var("PAGERDUTY_API_TOKEN", "secret123");
env::set_var("PAGERDUTY_INTEGRATION_KEY", "crates-io-service-key");

// setup the pagerduty API endpoint mock
let response_body = json!({
"description": "possible spam attack underway",
"event_type": "trigger",
"incident_key": "spam_attack",
"service_key": "crates-io-service-key"
});

let mock = mock("POST", "/generic/2010-04-15/create_event.json")
.match_header("Accept", "application/vnd.pagerduty+json;version=2")
.match_header("Authorization", "Token token=secret123")
.match_header("Content-Type", "application/json")
.match_body(Matcher::Json(response_body))
.with_status(200)
.create();

// create and send the event
let event = Event::Trigger {
incident_key: Some("spam_attack".into()),
description: "possible spam attack underway".into(),
};

let result = event.send();

// check that the mock endpoint was triggered
mock.assert();
assert_ok!(result);
}

#[test]
fn test_send_with_400_error() {
// set environment variables for this test
env::set_var("PAGERDUTY_API_TOKEN", "secret123");
env::set_var("PAGERDUTY_INTEGRATION_KEY", "crates-io-service-key");

// setup the pagerduty API endpoint mock
let request_body = json!({
"message": "oops",
"errors": ["something", "went", "wrong"],
});

let mock = mock("POST", "/generic/2010-04-15/create_event.json")
.with_status(400)
.with_body(request_body.to_string())
.create();

// create and send the event
let event = Event::Trigger {
incident_key: Some("spam_attack".into()),
description: "possible spam attack underway".into(),
};

let result = event.send();

// check that the mock endpoint was triggered
mock.assert();
assert_err!(result);
}
}