File: user_social_accounts_tests.rs

package info (click to toggle)
rust-octocrab 0.43.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,532 kB
  • sloc: makefile: 2
file content (104 lines) | stat: -rw-r--r-- 3,312 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use http::StatusCode;
use wiremock::{
    matchers::{method, path},
    Mock, MockServer, ResponseTemplate,
};

use mock_error::setup_error_handler;
use octocrab::models::SocialAccount;
use octocrab::Octocrab;

/// Tests API calls related to check runs of a specific commit.
mod mock_error;

async fn setup_social_accounts_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!("http method {http_method} 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_respond_to_social_accounts_list() {
    let mocked_response: Vec<SocialAccount> =
        serde_json::from_str(include_str!("resources/user_social_accounts.json")).unwrap();
    let template = ResponseTemplate::new(StatusCode::OK).set_body_json(&mocked_response);
    let mock_server = setup_social_accounts_mock("GET", "/user/social_accounts", template).await;
    let client = setup_octocrab(&mock_server.uri());
    let result = client
        .users("some_other_user")
        .social_accounts()
        .per_page(42)
        .page(3u32)
        .list()
        .await;
    assert!(
        result.is_ok(),
        "expected successful result, got error: {:#?}",
        result
    );
    let response = result.unwrap();
    let provider = &response.items.first().unwrap().provider;
    assert_eq!(provider, "twitter");
}

#[tokio::test]
async fn should_respond_to_social_accounts_add() {
    let mocked_response: Vec<SocialAccount> =
        serde_json::from_str(include_str!("resources/user_social_accounts.json")).unwrap();
    let template = ResponseTemplate::new(StatusCode::CREATED).set_body_json(&mocked_response);
    let mock_server = setup_social_accounts_mock("POST", "/user/social_accounts", template).await;
    let client = setup_octocrab(&mock_server.uri());
    let result = client
        .users("some_user")
        .social_accounts()
        .add(vec![
            "https://facebook.com/GitHub".to_string(),
            "https://www.youtube.com/@GitHub".to_string(),
        ])
        .await;
    assert!(
        result.is_ok(),
        "expected successful result, got error: {:#?}",
        result
    );
    let result = result.unwrap();
    assert_eq!(result.first().unwrap().provider, "twitter");
}

#[tokio::test]
async fn should_respond_to_social_account_delete() {
    let template = ResponseTemplate::new(StatusCode::NO_CONTENT);
    let mock_server = setup_social_accounts_mock("DELETE", "/user/social_accounts", template).await;
    let client = setup_octocrab(&mock_server.uri());
    let result = client
        .users("some_user")
        .social_accounts()
        .delete(vec![
            "https://facebook.com/GitHub".to_string(),
            "https://www.youtube.com/@GitHub".to_string(),
        ])
        .await;
    assert!(
        result.is_ok(),
        "expected successful result, got error: {:#?}",
        result
    );
}