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
|
use strict;
use warnings;
use Test::More;
use Data::Dumper;
use Storable;
use Term::ReadLine;
use Flickr::API;
if (defined($ENV{MAKETEST_FLICKR_AUTHED})) {
plan(skip_all => 'These tests are being bypassed because MAKETEST_FLICKR_AUTHED is defined, see README.');
}
if (defined($ENV{MAKETEST_FLICKR_CFG})) {
plan( tests => 15 );
}
else {
plan(skip_all => 'These tests require that MAKETEST_FLICKR_CFG points to a valid config, see README.');
}
my $config_file = $ENV{MAKETEST_FLICKR_CFG};
my $useperms = 'read';
if (defined($ENV{MAKETEST_PERMS}) && $ENV{MAKETEST_PERMS} =~ /^(read|write|delete)$/) {
$useperms = $ENV{MAKETEST_PERMS};
}
my $api;
my $term;
my $key='fail';
my $frob;
my $fileflag=0;
if (-r $config_file) { $fileflag = 1; }
is($fileflag, 1, "Is the config file: $config_file, readable?");
SKIP: {
skip "Skipping authentication tests, flickr config isn't there or is not readable", 14
if $fileflag == 0;
$term = Term::ReadLine->new('Testing Flickr::API');
$term->ornaments(0);
$api = Flickr::API->import_storable_config($config_file);
isa_ok($api, 'Flickr::API');
is($api->is_oauth, 0, 'Does Flickr::API object identify as Flickr');
like($api->{api_key}, qr/[0-9a-f]+/i, "Did we get an api key from $config_file");
like($api->{api_secret}, qr/[0-9a-f]+/i, "Did we get an api secret from $config_file");
SKIP: {
skip "Skip getting a frob, we already have " . $api->{fauth}->{frob} , 1
if (defined($api->{fauth}->{frob}) and $api->{fauth}->{frob} =~ m/^[0-9a-f\-]+/i);
my $url = $api->request_auth_url($useperms);
my $uri = $url->as_string();
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 $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, or a non-web-based API Key, just press [Enter]\n" .
"\nURL Here: ";
$input = $term->readline($prompt);
chomp($input);
SKIP: {
skip "Skip frob input test, no frob input. Desktop API?", 1
unless $input =~ m/.*frob=.*/;
my ($callback_returned,$frob_received) = split(/\?/,$input);
($key,$frob) = split(/\=/,$frob_received);
is($key, 'frob', "Was the returned key 'frob'");
}
}
if ( defined($key) and
$key ne 'frob' and
defined($api->{fauth}->{frob}) and
$api->{fauth}->{frob} =~ m/^[0-9a-f\-]+/i) {
$key = 'frob';
$frob = $api->{fauth}->{frob};
}
SKIP: {
skip "Skip frob to token tests, no frob received. Desktop API?", 9
if $key ne 'frob';
like($frob, qr/^[0-9a-f\-]+/i, "Is the returned frob, frob-shaped");
SKIP: {
skip "Skip getting a token, we already have " . $api->{fauth}->{token} , 3
if defined($api->{fauth}->{token}) and $api->{fauth}->{token} =~ m/^[0-9a-f\-]+/i;
my $rc = $api->flickr_access_token($frob);
is($rc, 'ok', 'Was flickr_access_token successful');
like($api->{fauth}->{token}, qr/^[0-9a-f\-]+/i, 'Is the token received token shaped');
like($api->{fauth}->{user}->{nsid}, qr/^[0-9]+\@[0-9a-z]/i, 'Did we get back an nsid');
}
$fileflag=0;
if (-w $config_file) { $fileflag = 1; }
is($fileflag, 1, "Is the config file: $config_file, writable?");
SKIP: {
skip "Skip saving of flickr config, ",$config_file," is not writeable", 4
if $fileflag == 0;
$api->export_storable_config($config_file);
my $api2 = Flickr::API->import_storable_config($config_file);
isa_ok($api2, 'Flickr::API');
is($api2->{api_key}, $api->{api_key}, 'were we able to import our api key');
is($api2->{api_secret}, $api->{api_secret}, 'were we able to import our api secret');
is($api2->{fauth}->{token},$api->{fauth}->{token}, 'What about the token');
}
}
} # skipping auth tests
exit;
__END__
# Local Variables:
# mode: Perl
# End:
|