ElggCoreWebServicesApiTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /**
  3. * Elgg Test Web Services - General Web Service API
  4. *
  5. * @package Elgg
  6. * @subpackage Test
  7. */
  8. class ElggCoreWebServicesApiTest extends ElggCoreUnitTest {
  9. /**
  10. * Called after each test method.
  11. */
  12. public function tearDown() {
  13. global $API_METHODS;
  14. $API_METHODS = array();
  15. }
  16. // elgg_ws_expose_function
  17. public function testExposeFunctionNoMethod() {
  18. try {
  19. @elgg_ws_expose_function();
  20. $this->assertTrue(FALSE);
  21. } catch (Exception $e) {
  22. $this->assertIsA($e, 'InvalidParameterException');
  23. $this->assertIdentical($e->getMessage(), elgg_echo('InvalidParameterException:APIMethodOrFunctionNotSet'));
  24. }
  25. }
  26. public function testExposeFunctionNoFunction() {
  27. try {
  28. @elgg_ws_expose_function('test');
  29. $this->assertTrue(FALSE);
  30. } catch (Exception $e) {
  31. $this->assertIsA($e, 'InvalidParameterException');
  32. $this->assertIdentical($e->getMessage(), elgg_echo('InvalidParameterException:APIMethodOrFunctionNotSet'));
  33. }
  34. }
  35. public function testExposeFunctionBadParameters() {
  36. try {
  37. @elgg_ws_expose_function('test', 'test', 'BAD');
  38. $this->assertTrue(FALSE);
  39. } catch (Exception $e) {
  40. $this->assertIsA($e, 'InvalidParameterException');
  41. $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('InvalidParameterException:APIParametersArrayStructure'), 'test'));
  42. }
  43. }
  44. public function testExposeFunctionParametersBadArray() {
  45. try {
  46. elgg_ws_expose_function('test', 'test', array('param1' => 'string'));
  47. $this->assertTrue(FALSE);
  48. } catch (Exception $e) {
  49. $this->assertIsA($e, 'InvalidParameterException');
  50. $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('InvalidParameterException:APIParametersArrayStructure'), 'test'));
  51. }
  52. }
  53. public function testExposeFunctionBadHttpMethod() {
  54. try {
  55. @elgg_ws_expose_function('test', 'test', null, '', 'BAD');
  56. $this->assertTrue(FALSE);
  57. } catch (Exception $e) {
  58. $this->assertIsA($e, 'InvalidParameterException');
  59. $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('InvalidParameterException:UnrecognisedHttpMethod'), 'BAD', 'test'));
  60. }
  61. }
  62. public function testExposeFunctionSuccess() {
  63. global $API_METHODS;
  64. // this is a general test but also tests specifically for setting 'required' correctly
  65. $parameters = array('param1' => array('type' => 'int', 'required' => true),
  66. 'param2' => array('type' => 'bool'),
  67. 'param3' => array('type' => 'string', 'required' => false), );
  68. $this->assertTrue(elgg_ws_expose_function('test', 'foo', $parameters));
  69. $parameters = array('param1' => array('type' => 'int', 'required' => true),
  70. 'param2' => array('type' => 'bool', 'required' => true),
  71. 'param3' => array('type' => 'string', 'required' => false), );
  72. $method['description'] = '';
  73. $method['function'] = 'foo';
  74. $method['parameters'] = $parameters;
  75. $method['call_method'] = 'GET';
  76. $method['require_api_auth'] = false;
  77. $method['require_user_auth'] = false;
  78. $this->assertIdentical($method, $API_METHODS['test']);
  79. }
  80. // elgg_ws_unexpose_function
  81. public function testUnexposeFunction() {
  82. global $API_METHODS;
  83. $this->registerFunction();
  84. elgg_ws_unexpose_function('test');
  85. $this->assertIdentical(array(), $API_METHODS);
  86. }
  87. // authenticate_method
  88. public function testAuthenticateMethodNotImplemented() {
  89. try {
  90. authenticate_method('BAD');
  91. $this->assertTrue(FALSE);
  92. } catch (Exception $e) {
  93. $this->assertIsA($e, 'APIException');
  94. $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:MethodCallNotImplemented'), 'BAD'));
  95. }
  96. }
  97. public function testAuthenticateMethodApiAuth() {
  98. $this->registerFunction(true);
  99. try {
  100. authenticate_method('test');
  101. $this->assertTrue(FALSE);
  102. } catch (Exception $e) {
  103. $this->assertIsA($e, 'APIException');
  104. $this->assertIdentical($e->getMessage(), elgg_echo('APIException:APIAuthenticationFailed'));
  105. }
  106. }
  107. public function testAuthenticateMethodUserAuth() {
  108. $this->registerFunction(false, true);
  109. try {
  110. authenticate_method('test');
  111. $this->assertTrue(FALSE);
  112. } catch (Exception $e) {
  113. $this->assertIsA($e, 'APIException');
  114. }
  115. }
  116. public function testAuthenticateMethod() {
  117. $this->registerFunction(false, false);
  118. // anonymous with no user authentication
  119. $this->assertTrue(authenticate_method('test'));
  120. }
  121. // execute_method
  122. public function testExecuteMethodNotImplemented() {
  123. try {
  124. execute_method('BAD');
  125. $this->assertTrue(FALSE);
  126. } catch (Exception $e) {
  127. $this->assertIsA($e, 'APIException');
  128. $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:MethodCallNotImplemented'), 'BAD'));
  129. }
  130. }
  131. public function testExecuteMethodNonCallable() {
  132. elgg_ws_expose_function('test', 'foo');
  133. try {
  134. execute_method('test');
  135. $this->assertTrue(FALSE);
  136. } catch (Exception $e) {
  137. $this->assertIsA($e, 'APIException');
  138. $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:FunctionDoesNotExist'), 'test'));
  139. }
  140. }
  141. public function testExecuteMethodWrongMethod() {
  142. $this->registerFunction();
  143. try {
  144. // GET when it should be a POST
  145. execute_method('test');
  146. $this->assertTrue(FALSE);
  147. } catch (Exception $e) {
  148. $this->assertIsA($e, 'CallException');
  149. $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('CallException:InvalidCallMethod'), 'test', 'POST'));
  150. }
  151. }
  152. // verify parameters
  153. public function testVerifyParametersTypeNotSet() {
  154. $params = array('param1' => array('required' => true));
  155. elgg_ws_expose_function('test', 'elgg_echo', $params);
  156. try {
  157. verify_parameters('test', array());
  158. $this->assertTrue(FALSE);
  159. } catch (Exception $e) {
  160. $this->assertIsA($e, 'APIException');
  161. $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:InvalidParameter'), 'param1', 'test'));
  162. }
  163. }
  164. public function testVerifyParametersMissing() {
  165. $params = array('param1' => array('type' => 'int', 'required' => true));
  166. elgg_ws_expose_function('test', 'elgg_echo', $params);
  167. try {
  168. verify_parameters('test', array());
  169. $this->assertTrue(FALSE);
  170. } catch (Exception $e) {
  171. $this->assertIsA($e, 'APIException');
  172. $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:MissingParameterInMethod'), 'param1', 'test'));
  173. }
  174. }
  175. public function testVerifyParameters() {
  176. $this->registerFunction();
  177. $parameters = array('param1' => 0);
  178. $this->assertTrue(verify_parameters('test', $parameters));
  179. }
  180. public function testSerialiseParameters() {
  181. // int and bool
  182. $this->registerFunction();
  183. $parameters = array('param1' => 1, 'param2' => 0);
  184. $s = serialise_parameters('test', $parameters);
  185. $this->assertIdentical($s, ',1,false');
  186. // string
  187. $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
  188. $parameters = array('param1' => 'testing');
  189. $s = serialise_parameters('test', $parameters);
  190. $this->assertIdentical($s, ",'testing'");
  191. // test string with " in it
  192. $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
  193. $parameters = array('param1' => 'test"ing');
  194. $s = serialise_parameters('test', $parameters);
  195. $this->assertIdentical($s, ',\'test"ing\'');
  196. // test string with ' in it
  197. $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
  198. $parameters = array('param1' => 'test\'ing');
  199. $s = serialise_parameters('test', $parameters);
  200. $this->assertIdentical($s, ",'test\'ing'");
  201. // test string with \ in it
  202. $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
  203. $parameters = array('param1' => 'test\ing');
  204. $s = serialise_parameters('test', $parameters);
  205. $this->assertIdentical($s, ",'test\\ing'");
  206. // test string with \' in it
  207. $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
  208. $parameters = array('param1' => "test\'ing");
  209. $s = serialise_parameters('test', $parameters);
  210. $this->assertIdentical($s, ",'test\\\\'ing'"); // test\\'ing
  211. // test string reported by twall in #1364
  212. $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
  213. $parameters = array('param1' => '{"html":"<div><img src=\\"http://foo.com\\"/>Blah Blah</div>"}');
  214. $s = serialise_parameters('test', $parameters);
  215. $this->assertIdentical($s, ",'{\"html\":\"<div><img src=\\\"http://foo.com\\\"/>Blah Blah</div>\"}'");
  216. // float
  217. $this->registerFunction(false, false, array('param1' => array('type' => 'float')));
  218. $parameters = array('param1' => 2.5);
  219. $s = serialise_parameters('test', $parameters);
  220. $this->assertIdentical($s, ',2.5');
  221. // indexed array of strings
  222. $this->registerFunction(false, false, array('param1' => array('type' => 'array')));
  223. $parameters = array('param1' => array('one', 'two'));
  224. $s = serialise_parameters('test', $parameters);
  225. $this->assertIdentical($s, ",array('0'=>'one','1'=>'two')");
  226. // associative array of strings
  227. $this->registerFunction(false, false, array('param1' => array('type' => 'array')));
  228. $parameters = array('param1' => array('first' => 'one', 'second' => 'two'));
  229. $s = serialise_parameters('test', $parameters);
  230. $this->assertIdentical($s, ",array('first'=>'one','second'=>'two')");
  231. // indexed array of strings
  232. $this->registerFunction(false, false, array('param1' => array('type' => 'array')));
  233. $parameters = array('param1' => array(1, 2));
  234. $s = serialise_parameters('test', $parameters);
  235. $this->assertIdentical($s, ",array('0'=>'1','1'=>'2')");
  236. // test unknown type
  237. $this->registerFunction(false, false, array('param1' => array('type' => 'bad')));
  238. $parameters = array('param1' => 'test');
  239. $this->expectException('APIException');
  240. $s = serialise_parameters('test', $parameters);
  241. }
  242. // api key methods
  243. //public function testApiAuthenticate() {
  244. // $this->assertFalse(pam_authenticate(null, "api"));
  245. //}
  246. public function testApiAuthKeyNoKey() {
  247. try {
  248. api_auth_key();
  249. $this->assertTrue(FALSE);
  250. } catch (Exception $e) {
  251. $this->assertIsA($e, 'APIException');
  252. $this->assertIdentical($e->getMessage(), elgg_echo('APIException:MissingAPIKey'));
  253. }
  254. }
  255. public function testApiAuthKeyBadKey() {
  256. global $CONFIG;
  257. set_input('api_key', 'BAD');
  258. try {
  259. api_auth_key();
  260. $this->assertTrue(FALSE);
  261. } catch (Exception $e) {
  262. $this->assertIsA($e, 'APIException');
  263. $this->assertIdentical($e->getMessage(), elgg_echo('APIException:BadAPIKey'));
  264. }
  265. }
  266. protected function registerFunction($api_auth = false, $user_auth = false, $params = null) {
  267. $parameters = array('param1' => array('type' => 'int', 'required' => true),
  268. 'param2' => array('type' => 'bool', 'required' => false), );
  269. if ($params == null) {
  270. $params = $parameters;
  271. }
  272. elgg_ws_expose_function('test', 'elgg_echo', $params, '', 'POST', $api_auth, $user_auth);
  273. }
  274. }