Skip to content

Commit 4dc6044

Browse files
committed
admin::on_call: Add basic send() tests
1 parent 2d82b5a commit 4dc6044

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed

Cargo.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ conduit-test = "0.9.0-alpha.3"
9393
diesel_migrations = { version = "1.3.0", features = ["postgres"] }
9494
hyper-tls = "0.4"
9595
lazy_static = "1.0"
96+
mockito = "0.28"
9697
tokio = { version = "0.2", default-features = false, features = ["stream"]}
9798
tower-service = "0.3.0"
9899

src/admin/on_call.rs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ impl Event {
2525
/// If the variant is `Trigger`, this will page whoever is on call
2626
/// (potentially waking them up at 3 AM).
2727
pub fn send(self) -> Result<()> {
28+
#[cfg(not(test))]
29+
let base_url = "https://events.pagerduty.com";
30+
#[cfg(test)]
31+
let base_url = mockito::server_url();
32+
2833
let api_token = dotenv::var("PAGERDUTY_API_TOKEN")?;
2934
let service_key = dotenv::var("PAGERDUTY_INTEGRATION_KEY")?;
3035

36+
let url = format!("{}/generic/2010-04-15/create_event.json", base_url);
3137
let response = Client::new()
32-
.post("https://events.pagerduty.com/generic/2010-04-15/create_event.json")
38+
.post(&url)
3339
.header(header::ACCEPT, "application/vnd.pagerduty+json;version=2")
3440
.header(header::AUTHORIZATION, format!("Token token={}", api_token))
3541
.json(&FullEvent {
@@ -66,3 +72,75 @@ struct InvalidEvent {
6672
message: String,
6773
errors: Vec<String>,
6874
}
75+
76+
#[cfg(test)]
77+
mod tests {
78+
use super::Event;
79+
use mockito::{mock, Matcher};
80+
use std::env;
81+
82+
#[test]
83+
fn test_send() {
84+
// set environment variables for this test
85+
env::set_var("PAGERDUTY_API_TOKEN", "secret123");
86+
env::set_var("PAGERDUTY_INTEGRATION_KEY", "crates-io-service-key");
87+
88+
// setup the pagerduty API endpoint mock
89+
let response_body = json!({
90+
"description": "possible spam attack underway",
91+
"event_type": "trigger",
92+
"incident_key": "spam_attack",
93+
"service_key": "crates-io-service-key"
94+
});
95+
96+
let mock = mock("POST", "/generic/2010-04-15/create_event.json")
97+
.match_header("Accept", "application/vnd.pagerduty+json;version=2")
98+
.match_header("Authorization", "Token token=secret123")
99+
.match_header("Content-Type", "application/json")
100+
.match_body(Matcher::Json(response_body))
101+
.with_status(200)
102+
.create();
103+
104+
// create and send the event
105+
let event = Event::Trigger {
106+
incident_key: Some("spam_attack".into()),
107+
description: "possible spam attack underway".into(),
108+
};
109+
110+
let result = event.send();
111+
112+
// check that the mock endpoint was triggered
113+
mock.assert();
114+
assert_ok!(result);
115+
}
116+
117+
#[test]
118+
fn test_send_with_400_error() {
119+
// set environment variables for this test
120+
env::set_var("PAGERDUTY_API_TOKEN", "secret123");
121+
env::set_var("PAGERDUTY_INTEGRATION_KEY", "crates-io-service-key");
122+
123+
// setup the pagerduty API endpoint mock
124+
let request_body = json!({
125+
"message": "oops",
126+
"errors": ["something", "went", "wrong"],
127+
});
128+
129+
let mock = mock("POST", "/generic/2010-04-15/create_event.json")
130+
.with_status(400)
131+
.with_body(request_body.to_string())
132+
.create();
133+
134+
// create and send the event
135+
let event = Event::Trigger {
136+
incident_key: Some("spam_attack".into()),
137+
description: "possible spam attack underway".into(),
138+
};
139+
140+
let result = event.send();
141+
142+
// check that the mock endpoint was triggered
143+
mock.assert();
144+
assert_err!(result);
145+
}
146+
}

0 commit comments

Comments
 (0)