Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some .gists() functions, and wrote tests. #606

Merged
merged 2 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/api/checks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use chrono::{DateTime, Utc};
use hyper::body::Body;

use crate::models::checks::{AutoTriggerCheck, CheckSuite, CheckSuitePreferences};
use crate::models::{AppId, CheckRunId, CheckSuiteId};
Expand Down
43 changes: 33 additions & 10 deletions src/api/gists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,16 @@ impl<'octo> GistsHandler<'octo> {
/// ```
pub async fn delete(&self, gist_id: impl AsRef<str>) -> Result<()> {
let gist_id = gist_id.as_ref();
self.crab
let response = self
.crab
._delete(format!("/gists/{gist_id}"), None::<&()>)
.await
.map(|_| ())
.await?;

if response.status() != StatusCode::NOT_MODIFIED && !response.status().is_success() {
return Err(crate::map_github_error(response).await.unwrap_err());
}

Ok(())
}

/// Get a single gist revision.
Expand Down Expand Up @@ -263,7 +269,12 @@ impl<'octo> GistsHandler<'octo> {
let gist_id = gist_id.as_ref();
let response = self.crab._get(format!("/gists/{gist_id}/star")).await?;
// Gist API returns 204 (NO CONTENT) if a gist is starred
Ok(response.status() == StatusCode::NO_CONTENT)

match response.status() {
StatusCode::NO_CONTENT => Ok(true),
StatusCode::NOT_FOUND => Ok(false),
_ => Err(crate::map_github_error(response).await.unwrap_err()),
}
}

/// Star the given gist. See [GitHub API Documentation][docs] more
Expand All @@ -284,10 +295,16 @@ impl<'octo> GistsHandler<'octo> {
let gist_id = gist_id.as_ref();
// PUT here returns an empty body, ignore it since it doesn't make
// sense to deserialize it as JSON.
self.crab
let response = self
.crab
._put(format!("/gists/{gist_id}/star"), None::<&()>)
.await
.map(|_| ())
.await?;

if !response.status().is_success() {
return Err(crate::map_github_error(response).await.unwrap_err());
}

Ok(())
}

/// Unstar the given gist. See [GitHub API Documentation][docs] more
Expand All @@ -308,10 +325,16 @@ impl<'octo> GistsHandler<'octo> {
let gist_id = gist_id.as_ref();
// DELETE here returns an empty body, ignore it since it doesn't make
// sense to deserialize it as JSON.
self.crab
let response = self
.crab
._delete(format!("/gists/{gist_id}/star"), None::<&()>)
.await
.map(|_| ())
.await?;

if response.status() != StatusCode::NOT_MODIFIED && !response.status().is_success() {
return Err(crate::map_github_error(response).await.unwrap_err());
}

Ok(())
}

/// Retrieve all the gists that forked the given `gist_id`. See
Expand Down
304 changes: 304 additions & 0 deletions tests/gists_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
mod mock_error;

use mock_error::setup_error_handler;
use octocrab::Octocrab;
use wiremock::{
matchers::{method, path},
Mock, MockServer, ResponseTemplate,
};

async fn setup_get_api(template: ResponseTemplate) -> MockServer {
let gist_id: &str = "12c55a94bd03166ff33ed0596263b4c6";

let mock_server = MockServer::start().await;

Mock::given(method("GET"))
.and(path(format!("/gists/{gist_id}/star")))
.respond_with(template.clone())
.mount(&mock_server)
.await;

setup_error_handler(
&mock_server,
&format!("GET on /gists/{gist_id}/star was not received"),
)
.await;
mock_server
}

async fn setup_delete_star_api(template: ResponseTemplate) -> MockServer {
let gist_id: &str = "12c55a94bd03166ff33ed0596263b4c6";

let mock_server = MockServer::start().await;

Mock::given(method("DELETE"))
.and(path(format!("/gists/{gist_id}/star")))
.respond_with(template.clone())
.mount(&mock_server)
.await;

setup_error_handler(
&mock_server,
&format!("DELETE on /gists/{gist_id}/star was not received"),
)
.await;
mock_server
}

async fn setup_delete_gist_api(template: ResponseTemplate) -> MockServer {
let gist_id: &str = "12c55a94bd03166ff33ed0596263b4c6";

let mock_server = MockServer::start().await;

Mock::given(method("DELETE"))
.and(path(format!("/gists/{gist_id}")))
.respond_with(template.clone())
.mount(&mock_server)
.await;

setup_error_handler(
&mock_server,
&format!("DELETE on /gists/{gist_id} was not received"),
)
.await;
mock_server
}

async fn setup_put_api(template: ResponseTemplate) -> MockServer {
let gist_id: &str = "12c55a94bd03166ff33ed0596263b4c6";

let mock_server = MockServer::start().await;

Mock::given(method("PUT"))
.and(path(format!("/gists/{gist_id}/star")))
.respond_with(template.clone())
.mount(&mock_server)
.await;

setup_error_handler(
&mock_server,
&format!("PUT on /gists/{gist_id}/star was not received"),
)
.await;
mock_server
}

fn setup_octocrab(uri: &str) -> Octocrab {
Octocrab::builder().base_uri(uri).unwrap().build().unwrap()
}

const GIST_ID: &str = "12c55a94bd03166ff33ed0596263b4c6";

#[tokio::test]
async fn test_get_gists_star_204() {
let template = ResponseTemplate::new(204);
let mock_server = setup_get_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().is_starred(GIST_ID.to_owned()).await;

assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);
let result = result.unwrap();
assert!(result, "expected the result to be true: {}", result);
}

