RestAPIUserTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file test/phpunit/RestAPIUserTest.php
  21. * \ingroup test
  22. * \brief PHPUnit test
  23. * \remarks To run this script as CLI: phpunit filename.php
  24. */
  25. global $conf,$user,$langs,$db;
  26. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  27. //require_once 'PHPUnit/Autoload.php';
  28. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  29. require_once dirname(__FILE__).'/../../htdocs/core/lib/date.lib.php';
  30. require_once dirname(__FILE__).'/../../htdocs/core/lib/geturl.lib.php';
  31. if (empty($user->id)) {
  32. print "Load permissions for admin user nb 1\n";
  33. $user->fetch(1);
  34. $user->getrights();
  35. }
  36. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  37. $conf->global->MAIN_UMASK='0666';
  38. /**
  39. * Class for PHPUnit tests
  40. *
  41. * @backupGlobals disabled
  42. * @backupStaticAttributes enabled
  43. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  44. */
  45. class RestAPIUserTest extends PHPUnit\Framework\TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. protected $api_url;
  52. protected $api_key;
  53. /**
  54. * Constructor
  55. * We save global variables into local variables
  56. *
  57. * @param string $name Name
  58. * @return RestAPIUserTest
  59. */
  60. public function __construct($name = '')
  61. {
  62. parent::__construct($name);
  63. //$this->sharedFixture
  64. global $conf,$user,$langs,$db;
  65. $this->savconf=$conf;
  66. $this->savuser=$user;
  67. $this->savlangs=$langs;
  68. $this->savdb=$db;
  69. if (!isModEnabled('api')) {
  70. print __METHOD__." module api must be enabled.\n"; die(1);
  71. }
  72. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  73. //print " - db ".$db->db;
  74. print "\n";
  75. }
  76. /**
  77. * setUpBeforeClass
  78. *
  79. * @return void
  80. */
  81. public static function setUpBeforeClass(): void
  82. {
  83. global $conf,$user,$langs,$db;
  84. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  85. print __METHOD__."\n";
  86. }
  87. /**
  88. * tearDownAfterClass
  89. *
  90. * @return void
  91. */
  92. public static function tearDownAfterClass(): void
  93. {
  94. global $conf,$user,$langs,$db;
  95. $db->rollback();
  96. print __METHOD__."\n";
  97. }
  98. /**
  99. * Init phpunit tests
  100. *
  101. * @return void
  102. */
  103. protected function setUp(): void
  104. {
  105. global $conf,$user,$langs,$db;
  106. $conf=$this->savconf;
  107. $user=$this->savuser;
  108. $langs=$this->savlangs;
  109. $db=$this->savdb;
  110. $this->api_url=DOL_MAIN_URL_ROOT.'/api/index.php';
  111. $login='admin';
  112. $password='admin';
  113. $url=$this->api_url.'/login?login='.$login.'&password='.$password;
  114. // Call the API login method to save api_key for this test class
  115. $result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
  116. print __METHOD__." result = ".var_export($result, true)."\n";
  117. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  118. $this->assertEquals($result['curl_error_no'], '');
  119. $object=json_decode($result['content'], true);
  120. $this->assertNotNull($object, "Parsing of json result must not be null");
  121. $this->assertEquals('200', $object['success']['code']);
  122. $this->api_key = $object['success']['token'];
  123. print __METHOD__." api_key: $this->api_key \n";
  124. print __METHOD__."\n";
  125. }
  126. /**
  127. * End phpunit tests
  128. *
  129. * @return void
  130. */
  131. protected function tearDown(): void
  132. {
  133. print __METHOD__."\n";
  134. }
  135. /**
  136. * testRestGetUser
  137. *
  138. * @return int
  139. */
  140. public function testRestGetUser()
  141. {
  142. global $conf,$user,$langs,$db;
  143. $url = $this->api_url.'/users/123456789?api_key='.$this->api_key;
  144. //$addheaders=array('Content-Type: application/json');
  145. print __METHOD__." Request GET url=".$url."\n";
  146. $result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
  147. //print __METHOD__." result for get on unexisting user: ".var_export($result, true)."\n";
  148. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  149. $this->assertEquals($result['curl_error_no'], '');
  150. $object=json_decode($result['content'], true);
  151. $this->assertNotNull($object, "Parsing of json result must not be null");
  152. $this->assertEquals(404, $object['error']['code'], 'Error code is not 404');
  153. $url = $this->api_url.'/users/1?api_key='.$this->api_key;
  154. print __METHOD__." Request GET url=".$url."\n";
  155. $result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
  156. print __METHOD__." result for get on an existing user: ".var_export($result, true)."\n";
  157. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  158. $this->assertEquals($result['curl_error_no'], '');
  159. $object=json_decode($result['content'], true);
  160. $this->assertNotNull($object, "Parsing of json result must not be null");
  161. $this->assertEquals(1, $object['statut']);
  162. }
  163. /**
  164. * testRestCreateUser
  165. *
  166. * @return void
  167. */
  168. public function testRestCreateUser()
  169. {
  170. // attemp to create without mandatory fields :
  171. $url = $this->api_url.'/users?api_key='.$this->api_key;
  172. $addheaders=array('Content-Type: application/json');
  173. $bodyobj = array(
  174. "lastname"=>"testRestUser",
  175. "password"=>"testRestPassword",
  176. "email"=>"test@restuser.com"
  177. );
  178. $body = json_encode($bodyobj);
  179. print __METHOD__." Request POST url=".$url."\n";
  180. $result=getURLContent($url, 'POST', $body, 1, $addheaders, array('http', 'https'), 2);
  181. //print __METHOD__." Result for creating incomplete user".var_export($result, true)."\n";
  182. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  183. $this->assertEquals($result['curl_error_no'], '');
  184. $object=json_decode($result['content'], true);
  185. $this->assertNotNull($object, "Parsing of json result must no be null");
  186. $this->assertEquals(500, $object['error']['code'], $object['error']['code'].' '.$object['error']['message']);
  187. // create regular user
  188. unset($result);
  189. $bodyobj = array(
  190. "login"=>"testRestLogin".mt_rand(),
  191. "lastname"=>"testRestUser",
  192. "password"=>"testRestPassword",
  193. "email"=>"test@restuser.com"
  194. );
  195. $body = json_encode($bodyobj);
  196. print __METHOD__." Request POST url=".$url."\n";
  197. $result=getURLContent($url, 'POST', $body, 1, $addheaders, array('http', 'https'), 2);
  198. print __METHOD__." rclsesult code for creating user ".var_export($result, true)."\n";
  199. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  200. $this->assertEquals($result['curl_error_no'], '');
  201. $resid=json_decode($result['content'], true);
  202. $this->assertNotNull($resid, "Parsing of json result must no be null");
  203. $this->assertGreaterThan(0, $resid, $object['error']['code'].' '.$object['error']['message']);
  204. // attempt to create duplicated user
  205. print __METHOD__." Request POST url=".$url."\n";
  206. $result=getURLContent($url, 'POST', $body, 1, $addheaders, array('http', 'https'), 2);
  207. //print __METHOD__." Result for creating duplicate user".var_export($result, true)."\n";
  208. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  209. $this->assertEquals($result['curl_error_no'], '');
  210. $object=json_decode($result['content'], true);
  211. $this->assertNotNull($object, "Parsing of json result must no be null");
  212. $this->assertEquals(500, $object['error']['code'], $object['error']['code'].' '.$object['error']['message']);
  213. }
  214. }