-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
137 lines (121 loc) · 4.43 KB
/
lib.rs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! Super-simple client for interacting with the Github API. This is
//! incomplete, with only enough API calls to get through an OAuth
//! workflow.
//!
//! This also includes a "fakehub" that can be used for automated
//! testing by mocking out the Github website and API.
//!
//! ```
//! # use ghoauth::{GithubClient, fakehub::{Error, Fakehub, User}};
//! # const CLIENT_ID: &str = "1234567890";
//! # const CLIENT_SECRET: &str = "SECRET_SQUIRREL_STUFF";
//! # const USER: &str = "user";
//! # const USER_ID: i64 = 1;
//! # const USER_AVATAR_URL: &str = "https://github.com/";
//! # const USER_HTML_URL: &str = "https://github.com/";
//! # #[tokio::main]
//! # async fn main() -> Result<(), Error> {
//! // Fakehub is a mock Github with just enough functionality to drive
//! // a login flow.
//! let fakehub = Fakehub::new_at_starting_port(3040).await
//! .expect("cannot start local fakehub server");
//! // Train Fakehub with some fake data with add_client and add_user.
//! // Outside of tests, use GithubClient::new.
//! let github_client = fakehub.add_client(CLIENT_ID, CLIENT_SECRET)
//! .await?;
//! // train fakehub with a pre-configured user
//! fakehub.add_user(USER_ID, User {
//! login: USER.to_string(),
//! avatar_url: USER_AVATAR_URL.to_string(),
//! html_url: USER_HTML_URL.to_string()
//! }).await;
//! // Simulate a user visiting github's login page with your oauth
//! // application, logging in, and being redirected to your application
//! // with a code.
//! let code = fakehub.get_code(USER_ID).await?;
//! // Exchange that code for an api token.
//! let token = github_client.get_access_token(&code).await?;
//! // Ask github about the user that just authenticated.
//! let user_detail = github_client.get_user_detail(
//! &token.access_token
//! ).await?;
//!
//! assert_eq!(USER, user_detail.login);
//!
//! fakehub.shutdown().await;
//! # Ok(())
//! # }
//! ```
pub use crate::{
client::{GithubClient, GithubSyncClient, API_BASE_URL, BASE_URL},
error::Error,
shapes::{GetAccessTokenResponse, UserDetailResponse},
};
mod client;
mod error;
mod shapes;
#[cfg(feature = "fakehub")]
pub mod fakehub;
#[cfg(test)]
mod tests {
use crate::{
fakehub::{Fakehub, FakehubSync, User},
GithubSyncClient,
};
const CLIENT_ID: &str = "1234567890";
const CLIENT_SECRET: &str = "SECRET_SQUIRREL_STUFF";
const USER: &str = "user";
const USER_ID: i64 = 1;
const USER_AVATAR_URL: &str = "https://github.com/";
const USER_HTML_URL: &str = "https://github.com/";
#[tokio::test]
async fn oauth_flow() {
// if the two tests launch close enough together they race for
// the starting port
let fakehub = Fakehub::new_at_starting_port(3050)
.await
.expect("cannot start local fakehub server");
let github_client = fakehub.add_client(CLIENT_ID, CLIENT_SECRET).await.unwrap();
fakehub
.add_user(
USER_ID,
User {
login: USER.to_string(),
avatar_url: USER_AVATAR_URL.to_string(),
html_url: USER_HTML_URL.to_string(),
},
)
.await;
let code = fakehub.get_code(USER_ID).await.unwrap();
let token = github_client.get_access_token(&code).await.unwrap();
let user_detail = github_client
.get_user_detail(&token.access_token)
.await
.unwrap();
assert_eq!(USER, user_detail.login);
fakehub.shutdown().await;
}
#[test]
fn oauth_flow_sync() {
// if the two tests launch close enough together they race for
// the starting port
let fakehub =
FakehubSync::new_at_starting_port(3060).expect("cannot start local fakehub server");
let github_client = GithubSyncClient::new_from_async_client(
fakehub.add_client(CLIENT_ID, CLIENT_SECRET).unwrap(),
);
fakehub.add_user(
USER_ID,
User {
login: USER.to_string(),
avatar_url: USER_AVATAR_URL.to_string(),
html_url: USER_HTML_URL.to_string(),
},
);
let code = fakehub.get_code(USER_ID).unwrap();
let token = github_client.get_access_token(&code).unwrap();
let user_detail = github_client.get_user_detail(&token.access_token).unwrap();
assert_eq!(USER, user_detail.login);
fakehub.shutdown();
}
}