1extern crate env_logger;
23pub mod oauth;
24pub mod transform;
25
26#[cfg(test)]
27mod tests {
28
29 use super::oauth::OAuthContext;
30 use mockito::mock;
31 use std::env;
32
33 #[test]
34 fn can_get_oauth_scope() {
35 let m = mock("GET", "/rate_limit")
36 .match_header("user-agent", env!("CARGO_PKG_NAME"))
37 .match_header("authorization", "token dummy-token")
38 .with_status(200)
39 .with_header("x-oauth-scopes", "repo")
40 .expect_at_most(1)
41 .create();
42
43 let scope = OAuthContext::with_domain("dummy-token", mockito::server_url().as_ref());
44 m.assert();
45 assert!(scope.is_ok());
46
47 let permissions = scope.unwrap().get_scope_permissions();
48 assert!(permissions.repo.all);
49 }
50
51 #[test]
52 fn cat_refresh_oauth_scope_with_new_token() {
53 let mock_first_request = mock("GET", "/rate_limit")
54 .match_header("user-agent", env!("CARGO_PKG_NAME"))
55 .match_header("authorization", "token dummy-token")
56 .with_status(200)
57 .with_header("x-oauth-scopes", "")
58 .expect_at_most(1)
59 .create();
60
61 let mock_refresh_request = mock("GET", "/rate_limit")
62 .match_header("user-agent", env!("CARGO_PKG_NAME"))
63 .match_header("authorization", "token dummy-token2")
64 .with_status(200)
65 .with_header("x-oauth-scopes", "repo")
66 .expect_at_most(1)
67 .create();
68
69 let mut scope =
70 OAuthContext::with_domain("dummy-token", mockito::server_url().as_ref()).unwrap();
71 mock_first_request.assert();
72
73 let permissions = scope.get_scope_permissions();
74 assert!(!permissions.repo.all);
75
76 let refresh = scope.refresh(Some("dummy-token2".to_string()));
77 mock_refresh_request.assert();
78 assert!(refresh.is_ok());
79
80 let permissions = scope.get_scope_permissions();
81 assert!(permissions.repo.all);
82 }
83
84 #[test]
85 fn invalid_status_code() {
86 let m = mock("GET", "/rate_limit")
87 .match_header("user-agent", env!("CARGO_PKG_NAME"))
88 .match_header("authorization", "token dummy-token")
89 .with_status(500)
90 .with_header("x-oauth-scopes", "repo")
91 .expect_at_most(1)
92 .create();
93
94 let scope = OAuthContext::with_domain("dummy-token", mockito::server_url().as_ref());
95 m.assert();
96 assert!(scope.is_err());
97 }
98
99 #[test]
100 fn scope_header_not_found() {
101 let m = mock("GET", "/rate_limit")
102 .match_header("user-agent", env!("CARGO_PKG_NAME"))
103 .match_header("authorization", "token dummy-token")
104 .with_status(200)
105 .expect_at_most(1)
106 .create();
107
108 let scope = OAuthContext::with_domain("dummy-token", mockito::server_url().as_ref());
109 m.assert();
110 assert!(scope.is_err());
111 }
112}