Google-RestApi
view release on metacpan or search on metacpan
t/unit/Test/Google/RestApi.pm view on Meta::CPAN
sub _constructor : Tests(8) {
my $self = shift;
throws_ok sub { RestApi->new(config_file => 'x'); }, qr/did not pass type constraint/i, 'Constructor from bad config file should throw';
ok my $api = RestApi->new(config_file => mock_config_file()), 'Constructor from proper config_file should succeed';
isa_ok $api, RestApi, 'Constructor returns';
# config file with google_restapi key and extra app-level keys should work.
my $google_restapi_config = _write_temp_config({
my_app => { db => 'mydb' },
google_restapi => {
auth => {
class => 'OAuth2Client',
client_id => 'x',
client_secret => 'x',
token_file => mock_token_file(),
},
},
});
ok $api = RestApi->new(config_file => $google_restapi_config),
'Constructor with google_restapi key and extra keys should succeed';
isa_ok $api->auth(), OAuth2Client, 'Auth resolved from google_restapi config';
ok !exists $api->{my_app}, 'App-level keys outside google_restapi are not passed through';
# log4perl_config in the config file should be accepted and resolved.
# Use a quiet config so initializing Log4perl here doesn't spam subsequent tests.
require FindBin;
require File::Spec;
my $log4perl_conf = File::Spec->catfile($FindBin::RealBin, 'etc', 'log4perl_quiet.conf');
my $log4perl_config = _write_temp_config({
google_restapi => {
auth => {
class => 'OAuth2Client',
client_id => 'x',
client_secret => 'x',
token_file => mock_token_file(),
},
log4perl_config => $log4perl_conf,
},
});
ok $api = RestApi->new(config_file => $log4perl_config),
'Constructor with log4perl_config should succeed';
ok Log::Log4perl::initialized(), 'Log4perl initialized from log4perl_config';
return;
}
my @_temp_configs;
sub _write_temp_config {
my ($data, $dir) = @_;
require File::Basename;
require File::Temp;
require YAML::Any;
require FindBin;
require File::Spec;
$dir //= File::Spec->catdir($FindBin::RealBin, 'etc');
my $fh = File::Temp->new(SUFFIX => '.yaml', DIR => $dir, UNLINK => 1);
print $fh YAML::Any::Dump($data);
$fh->flush();
push @_temp_configs, $fh; # keep object alive; deleted at program end
return $fh->filename();
}
sub auth : Tests(4) {
my $self = shift;
my %auth = (
auth => {
class => 'x',
client_id => 'x',
client_secret => 'x',
token_file => 'x',
},
);
my $api = RestApi->new(%auth);
throws_ok sub { $api->auth(); }, qr/you may need to install/i, 'Bad auth class should throw';
$auth{auth}->{class} = 'OAuth2Client';
$api = RestApi->new(%auth);
throws_ok sub { $api->auth() }, qr/unable to resolve/i, 'Bad token file should throw';
$auth{auth}->{class} = 'OAuth2Client';
$auth{auth}->{token_file} = mock_token_file();
$api = RestApi->new(%auth);
isa_ok $api->auth(), OAuth2Client, 'Proper token file should be found';
%auth = (
auth => {
class => 'ServiceAccount',
account_file => 'x',
scope => ['x'],
},
);
$api = RestApi->new(%auth);
throws_ok sub { $api->auth()->account_file() }, qr/unable to resolve/i, 'Bad account file should throw';
return;
}
# token_file path resolution: absolute paths pass through, bare names resolve
# against the auth config_file's dir first, then the main config_file's dir.
sub auth_token_file_paths : Tests(4) {
my $self = shift;
require File::Basename;
require File::Spec;
my $token_name = File::Basename::basename(mock_token_file());
my $main_abs = _write_temp_config({
auth => {
class => 'OAuth2Client',
client_id => 'x',
client_secret => 'x',
token_file => mock_token_file(),
},
});
isa_ok RestApi->new(config_file => $main_abs)->auth(), OAuth2Client,
'Absolute token_file in main YAML resolves';
( run in 2.649 seconds using v1.01-cache-2.11-cpan-df04353d9ac )