Skip to content

Commit

Permalink
admin::on_call: Add basic send() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Jan 7, 2021
1 parent 13fdc94 commit 0eeeea1
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 2 deletions.
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 @@ -93,6 +93,7 @@ conduit-test = "0.9.0-alpha.4"
diesel_migrations = { version = "1.3.0", features = ["postgres"] }
hyper-tls = "0.4"
lazy_static = "1.0"
mockito = "0.28"
tokio = { version = "0.2", default-features = false, features = ["stream"]}
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);
}
}

0 comments on commit 0eeeea1

Please sign in to comment.