forked from XAMPPRocky/octocrab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement repos/{owner}/{repo}/releases/{number}
resolves XAMPPRocky#663
- Loading branch information
1 parent
abec8a6
commit d4a035e
Showing
9 changed files
with
584 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/// Tests API calls related to check runs of a specific commit. | ||
mod mock_error; | ||
|
||
use mock_error::setup_error_handler; | ||
use octocrab::models::repos::Release; | ||
use octocrab::models::ReleaseId; | ||
use octocrab::{Error, Octocrab}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::{json, Value}; | ||
use wiremock::{ | ||
matchers::{method, path}, | ||
Mock, MockServer, ResponseTemplate, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct FakePage<T> { | ||
items: Vec<T>, | ||
} | ||
|
||
const OWNER: &str = "XAMPPRocky"; | ||
const REPO: &str = "octocrab"; | ||
|
||
async fn setup_get_api(template: ResponseTemplate, number: u64) -> MockServer { | ||
let mock_server = MockServer::start().await; | ||
|
||
Mock::given(method("GET")) | ||
.and(path(format!( | ||
"/repos/{OWNER}/{REPO}/releases/{number}" | ||
))) | ||
.respond_with(template) | ||
.mount(&mock_server) | ||
.await; | ||
setup_error_handler( | ||
&mock_server, | ||
&format!("GET on /repos/{OWNER}/{REPO}/releases/{number} 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_release_for_repository_by_id() { | ||
let number: u64 = 148681297; | ||
let mocked_response: Release = | ||
serde_json::from_str(include_str!("resources/repos_releases_get_by_id.json")).unwrap(); | ||
let template = ResponseTemplate::new(200).set_body_json(&mocked_response); | ||
let mock_server = setup_get_api(template, number).await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.repos(OWNER, REPO).releases().get(number).await; | ||
|
||
assert!( | ||
result.is_ok(), | ||
"expected successful result, got error: {:#?}", | ||
result | ||
); | ||
|
||
let release = result.unwrap(); | ||
|
||
|
||
|
||
assert_eq!(ReleaseId(number), release.id); | ||
assert_eq!("v0.37.0", release.tag_name); | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_fail_when_no_releases_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_get_api(template, 404).await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.repos(OWNER, REPO).releases().get(404).await; | ||
|
||
match result.unwrap_err() { | ||
Error::GitHub { source, .. } => { | ||
assert_eq!("Its gone", source.message) | ||
} | ||
other => panic!("Unexpected error: {:?}", other), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/// Tests API calls related to check runs of a specific commit. | ||
mod mock_error; | ||
|
||
use mock_error::setup_error_handler; | ||
use octocrab::models::repos::Release; | ||
use octocrab::models::ReleaseId; | ||
use octocrab::{Error, Octocrab}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::{json, Value}; | ||
use wiremock::{ | ||
matchers::{method, path}, | ||
Mock, MockServer, ResponseTemplate, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct FakePage<T> { | ||
items: Vec<T>, | ||
} | ||
|
||
const OWNER: &str = "XAMPPRocky"; | ||
const REPO: &str = "octocrab"; | ||
|
||
async fn setup_get_api(template: ResponseTemplate, tag: &str) -> MockServer { | ||
let mock_server = MockServer::start().await; | ||
|
||
Mock::given(method("GET")) | ||
.and(path(format!("/repos/{OWNER}/{REPO}/releases/tags/{tag}"))) | ||
.respond_with(template) | ||
.mount(&mock_server) | ||
.await; | ||
setup_error_handler( | ||
&mock_server, | ||
&format!("GET on /repos/{OWNER}/{REPO}/releases/{tag} 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_release_for_repository_by_tag() { | ||
let tag: String = "v0.37.0".to_string(); | ||
let mocked_response: Release = | ||
serde_json::from_str(include_str!("resources/repos_releases_get_by_tag.json")).unwrap(); | ||
let template = ResponseTemplate::new(200).set_body_json(&mocked_response); | ||
let mock_server = setup_get_api(template, &tag).await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.repos(OWNER, REPO).releases().get_by_tag(&tag).await; | ||
|
||
assert!( | ||
result.is_ok(), | ||
"expected successful result, got error: {:#?}", | ||
result | ||
); | ||
|
||
let release = result.unwrap(); | ||
|
||
assert_eq!(ReleaseId(148681297), release.id); | ||
assert_eq!(tag, release.tag_name); | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_fail_when_no_releases_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_get_api(template, "v0.37.0").await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client | ||
.repos(OWNER, REPO) | ||
.releases() | ||
.get_by_tag("v0.37.0") | ||
.await; | ||
|
||
match result.unwrap_err() { | ||
Error::GitHub { source, .. } => { | ||
assert_eq!("Its gone", source.message) | ||
} | ||
other => panic!("Unexpected error: {:?}", other), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/// Tests API calls related to check runs of a specific commit. | ||
mod mock_error; | ||
|
||
use mock_error::setup_error_handler; | ||
use octocrab::models::repos::Release; | ||
use octocrab::models::ReleaseId; | ||
use octocrab::{Error, Octocrab}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::{json, Value}; | ||
use wiremock::{ | ||
matchers::{method, path}, | ||
Mock, MockServer, ResponseTemplate, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct FakePage<T> { | ||
items: Vec<T>, | ||
} | ||
|
||
const OWNER: &str = "XAMPPRocky"; | ||
const REPO: &str = "octocrab"; | ||
|
||
async fn setup_get_api(template: ResponseTemplate) -> MockServer { | ||
let mock_server = MockServer::start().await; | ||
|
||
Mock::given(method("GET")) | ||
.and(path(format!("/repos/{OWNER}/{REPO}/releases/latest"))) | ||
.respond_with(template) | ||
.mount(&mock_server) | ||
.await; | ||
setup_error_handler( | ||
&mock_server, | ||
&format!("GET on /repos/{OWNER}/{REPO}/releases/latest 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_latest_release_for_repository() { | ||
let number: u64 = 150372594; | ||
let mocked_response: Release = | ||
serde_json::from_str(include_str!("resources/repos_releases_get_latest.json")).unwrap(); | ||
let template = ResponseTemplate::new(200).set_body_json(&mocked_response); | ||
let mock_server = setup_get_api(template).await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.repos(OWNER, REPO).releases().get_latest().await; | ||
|
||
assert!( | ||
result.is_ok(), | ||
"expected successful result, got error: {:#?}", | ||
result | ||
); | ||
|
||
let release = result.unwrap(); | ||
|
||
assert_eq!(ReleaseId(number), release.id); | ||
assert_eq!("v0.38.0", release.tag_name); | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_fail_when_no_releases_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_get_api(template).await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.repos(OWNER, REPO).releases().get_latest().await; | ||
|
||
match result.unwrap_err() { | ||
Error::GitHub { source, .. } => { | ||
assert_eq!("Its gone", source.message) | ||
} | ||
other => panic!("Unexpected error: {:?}", other), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/// Tests API calls related to check runs of a specific commit. | ||
mod mock_error; | ||
|
||
use mock_error::setup_error_handler; | ||
use octocrab::models::repos::Release; | ||
use octocrab::models::ReleaseId; | ||
use octocrab::{Error, Octocrab}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::{json, Value}; | ||
use wiremock::{ | ||
matchers::{method, path}, | ||
Mock, MockServer, ResponseTemplate, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct FakePage<T> { | ||
items: Vec<T>, | ||
} | ||
|
||
const OWNER: &str = "XAMPPRocky"; | ||
const REPO: &str = "octocrab"; | ||
|
||
async fn setup_get_api(template: ResponseTemplate) -> MockServer { | ||
let mock_server = MockServer::start().await; | ||
|
||
Mock::given(method("GET")) | ||
.and(path(format!( | ||
"/repos/{OWNER}/{REPO}/releases" | ||
))) | ||
.respond_with(template) | ||
.mount(&mock_server) | ||
.await; | ||
setup_error_handler( | ||
&mock_server, | ||
&format!("GET on /repos/{OWNER}/{REPO}/releases 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_releases_for_repository() { | ||
let mocked_response: Vec<Release> = | ||
serde_json::from_str(include_str!("resources/repos_releases_list.json")).unwrap(); | ||
let template = ResponseTemplate::new(200).set_body_json(&mocked_response); | ||
let mock_server = setup_get_api(template).await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.repos(OWNER, REPO).releases().list().send().await; | ||
|
||
assert!( | ||
result.is_ok(), | ||
"expected successful result, got error: {:#?}", | ||
result | ||
); | ||
|
||
let response = result.unwrap(); | ||
let items = response.items; | ||
|
||
assert_eq!(items.len(), 2); | ||
|
||
{ | ||
let item = &items[0]; | ||
|
||
assert_eq!(ReleaseId(150372594), item.id); | ||
assert_eq!("v0.38.0", item.tag_name); | ||
} | ||
|
||
{ | ||
let item = &items[1]; | ||
|
||
assert_eq!(ReleaseId(148681297), item.id); | ||
assert_eq!("v0.37.0", item.tag_name); | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_fail_when_no_releases_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_get_api(template).await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.repos(OWNER, REPO).releases().list().send().await; | ||
|
||
match result.unwrap_err() { | ||
Error::GitHub { source, .. } => { | ||
assert_eq!("Its gone", source.message) | ||
} | ||
other => panic!("Unexpected error: {:?}", other), | ||
} | ||
} |
Oops, something went wrong.