TestRail-API.t 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. use strict;
  2. use warnings;
  3. use FindBin;
  4. use lib "$FindBin::Bin/lib";
  5. use TestRail::API;
  6. use Test::LWP::UserAgent::TestRailMock;
  7. use Test::More tests => 96;
  8. use Test::Fatal;
  9. use Test::Deep;
  10. use Scalar::Util ();
  11. use Capture::Tiny qw{capture};
  12. my $apiurl = $ENV{'TESTRAIL_API_URL'};
  13. my $login = $ENV{'TESTRAIL_USER'};
  14. my $pw = $ENV{'TESTRAIL_PASSWORD'};
  15. #Mock if nothing is provided
  16. my $is_mock = (!$apiurl && !$login && !$pw);
  17. like(exception {TestRail::API->new('trash','bogus','bogus');}, qr/invalid uri/i, "Non-URIs bounce constructor");
  18. #XXX for some insane reason 'hokum.bogus' seems to be popular with cpantesters
  19. my $bogoError = exception { capture { TestRail::API->new('http://hokum.bogus','lies','moreLies',undef,0); } };
  20. SKIP: {
  21. skip("Some CPANTesters like to randomly redirect all DNS misses to some other host, apparently", 1) if ($bogoError =~ m/404|302/);
  22. skip("Occasionally upon first run of tests, LWP mocking will just not work.",1) if ($bogoError =~ m/Unknown error occurred/i);
  23. like($bogoError, qr/Could not communicate with TestRail Server/i,"Bogus Testrail URI rejected");
  24. }
  25. SKIP: {
  26. skip("Testing authentication not supported with mock",2) if ($is_mock);
  27. like(exception {TestRail::API->new($apiurl,'lies','moreLies',undef,0,0); }, qr/Bad user credentials/i,"Bogus Testrail User rejected");
  28. like(exception {TestRail::API->new($apiurl,$login,'m043L13s ',undef,0); }, qr/Bad user credentials/i,"Bogus Testrail Password rejected");
  29. }
  30. ($apiurl,$login,$pw) = ('http://testrail.local','teodesian@cpan.org','fake') if $is_mock;
  31. my $tr = new TestRail::API($apiurl,$login,$pw,undef,1);
  32. #Mock if necesary
  33. $tr->{'debug'} = 0;
  34. $tr->{'browser'} = $Test::LWP::UserAgent::TestRailMock::mockObject if $is_mock;
  35. my $res;
  36. capture { $res = $tr->_doRequest('noSuchMethod') };
  37. is($res,-404,'Requesting bad URI returns 404');
  38. #Test USER methods
  39. my $userlist = $tr->getUsers();
  40. ok(@$userlist,"Get Users returns list");
  41. my $myuser = $tr->getUserByEmail($login);
  42. is($myuser->{'email'},$login,"Can get user by email");
  43. is($tr->getUserByID($myuser->{'id'})->{'id'},$myuser->{'id'},"Can get user by ID");
  44. is($tr->getUserByName($myuser->{'name'})->{'name'},$myuser->{'name'},"Can get user by Name");
  45. my @user_names = map {$_->{'name'}} @$userlist;
  46. my @user_ids = map {$_->{'id'}} @$userlist;
  47. my @cuser_ids = $tr->userNamesToIds(@user_names);
  48. cmp_deeply(\@cuser_ids,\@user_ids,"userNamesToIds functions correctly");
  49. isnt(exception {$tr->userNamesToIds(@user_names,'potzrebie'); }, undef, "Passing invalid user name throws exception");
  50. #Test CASE TYPE method
  51. my $caseTypes = $tr->getCaseTypes();
  52. is(ref($caseTypes),'ARRAY',"getCaseTypes returns ARRAY of case types");
  53. my @type_names = map {$_->{'name'}} @$caseTypes;
  54. my @type_ids = map {$_->{'id'}} @$caseTypes;
  55. is($tr->getCaseTypeByName($type_names[0])->{'id'},$type_ids[0],"Can get case type by name correctly");
  56. my @computed_type_ids = $tr->typeNamesToIds(@type_names);
  57. cmp_deeply(\@computed_type_ids,\@type_ids,"typeNamesToIds returns the correct type IDs in the correct order");
  58. #Test PRIORITY method
  59. my $priorities = $tr->getPriorities();
  60. is(ref($priorities),'ARRAY',"getPriorities returns ARRAY of priorities");
  61. my @priority_names = map {$_->{'name'}} @$priorities;
  62. my @priority_ids = map {$_->{'id'}} @$priorities;
  63. is($tr->getPriorityByName($priority_names[0])->{'id'},$priority_ids[0],"Can get case priority by name correctly");
  64. my @computed_priority_ids = $tr->priorityNamesToIds(@priority_names);
  65. cmp_deeply(\@computed_priority_ids,\@priority_ids,"priorityNamesToIds returns the correct priority IDs in the correct order");
  66. #Test PROJECT methods
  67. my $project_name = 'CRUSH ALL HUMANS';
  68. my $new_project = $tr->createProject($project_name,'Robo-Signed Soviet 5 Year Project');
  69. is($new_project->{'name'},$project_name,"Can create new project");
  70. ok($tr->getProjects(),"Get Projects returns list");
  71. ok($tr->getProjects({ is_completed => 1 }),"Get Projects returns a filtered list");
  72. is($tr->getProjectByName($project_name)->{'name'},$project_name,"Can get project by name");
  73. my $pjid = $tr->getProjectByID($new_project->{'id'});
  74. is(Scalar::Util::reftype($pjid) eq 'HASH' ? $pjid->{'id'} : $pjid,$new_project->{'id'},"Can get project by id");
  75. #Test TESTSUITE methods
  76. my $suite_name = 'HAMBURGER-IZE HUMANITY';
  77. my $new_suite = $tr->createTestSuite($new_project->{'id'},$suite_name,"Robo-Signed Patriotic People's TestSuite");
  78. is($new_suite->{'name'},$suite_name,"Can create new testsuite");
  79. ok($tr->getTestSuites($new_project->{'id'}),"Can get listing of testsuites for project");
  80. is($tr->getTestSuiteByName($new_project->{'id'},$new_suite->{'name'})->{'name'},$new_suite->{'name'},"Can get suite by name");
  81. is($tr->getTestSuiteByID($new_suite->{'id'})->{'id'},$new_suite->{'id'},"Can get suite by id");
  82. #Test SECTION methods -- roughly analogous to TESTSUITES in TL
  83. my $section_name = 'CARBON LIQUEFACTION';
  84. my $new_section = $tr->createSection($new_project->{'id'},$new_suite->{'id'},$section_name);
  85. is($new_section->{'name'},$section_name,"Can create new section");
  86. ok($tr->getSections($new_project->{'id'},$new_suite->{'id'}),"Can get section listing");
  87. is($tr->getSectionByName($new_project->{'id'},$new_suite->{'id'},$section_name)->{'name'},$section_name,"Can get section by name");
  88. is($tr->getSectionByID($new_section->{'id'})->{'id'},$new_section->{'id'},"Can get new section by id");
  89. my @cids = $tr->sectionNamesToIds($new_project->{'id'},$new_suite->{'id'},$section_name);
  90. is($cids[0],$new_section->{'id'},"sectionNamesToIds returns correct IDs");
  91. isnt(exception {$tr->sectionNamesToIds($new_project->{'id'},$new_suite->{'id'},"No such Section");},undef,"Passing bogus section to sectionNamesToIds throws exception");
  92. #Test CASE methods
  93. my $case_name = 'STROGGIFY POPULATION CENTERS';
  94. my $new_case = $tr->createCase($new_section->{'id'},$case_name);
  95. is($new_case->{'title'},$case_name,"Can create new test case");
  96. my $updated_case = $tr->updateCase($new_case->{'id'}, {'custom_preconds' => 'do some stuff'});
  97. is($updated_case->{'custom_preconds'},'do some stuff',"updateCase works");
  98. my $case_filters = {
  99. 'section_id' => $new_section->{'id'}
  100. };
  101. ok($tr->getCases($new_project->{'id'},$new_suite->{'id'},$case_filters),"Can get case listing");
  102. ok($tr->getCases($new_project->{'id'},$new_suite->{'id'},{'hokum' => 'bogus'}),"Passing bogus filter returns cases");
  103. is($tr->getCaseByName($new_project->{'id'}, $new_suite->{'id'}, $case_name, $case_filters)->{'title'},$case_name,"Can get case by name");
  104. is($tr->getCaseByID($new_case->{'id'})->{'id'},$new_case->{'id'},"Can get case by ID");
  105. #Test RUN methods
  106. my $run_name = 'SEND T-1000 INFILTRATION UNITS BACK IN TIME';
  107. my $new_run = $tr->createRun($new_project->{'id'},$new_suite->{'id'},$run_name,"ACQUIRE CLOTHES, BOOTS AND MOTORCYCLE");
  108. is($new_run->{'name'},$run_name,"Can create new run");
  109. ok($tr->getRuns($new_project->{'id'}),"Can get list of all runs in a project");
  110. ok($tr->getRuns($new_project->{'id'},{is_completed => 1,milestone_id => 3}),"Can get list of runs, filtered by milestone ID & completion status");
  111. is($tr->getRunByName($new_project->{'id'},$run_name)->{'name'},$run_name,"Can get run by name");
  112. is($tr->getRunByID($new_run->{'id'})->{'id'},$new_run->{'id'},"Can get run by ID");
  113. #Test MILESTONE methods
  114. my $milestone_name = "Humanity Exterminated";
  115. 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 :)
  116. is($new_milestone->{'name'},$milestone_name,"Can create new milestone");
  117. ok($tr->getMilestones($new_project->{'id'}),"Can get list of milestones");
  118. is($tr->getMilestoneByName($new_project->{'id'},$milestone_name)->{'name'},$milestone_name,"Can get milestone by name");
  119. is($tr->getMilestoneByID($new_milestone->{'id'})->{'id'},$new_milestone->{'id'},"Can get milestone by ID");
  120. #Test PLAN methods
  121. my $plan_name = "GosPlan";
  122. 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"}]);
  123. is($new_plan->{'name'},$plan_name,"Can create new plan");
  124. ok($tr->getPlans($new_project->{'id'}),"Can get list of all plans");
  125. ok($tr->getPlans($new_project->{'id'},{is_completed => 1,milestone_id => 3}),"Can get list of plans, filtered by milestone ID & completion status");
  126. my $namePlan = $tr->getPlanByName($new_project->{'id'},$plan_name);
  127. is($namePlan->{'name'},$plan_name,"Can get plan by name");
  128. is($tr->getPlanByID($new_plan->{'id'})->{'id'},$new_plan->{'id'},"Can get plan by ID");
  129. #Get runs per plan, create runs in plan
  130. my $prun = $new_plan->{'entries'}->[0]->{'runs'}->[0];
  131. is($tr->getRunByID($prun->{'id'})->{'name'},"Executing the great plan","Can get child run of plan by ID");
  132. is($tr->getChildRunByName($new_plan,"Executing the great plan", [], 9)->{'id'},$prun->{'id'},"Can find child run of plan by name filtering by testsuite");
  133. is($tr->getChildRunByName($new_plan,"Executing the great plan", [], 8),0,"Can't find child run of plan by name filtering by (bad) testsuite");
  134. SKIP: {
  135. skip("Cannot create configurations programattically in the API like in mocks",2) if !$is_mock;
  136. isnt($tr->getChildRunByName($namePlan,"Executing the great plan",['testConfig']),0,"Getting run by name returns child runs");
  137. is($tr->getChildRunByName($namePlan,"Executing the great plan"),0,"Getting run by name without sufficient configuration data returns child runs");
  138. }
  139. #Test createRunInPlan
  140. my $updatedPlan = $tr->createRunInPlan($new_plan->{'id'},$new_suite->{'id'},'Dynamic Plan Run');
  141. $prun = $updatedPlan->{'runs'}->[0];
  142. is($tr->getRunByID($prun->{'id'})->{'name'},"Dynamic Plan Run","Can get newly created child run of plan by ID");
  143. #Test TEST/RESULT methods
  144. my $tests = $tr->getTests($new_run->{'id'});
  145. ok($tests,"Can get tests");
  146. is($tr->getTestByName($new_run->{'id'},$tests->[0]->{'title'})->{'title'},$tests->[0]->{'title'},"Can get test by name");
  147. is($tr->getTestByID($tests->[0]->{'id'})->{'id'},$tests->[0]->{'id'},"Can get test by ID");
  148. my $resTypes = $tr->getTestResultFields();
  149. my $statusTypes = $tr->getPossibleTestStatuses();
  150. ok($resTypes,"Can get test result fields");
  151. ok($statusTypes,"Can get possible test statuses");
  152. my @status_names = map {$_->{'name'}} @$statusTypes;
  153. my @status_ids = map {$_->{'id'}} @$statusTypes;
  154. my @status_labels = map {$_->{'label'}} @$statusTypes;
  155. my @computed_ids = $tr->statusNamesToIds(@status_names);
  156. my @computed_labels = $tr->statusNamesToLabels(@status_names);
  157. cmp_deeply(\@computed_ids,\@status_ids,"statusNamesToIds functions correctly");
  158. cmp_deeply(\@computed_labels,\@status_labels,"statusNamesToLabels functions correctly");
  159. isnt(exception {$tr->statusNamesToIds(@status_names,'potzrebie'); }, undef, "Passing invalid status name throws exception in statusNamesToIds");
  160. isnt(exception {$tr->statusNamesToLabels(@status_names,'potzrebie'); }, undef, "Passing invalid status name throws exception in statusNamesToLabels");
  161. #TODO make more thorough tests for options, custom options
  162. my $result = $tr->createTestResults($tests->[0]->{'id'},$statusTypes->[0]->{'id'},"REAPER FORCES INBOUND");
  163. ok(defined($result->{'id'}),"Can add test results");
  164. my $results = $tr->getTestResults($tests->[0]->{'id'});
  165. is($results->[0]->{'id'},$result->{'id'},"Can get results for test");
  166. #Bulk add results
  167. $results = $tr->bulkAddResults($new_run->{'id'}, [{ 'test_id' => $tests->[0]->{'id'},'status_id' => $statusTypes->[0]->{'id'}, "comment" => "REAPER FORCES INBOUND" }]);
  168. ok(defined($results->[0]->{'id'}),"Can bulk add test results");
  169. #Test status and assignedto filtering
  170. my $filteredTests = $tr->getTests($new_run->{'id'},[$status_ids[0]]);
  171. is(scalar(@$filteredTests),1,"Test Filtering works: status id positive");
  172. $filteredTests = $tr->getTests($new_run->{'id'},[$status_ids[1]]);
  173. is(scalar(@$filteredTests),0,"Test Filtering works: status id negative");
  174. $filteredTests = $tr->getTests($new_run->{'id'},[$status_ids[0]],[$userlist->[0]->{'id'}]);
  175. is(scalar(@$filteredTests),0,"Test Filtering works: status id positive, user id negative");
  176. $filteredTests = $tr->getTests($new_run->{'id'},undef,[$userlist->[0]->{'id'}]);
  177. is(scalar(@$filteredTests),0,"Test Filtering works: status id undef, user id negative");
  178. #XXX there is no way to programmatically assign things :( so this will remain somewhat uncovered
  179. #Get run summary
  180. my $runs = $tr->getRuns($new_project->{'id'});
  181. my ($summary) = $tr->getRunSummary(@$runs); #I only care about the first one
  182. isnt($summary->{'run_status'},undef,"Can get run statuses correctly");
  183. is($summary->{'run_status'}->{'Passed'},int(!$is_mock),"Gets # of passed cases correctly");
  184. is($summary->{'run_status'}->{'Untested'},int($is_mock),"Gets # of untested cases correctly");
  185. #Get run results
  186. my $run_results = $tr->getRunResults($new_run->{'id'});
  187. is(scalar(@$run_results),3,"Correct # of results returned by getRunResults");
  188. #Test configuration methods
  189. my $configs = $tr->getConfigurations($new_project->{'id'});
  190. my $is_arr = is(Scalar::Util::reftype($configs),'ARRAY',"Can get configurations for a project");
  191. my (@config_names,@config_ids);
  192. if ($is_arr) {
  193. @config_names = map {$_->{'name'}} @$configs;
  194. @config_ids = map {$_->{'id'}} @$configs;
  195. }
  196. my @t_config_ids = $tr->translateConfigNamesToIds($new_project->{'id'},@config_names);
  197. is_deeply(\@config_ids,\@t_config_ids, "Can correctly translate Project names to IDs, and they are correctly sorted");
  198. my $grp = $tr->addConfigurationGroup($new_project->{'id'},"zippy");
  199. is($grp->{'name'},'zippy',"Can add configuration group successfully");
  200. my $fetchedgrp = $tr->getConfigurationGroupByName($new_project->{'id'},'zippy');
  201. is($fetchedgrp->{'id'},$grp->{'id'},"Can get configuration group by name");
  202. my $newgrp = $tr->editConfigurationGroup($grp->{'id'},"doodah");
  203. is($newgrp->{'name'},'doodah',"Can edit configuration group successfully");
  204. my $config = $tr->addConfiguration($grp->{'id'},"potzrebie");
  205. is($config->{'name'},"potzrebie","Can add configuration successfully");
  206. my $newconfig = $tr->editConfiguration($config->{'id'},"poyiut");
  207. is($newconfig->{'name'},"poyiut","Can edit configuration successfully");
  208. ok($tr->deleteConfiguration($config->{'id'}),"Can delete configuration successfully");
  209. ok($tr->deleteConfigurationGroup($grp->{'id'}),"Can delete group successfully");
  210. ############################################################
  211. # TestRail arbitrarily limits many calls to 250 result sets.
  212. # Let's make sure our getters actually get everything.
  213. ############################################################
  214. SKIP: {
  215. skip("Skipping slow tests...", 2) if $ENV{'TESTRAIL_SLOW_TESTS'};
  216. #Check get_plans
  217. foreach my $i (0..$tr->{'global_limit'}) {
  218. $tr->createPlan($new_project->{'id'},$plan_name,"PETE & RE-PIOTR");
  219. }
  220. is(scalar(@{$tr->getPlans($new_project->{'id'})}),($tr->{'global_limit'} + 2),"Can get list of plans beyond ".$tr->{'global_limit'});
  221. #Check get_runs
  222. foreach my $i (0..$tr->{'global_limit'}) {
  223. $tr->createRun($new_project->{'id'},$new_suite->{'id'},$run_name,"ACQUIRE CLOTHES, BOOTS AND MOTORCYCLE");
  224. }
  225. is(scalar(@{$tr->getRuns($new_project->{'id'})}),($tr->{'global_limit'} + 2),"Can get list of runs beyond ".$tr->{'global_limit'});
  226. }
  227. ##########
  228. # Clean up
  229. ##########
  230. #Delete a plan
  231. ok($tr->deletePlan($new_plan->{'id'}),"Can delete plan");
  232. #Delete a milestone
  233. ok($tr->deleteMilestone($new_milestone->{'id'}),"Can delete milestone");
  234. #Delete a run
  235. ok($tr->deleteRun($new_run->{'id'}),"Can delete run");
  236. #Delete a case
  237. ok($tr->deleteCase($new_case->{'id'}),"Can delete Case");
  238. #Delete a section
  239. ok($tr->deleteSection($new_section->{'id'}),"Can delete Section");
  240. #Delete a testsuite
  241. ok($tr->deleteTestSuite($new_suite->{'id'}),"Can delete TestSuite");
  242. #Delete project now that we are done with it
  243. ok($tr->deleteProject($new_project->{'id'}),"Can delete project");
  244. 1;