1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
/// Tests API calls related to check runs of a specific commit.
mod mock_error;
use mock_error::setup_error_handler;
use octocrab::models::checks::ListCheckRuns;
use octocrab::models::CheckRunId;
use octocrab::params::repos::Reference;
use octocrab::{Error, Octocrab};
use serde_json::{json, Value};
use wiremock::{
matchers::{method, path},
Mock, MockServer, ResponseTemplate,
};
async fn setup_api(template: ResponseTemplate) -> MockServer {
let mock_server = MockServer::start().await;
let mocked_path = "/repos/some-owner/some-repo/commits/refs/heads/some-branch/check-runs";
Mock::given(method("GET"))
.and(path(mocked_path))
.respond_with(template)
.mount(&mock_server)
.await;
setup_error_handler(
&mock_server,
&format!("GET on {mocked_path} was not received"),
)
.await;
mock_server
}
fn setup_octocrab(uri: &str) -> Octocrab {
Octocrab::builder().base_uri(uri).unwrap().build().unwrap()
}
#[tokio::test]
async fn should_return_page_with_check_runs() {
let mocked_response: ListCheckRuns =
serde_json::from_str(include_str!("resources/commit_check_runs.json")).unwrap();
let template = ResponseTemplate::new(200).set_body_json(&mocked_response);
let mock_server = setup_api(template).await;
let client = setup_octocrab(&mock_server.uri());
let result = client
.commits("some-owner", "some-repo")
.associated_check_runs(Reference::Branch("some-branch".into()))
.send()
.await;
assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);
let response = result.unwrap();
assert_eq!(response.total_count, 2);
let items = response.check_runs;
assert_eq!(items.len(), 2);
{
let item = &items[0];
assert_eq!(CheckRunId(16354767716), item.id);
assert_eq!("Cargo test on nix (ubuntu-20.04, stable)", item.name);
assert_eq!(Some("success".into()), item.conclusion);
}
{
let item = &items[1];
assert_eq!(CheckRunId(16354767496), item.id);
assert_eq!("Cargo test on nix (ubuntu-20.04, 1.68)", item.name);
assert_eq!(Some("success".into()), item.conclusion);
}
}
#[tokio::test]
async fn should_fail_when_not_found() {
let mocked_response = json!({
"documentation_url": json!("rtm"),
"errors": Value::Null,
"message": json!("Its gone")
});
let template = ResponseTemplate::new(404).set_body_json(&mocked_response);
let mock_server = setup_api(template).await;
let client = setup_octocrab(&mock_server.uri());
let result = client
.commits("some-owner", "some-repo")
.associated_check_runs(Reference::Branch("some-branch".into()))
.send()
.await;
match result.unwrap_err() {
Error::GitHub { source, .. } => {
assert_eq!(http::StatusCode::NOT_FOUND, source.status_code);
assert_eq!("Its gone", source.message);
}
other => panic!("Unexpected error: {:?}", other),
}
}
|