RestAPIUserTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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";
  71. die(1);
  72. }
  73. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  74. //print " - db ".$db->db;
  75. print "\n";
  76. }
  77. /**
  78. * setUpBeforeClass
  79. *
  80. * @return void
  81. */
  82. public static function setUpBeforeClass(): void
  83. {
  84. global $conf,$user,$langs,$db;
  85. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  86. print __METHOD__."\n";
  87. }
  88. /**
  89. * tearDownAfterClass
  90. *
  91. * @return void
  92. */
  93. public static function tearDownAfterClass(): void
  94. {
  95. global $conf,$user,$langs,$db;
  96. $db->rollback();
  97. print __METHOD__."\n";
  98. }
  99. /**
  100. * Init phpunit tests
  101. *
  102. * @return void
  103. */
  104. protected function setUp(): void
  105. {
  106. global $conf,$user,$langs,$db;
  107. $conf=$this->savconf;
  108. $user=$this->savuser;
  109. $langs=$this->savlangs;
  110. $db=$this->savdb;
  111. $this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php';
  112. $login='admin';
  113. $password='admin';
  114. $url=$this->api_url.'/login?login='.$login.'&password='.$password;
  115. // Call the API login method to save api_key for this test class.
  116. // At first call, if token is not defined a random value is generated and returned.
  117. $result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
  118. print __METHOD__." result = ".var_export($result, true)."\n";
  119. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  120. $this->assertEquals($result['curl_error_no'], '');
  121. $object = json_decode($result['content'], true); // If success content is just an id, if not an array
  122. $this->assertNotNull($object, "Parsing of json result must not be null");
  123. $this->assertNotEquals(500, (empty($object['error']['code']) ? 0 : $object['error']['code']), 'Error'.(empty($object['error']['message']) ? '' : ' '.$object['error']['message']));
  124. $this->assertEquals('200', $object['success']['code']);
  125. $this->api_key = $object['success']['token'];
  126. print __METHOD__." api_key: $this->api_key \n";
  127. }
  128. /**
  129. * End phpunit tests
  130. *
  131. * @return void
  132. */
  133. protected function tearDown(): void
  134. {
  135. print __METHOD__."\n";
  136. }
  137. /**
  138. * testRestGetUser
  139. *
  140. * @return int
  141. */
  142. public function testRestGetUser()
  143. {
  144. global $conf,$user,$langs,$db;
  145. $url = $this->api_url.'/users/123456789?api_key='.$this->api_key;
  146. //$addheaders=array('Content-Type: application/json');
  147. print __METHOD__." Request GET url=".$url."\n";
  148. $result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
  149. //print __METHOD__." result for get on unexisting user: ".var_export($result, true)."\n";
  150. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  151. $this->assertEquals($result['curl_error_no'], '');
  152. $object=json_decode($result['content'], true);
  153. $this->assertNotNull($object, "Parsing of json result must not be null");
  154. $this->assertEquals(404, (empty($object['error']['code']) ? 0 : $object['error']['code']), 'Error code is not 404');
  155. $url = $this->api_url.'/users/1?api_key='.$this->api_key;
  156. print __METHOD__." Request GET url=".$url."\n";
  157. $result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
  158. print __METHOD__." result for get on an existing user: ".var_export($result, true)."\n";
  159. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  160. $this->assertEquals($result['curl_error_no'], '');
  161. $object=json_decode($result['content'], true);
  162. $this->assertNotNull($object, "Parsing of json result must not be null");
  163. $this->assertEquals(1, $object['statut']);
  164. return $object['id'];
  165. }
  166. /**
  167. * testRestCreateUser
  168. *
  169. * @return void
  170. *
  171. * @depends testRestGetUser
  172. * The depends says test is run only if previous is ok
  173. */
  174. public function testRestCreateUser()
  175. {
  176. // attemp to create without mandatory fields :
  177. $url = $this->api_url.'/users?api_key='.$this->api_key;
  178. $addheaders=array('Content-Type: application/json');
  179. $bodyobj = array(
  180. "lastname"=>"testRestUser",
  181. "password"=>"testRestPassword",
  182. "email"=>"test@restuser.com"
  183. );
  184. $body = json_encode($bodyobj);
  185. print __METHOD__." Request POST url=".$url."\n";
  186. $result=getURLContent($url, 'POST', $body, 1, $addheaders, array('http', 'https'), 2);
  187. //print __METHOD__." Result for creating incomplete user".var_export($result, true)."\n";
  188. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  189. $this->assertEquals($result['curl_error_no'], '');
  190. $object=json_decode($result['content'], true);
  191. $this->assertNotNull($object, "Parsing of json result must no be null");
  192. $this->assertEquals(500, (empty($object['error']['code']) ? 0 : $object['error']['code']), 'Error'.(empty($object['error']['message']) ? '' : ' '.$object['error']['message']));
  193. // create regular user
  194. unset($result);
  195. $bodyobj = array(
  196. "login"=>"testRestLogin".mt_rand(),
  197. "lastname"=>"testRestUser",
  198. "password"=>"testRestPassword",
  199. "email"=>"test".mt_rand()."@restuser.com"
  200. );
  201. $body = json_encode($bodyobj);
  202. print __METHOD__." Request POST url=".$url."\n";
  203. $result=getURLContent($url, 'POST', $body, 1, $addheaders, array('http', 'https'), 2);
  204. print __METHOD__." result code for creating non existing user = ".var_export($result, true)."\n";
  205. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  206. $this->assertEquals($result['curl_error_no'], '');
  207. $object = json_decode($result['content'], true); // If success content is just an id, if not an array
  208. $this->assertNotNull($object, "Parsing of json result must no be null");
  209. $this->assertNotEquals(500, ((is_scalar($object) || empty($object['error']) || empty($object['error']['code'])) ? 0 : $object['error']['code']), 'Error'.(empty($object['error']['message']) ? '' : ' '.$object['error']['message']));
  210. $this->assertGreaterThan(0, $object, 'ID returned is no > 0');
  211. // attempt to create duplicated user
  212. print __METHOD__." Request POST url=".$url."\n";
  213. $result=getURLContent($url, 'POST', $body, 1, $addheaders, array('http', 'https'), 2);
  214. //print __METHOD__." Result for creating duplicate user".var_export($result, true)."\n";
  215. print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
  216. $this->assertEquals($result['curl_error_no'], '');
  217. $object=json_decode($result['content'], true);
  218. $this->assertNotNull($object, "Parsing of json result must no be null");
  219. $this->assertEquals(500, (empty($object['error']['code']) ? 0 : $object['error']['code']), 'Error'.(empty($object['error']['message']) ? '' : ' '.$object['error']['message']));
  220. }
  221. }