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
|
use wiremock::{
matchers::{method, path},
Mock, MockServer, ResponseTemplate,
};
use mock_error::setup_error_handler;
use octocrab::models::{Author, Contributor};
use octocrab::Octocrab;
/// Unit test for calls to the `/repos/OWNER/REPO/contributors` endpoint
mod mock_error;
const OWNER: &str = "XAMPPRocky";
const REPO: &str = "octocrab";
async fn setup_api(template: ResponseTemplate) -> MockServer {
let mock_server = MockServer::start().await;
Mock::given(method("GET"))
.and(path(format!("/repos/{OWNER}/{REPO}/contributors")))
.respond_with(template)
.mount(&mock_server)
.await;
setup_error_handler(
&mock_server,
"GET on /repos/OWNER/REPO/contributors not called",
)
.await;
mock_server
}
fn setup_octocrab(uri: &str) -> Octocrab {
Octocrab::builder().base_uri(uri).unwrap().build().unwrap()
}
#[tokio::test]
async fn should_return_repo_contributors() {
let repo_contributors_response: Vec<Contributor> =
serde_json::from_str(include_str!("resources/repo_contributors.json")).unwrap();
let template = ResponseTemplate::new(200).set_body_json(&repo_contributors_response);
let mock_server = setup_api(template).await;
let client = setup_octocrab(&mock_server.uri());
let result = client
.repos(OWNER, REPO)
.list_contributors()
.anon(true)
.send()
.await;
assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);
let contributors = result.unwrap();
assert!(!contributors.items.is_empty());
let Contributor {
author: Author { login, .. },
contributions,
..
} = contributors.items.first().unwrap();
{
assert_eq!(login, "XAMPPRocky");
assert!(*contributions > 0);
}
}
|