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

Changing the user name from required to optional parameter #650

Merged
merged 1 commit into from
Jun 11, 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
2 changes: 1 addition & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,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());
}
}
Loading