TestRail-API.t 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. use strict;
  2. use warnings;
  3. use TestRail::API;
  4. use Test::LWP::UserAgent::TestRailMock;
  5. use Test::More tests => 71;
  6. use Test::Fatal;
  7. use Test::Deep;
  8. use Scalar::Util 'reftype';
  9. use ExtUtils::MakeMaker qw{prompt};
  10. my $apiurl = $ENV{'TESTRAIL_API_URL'};
  11. my $login = $ENV{'TESTRAIL_USER'};
  12. my $pw = $ENV{'TESTRAIL_PASSWORD'};
  13. #Mock if nothing is provided
  14. my $is_mock = (!$apiurl && !$login && !$pw);
  15. like(exception {TestRail::API->new('trash');}, qr/invalid uri/i, "Non-URIs bounce constructor");
  16. #XXX for some insane reason 'hokum.bogus' seems to be popular with cpantesters
  17. my $bogoError = exception {TestRail::API->new('http://hokum.bogus','lies','moreLies',0); };
  18. SKIP: {
  19. skip("Some CPANTesters like to randomly redirect all DNS misses to some other host, apparently", 1) if ($bogoError =~ m/404/);
  20. like($bogoError, qr/Could not communicate with TestRail Server/i,"Bogus Testrail URI rejected");
  21. }
  22. SKIP: {
  23. skip("Testing authentication not supported with mock",2) if ($is_mock);
  24. like(exception {TestRail::API->new($apiurl,'lies','moreLies',0); }, qr/Bad user credentials/i,"Bogus Testrail User rejected");
  25. like(exception {TestRail::API->new($apiurl,$login,'m043L13s ',0); }, qr/Bad user credentials/i,"Bogus Testrail Password rejected");
  26. }
  27. ($apiurl,$login,$pw) = ('http://testrail.local','teodesian@cpan.org','fake') if $is_mock;
  28. my $tr = new TestRail::API($apiurl,$login,$pw,1);
  29. #Mock if necesary
  30. $tr->{'debug'} = 0;
  31. $tr->{'browser'} = $Test::LWP::UserAgent::TestRailMock::mockObject if $is_mock;
  32. is($tr->_doRequest('noSuchMethod'),-404,'Requesting bad URI returns 404');
  33. #Test USER methods
  34. my $userlist = $tr->getUsers();
  35. ok(@$userlist,"Get Users returns list");
  36. my $myuser = $tr->getUserByEmail($login);
  37. is($myuser->{'email'},$login,"Can get user by email");
  38. is($tr->getUserByID($myuser->{'id'})->{'id'},$myuser->{'id'},"Can get user by ID");
  39. is($tr->getUserByName($myuser->{'name'})->{'name'},$myuser->{'name'},"Can get user by Name");
  40. my @user_names = map {$_->{'name'}} @$userlist;
  41. my @user_ids = map {$_->{'id'}} @$userlist;
  42. my @cuser_ids = $tr->userNamesToIds(@user_names);
  43. cmp_deeply(\@cuser_ids,\@user_ids,"userNamesToIds functions correctly");
  44. isnt(exception {$tr->userNamesToIds(@user_names,'potzrebie'); }, undef, "Passing invalid user name throws exception");
  45. #Test PROJECT methods
  46. my $project_name = 'CRUSH ALL HUMANS';
  47. my $new_project = $tr->createProject($project_name,'Robo-Signed Soviet 5 Year Project');
  48. is($new_project->{'name'},$project_name,"Can create new project");
  49. ok($tr->getProjects(),"Get Projects returns list");
  50. is($tr->getProjectByName($project_name)->{'name'},$project_name,"Can get project by name");
  51. my $pjid = $tr->getProjectByID($new_project->{'id'});
  52. is(reftype($pjid) eq 'HASH' ? $pjid->{'id'} : $pjid,$new_project->{'id'},"Can get project by id");
  53. #Test TESTSUITE methods
  54. my $suite_name = 'HAMBURGER-IZE HUMANITY';
  55. my $new_suite = $tr->createTestSuite($new_project->{'id'},$suite_name,"Robo-Signed Patriotic People's TestSuite");
  56. is($new_suite->{'name'},$suite_name,"Can create new testsuite");
  57. ok($tr->getTestSuites($new_project->{'id'}),"Can get listing of testsuites for project");
  58. is($tr->getTestSuiteByName($new_project->{'id'},$new_suite->{'name'})->{'name'},$new_suite->{'name'},"Can get suite by name");
  59. is($tr->getTestSuiteByID($new_suite->{'id'})->{'id'},$new_suite->{'id'},"Can get suite by id");
  60. #Test SECTION methods -- roughly analogous to TESTSUITES in TL
  61. my $section_name = 'CARBON LIQUEFACTION';
  62. my $new_section = $tr->createSection($new_project->{'id'},$new_suite->{'id'},$section_name);
  63. is($new_section->{'name'},$section_name,"Can create new section");
  64. ok($tr->getSections($new_project->{'id'},$new_suite->{'id'}),"Can get section listing");
  65. is($tr->getSectionByName($new_project->{'id'},$new_suite->{'id'},$section_name)->{'name'},$section_name,"Can get section by name");
  66. is($tr->getSectionByID($new_section->{'id'})->{'id'},$new_section->{'id'},"Can get new section by id");
  67. #Test CASE methods
  68. my $case_name = 'STROGGIFY POPULATION CENTERS';
  69. my $new_case = $tr->createCase($new_section->{'id'},$case_name);
  70. is($new_case->{'title'},$case_name,"Can create new test case");
  71. ok($tr->getCases($new_project->{'id'},$new_suite->{'id'},$new_section->{'id'}),"Can get case listing");
  72. is($tr->getCaseByName($new_project->{'id'}, $new_suite->{'id'}, $new_section->{'id'}, $case_name)->{'title'},$case_name,"Can get case by name");
  73. is($tr->getCaseByID($new_case->{'id'})->{'id'},$new_case->{'id'},"Can get case by ID");
  74. #Test RUN methods
  75. my $run_name = 'SEND T-1000 INFILTRATION UNITS BACK IN TIME';
  76. my $new_run = $tr->createRun($new_project->{'id'},$new_suite->{'id'},$run_name,"ACQUIRE CLOTHES, BOOTS AND MOTORCYCLE");
  77. is($new_run->{'name'},$run_name,"Can create new run");
  78. ok($tr->getRuns($new_project->{'id'}),"Can get list of runs");
  79. is($tr->getRunByName($new_project->{'id'},$run_name)->{'name'},$run_name,"Can get run by name");
  80. is($tr->getRunByID($new_run->{'id'})->{'id'},$new_run->{'id'},"Can get run by ID");
  81. #Test MILESTONE methods
  82. my $milestone_name = "Humanity Exterminated";
  83. my $new_milestone = $tr->createMilestone($new_project->{'id'},$milestone_name,"Kill quota reached if not achieved in 5 years",time()+157788000); #It IS a soviet 5-year plan after all :)
  84. is($new_milestone->{'name'},$milestone_name,"Can create new milestone");
  85. ok($tr->getMilestones($new_project->{'id'}),"Can get list of milestones");
  86. is($tr->getMilestoneByName($new_project->{'id'},$milestone_name)->{'name'},$milestone_name,"Can get milestone by name");
  87. is($tr->getMilestoneByID($new_milestone->{'id'})->{'id'},$new_milestone->{'id'},"Can get milestone by ID");
  88. #Test PLAN methods
  89. my $plan_name = "GosPlan";
  90. my $new_plan = $tr->createPlan($new_project->{'id'},$plan_name,"Soviet 5-year agriculture plan to liquidate Kulaks",$new_milestone->{'id'},[{ suite_id => $new_suite->{'id'}, name => "Executing the great plan"}]);
  91. is($new_plan->{'name'},$plan_name,"Can create new plan");
  92. ok($tr->getPlans($new_project->{'id'}),"Can get list of plans");
  93. my $namePlan = $tr->getPlanByName($new_project->{'id'},$plan_name);
  94. is($namePlan->{'name'},$plan_name,"Can get plan by name");
  95. is($tr->getPlanByID($new_plan->{'id'})->{'id'},$new_plan->{'id'},"Can get plan by ID");
  96. #Get runs per plan, create runs in plan
  97. my $prun = $new_plan->{'entries'}->[0]->{'runs'}->[0];
  98. is($tr->getRunByID($prun->{'id'})->{'name'},"Executing the great plan","Can get child run of plan by ID");
  99. is($tr->getChildRunByName($new_plan,"Executing the great plan")->{'id'},$prun->{'id'},"Can find child run of plan by name");
  100. SKIP: {
  101. skip("Cannot create configurations programattically in the API like in mocks",2) if !$is_mock;
  102. isnt($tr->getChildRunByName($namePlan,"Executing the great plan",['testConfig']),0,"Getting run by name returns child runs");
  103. is($tr->getChildRunByName($namePlan,"Executing the great plan"),0,"Getting run by name without sufficient configuration data returns child runs");
  104. }
  105. #Test createRunInPlan
  106. my $updatedPlan = $tr->createRunInPlan($new_plan->{'id'},$new_suite->{'id'},'Dynamic Plan Run');
  107. $prun = $updatedPlan->{'runs'}->[0];
  108. is($tr->getRunByID($prun->{'id'})->{'name'},"Dynamic Plan Run","Can get newly created child run of plan by ID");
  109. #Test TEST/RESULT methods
  110. my $tests = $tr->getTests($new_run->{'id'});
  111. ok($tests,"Can get tests");
  112. is($tr->getTestByName($new_run->{'id'},$tests->[0]->{'title'})->{'title'},$tests->[0]->{'title'},"Can get test by name");
  113. is($tr->getTestByID($tests->[0]->{'id'})->{'id'},$tests->[0]->{'id'},"Can get test by ID");
  114. my $resTypes = $tr->getTestResultFields();
  115. my $statusTypes = $tr->getPossibleTestStatuses();
  116. ok($resTypes,"Can get test result fields");
  117. ok($statusTypes,"Can get possible test statuses");
  118. my @status_names = map {$_->{'name'}} @$statusTypes;
  119. my @status_ids = map {$_->{'id'}} @$statusTypes;
  120. my @computed_ids = $tr->statusNamesToIds(@status_names);
  121. cmp_deeply(\@computed_ids,\@status_ids,"statusNamesToIds functions correctly");
  122. isnt(exception {$tr->statusNamesToIds(@status_names,'potzrebie'); }, undef, "Passing invalid status name throws exception");
  123. #TODO make more thorough tests for options, custom options
  124. my $result = $tr->createTestResults($tests->[0]->{'id'},$statusTypes->[0]->{'id'},"REAPER FORCES INBOUND");
  125. ok(defined($result->{'id'}),"Can add test results");
  126. my $results = $tr->getTestResults($tests->[0]->{'id'});
  127. is($results->[0]->{'id'},$result->{'id'},"Can get results for test");
  128. #Test status and assignedto filtering
  129. my $filteredTests = $tr->getTests($new_run->{'id'},[$status_ids[0]]);
  130. is(scalar(@$filteredTests),1,"Test Filtering works: status id positive");
  131. $filteredTests = $tr->getTests($new_run->{'id'},[$status_ids[1]]);
  132. is(scalar(@$filteredTests),0,"Test Filtering works: status id negative");
  133. $filteredTests = $tr->getTests($new_run->{'id'},[$status_ids[0]],[$userlist->[0]->{'id'}]);
  134. is(scalar(@$filteredTests),0,"Test Filtering works: status id positive, user id negative");
  135. $filteredTests = $tr->getTests($new_run->{'id'},undef,[$userlist->[0]->{'id'}]);
  136. is(scalar(@$filteredTests),0,"Test Filtering works: status id undef, user id negative");
  137. #XXX there is no way to programmatically assign things :( so this will remain somewhat uncovered
  138. #Get run summary
  139. my $runs = $tr->getRuns($new_project->{'id'});
  140. my ($summary) = $tr->getRunSummary(@$runs); #I only care about the first one
  141. isnt($summary->{'run_status'},undef,"Can get run statuses correctly");
  142. is($summary->{'run_status'}->{'passed'},int(!$is_mock),"Gets # of passed cases correctly");
  143. is($summary->{'run_status'}->{'untested'},int($is_mock),"Gets # of untested cases correctly");
  144. #Test configuration methods
  145. my $configs = $tr->getConfigurations($new_project->{'id'});
  146. my $is_arr = is(reftype($configs),'ARRAY',"Can get configurations for a project");
  147. my (@config_names,@config_ids);
  148. if ($is_arr) {
  149. @config_names = map {$_->{'name'}} @$configs;
  150. @config_ids = map {$_->{'id'}} @$configs;
  151. }
  152. my $t_config_ids = $tr->translateConfigNamesToIds($new_project->{'id'},\@config_names);
  153. @config_ids = sort(@config_ids);
  154. @$t_config_ids = sort(@$t_config_ids);
  155. is_deeply(\@config_ids,$t_config_ids, "Can correctly translate Project names to IDs");
  156. ############################################################
  157. # TestRail arbitrarily limits many calls to 250 result sets.
  158. # Let's make sure our getters actually get everything.
  159. ############################################################
  160. SKIP: {
  161. skip("Skipping slow tests...", 2) if $ENV{'TESTRAIL_SLOW_TESTS'};
  162. #Check get_plans
  163. foreach my $i (0..$tr->{'global_limit'}) {
  164. $tr->createPlan($new_project->{'id'},$plan_name,"PETE & RE-PIOTR");
  165. }
  166. is(scalar(@{$tr->getPlans($new_project->{'id'})}),($tr->{'global_limit'} + 2),"Can get list of plans beyond ".$tr->{'global_limit'});
  167. #Check get_runs
  168. foreach my $i (0..$tr->{'global_limit'}) {
  169. $tr->createRun($new_project->{'id'},$new_suite->{'id'},$run_name,"ACQUIRE CLOTHES, BOOTS AND MOTORCYCLE");
  170. }
  171. is(scalar(@{$tr->getRuns($new_project->{'id'})}),($tr->{'global_limit'} + 2),"Can get list of runs beyond ".$tr->{'global_limit'});
  172. }
  173. ##########
  174. # Clean up
  175. ##########
  176. #Delete a plan
  177. ok($tr->deletePlan($new_plan->{'id'}),"Can delete plan");
  178. #Delete a milestone
  179. ok($tr->deleteMilestone($new_milestone->{'id'}),"Can delete milestone");
  180. #Delete a run
  181. ok($tr->deleteRun($new_run->{'id'}),"Can delete run");
  182. #Delete a case
  183. ok($tr->deleteCase($new_case->{'id'}),"Can delete Case");
  184. #Delete a section
  185. ok($tr->deleteSection($new_section->{'id'}),"Can delete Section");
  186. #Delete a testsuite
  187. ok($tr->deleteTestSuite($new_suite->{'id'}),"Can delete TestSuite");
  188. #Delete project now that we are done with it
  189. ok($tr->deleteProject($new_project->{'id'}),"Can delete project");
  190. 1;