Skip to content

Commit

Permalink
Changing the user name from required to optional parameter (#650)
Browse files Browse the repository at this point in the history
Changing the user name parameter from required to optional following the official API guidelines. See https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28 for more
  • Loading branch information
Kapple14 authored Jun 11, 2024
1 parent 3aa2415 commit 07557f5
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ pub struct UserProfile {
pub received_events_url: Url,
pub r#type: String,
pub site_admin: bool,
pub name: String,
pub name: Option<String>,
pub company: Option<String>,
#[serde(deserialize_with = "empty_string_is_none")]
pub blog: Option<String>,
Expand Down
34 changes: 34 additions & 0 deletions tests/resources/user_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"login": "octocat",
"id": 583231,
"node_id": "MDQ6VXNlcjU4MzIzMQ==",
"avatar_url": "https://avatars.githubusercontent.com/u/583231?v=4",
"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,
"name": null,
"company": "@github",
"blog": "https://github.blog",
"location": "San Francisco",
"email": null,
"hireable": null,
"bio": null,
"twitter_username": null,
"public_repos": 8,
"public_gists": 8,
"followers": 13802,
"following": 9,
"created_at": "2011-01-25T18:44:36Z",
"updated_at": "2024-05-22T11:18:34Z"
}
56 changes: 56 additions & 0 deletions tests/user_deserialize_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// Tests API calls related to check runs of a specific commit.
mod mock_error;
use mock_error::setup_error_handler;
use octocrab::models::UserProfile;
use octocrab::{Error, Octocrab};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use wiremock::{
matchers::{method, path},
Mock, MockServer, ResponseTemplate,
};

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

async fn setup_api(template: ResponseTemplate) -> MockServer {
let mock_server = MockServer::start().await;

let mocked_path = "/users/some-user";

Mock::given(method("GET"))
.and(path(mocked_path))
.respond_with(template)
.mount(&mock_server)
.await;
setup_error_handler(
&mock_server,
&format!("GET on {mocked_path} was not received"),
)
.await;
mock_server
}

#[tokio::test]
async fn should_return_desirialized_user() {
let mocked_response: UserProfile =
serde_json::from_str(include_str!("resources/user_data.json")).unwrap();
let template = ResponseTemplate::new(200).set_body_json(&mocked_response);
let mock_server = setup_api(template).await;
let client = setup_octocrab(&mock_server.uri());
let result = client.users("some-user").profile().await;

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

let user = result.unwrap();

{
assert_eq!("octocat", user.login);
assert_eq!(None, user.name.as_deref());
}
}

0 comments on commit 07557f5

Please sign in to comment.