Skip to content

Commit ced6aee

Browse files
Merge #76
76: make mocking better and some ci changes r=Emilgardis a=Emilgardis Co-authored-by: Emil Gardström <emil.gardstrom@gmail.com>
2 parents 82d8121 + f9b5bd5 commit ced6aee

File tree

7 files changed

+36
-76
lines changed

7 files changed

+36
-76
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: CI
22
env:
3-
CI_TWITCH_OAUTH2_FEATURES: "all"
3+
CI_TWITCH_OAUTH2_FEATURES: "all mock_api"
44
on:
55
pull_request:
66
branches: [master]
@@ -39,7 +39,9 @@ jobs:
3939
id: cache
4040
with:
4141
path: |
42-
~/.cargo
42+
~/.cargo/registry/index
43+
~/.cargo/registry/cache
44+
~/.cargo/git
4345
target
4446
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
4547
- name: Test twitch_oauth2
@@ -80,14 +82,16 @@ jobs:
8082
id: cache
8183
with:
8284
path: |
83-
~/.cargo
85+
~/.cargo/registry/index
86+
~/.cargo/registry/cache
87+
~/.cargo/git
8488
target
8589
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
8690
- name: Run clippy --all-targets --all-features -- -D warnings
8791
uses: actions-rs/cargo@v1
8892
with:
8993
command: clippy
90-
args: --all-targets --all-features -- -D warnings
94+
args: --locked --all-targets --all-features -- -D warnings
9195
docs:
9296
name: Docs
9397
runs-on: ubuntu-latest
@@ -103,7 +107,9 @@ jobs:
103107
id: cache
104108
with:
105109
path: |
106-
~/.cargo
110+
~/.cargo/registry/index
111+
~/.cargo/registry/cache
112+
~/.cargo/git
107113
target
108114
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
109115
# We do the following to make sure docs.rs can document properly without anything broken, and that docs are working.
@@ -116,7 +122,7 @@ jobs:
116122
uses: actions-rs/cargo@v1
117123
with:
118124
command: doc
119-
args: --no-deps --features "${{ env.CI_TWITCH_OAUTH2_FEATURES }}"
125+
args: --locked --no-deps --features "${{ env.CI_TWITCH_OAUTH2_FEATURES }}"
120126
release:
121127
name: Release
122128
runs-on: ubuntu-latest

.github/workflows/gh-pages.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: github pages
22
env:
3-
CI_TWITCH_OAUTH2_FEATURES: "all"
3+
CI_TWITCH_OAUTH2_FEATURES: "all mock_api"
44
RUSTDOCFLAGS: "-Z unstable-options --html-in-header=docs/pre-content.html"
55
on:
66
push:
@@ -20,7 +20,11 @@ jobs:
2020
uses: actions/cache@v1
2121
id: cache
2222
with:
23-
path: ~/.cargo
23+
path:
24+
~/.cargo/registry/index
25+
~/.cargo/registry/cache
26+
~/.cargo/git
27+
target
2428
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
2529
- name: mention 40 character commit rev in doc
2630
run: sed -i "s/{{commit}}/$(git rev-parse HEAD)/g" docs/pre-content.html

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"rust-analyzer.checkOnSave.allTargets": false,
33
"rust-analyzer.checkOnSave.features": ["all"],
44
"rust-analyzer.checkOnSave.enable": true,
5-
"rust-analyzer.cargo.features": ["all"],
5+
"rust-analyzer.cargo.features": ["all", "mock_api"],
66
"rust-analyzer.experimental.procAttrMacros": true
77
}

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ path = "examples/mock_user.rs"
7575
required-features = ["reqwest_client", "mock_api"]
7676

7777
[package.metadata.docs.rs]
78-
features = ["all"]
78+
features = ["all", "mock_api"]

