summaryrefslogtreecommitdiff
path: root/tests/kraken.rs
blob: 8300b695e6b0a77ea81cf3b3d0a8a99fe3947121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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));
}