File: migration.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 (214 lines) | stat: -rw-r--r-- 7,296 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!perl
use 5.14.1;
use warnings;
use HTTP::Response;
use Test::Fatal;
use Test::Spec;
use Test::Warnings qw/warning/;
use URL::Encode qw/url_decode/;

# WWW::OAuth recommends WWW::Form::UrlEncoded::XS. If it is available, but its
# version number is less than WWW::Form::UrlEncoded's version, it warns and
# falls back to WWW::Form::UrlEncoded::PP. That warning causes test failures.
# We can avoid that by forcing use of WWW::Form:UrlEncoded::PP, which is
# sufficient for tests.
BEGIN { $ENV{WWW_FORM_URLENCODED_PP} = 1 }

use Twitter::API;

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

context 'Net::Twitter migration' => sub {
    my $client;
    before each => sub {
        $client = new_client;
    };

    it 'dies with traits' => sub {
        like exception {
            Twitter::API->new(
                traits          => [ qw/Migration WrapResult/ ],
                consumer_key    => 'key',
                consumer_secret => 'secret',
            );
        }, qr/use new_with_traits/;
    };

    for ( [ authenticate => 'get_authentication_url' ],
          [ authorize    => 'get_authorization_url'  ] )
    {
        my ( $endpoint, $method ) = @$_;

        describe $method => sub {
            my $uri;
            before each => sub {
                $client->stubs(oauth_request_token => {
                    oauth_token              => 'token',
                    oauth_token_secret       => 'token-secret',
                    oauth_callback_confirmed => 'true',
                })->exactly(1);
                local $ENV{TWITTER_API_NO_MIGRATION_WARNINGS} = 1;
                $uri = $client->$method(callback => 'foo/bar');
            };

            it 'calls oauth_request_token' => sub {};
            it 'sets request_token' => sub {
                is $client->request_token, 'token';
            };
            it 'sets request_token_secret' => sub {
                is $client->request_token_secret, 'token-secret';
            };
            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',
                };
            };
        };
    }
    for my $method ( qw/
        get_authentication_url
        get_authorization_url
        request_access_token
    / )
    {
        describe $method => sub {
            it 'has migration warning' => sub {
                my $client = new_client;
                $client->stubs('request');
                like(
                    warning {
                        $client->$method(callback => 'nope');
                    }, qr/will be removed in a future release/
                );
            };
        };
    }
    describe get_access_token => sub {
        my @result;
        before each => sub {
            my $content = 'oauth_token=token&oauth_token_secret=token-secret'
                .'&user_id=666&screen_name=trump';
            $client->user_agent->stubs(request => sub {
                HTTP::Response->new(200, 'OK', [
                        content_type => 'application/x-www-form-urlencoded',
                        content_length => length $content,
                    ],
                    $content,
                );
            });
            $client->request_token('request-token');
            $client->request_token_secret('request-token-secret');
            local $ENV{TWITTER_API_NO_MIGRATION_WARNINGS} = 1;
            @result = $client->request_access_token(
                verifier => 'callback-verifier');
        };

        it 'returns (token, secret, screen_name, user_id)' => sub {
            is_deeply [ @result[0..3] ],
                      [ qw/token token-secret 666 trump/ ];
        };
        it 'sets access_token' => sub {
            is $client->access_token, 'token';
        };
        it 'sets access_token_secret' => sub {
            is $client->access_token_secret, 'token-secret';
        };
        it 'clears request_token' => sub {
            ok !$client->has_request_token;
        };
        it 'clears request_token_secret' => sub {
            ok !$client->has_request_token_secret;
        };
    };
    describe wrap_result => sub {
        before each => sub {
            $client = Twitter::API->new_with_traits(
                traits              => 'Migration',
                wrap_result         => 1,
                consumer_key        => 'key',
                consumer_secret     => 'secret',
                access_token        => 'token',
                access_token_secret => 'token-secret',
            );
            $client->stubs(send_request => 1);
            $client->stubs(inflate_response => sub {
                $_[1]->set_result({});
            });
            $client->access_token('token');
            $client->access_token_secret('token-secret');
        };

        it 'returns a context object' => sub {
            local $ENV{TWITTER_API_NO_MIGRATION_WARNINGS} = 1;
            my $r = $client->get('some/endpoint');
            ok( blessed $r && $r->isa('Twitter::API::Context'));
        };
        it 'has migration warning' => sub {
            like( warning { $client->get('some/endpoint') },
                qr/wrap_result is enabled/);
        };
    };
    describe ua => sub {
        it 'has migration warning' => sub {
            like warning { $client->ua }, qr/will be removed/;
        };
        it 'returns an HTTP::Thin' => sub {
            local $ENV{TWITTER_API_NO_MIGRATION_WARNINGS} = 1;
            my $ua = $client->ua;
            ok blessed $ua && $ua->isa('HTTP::Thin');
        };
    };
};

context 'with AppAuth' => sub {
    # token straight out of Twitter docs
    my $url_encoded_token = 'AAAA%2FAAA%3DAAAAAAAA';
    my $url_decoded_token = url_decode($url_encoded_token);

    my $client;
    before each => sub {
        $client = Twitter::API->new_with_traits(
            traits          => [ qw/Migration AppAuth/ ],
            consumer_key    => 'key',
            consumer_secret => 'secret',
        );
        my $content = '{"token_type":"bearer","access_token"'
            .':"'.$url_encoded_token.'"}';
        $client->user_agent->stubs(request => sub {
            HTTP::Response->new(200, 'OK', [
                    content_type   => 'application/json; charset=utf-8',
                    content_length => length $content,
                ],
                $content,
            );
        });
    };

    describe request_access_token => sub {
        it 'client does not have an access_token before the call' => sub {
            ok !$client->has_access_token;
        };
        it 'sets access_token' => sub {
            local $ENV{TWITTER_API_NO_MIGRATION_WARNINGS} = 1;
            $client->request_access_token;
            is $client->access_token, $url_decoded_token;
        };
    };
};

runtests;