From 07557f5dca57048f6a7d1637fd06c341459d587d Mon Sep 17 00:00:00 2001 From: Karl Miklautz <92126744+Kapple14@users.noreply.github.com> Date: Tue, 11 Jun 2024 13:35:07 +0200 Subject: [PATCH] Changing the user name from required to optional parameter (#650) 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 --- src/models.rs | 2 +- tests/resources/user_data.json | 34 +++++++++++++++++++++ tests/user_deserialize_test.rs | 56 ++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/resources/user_data.json create mode 100644 tests/user_deserialize_test.rs diff --git a/src/models.rs b/src/models.rs index 9640c098..745002a3 100644 --- a/src/models.rs +++ b/src/models.rs @@ -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, pub company: Option, #[serde(deserialize_with = "empty_string_is_none")] pub blog: Option, diff --git a/tests/resources/user_data.json b/tests/resources/user_data.json new file mode 100644 index 00000000..93c3a45e --- /dev/null +++ b/tests/resources/user_data.json @@ -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" +} diff --git a/tests/user_deserialize_test.rs b/tests/user_deserialize_test.rs new file mode 100644 index 00000000..11e70281 --- /dev/null +++ b/tests/user_deserialize_test.rs @@ -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()); + } +}