RestAPIUserTest.php 7.6 KB

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