File: oauth.t

package info (click to toggle)
libtwitter-api-perl 1.0006-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 424 kB
  • sloc: perl: 2,868; makefile: 7
file content (169 lines) | stat: -rw-r--r-- 6,200 bytes parent folder | download | duplicates (2)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!perl
use 5.14.1;
use warnings;
use HTTP::Response;
use Test::Spec;

use Twitter::API;

sub new_client {
    Twitter::API->new(
        consumer_key    => 'key',
        consumer_secret => 'secret',
    );
}

my $token = 'Z6eEdO8MOmk394WozF5oKyuAv855l4Mlqo7hhlSLik';
my $secret = 'Kd75W4OQfb2oJTV0vzGzeXftVAwgMnEK9MumzYcM';

describe oauth => sub {
    my $client;
    before each => sub { $client = new_client };

    describe 'authentication urls' => sub {
        for ( [ authenticate => 'oauth_authentication_url' ],
              [ authorize    => 'oauth_authorization_url'  ] )
        {
            my ( $endpoint, $method ) = @$_;

            describe $method => sub {
                my $uri;
                before each => sub {
                    $uri = $client->$method(
                        oauth_token => $token,
                        force_login => 'true',
                        screen_name => 'bogus',
                    );
                };
                it 'has correct scheme' => sub {
                    is($uri->scheme, 'https');
                };
                it 'has correct host '  => sub {
                    is($uri->host, 'api.twitter.com');
                };
                it 'has correct path'   => sub {
                    is($uri->path, "/oauth/$endpoint");
                };
                it 'has correct query' => sub {
                    is_deeply { $uri->query_form }, {
                        oauth_token => $token,
                        force_login => 'true',
                        screen_name => 'bogus',
                    };
                };
            };
        }
    };
    describe oauth_request_token => sub {
        before each => sub {
            my $content = "oauth_token=$token&oauth_token_secret=$secret"
                ."&oauth_callback_confirmed=true";
            $client->user_agent->stubs(request => sub {
                HTTP::Response->new(200, 'OK', [
                        content_type => 'application/x-www-form-urlencoded',
                        content_length => length $content,
                    ],
                    $content,
                );
            });
        };

        it 'has valid authorization header' => sub {
            my ( $r, $c ) = $client->oauth_request_token;
            like $c->http_request->header('authorization'), qr/
                OAuth\s+
                oauth_callback="oob",\s+
                oauth_consumer_key="key",\s+
                oauth_nonce="[^"]+",\s+
                oauth_signature="[^"]+",\s+
                oauth_signature_method="HMAC-SHA1",\s+
                oauth_timestamp="\d+",\s+
                oauth_version="1.0"
            /x;
        };

        it 'returns a hashref with oauth_token/secret' => sub {
            my $r = $client->oauth_request_token;
            is_deeply $r, {
                oauth_token              => $token,
                oauth_token_secret       => $secret,
                oauth_callback_confirmed => 'true',
            };
        };
    };

    describe oauth_access_token => sub {
        before each => sub {
            # from the Twitter docs
            my $content = 'oauth_token=6253282-eWudHldSbIaelX7swmsiHImEL4Kinw'
                .'aGloHANdrY&oauth_token_secret=2EEfA6BG3ly3sR3RjE0IBSnlQu4Zr'
                .'UzPiYKmrkVU&user_id=6253282&screen_name=twitterapi';
            $client->user_agent->stubs(request => sub {
                HTTP::Response->new(200, 'OK', [
                        content_type => 'application/x-www-form-urlencoded',
                        content_length => length $content,
                    ],
                    $content,
                );
            });
        };

        it 'has a valid autorization header' => sub {
            my ( $r, $c ) = $client->oauth_access_token(
                token        => 'request-token',
                token_secret => 'request-token-secret',
                verifier     => 'verifier',
            );
            like $c->http_request->header('authorization'), qr/
                OAuth\s+
                oauth_consumer_key="key",\s+
                oauth_nonce="[^"]+",\s+
                oauth_signature="[^"]+",\s+
                oauth_signature_method="HMAC-SHA1",\s+
                oauth_timestamp="\d+",\s+
                oauth_token="request-token",\s+
                oauth_verifier="verifier",\s+
                oauth_version="1.0"
            /x;
        };
        it 'returns  a hashref with oauth_token/secret' => sub {
            my $r = $client->oauth_access_token(
                token        => 'request-token',
                token_secret => 'request-token-secret',
                verifier     => 'verifier',
            );
            is $$r{user_id}, 6253282;
            is $$r{screen_name}, 'twitterapi';
            like $$r{oauth_token}, qr/^6253282-eWudHldSb/;
            like $$r{oauth_token_secret}, qr/^2EEfA6BG3l/;
        };
        describe xauth => sub {
            it 'has a valid authorization header' => sub {
                my ( $r, $c ) = $client->xauth('alice', 'rabbit');
                like $c->http_request->header('authorization'), qr/
                    OAuth\s+
                    oauth_consumer_key="key",\s+
                    oauth_nonce="[^"]+",\s+
                    oauth_signature="[^"]+",\s+
                    oauth_signature_method="HMAC-SHA1",\s+
                    oauth_timestamp="\d+",\s+
                    oauth_version="1.0"
                /x;
            };
            it 'has required args' => sub {
                my ( $r, $c ) = $client->xauth('alice', 'rabbit');
                my $args = URL::Encode::url_params_mixed(
                    $c->http_request->content);
                is_deeply [ sort keys %$args ],
                    [ qw/x_auth_mode x_auth_password x_auth_username/ ];
            };
            it 'returns a hashref with oauth_token/secret' => sub {
                my $r = $client->xauth('foo@bar.baz', 'SeCrEt');
                is_deeply [ sort keys %$r ],
                    [ qw/oauth_token oauth_token_secret screen_name user_id/ ];
            };
        };
    };
};

runtests;