diff options
author | David Blajda <blajda@hotmail.com> | 2019-05-08 15:38:37 +0000 |
---|---|---|
committer | David Blajda <blajda@hotmail.com> | 2019-05-08 15:38:37 +0000 |
commit | 98fb79b85e3cfbb547e5340b30623511daf09ef5 (patch) | |
tree | a03bd7a95c86a92eb7510409429f50433056d7ab /tests | |
parent | 0e09c2c06fc0b81f11d12985a4172c815233db17 (diff) |
:WIP: Move types to a different crate
Diffstat (limited to 'tests')
-rw-r--r-- | tests/common/mod.rs | 32 | ||||
-rw-r--r-- | tests/helix.rs | 253 | ||||
-rw-r--r-- | tests/kraken.rs | 76 |
3 files changed, 0 insertions, 361 deletions
diff --git a/tests/common/mod.rs b/tests/common/mod.rs deleted file mode 100644 index ff2eb08..0000000 --- a/tests/common/mod.rs +++ /dev/null @@ -1,32 +0,0 @@ -extern crate twitch_api; -extern crate hyper; -extern crate futures; -extern crate reqwest; -extern crate url; -extern crate http; - -use tokio::runtime::current_thread::Runtime; -use twitch_api::{ClientConfig, TestConfig}; -use reqwest::r#async::Response; -use http::response::Builder; - -pub const CLIENT_ID: &str = "cfabdegwdoklmawdzdo98xt2fo512y"; -pub const CLIENT_SECRET: &str = "nyo51xcdrerl8z9m56w9w6wg"; - -pub fn test_config() -> (ClientConfig, TestConfig) { - let test_config = TestConfig::default(); - (ClientConfig { - test_config: Some(test_config.clone()), - max_retrys: 0, - ..ClientConfig::default() - }, test_config) -} - -pub fn okay_response(data: &'static str) -> Response { - - let response = - Builder::new() - .status(200) - .body(data).unwrap(); - Response::from(response) -}
\ No newline at end of file diff --git a/tests/helix.rs b/tests/helix.rs deleted file mode 100644 index 8c85c3b..0000000 --- a/tests/helix.rs +++ /dev/null @@ -1,253 +0,0 @@ -extern crate twitch_api; -extern crate tokio; -extern crate hyper; -extern crate futures; -extern crate url; -extern crate http; - -pub mod common; - -use futures::Stream; -use twitch_api::HelixClient; -use twitch_api::{ClientConfig, TestConfig}; -use twitch_api::client::ClientTrait; -use twitch_api::error::Error; -use twitch_api::client::HelixScope; - - -use tokio::runtime::current_thread::Runtime; -use reqwest::r#async::Response; -use http::response::Builder; - -use crate::common::*; - -const USER_RESPONSE: &str = r#"{ - "data": [{ - "id": "44322889", - "login": "dallas", - "display_name": "dallas", - "type": "staff", - "broadcaster_type": "", - "description": "Just a gamer playing games and chatting. :)", - "profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/dallas-profile_image-1a2c906ee2c35f12-300x300.png", - "offline_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/dallas-channel_offline_image-1a2c906ee2c35f12-1920x1080.png", - "view_count": 191836881, - "email": "login@provider.com" - }]}"#; - - -const AUTH_RESPONSE: &str = r#" -{ - "access_token": "prau3ol6mg5glgek8m89ec2s9q5i3i", - "refresh_token": "", - "expires_in": 3600, - "scope": ["user:read:email", "bits:read"], - "token_type": "bearer" -}"#; - -#[test] -fn test_invalid_client_id() { - let response = r#"{"error":"Unauthorized","status":401,"message":"Must provide a valid Client-ID or OAuth token"}"#; - - let response = - Builder::new() - .status(401) - .body(response).unwrap(); - let response = Response::from(response); - - let (config, test_config) = test_config(); - test_config.push_response(response); - - let mut runtime = Runtime::new().unwrap(); - - let client = HelixClient::new_with_config(CLIENT_ID, config); - let user_future = client.users().users(&[], &["freakey"]); - let result = runtime.block_on(user_future); - - assert!(result.is_err()); - if let Err(err) = result { - assert!(err.is_auth_error()) - } -} - -#[test] -fn test_invalid_auth() { - let response = r#"{"message":"invalid client secret","status":403}"#; - - let response = - Builder::new() - .status(403) - .body(response).unwrap(); - let response = Response::from(response); - - let (config, test_config) = test_config(); - test_config.push_response(response); - let mut runtime = Runtime::new().unwrap(); - - let client = HelixClient::new_with_config(CLIENT_ID, config) - .authenticate(CLIENT_SECRET).build(); - assert!(!client.authenticated()); - - let user_future = client.users().users(&[], &["dallas"]); - let result = runtime.block_on(user_future); - - assert!(!client.authenticated()); - assert!(result.is_err()); - if let Err(err) = result { - println!("{:?}", err); - assert!(err.is_auth_error()) - } -} - -#[test] -fn test_ratelimit_hit() { - let response = r#"{"error":"Too Many Requests","message":"Thou Shall Not Pass","status":429}"#; - let response = - Builder::new() - .status(429) - .body(response).unwrap(); - let response = Response::from(response); - - let (config, test_config) = test_config(); - test_config.push_response(response); - let mut runtime = Runtime::new().unwrap(); - - let client = HelixClient::new_with_config(CLIENT_ID, config); - let user_future = client.users().users(&[], &["freakey"]); - let result = runtime.block_on(user_future); - - assert!(result.is_err()); - if let Err(err) = result { - assert!(err.is_ratelimit_error()) - } -} - -#[test] -fn test_client_header() { - let (config, test_config) = test_config(); - test_config.push_response(okay_response(USER_RESPONSE)); - let mut runtime = Runtime::new().unwrap(); - - let client = HelixClient::new_with_config(CLIENT_ID, config); - let user_future = client.users().users(&[], &["dallas"]); - let result = runtime.block_on(user_future); - assert!(result.is_ok()); - - let request = { - let config = &mut test_config.inner.lock().unwrap(); - config.requests.pop().unwrap().unwrap() - }; - - let headers = request.headers(); - let header = headers.get("Client-Id"); - assert_eq!(Some(CLIENT_ID), header.and_then(|s| Some(s.to_str().unwrap()))); -} - -#[test] -fn test_auth_header() { - - let (config, test_config) = test_config(); - test_config.push_response(okay_response(USER_RESPONSE)); - test_config.push_response(okay_response(AUTH_RESPONSE)); - let mut runtime = Runtime::new().unwrap(); - - let client = HelixClient::new_with_config(CLIENT_ID, config) - .authenticate(CLIENT_SECRET).build(); - - /*Authentication is lazy*/ - assert!(!client.authenticated()); - - let user_future = client.users().users(&[], &["dallas"]); - let result = runtime.block_on(user_future); - assert!(result.is_ok()); - - let request = { - let config = &mut test_config.inner.lock().unwrap(); - config.requests.pop().unwrap().unwrap() - }; - - let headers = request.headers(); - let client_id = headers.get("Client-Id"); - let secret = headers.get("Authorization"); - - let scopes = client.scopes(); - - assert!(client.authenticated()); - assert_eq!(Some(CLIENT_ID), client_id.and_then(|s| Some(s.to_str().unwrap()))); - assert_eq!(Some("Bearer ".to_owned() + "prau3ol6mg5glgek8m89ec2s9q5i3i"), secret.and_then(|s| Some(s.to_str().unwrap().to_owned()))); - assert!(scopes.contains(&HelixScope::UserReadEmail)); - assert!(scopes.contains(&HelixScope::BitsRead)); -} - - -#[test] -fn test_single_request() { - let (config, test_config) = test_config(); - test_config.push_response(okay_response(USER_RESPONSE)); - let mut runtime = Runtime::new().unwrap(); - - let client = HelixClient::new_with_config(CLIENT_ID, config); - let user_future = client.users().users(&[], &["dallas"]); - let result = runtime.block_on(user_future); - - assert!(result.is_ok()) -} - -#[test] -fn test_pagination() { - - let response = r#"{ - "data": [{ - "id": "234482848", - "user_id": "67955580", - "user_name": "ChewieMelodies", - "title": "-", - "description": "", - "created_at": "2018-03-02T20:53:41Z", - "published_at": "2018-03-02T20:53:41Z", - "url": "https://www.twitch.tv/videos/234482848", - "thumbnail_url": "https://static-cdn.jtvnw.net/s3_vods/bebc8cba2926d1967418_chewiemelodies_27786761696_805342775/thumb/thumb0-%{width}x%{height}.jpg", - "viewable": "public", - "view_count": 142, - "language": "en", - "type": "archive", - "duration": "3h8m33s" - }], - "pagination":{"cursor":"eyJiIjpudWxsLCJhIjoiMTUwMzQ0MTc3NjQyNDQyMjAwMCJ9"} - }"#; - - let response2 = r#"{ - "data": [{ - "id": "234482848", - "user_id": "67955580", - "user_name": "ChewieMelodies", - "title": "-", - "description": "", - "created_at": "2018-03-02T20:53:41Z", - "published_at": "2018-03-02T20:53:41Z", - "url": "https://www.twitch.tv/videos/234482848", - "thumbnail_url": "https://static-cdn.jtvnw.net/s3_vods/bebc8cba2926d1967418_chewiemelodies_27786761696_805342775/thumb/thumb0-%{width}x%{height}.jpg", - "viewable": "public", - "view_count": 142, - "language": "en", - "type": "archive", - "duration": "3h8m33s" - }], - "pagination":{"cursor":""} - }"#; - - let (config, test_config) = test_config(); - test_config.push_response(okay_response(response)); - test_config.push_response(okay_response(response2)); - let mut runtime = Runtime::new().unwrap(); - - let client = HelixClient::new_with_config(CLIENT_ID, config); - let video_future = client.videos().by_user(&"67955580"); - let result = runtime.block_on(video_future.into_future()); - assert!(result.is_ok()); - if let Ok((Some(data), next)) = result { - - let result = runtime.block_on(next.into_future()); - assert!(result.is_ok()); - } else {unreachable!()} -}
\ No newline at end of file diff --git a/tests/kraken.rs b/tests/kraken.rs deleted file mode 100644 index 8300b69..0000000 --- a/tests/kraken.rs +++ /dev/null @@ -1,76 +0,0 @@ -extern crate twitch_api; -extern crate tokio; -extern crate hyper; -extern crate futures; -extern crate url; -extern crate http; - -pub mod common; - -use twitch_api::KrakenClient; -use twitch_api::{ClientConfig, TestConfig}; -use twitch_api::client::ClientTrait; -use twitch_api::error::Error; -use twitch_api::client::KrakenScope; - -use tokio::runtime::current_thread::Runtime; -use reqwest::r#async::Response; -use http::response::Builder; - -use crate::common::*; - -const AUTH_RESPONSE: &str = r#" -{ - "access_token": "prau3ol6mg5glgek8m89ec2s9q5i3i", - "refresh_token": "", - "expires_in": 3600, - "scope": ["user_read", "channel_commercial"], - "token_type": "bearer" -}"#; - -const USER_RESPONSE: &str = r#"{ - "_id": "44322889", - "bio": "Just a gamer playing games and chatting. :)", - "created_at": "2013-06-03T19:12:02.580593Z", - "display_name": "dallas", - "logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/dallas-profile_image-1a2c906ee2c35f12-300x300.png", - "name": "dallas", - "type": "staff", - "updated_at": "2016-12-13T16:31:55.958584Z" -}"#; - -#[test] -fn test_auth_header() { - - let (config, test_config) = test_config(); - test_config.push_response(okay_response(USER_RESPONSE)); - test_config.push_response(okay_response(AUTH_RESPONSE)); - let mut runtime = Runtime::new().unwrap(); - - let client = KrakenClient::new_with_config(CLIENT_ID, config) - .authenticate(CLIENT_SECRET).build(); - - /*Authentication is lazy*/ - assert!(!client.authenticated()); - - let user_future = client.users().by_id(&"44322889"); - let result = runtime.block_on(user_future); - assert!(result.is_ok()); - - let request = { - let config = &mut test_config.inner.lock().unwrap(); - config.requests.pop().unwrap().unwrap() - }; - - let headers = request.headers(); - let client_id = headers.get("Client-Id"); - let secret = headers.get("Authorization"); - - let scopes = client.scopes(); - - assert!(client.authenticated()); - assert_eq!(Some(CLIENT_ID), client_id.and_then(|s| Some(s.to_str().unwrap()))); - assert_eq!(Some("OAuth ".to_owned() + "prau3ol6mg5glgek8m89ec2s9q5i3i"), secret.and_then(|s| Some(s.to_str().unwrap().to_owned()))); - assert!(scopes.contains(&KrakenScope::UserRead)); - assert!(scopes.contains(&KrakenScope::ChannelCommercial)); -}
\ No newline at end of file |