Test Expiring Lifetime in mocks
This commit is contained in:
parent
3125a61b0c
commit
b65caa33b4
|
@ -9,7 +9,7 @@ use inth_oauth2::{Client, ClientError, Token, Lifetime};
|
||||||
use inth_oauth2::error::OAuth2ErrorCode;
|
use inth_oauth2::error::OAuth2ErrorCode;
|
||||||
|
|
||||||
mod provider {
|
mod provider {
|
||||||
use inth_oauth2::token::{Bearer, Static, Refresh};
|
use inth_oauth2::token::{Bearer, Static, Expiring, Refresh};
|
||||||
use inth_oauth2::provider::Provider;
|
use inth_oauth2::provider::Provider;
|
||||||
|
|
||||||
pub struct BearerStatic;
|
pub struct BearerStatic;
|
||||||
|
@ -20,6 +20,14 @@ mod provider {
|
||||||
fn token_uri() -> &'static str { "https://example.com/oauth/token" }
|
fn token_uri() -> &'static str { "https://example.com/oauth/token" }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct BearerExpiring;
|
||||||
|
impl Provider for BearerExpiring {
|
||||||
|
type Lifetime = Expiring;
|
||||||
|
type Token = Bearer<Expiring>;
|
||||||
|
fn auth_uri() -> &'static str { "https://example.com/oauth/auth" }
|
||||||
|
fn token_uri() -> &'static str { "https://example.com/oauth/token" }
|
||||||
|
}
|
||||||
|
|
||||||
pub struct BearerRefresh;
|
pub struct BearerRefresh;
|
||||||
impl Provider for BearerRefresh {
|
impl Provider for BearerRefresh {
|
||||||
type Lifetime = Refresh;
|
type Lifetime = Refresh;
|
||||||
|
@ -36,6 +44,10 @@ mod connector {
|
||||||
include_str!("response/request_token_bearer_static.http")
|
include_str!("response/request_token_bearer_static.http")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
mock_connector_in_order!(BearerExpiring {
|
||||||
|
include_str!("response/request_token_bearer_expiring.http")
|
||||||
|
});
|
||||||
|
|
||||||
mock_connector_in_order!(BearerRefresh {
|
mock_connector_in_order!(BearerRefresh {
|
||||||
include_str!("response/request_token_bearer_refresh.http")
|
include_str!("response/request_token_bearer_refresh.http")
|
||||||
include_str!("response/refresh_token_bearer_full.http")
|
include_str!("response/refresh_token_bearer_full.http")
|
||||||
|
@ -76,6 +88,17 @@ fn request_token_bearer_static_success() {
|
||||||
assert_eq!(Some("example"), token.scope());
|
assert_eq!(Some("example"), token.scope());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn request_token_bearer_expiring_success() {
|
||||||
|
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::BearerExpiring);
|
||||||
|
let token = client.request_token(&http_client, "code").unwrap();
|
||||||
|
assert_eq!("aaaaaaaa", token.access_token());
|
||||||
|
assert_eq!(Some("example"), token.scope());
|
||||||
|
assert_eq!(false, token.lifetime().expired());
|
||||||
|
assert!(token.lifetime().expires() > &UTC::now());
|
||||||
|
assert!(token.lifetime().expires() <= &(UTC::now() + Duration::seconds(3600)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn request_token_bearer_refresh_success() {
|
fn request_token_bearer_refresh_success() {
|
||||||
let (client, http_client) = mock_client!(provider::BearerRefresh, connector::BearerRefresh);
|
let (client, http_client) = mock_client!(provider::BearerRefresh, connector::BearerRefresh);
|
||||||
|
@ -121,6 +144,13 @@ fn request_token_bearer_static_wrong_lifetime() {
|
||||||
assert!(match err { ClientError::Parse(..) => true, _ => false });
|
assert!(match err { ClientError::Parse(..) => true, _ => false });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn request_token_bearer_expiring_wrong_lifetime() {
|
||||||
|
let (client, http_client) = mock_client!(provider::BearerExpiring, connector::BearerRefresh);
|
||||||
|
let err = client.request_token(&http_client, "code").unwrap_err();
|
||||||
|
assert!(match err { ClientError::Parse(..) => true, _ => false });
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn request_token_bearer_refresh_wrong_lifetime() {
|
fn request_token_bearer_refresh_wrong_lifetime() {
|
||||||
let (client, http_client) = mock_client!(provider::BearerRefresh, connector::BearerStatic);
|
let (client, http_client) = mock_client!(provider::BearerRefresh, connector::BearerStatic);
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
HTTP/1.1 200 OK
|
||||||
|
Content-Type: application/json;charset=UTF-8
|
||||||
|
Cache-Control: no-store
|
||||||
|
Pragma: no-cache
|
||||||
|
|
||||||
|
{
|
||||||
|
"access_token":"aaaaaaaa",
|
||||||
|
"token_type":"bearer",
|
||||||
|
"expires_in":3600,
|
||||||
|
"scope":"example"
|
||||||
|
}
|
Loading…
Reference in New Issue