#[tokio::test]
async fn test_get_gists_star_404() {
let template = ResponseTemplate::new(404);
let mock_server = setup_get_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().is_starred(GIST_ID.to_owned()).await;

assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);
let result = result.unwrap();
assert!(!result, "expected the result to be false: {}", result);
}

#[tokio::test]
async fn test_get_gists_star_500() {
let template = ResponseTemplate::new(500);
let mock_server = setup_get_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().is_starred(GIST_ID.to_owned()).await;

assert!(
result.is_err(),
"expected error result, got success: {:#?}",
result
);
}

#[tokio::test]
async fn test_put_gists_star_204() {
let template = ResponseTemplate::new(204);
let mock_server = setup_put_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().star(GIST_ID.to_owned()).await;

assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);
}

#[tokio::test]
async fn test_put_gists_star_404() {
let template = ResponseTemplate::new(404);
let mock_server = setup_put_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().star(GIST_ID.to_owned()).await;

assert!(
result.is_err(),
"expected error result, got success: {:#?}",
result
);
}

#[tokio::test]
async fn test_put_gists_star_500() {
let template = ResponseTemplate::new(500);
let mock_server = setup_put_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().star(GIST_ID.to_owned()).await;

assert!(
result.is_err(),
"expected error result, got success: {:#?}",
result
);
}

#[tokio::test]
async fn test_delete_gists_star_204() {
let template = ResponseTemplate::new(204);
let mock_server = setup_delete_star_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().unstar(GIST_ID.to_owned()).await;

assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);
}

#[tokio::test]
async fn test_delete_gists_star_304() {
let template = ResponseTemplate::new(304);
let mock_server = setup_delete_star_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().unstar(GIST_ID.to_owned()).await;

assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);
}

#[tokio::test]
async fn test_delete_gists_star_404() {
let template = ResponseTemplate::new(404);
let mock_server = setup_delete_star_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().unstar(GIST_ID.to_owned()).await;

assert!(
result.is_err(),
"expected error result, got success: {:#?}",
result
);
}

#[tokio::test]
async fn test_delete_gists_star_500() {
let template = ResponseTemplate::new(500);
let mock_server = setup_delete_star_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().unstar(GIST_ID.to_owned()).await;

assert!(
result.is_err(),
"expected error result, got success: {:#?}",
result
);
}

#[tokio::test]
async fn test_delete_gist_204() {
let template = ResponseTemplate::new(204);
let mock_server = setup_delete_gist_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().delete(GIST_ID.to_owned()).await;

assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);
}

#[tokio::test]
async fn test_delete_gist_304() {
let template = ResponseTemplate::new(304);
let mock_server = setup_delete_gist_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().delete(GIST_ID.to_owned()).await;

assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);
}

#[tokio::test]
async fn test_delete_gist_404() {
let template = ResponseTemplate::new(404);
let mock_server = setup_delete_gist_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().delete(GIST_ID.to_owned()).await;

assert!(
result.is_err(),
"expected error result, got success: {:#?}",
result
);
}

#[tokio::test]
async fn test_delete_gist_500() {
let template = ResponseTemplate::new(500);
let mock_server = setup_delete_gist_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.gists().delete(GIST_ID.to_owned()).await;

assert!(
result.is_err(),
"expected error result, got success: {:#?}",
result
);
}
Loading