Cloudflare-API
view release on metacpan or search on metacpan
close($wrangler_fh) || die "unable to close mock Wrangler: $!";
chmod(0755, $wrangler_fn) || die "unable to make mock Wrangler executable: $!";
}
{
local $ENV{'PATH'}=$wrangler_dn.$Config{'path_sep'}.$ENV{'PATH'};
local $ENV{'MOCK_WRANGLER_JSON'}='{"type":"oauth","token":"oauth-test-token"}';
local $ENV{'MOCK_WRANGLER_WHOAMI_JSON'}=
'{"loggedIn":true,"accounts":[{"id":"account-id","name":"Example"}]}';
is(wrangler_token(), 'oauth-test-token', 'Wrangler OAuth token accepted');
is(wrangler_account_id(), 'account-id', 'sole Wrangler account ID accepted');
$ENV{'MOCK_WRANGLER_WHOAMI_JSON'}=
'{"loggedIn":true,"accounts":[{"id":"one"},{"id":"two"}]}';
$error=eval { wrangler_account_id(); 1 };
ok(!$error && $@=~/multiple accounts.*--account-id/, 'multiple Wrangler accounts rejected');
$ENV{'MOCK_WRANGLER_WHOAMI_JSON'}='{"loggedIn":true,"accounts":[]}';
$error=eval { wrangler_account_id(); 1 };
ok(!$error && $@=~/no available accounts/, 'empty Wrangler account list rejected');
$ENV{'MOCK_WRANGLER_WHOAMI_JSON'}='{"loggedIn":true}';
$error=eval { wrangler_account_id(); 1 };
ok(!$error && $@=~/no account list/, 'missing Wrangler account list rejected');
$ENV{'MOCK_WRANGLER_JSON'}='{"type":"api_token","token":"api-test-token"}';
is(wrangler_token(), 'api-test-token', 'Wrangler API token accepted');
$ENV{'MOCK_WRANGLER_JSON'}='{"type":"api_key","key":"secret-key","email":"x@example.test"}';
$error=eval { wrangler_token(); 1 };
ok(!$error && $@=~/unsupported credential type/, 'API key credentials rejected');
unlike($@, qr/secret-key/, 'API key is absent from error');
$ENV{'MOCK_WRANGLER_JSON'}='secret-output';
$error=eval { wrangler_token(); 1 };
ok(!$error && $@=~/invalid JSON/, 'invalid Wrangler output rejected');
unlike($@, qr/secret-output/, 'invalid output is absent from error');
$ENV{'MOCK_WRANGLER_JSON'}='{"type":"oauth","token":"bad\\nheader"}';
$error=eval { wrangler_token(); 1 };
ok(!$error && $@=~/no usable token/, 'token containing a control character rejected');
local $ENV{'MOCK_WRANGLER_FAIL'}=1;
$error=eval { wrangler_token(); 1 };
ok(!$error && $@=~/check Wrangler login/, 'Wrangler failure explains login');
}
{
local $ENV{'PATH'}=$wrangler_dn.$Config{'path_sep'}.$ENV{'PATH'};
local $ENV{'MOCK_WRANGLER_JSON'}='{"type":"oauth","token":"oauth-test-token"}';
local $ENV{'MOCK_WRANGLER_WHOAMI_JSON'}=
'{"loggedIn":true,"accounts":[{"id":"account-id","name":"Example"}]}';
local $ENV{'CLOUDFLARE_ACCOUNT_ID'};
my %client_opt;
no warnings qw(redefine once);
local *Cloudflare::API::new=sub {
my $class=shift();
%client_opt=@_;
return bless({}, $class);
};
local *Cloudflare::API::r2=sub { return bless({}, 'Cloudflare::API::R2') };
local *Cloudflare::API::R2::list_buckets_page_response=sub {
die "stop after client creation\n"
};
local *Cloudflare::API::workers=sub { return bless({}, 'Cloudflare::API::Workers') };
local *Cloudflare::API::Workers::list_routes_page_response=sub {
die "stop after client creation\n"
};
{
local @ARGV=qw(--auth=wrangler --resource r2 --action list_buckets);
$error=eval { main(\@ARGV); 1 };
}
ok(!$error && $@=~/stop after client creation/,
'Wrangler authentication reaches an account-scoped request');
is($client_opt{'token'}, 'oauth-test-token', 'Wrangler token passed to client');
is($client_opt{'account_id'}, 'account-id', 'Wrangler account ID passed to client');
$ENV{'MOCK_WRANGLER_WHOAMI_JSON'}='invalid';
%client_opt=();
{
local @ARGV=qw(--auth=wrangler --account-id explicit-id --resource r2 --action list_buckets);
$error=eval { main(\@ARGV); 1 };
}
ok(!$error && $@=~/stop after client creation/,
'explicit account ID skips Wrangler account discovery');
is($client_opt{'account_id'}, 'explicit-id', 'explicit account ID takes precedence');
$ENV{'MOCK_WRANGLER_WHOAMI_JSON'}='invalid';
%client_opt=();
{
local @ARGV=qw(--auth=wrangler --resource workers --action list_routes --arg zone-id);
$error=eval { main(\@ARGV); 1 };
}
ok(!$error && $@=~/stop after client creation/,
'zone-scoped Worker route action skips Wrangler account discovery');
ok(!exists($client_opt{'account_id'}), 'zone-scoped action needs no account ID');
}
($exit, $output)=run_cli(undef, qw(--auth=other --resource accounts --action
list --dump-opt));
isnt($exit, 0, 'unknown authentication source rejected');
like($output, qr/auth must be wrangler/, 'authentication source error is clear');
($exit, $output)=run_cli(undef, qw(--auth=wrangler --resource accounts --action
list --dump-opt));
is($exit, 0, 'Wrangler option accepted without fetching credentials in dump mode');
($exit, $output)=run_cli(undef, qw(--resource r2 --action delete_bucket --arg x
--paginate --dump-opt));
isnt($exit, 0, 'pagination rejected for non-list action');
like($output, qr/--paginate requires a list or search action/, 'pagination error is clear');
($exit, $output)=run_cli(undef, '--resource', 'secrets_store', '--action',
'create_secret', '--arg', 'store', '--arg-json',
'[{"name":"x","value":"secret","scopes":["workers"]}]', '--dump-opt');
isnt($exit, 0, 'secret-bearing action cannot dump parsed options');
unlike($output, qr/secret\"/, 'secret value not dumped');
done_testing();
( run in 2.406 seconds using v1.01-cache-2.11-cpan-ac8be02dad9 )