test: start adding Bearer Auth so Prometheus can connect to the scraper API

main
_ 2022-07-27 17:31:37 -05:00
parent 140434cf66
commit 96fdf642c3
1 changed files with 51 additions and 21 deletions

View File

@ -265,8 +265,9 @@ mod tests {
struct TestCase { struct TestCase {
// Inputs // Inputs
path_rest: &'static str, path_rest: &'static str,
auth_header: Option <&'static str>,
valid_key: Option <&'static str>, valid_key: Option <&'static str>,
input_key: Option <&'static str>, x_api_key: Option <&'static str>,
// Expected // Expected
expected_status: StatusCode, expected_status: StatusCode,
@ -287,9 +288,15 @@ mod tests {
x x
} }
fn input_key (&self, v: Option <&'static str>) -> Self { fn auth_header (&self, v: Option <&'static str>) -> Self {
let mut x = self.clone (); let mut x = self.clone ();
x.input_key = v; x.auth_header = v;
x
}
fn x_api_key (&self, v: Option <&'static str>) -> Self {
let mut x = self.clone ();
x.x_api_key = v;
x x
} }
@ -322,8 +329,11 @@ mod tests {
.method ("GET") .method ("GET")
.uri (format! ("http://127.0.0.1:4000/scraper/{}", self.path_rest)); .uri (format! ("http://127.0.0.1:4000/scraper/{}", self.path_rest));
if let Some (input_key) = self.input_key { if let Some (auth_header) = self.auth_header {
input = input.header ("X-ApiKey", input_key); input = input.header ("Authorization", auth_header);
}
if let Some (x_api_key) = self.x_api_key {
input = input.header ("X-ApiKey", x_api_key);
} }
let input = input.body (Body::empty ()).unwrap (); let input = input.body (Body::empty ()).unwrap ();
@ -370,7 +380,8 @@ mod tests {
let base_case = TestCase { let base_case = TestCase {
path_rest: "v1/test", path_rest: "v1/test",
valid_key: Some ("bogus"), valid_key: Some ("bogus"),
input_key: Some ("bogus"), auth_header: None,
x_api_key: Some ("bogus"),
expected_status: StatusCode::OK, expected_status: StatusCode::OK,
expected_headers: vec! [ expected_headers: vec! [
("content-type", "text/plain"), ("content-type", "text/plain"),
@ -378,21 +389,40 @@ mod tests {
expected_body: "You're valid!\n".to_string (), expected_body: "You're valid!\n".to_string (),
}; };
for case in &[ base_case
base_case.clone (), .test ().await;
base_case.path_rest ("v9999/test")
.expected (StatusCode::NOT_FOUND, strings::UNKNOWN_API_VERSION), base_case
base_case.valid_key (None) .path_rest ("v9999/test")
.expected (StatusCode::FORBIDDEN, strings::FORBIDDEN), .expected (StatusCode::NOT_FOUND, strings::UNKNOWN_API_VERSION)
base_case.input_key (Some ("borgus")) .test ().await;
.expected (StatusCode::FORBIDDEN, strings::FORBIDDEN),
base_case.path_rest ("v1/toast") base_case
.expected (StatusCode::NOT_FOUND, strings::UNKNOWN_API_ENDPOINT), .valid_key (None)
base_case.input_key (None) .expected (StatusCode::FORBIDDEN, strings::FORBIDDEN)
.expected (StatusCode::FORBIDDEN, strings::NO_API_KEY), .test ().await;
] {
case.test ().await; base_case
} .x_api_key (Some ("borgus"))
.expected (StatusCode::FORBIDDEN, strings::FORBIDDEN)
.test ().await;
base_case
.path_rest ("v1/toast")
.expected (StatusCode::NOT_FOUND, strings::UNKNOWN_API_ENDPOINT)
.test ().await;
base_case
.x_api_key (None)
.expected (StatusCode::FORBIDDEN, strings::NO_API_KEY)
.test ().await;
base_case
.x_api_key (None)
.auth_header (Some ("Bearer: bogus"))
.expected (StatusCode::FORBIDDEN, strings::NO_API_KEY)
.test ().await;
}); });
} }
} }