The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

use strict;
use FindBin;
use lib "$FindBin::Bin/lib";
#Test things we can only mock, because the API doesn't support them.
use Test::More 'tests' => 18;
use Scalar::Util qw{reftype};
use Capture::Tiny qw{capture capture_stderr};
my $browser = $Test::LWP::UserAgent::TestRailMock::mockObject;
my $tr = TestRail::API->new('http://hokum.bogus','fake','fake',undef,1);
$tr->{'browser'} = $browser;
$tr->{'debug'} = 0;
#Have to mock anything requiring configs
my $project = $tr->getProjectByName('TestProject');
my $plan = $tr->getPlanByName($project->{'id'},'HooHaaPlan');
my $runs = $tr->getChildRuns($plan);
is(reftype($runs),'ARRAY',"getChildRuns returns array");
is(scalar(@$runs),4,"getChildRuns with multi-configs in the same group returns correct # of runs");
my $summary = $tr->getPlanSummary($plan->{'id'});
is($summary->{'plan'},1094,"Plan ID makes it through in summary method");
is($summary->{'totals'}->{'Untested'},4,"Gets total number of tests correctly");
is($summary->{'percentages'}->{'Untested'},'100.00%',"Gets total percentages correctly");
#Also have to mock anything requiring test result fields (all are custom)
my $projResType = $tr->getTestResultFieldByName('step_results');
is($projResType->{'id'},6,"Can get result field by name");
$projResType = $tr->getTestResultFieldByName('step_results',$project->{'id'});
is($projResType->{'id'},6,"Can get result field by name, AND filter by project ID");
$projResType = $tr->getTestResultFieldByName('moo_results');
is($projResType,0,"Bad name returns no result field");
$projResType = $tr->getTestResultFieldByName('step_results',66669);
is($projResType,-3,"Bad project returns no result field");
# I can't delete closed plans, so...test closePlan et cetera
is(reftype($tr->closeRun(666)),'HASH',"Can close run that exists");
my $res;
capture { $res = $tr->closeRun(90210) };
is($res,-404,"Can't close run that doesn't exist");
is(reftype($tr->closePlan(23)),'HASH',"Can close plan that exists");
capture { $res = $tr->closePlan(75020) };
is($res,-404,"Can't close plan that doesn't exist");
# Test case type method
my $ct = $tr->getCaseTypeByName("Automated");
is($ct->{'id'},1,"Can get case type by name");
#Test #142
$tr = TestRail::API->new('http://locked.out','fake','fake',undef,1);
$tr->{'browser'} = $browser;
$tr->{'debug'} = 0;
like( exception { $tr->getUsers() } , qr/stay out you red menace/i, "API dies on bad auth");
$tr = TestRail::API->new('http://locked.out/worse','fake','fake',undef,1);
$tr->{'browser'} = $browser;
$tr->{'debug'} = 0;
like( exception { $tr->getUsers() } , qr/could not find pants/i, "API dies on no auth or auth backend failure");
$tr = TestRail::API->new('http://bork.bork','fake','fake',undef,1,undef,2);
$tr->{'browser'} = $browser;
$tr->{'debug'} = 0;
$tr->{retry_delay} = 0;
my $rc;
my $oot = capture_stderr { $rc = $tr->getUsers() };
like ($oot,qr/re-trying request/i, "Tried twice to make the call work");
is($rc,-500,"Right code returned when re-tries run out");