examples/mock_user.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ async fn main() -> anyhow::Result<()> {
3030
&reqwest::Client::builder()
3131
.redirect(reqwest::redirect::Policy::none())
3232
.build()?,
33-
None,
3433
client_id,
3534
client_secret,
3635
user_id,

src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,29 @@ macro_rules! mock_env_url {
7575
static TWITCH_OAUTH2_URL: once_cell::sync::Lazy<url::Url> =
7676
mock_env_url!("TWITCH_OAUTH2_URL", "https://id.twitch.tv/oauth2/");
7777

78-
/// Authorization URL (`/authorize`) for `id.twitch.tv`
78+
/// Authorization URL (`https://id.twitch.tv/oauth2/authorize`) for `id.twitch.tv`
7979
///
80-
/// Can be overridden when feature `mock_api` is enabled with environment variable `TWITCH_OAUTH2_URL` to set the root path, or with `TWITCH_OAUTH2_AUTH_URL` to override the full url.
80+
/// Can be overridden when feature `mock_api` is enabled with environment variable `TWITCH_OAUTH2_URL` to set the root path, or with `TWITCH_OAUTH2_AUTH_URL` to override the base (`https://id.twitch.tv/oauth2/`) url.
8181
///
8282
/// # Examples
8383
///
8484
/// Set the environment variable `TWITCH_OAUTH2_URL` to `http://localhost:8080/auth/` to use [`twitch-cli` mock](https://github.com/twitchdev/twitch-cli/blob/main/docs/mock-api.md) endpoints.
8585
pub static AUTH_URL: once_cell::sync::Lazy<url::Url> = mock_env_url!("TWITCH_OAUTH2_AUTH_URL", {
8686
TWITCH_OAUTH2_URL.to_string() + "authorize"
8787
},);
88-
/// Token URL (`/token`) for `id.twitch.tv`
88+
/// Token URL (`https://id.twitch.tv/oauth2/token`) for `id.twitch.tv`
8989
///
90-
/// Can be overridden when feature `mock_api` is enabled with environment variable `TWITCH_OAUTH2_URL` to set the root path, or with `TWITCH_OAUTH2_TOKEN_URL` to override the full url.
90+
/// Can be overridden when feature `mock_api` is enabled with environment variable `TWITCH_OAUTH2_URL` to set the root path, or with `TWITCH_OAUTH2_TOKEN_URL` to override the base (`https://id.twitch.tv/oauth2/`) url.
9191
///
9292
/// # Examples
9393
///
9494
/// Set the environment variable `TWITCH_OAUTH2_URL` to `http://localhost:8080/auth/` to use [`twitch-cli` mock](https://github.com/twitchdev/twitch-cli/blob/main/docs/mock-api.md) endpoints.
9595
pub static TOKEN_URL: once_cell::sync::Lazy<url::Url> = mock_env_url!("TWITCH_OAUTH2_TOKEN_URL", {
9696
TWITCH_OAUTH2_URL.to_string() + "token"
9797
},);
98-
/// Validation URL (`/validate`) for `id.twitch.tv`
98+
/// Validation URL (`https://id.twitch.tv/oauth2/validate`) for `id.twitch.tv`
9999
///
100-
/// Can be overridden when feature `mock_api` is enabled with environment variable `TWITCH_OAUTH2_URL` to set the root path, or with `TWITCH_OAUTH2_VALIDATE_URL` to override the full url.
100+
/// Can be overridden when feature `mock_api` is enabled with environment variable `TWITCH_OAUTH2_URL` to set the root path, or with `TWITCH_OAUTH2_VALIDATE_URL` to override the base (`https://id.twitch.tv/oauth2/`) url.
101101
///
102102
/// # Examples
103103
///
@@ -106,9 +106,9 @@ pub static VALIDATE_URL: once_cell::sync::Lazy<url::Url> =
106106
mock_env_url!("TWITCH_OAUTH2_VALIDATE_URL", {
107107
TWITCH_OAUTH2_URL.to_string() + "validate"
108108
},);
109-
/// Revokation URL (`/revoke`) for `id.twitch.tv`
109+
/// Revokation URL (`https://id.twitch.tv/oauth2/revoke`) for `id.twitch.tv`
110110
///
111-
/// Can be overridden when feature `mock_api` is enabled with environment variable `TWITCH_OAUTH2_URL` to set the root path, or with `TWITCH_OAUTH2_REVOKE_URL` to override the full url.
111+
/// Can be overridden when feature `mock_api` is enabled with environment variable `TWITCH_OAUTH2_URL` to set the root path, or with `TWITCH_OAUTH2_REVOKE_URL` to override the base (`https://id.twitch.tv/oauth2/`) url.
112112
///
113113
/// # Examples
114114
///

src/tokens/user_token.rs

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::tokens::{
33
errors::{RefreshTokenError, UserTokenExchangeError, ValidationError},
44
Scope, TwitchToken,
55
};
6-
use crate::{parse_response, ClientSecret};
6+
use crate::ClientSecret;
77

88
use super::errors::ImplicitUserTokenExchangeError;
99
use crate::types::{AccessToken, ClientId, RefreshToken};
@@ -132,8 +132,6 @@ impl UserToken {
132132

133133
/// Generate a user token from [mock-api](https://github.com/twitchdev/twitch-cli/blob/main/docs/mock-api.md#auth-namespace)
134134
///
135-
/// This will call `/mock/users?user_id={user_id}` to validate the token, since there is no `auth/validate` endpoint in mock-api.
136-
///
137135
/// # Examples
138136
///
139137
/// ```rust,no_run
@@ -143,8 +141,6 @@ impl UserToken {
143141
/// &reqwest::Client::builder()
144142
/// .redirect(reqwest::redirect::Policy::none())
145143
/// .build()?,
146-
/// // Pass in the mock api url to mock/users, if this is none, we'll assume the host is the same as the `/auth` url, but living instead on `/mock/users`
147-
/// None,
148144
/// "mockclientid".into(),
149145
/// "mockclientsecret".into(),
150146
/// "user_id",
@@ -154,9 +150,9 @@ impl UserToken {
154150
/// # fn main() {run();}
155151
/// ```
156152
#[cfg_attr(nightly, doc(cfg(feature = "mock_api")))]
153+
#[cfg(feature = "mock_api")]
157154
pub async fn mock_token<'a, C>(
158155
http_client: &'a C,
159-
mock_api_users_url: impl Into<Option<url::Url>>,
160156
client_id: ClientId,
161157
client_secret: ClientSecret,
162158
user_id: impl AsRef<str>,
@@ -189,61 +185,16 @@ impl UserToken {
189185
.req(req)
190186
.await
191187
.map_err(UserTokenExchangeError::RequestError)?;
192-
let response: crate::id::TwitchTokenResponse = parse_response(&resp)?;
193-
let expires_in = response.expires_in();
194-
let auth_header = format!("bearer {}", response.access_token().secret());
195-
let mut user_headers = HeaderMap::new();
196-
user_headers.insert(
197-
http::header::AUTHORIZATION,
198-
auth_header
199-
.parse()
200-
.expect("Failed to parse header for validation"),
201-
);
202-
user_headers.insert(
203-
"Client-ID",
204-
client_id
205-
.as_str()
206-
.parse()
207-
.expect("Failed to parse header for validation"),
208-
);
209-
let mock_api_users_url = if let Some(url) = mock_api_users_url.into() {
210-
url
211-
} else {
212-
// user didn't provide a url for user api mock. We assume the mock api lives on the same place as /auth
213-
let mut url = crate::TWITCH_OAUTH2_URL.clone();
214-
url.set_path("mock/users");
215-
url
216-
};
217-
218-
let user_req = crate::construct_request(
219-
&mock_api_users_url,
220-
&[("user_id", user_id)],
221-
user_headers,
222-
Method::GET,
223-
vec![],
224-
);
225-
226-
let user = http_client
227-
.req(user_req)
228-
.await
229-
.map_err(UserTokenExchangeError::RequestError)?;
230-
231-
let user: serde_json::Value = crate::parse_response(&user)?;
188+
let response: crate::id::TwitchTokenResponse = crate::parse_response(&resp)?;
232189

233-
Ok(UserToken::from_existing_unchecked(
190+
UserToken::from_existing(
191+
http_client,
234192
response.access_token,
235193
response.refresh_token,
236-
client_id,
237194
client_secret,
238-
user.pointer("/data/0/login")
239-
.and_then(|login| login.as_str().map(|s| s.to_string()))
240-
.unwrap(),
241-
user.pointer("/data/0/id")
242-
.and_then(|id| id.as_str().map(|s| s.to_string()))
243-
.unwrap(),
244-
response.scopes,
245-
expires_in,
246-
))
195+
)
196+
.await
197+
.map_err(Into::into)
247198
}
248199

249200
/// Set the client secret

0 commit comments

Comments
 (0)