Skip to content

Commit

Permalink
Fixed some .gists() functions, and wrote tests. (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
manchicken authored Mar 17, 2024
1 parent 388c185 commit 45d6d80
Show file tree
Hide file tree
Showing 2 changed files with 337 additions and 10 deletions.
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
);
}

0 comments on commit 45d6d80

Please sign in to comment.