-
-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added /user/blocks functionality (#657)
* added /user/blocks functionality (is_blocked, block_user, unblock_user, list_blocked) * Update issues.rs (#634) * fix(builder): Change add_retry_config signature to match others in OctocrabBuilder (#643) * Fix issue #635 (#637) --------- Co-authored-by: Artur Yurii Korchynskyi <[email protected]> Co-authored-by: Kleo Davidson <[email protected]> Co-authored-by: Hans Avontuur <[email protected]>
- Loading branch information
1 parent
130a6e7
commit 736ccc1
Showing
5 changed files
with
320 additions
and
6 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,57 @@ | ||
use crate::api::users::UserHandler; | ||
use crate::models; | ||
|
||
#[derive(serde::Serialize)] | ||
pub struct BlockedUsersBuilder<'octo, 'b> { | ||
#[serde(skip)] | ||
handler: &'b UserHandler<'octo>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
per_page: Option<u8>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
page: Option<u32>, | ||
} | ||
|
||
impl<'octo, 'b> BlockedUsersBuilder<'octo, 'b> { | ||
pub(crate) fn new(handler: &'b UserHandler<'octo>) -> Self { | ||
Self { | ||
handler, | ||
per_page: None, | ||
page: None, | ||
} | ||
} | ||
|
||
/// Results per page (max 100). | ||
pub fn per_page(mut self, per_page: impl Into<u8>) -> Self { | ||
self.per_page = Some(per_page.into()); | ||
self | ||
} | ||
|
||
/// Page number of the results to fetch. | ||
pub fn page(mut self, page: impl Into<u32>) -> Self { | ||
self.page = Some(page.into()); | ||
self | ||
} | ||
|
||
///## List users blocked by the authenticated user | ||
///works with the following token types: | ||
///[GitHub App user access tokens](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app) | ||
///[Fine-grained personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token) | ||
/// | ||
///The token must have the following permission set: `blocking:read` | ||
/// | ||
///```no_run | ||
/// use octocrab::models::SimpleUser; | ||
/// async fn run() -> octocrab::Result<Vec<SimpleUser>> { | ||
/// let blocked_users = octocrab::instance() | ||
/// .users("current_user") | ||
/// .blocks() | ||
/// .per_page(42).page(3u32) | ||
/// .list() | ||
/// .await?; | ||
/// Ok(blocked_users.items) | ||
/// } | ||
pub async fn list(&self) -> crate::Result<crate::Page<models::SimpleUser>> { | ||
let route = "/user/blocks".to_string(); | ||
self.handler.crab.get(route, None::<&()>).await | ||
} | ||
} |
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,22 @@ | ||
[ | ||
{ | ||
"login": "octocat", | ||
"id": 1, | ||
"node_id": "MDQ6VXNlcjE=", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"html_url": "https://github.com/octocat", | ||
"followers_url": "https://api.github.com/users/octocat/followers", | ||
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | ||
"organizations_url": "https://api.github.com/users/octocat/orgs", | ||
"repos_url": "https://api.github.com/users/octocat/repos", | ||
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/octocat/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
} | ||
] |
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,119 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use wiremock::{ | ||
matchers::{method, path}, | ||
Mock, MockServer, ResponseTemplate, | ||
}; | ||
|
||
use mock_error::setup_error_handler; | ||
use octocrab::models::SimpleUser; | ||
use octocrab::Octocrab; | ||
|
||
/// Tests API calls related to check runs of a specific commit. | ||
mod mock_error; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct FakePage<T> { | ||
items: Vec<T>, | ||
} | ||
|
||
const NOT_BLOCKED: &str = "XAMPPRocky"; | ||
|
||
async fn setup_blocks_mock( | ||
http_method: &str, | ||
mocked_path: &str, | ||
template: ResponseTemplate, | ||
) -> MockServer { | ||
let mock_server = MockServer::start().await; | ||
|
||
Mock::given(method(http_method)) | ||
.and(path(mocked_path)) | ||
.respond_with(template.clone()) | ||
.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_list_of_blocked_by_user() { | ||
let mocked_response: Vec<SimpleUser> = | ||
serde_json::from_str(include_str!("resources/user_blocks.json")).unwrap(); | ||
let template = ResponseTemplate::new(200).set_body_json(&mocked_response); | ||
let mock_server = setup_blocks_mock("GET", "/user/blocks", template).await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.users("some-user").blocks().per_page(10).list().await; | ||
|
||
assert!( | ||
result.is_ok(), | ||
"expected successful result, got error: {:#?}", | ||
result | ||
); | ||
|
||
let response = result.unwrap(); | ||
let items = response.items; | ||
|
||
assert_eq!(items.len(), 1); | ||
|
||
{ | ||
let item = &items[0]; | ||
|
||
assert_eq!("octocat", item.login); | ||
assert_eq!( | ||
"https://api.github.com/users/octocat/received_events", | ||
item.received_events_url.as_str() | ||
); | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_check_if_user_blocked() { | ||
/* status 204 for blocked */ | ||
let template = ResponseTemplate::new(200); | ||
let mock_server = setup_blocks_mock( | ||
"GET", | ||
format!("/user/blocks/{}", NOT_BLOCKED).as_str(), | ||
template, | ||
) | ||
.await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.users("some-user").is_blocked(NOT_BLOCKED).await; | ||
assert!(!result.is_ok_and(|is_blocked| is_blocked)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_respond_user_blocked() { | ||
/* status 204 for blocked */ | ||
let template = ResponseTemplate::new(204); | ||
let mock_server = setup_blocks_mock( | ||
"PUT", | ||
format!("/user/blocks/{}", NOT_BLOCKED).as_str(), | ||
template, | ||
) | ||
.await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.users("some-user").block_user(NOT_BLOCKED).await; | ||
assert!(result.is_ok()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_respond_user_unblocked() { | ||
/* status 204 for unblocked */ | ||
let template = ResponseTemplate::new(200); | ||
let mock_server = setup_blocks_mock( | ||
"DELETE", | ||
format!("/user/blocks/{}", NOT_BLOCKED).as_str(), | ||
template, | ||
) | ||
.await; | ||
let client = setup_octocrab(&mock_server.uri()); | ||
let result = client.users("some-user").unblock_user(NOT_BLOCKED).await; | ||
assert!(!result.is_ok()); | ||
} |