From b65caa33b42d04f527eb827aa7bd41eea5ee0fcb Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 27 Feb 2016 14:23:05 -0500 Subject: [PATCH] Test Expiring Lifetime in mocks --- tests/mock.rs | 32 ++++++++++++++++++- .../request_token_bearer_expiring.http | 11 +++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/response/request_token_bearer_expiring.http diff --git a/tests/mock.rs b/tests/mock.rs index 881d394..f0e760b 100644 --- a/tests/mock.rs +++ b/tests/mock.rs @@ -9,7 +9,7 @@ use inth_oauth2::{Client, ClientError, Token, Lifetime}; use inth_oauth2::error::OAuth2ErrorCode; mod provider { - use inth_oauth2::token::{Bearer, Static, Refresh}; + use inth_oauth2::token::{Bearer, Static, Expiring, Refresh}; use inth_oauth2::provider::Provider; pub struct BearerStatic; @@ -20,6 +20,14 @@ mod provider { fn token_uri() -> &'static str { "https://example.com/oauth/token" } } + pub struct BearerExpiring; + impl Provider for BearerExpiring { + type Lifetime = Expiring; + type Token = Bearer; + fn auth_uri() -> &'static str { "https://example.com/oauth/auth" } + fn token_uri() -> &'static str { "https://example.com/oauth/token" } + } + pub struct BearerRefresh; impl Provider for BearerRefresh { type Lifetime = Refresh; @@ -36,6 +44,10 @@ mod connector { 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 { include_str!("response/request_token_bearer_refresh.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()); } +#[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] fn request_token_bearer_refresh_success() { 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 }); } +#[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] fn request_token_bearer_refresh_wrong_lifetime() { let (client, http_client) = mock_client!(provider::BearerRefresh, connector::BearerStatic); diff --git a/tests/response/request_token_bearer_expiring.http b/tests/response/request_token_bearer_expiring.http new file mode 100644 index 0000000..c715c22 --- /dev/null +++ b/tests/response/request_token_bearer_expiring.http @@ -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" +}