github_scopes_rs/
lib.rs

1//! `github-scopes-rs` Discover GitHub token scope permission and return you an easy interface for checking token permission before querying GitHub.
2//!
3//! In many cases, you try to do actions to GitHub, but you get unclear permissions errors. This project allows you to get which permission your token has before,
4//! called GitHub, and if you don’t have the right permissions, you can tell the user the exact permission the user needs.
5//!
6//! ## How it works
7//! We called Github api with the given token and get which permissions scope the token has in order the access to the API. Then, the permissions are being converted to a simple object that you can work with.
8//! click [here](https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps) read GitHub documentation.
9//!
10//! ## Get token permission
11//! ```rust
12//! use github_scopes_rs::oauth::OAuthContext;
13//!
14//! # fn run()  {
15//! let permissions = OAuthContext::new("token").unwrap().get_scope_permissions();
16//!
17//! if !permissions.repo.all {
18//!     // do something
19//! }
20//! # }
21//! ```
22extern 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}