File: 05-oauth_authentication.t

package info (click to toggle)
libflickr-api-perl 1.29-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: perl: 1,471; makefile: 22
file content (175 lines) | stat: -rw-r--r-- 5,934 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;
use Test::More;
use Term::ReadLine;

use Flickr::API;

if (defined($ENV{MAKETEST_OAUTH_AUTHED})) {

    plan(skip_all => 'These tests are being bypassed because MAKETEST_OAUTH_AUTHED is defined, see README.');

}

if (defined($ENV{MAKETEST_OAUTH_CFG})) {
    plan( tests => 21 );
}
else {
    plan(skip_all => 'These tests require that MAKETEST_OAUTH_CFG points to a valid config, see README.');
}


my $config_file  = $ENV{MAKETEST_OAUTH_CFG};
my $config_ref;

my $useperms = 'read';

if (defined($ENV{MAKETEST_PERMS}) && $ENV{MAKETEST_PERMS} =~ /^(read|write|delete)$/) {

    $useperms = $ENV{MAKETEST_PERMS};

}

my $api;
my $term;


$term   = Term::ReadLine->new('Testing Flickr::API');
$term->ornaments(0);

my $fileflag=0;
if (-r $config_file) { $fileflag = 1; }
is($fileflag, 1, "Is the config file: $config_file, readable?");

SKIP: {

    skip "Skipping authentication tests, oauth config isn't there or is not readable", 20
      if $fileflag == 0;

    $api = Flickr::API->import_storable_config($config_file);

    isa_ok($api, 'Flickr::API');
    is($api->is_oauth, 1, 'Does Flickr::API object identify as OAuth');

  SKIP: {

        skip "Skip request token and authentication tests, access token exists", 18
          if (defined($api->{oauth}->{token}) and $api->{oauth}->{token} =~ /^[0-9]+-[0-9a-f]+$/);

        like($api->{oauth}->{consumer_key},  qr/[0-9a-f]+/i, "Did we get a consumer key from $config_file");
        like($api->{oauth}->{consumer_secret}, qr/[0-9a-f]+/i, "Did we get a consumer secret from $config_file");

        like($api->{oauth}->{request_token}->{token}, qr/^[0-9]+-[0-9a-f]+$/i,
             "Did we get a request_token token from $config_file");
        like($api->{oauth}->{request_token}->{token_secret}, qr/^[0-9a-f]+$/i,
             "Did we get a request_token token_secret from $config_file");

        my $proceed = 0;
        if ($api->{oauth}->{request_token}->{token} =~ m/^[0-9]+-[0-9a-f]+$/i and
            $api->{oauth}->{request_token}->{token_secret} =~ m/^[0-9a-f]+$/i) {

            $proceed = 1;

        }

      SKIP: {

            skip "Skipping authentication tests, oauth request token seems wrong", 14
              if $proceed == 0;


            my $which_rl = $term->ReadLine;

            if ($which_rl eq "Term::ReadLine::Perl" or $which_rl eq "Term::ReadLine::Perl5") {

                diag "\n\nTerm::ReadLine::Perl and Term::ReadLine::Perl5 may display prompts" .
                     "\nincorrectly. If this is the case for you, try adding \"PERL_RL=Stub\"" .
                     "\nto the environment variables passed in with make test\n\n";

            }

            my $uri = $api->oauth_authorize_uri({ 'perms' => $useperms });
            my $prompt = "\n\n$uri\n\n" .
              "Copy the above url to a browser, and authenticate with Flickr\n" .
              "Press [ENTER] once you get the redirect (or error): ";
            my $input = $term->readline($prompt);

            $prompt = "\n\nCopy the redirect URL from your browser and enter it\n" .
                "(or if there was an error just press [Enter]\n" .
                "\nURL Here: ";
            $input = $term->readline($prompt);

            chomp($input);

            my ($callback_returned,$token_received) = split(/\?/,$input);
            my (@parms) = split(/\&/,$token_received);

            like($callback_returned, qr/^$api->{oauth}->{callback}/i, "Was the redirect to the callback");

            my %request_token;
            foreach my $pair (@parms) {

                my ($key,$val) = split(/=/,$pair);

                $key =~ s/oauth_//;

                $request_token{$key}=$val;

            }

            like($request_token{token}, qr/^[0-9]+-[0-9a-f]+/i, "Is the returned token, token-shaped");
            like($request_token{verifier}, qr/^[0-9a-f]+/i, "Is the returned token verifier a hex number");

            my $access_req = $api->oauth_access_token(\%request_token);

            is($access_req, 'ok', "Did oauth_access_token complete successfully");

            isa_ok($api->{oauth}->{access_token}, 'Net::OAuth::AccessTokenResponse');
            my $access_token  = $api->{oauth}->{access_token}->token();
            my $access_secret = $api->{oauth}->{access_token}->token_secret();
            like($access_token,  qr/^[0-9]+-[0-9a-f]+/i, "Is the access token, token-shaped");
            like($access_secret, qr/^[0-9a-f]+/i,        "Is the access token secret a hex number");

          SKIP: {

                skip "Skipping save of access token bearing api because access token wasn't received", 7
                  if !$access_token;


                $fileflag=0;
                if (-w $config_file) { $fileflag = 1; }
                is($fileflag, 1, "Is the config file: $config_file, writeable?");

                $api->export_storable_config($config_file);

                $fileflag=0;
                if (-r $config_file) { $fileflag = 1; }
                is($fileflag, 1, "Is the config file: $config_file, readable?");

                my $api2 = Flickr::API->import_storable_config($config_file);

                isa_ok($api2, 'Flickr::API');

                is_deeply($api2->{oauth}->{access_token}, $api->{oauth}->{access_token},
                          "Did import_storable_config get back the access token we stored");

                isa_ok($api2->{oauth}->{access_token}, 'Net::OAuth::AccessTokenResponse');
                my $access_token2  = $api2->{oauth}->{access_token}->token();
                my $access_secret2 = $api2->{oauth}->{access_token}->token_secret();
                like($access_token2,  qr/^[0-9]+-[0-9a-f]+/i, "Is the access token, token-shaped");
                like($access_secret2, qr/^[0-9a-f]+/i,        "Is the access token secret a hex number");

            }
        }
    }
}


exit;


__END__

# Local Variables:
# mode: Perl
# End: