| 1 | package WWW::Tumblr::Test; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use WWW::Tumblr; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Test::More; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 15 | my $t = WWW::Tumblr->new( | |||||||||||||||||||||||||||
| 10 | # These are "public" keys for my small perlapi blog test. | ||||||||||||||||||||||||||||
| 11 | # Don't be a jerk :) | ||||||||||||||||||||||||||||
| 12 | consumer_key => 'm2TqZPKBN87VXTf0HZCDbLBmV8IKhjDnSh5SL2MrWYPrvDKIKE', | ||||||||||||||||||||||||||||
| 13 | secret_key => 'DfNf21jsNPkDfz5rRW4tUPQf0gR1G8mYtxqBDM62XQSGHNJRY9', | ||||||||||||||||||||||||||||
| 14 | token => '5koNK32cgylbsxs9LsTDCWFUrPccYjFCqFIbZayCFLrVlm1zuP', | ||||||||||||||||||||||||||||
| 15 | token_secret => 'VbFLz3lZ3P2ghw5b4dHwNNw4IAq13uHgDp4reZy4N24b4VlfM8', | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Track if we've hit the rate limit during this test run | ||||||||||||||||||||||||||||
| 19 | 15 | my $rate_limited = 0; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub tumblr { $t } | ||||||||||||||||||||||||||
|
Subroutine
tumblr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 6 | ▶ | sub user { $t->user } | ||||||||||||||||||||||||||
|
Subroutine
user: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 8 | ▶ | sub blog { $t->blog('perlapi.tumblr.com') } | ||||||||||||||||||||||||||
|
Subroutine
blog: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Check if live tests should be skipped (env var or already rate limited) | ||||||||||||||||||||||||||||
| 26 | 5 | ▶ | sub skip_live_tests { | ||||||||||||||||||||||||||
|
Subroutine
skip_live_tests: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | return 'TUMBLR_SKIP_LIVE_TESTS environment variable is set' | ||||||||||||||||||||||||||||
| 28 | 5 | ▶ | if $ENV{TUMBLR_SKIP_LIVE_TESTS}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | return 'Rate limit was hit earlier in this test run' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if $rate_limited; | ||||||||||||||||||||||||||||
| 31 | 5 | return; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Check if we should skip posting tests specifically | ||||||||||||||||||||||||||||
| 35 | 5 | ▶ | sub skip_posting_tests { | ||||||||||||||||||||||||||
|
Subroutine
skip_posting_tests: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 5 | my $reason = skip_live_tests(); | |||||||||||||||||||||||||||
| 37 | 5 | ▶ | return $reason if $reason; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | return 'TUMBLR_SKIP_POSTING_TESTS environment variable is set' | ||||||||||||||||||||||||||||
| 39 | 5 | ▶ | if $ENV{TUMBLR_SKIP_POSTING_TESTS}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 5 | return; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Check an error object for rate limiting; sets flag and returns true if rate limited | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub check_rate_limit { | ||||||||||||||||||||||||||
|
Subroutine
check_rate_limit: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 0 | my $error = shift; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | return 0 unless $error && $error->can('is_rate_limited'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$error and $error->can("is_rate_limited")
MC/DC0%0/2
$error and $error->can("is_rate_limited")
$error
$error->can("is_rate_limited")
Truth table0%0/3
A and B
A= $errorB= $error->can("is_rate_limited")
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ($error->is_rate_limited) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | $rate_limited = 1; | |||||||||||||||||||||||||||
| 50 | 0 | diag("NOTE: Tumblr rate limit hit - skipping remaining posting tests"); | |||||||||||||||||||||||||||
| 51 | 0 | diag("This is harmless and does not indicate a bug in WWW::Tumblr"); | |||||||||||||||||||||||||||
| 52 | 0 | return 1; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 0 | return 0; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Returns true if rate limited flag is set | ||||||||||||||||||||||||||||
| 58 | 5 | ▶ | sub is_rate_limited { $rate_limited } | ||||||||||||||||||||||||||
|
Subroutine
is_rate_limited: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Helper to run a test that might hit rate limits | ||||||||||||||||||||||||||||
| 61 | # Usage: rate_limit_ok { $blog->post(...) } "post succeeded"; | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub rate_limit_ok (&$) { | ||||||||||||||||||||||||||
|
Subroutine
rate_limit_ok: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($code, $name) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if (my $skip = skip_posting_tests()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | SKIP: { | ||||||||||||||||||||||||||||
| 67 | 0 | skip $skip, 1; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | return 1; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my $result = $code->(); | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ($result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | pass($name); | |||||||||||||||||||||||||||
| 75 | 0 | return 1; | |||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | # Check if it's a rate limit error | ||||||||||||||||||||||||||||
| 78 | 0 | my $obj = $t->blog('perlapi.tumblr.com'); | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ($obj->error && check_rate_limit($obj->error)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$obj->error and check_rate_limit($obj->error)
MC/DC0%0/2
$obj->error and check_rate_limit($obj->error)
$obj->error
check_rate_limit($obj->error)
Truth table0%0/3
A and B
A= $obj->errorB= check_rate_limit($obj->error)
| |||||||||||||||||||||||||||||
| 80 | SKIP: { | ||||||||||||||||||||||||||||
| 81 | 0 | skip "Rate limit exceeded", 1; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | return 1; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 0 | fail($name); | |||||||||||||||||||||||||||
| 86 | 0 | return 0; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 15 | 1; | |||||||||||||||||||||||||||