]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
more unit tests for REST api
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Tue, 20 Oct 2009 11:39:20 +0000 (11:39 +0000)
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Tue, 20 Oct 2009 11:39:20 +0000 (11:39 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@3566 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/api.php
engine/tests/services/api.php
languages/en.php

index 493e2a47f9f3be74f3ff85f432cf5476b2e816bd..f2424c76644c189afebe430efec7783d9c7dbcf5 100644 (file)
@@ -494,7 +494,7 @@ function execute_method($method) {
 
        // function must be callable 
        if (!(isset($API_METHODS[$method]["function"])) || !(is_callable($API_METHODS[$method]["function"]))) {
-               throw new APIException(sprintf(elgg_echo('APIException:MethodCallNotImplemented'), $method));
+               throw new APIException(sprintf(elgg_echo('APIException:FunctionDoesNotExist'), $method));
        }
        
        // check http call method
index cf8a16bf566f441c32670da91bad28c5f3ed0909..4c30195385a7099acaa13d82ba85e29c9e8eed0b 100644 (file)
@@ -18,6 +18,7 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest {
                $API_METHODS = array();\r
        }\r
        \r
+// expose_function\r
        public function testExposeFunctionNoMethod() {\r
                $this->expectException('InvalidParameterException');\r
                expose_function();\r
@@ -51,7 +52,18 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest {
                $this->assertTrue(expose_function('test', 'foo', $parameters));\r
                $this->assertIdentical($method, $API_METHODS['test']);\r
        }\r
+\r
+// unexpose_function\r
+       public function testUnexposeFunction() {\r
+               global $API_METHODS;\r
+               \r
+               $this->registerFunction();\r
                \r
+               unexpose_function('test');\r
+               $this->assertIdentical(array(), $API_METHODS);\r
+       } \r
+\r
+// authenticate_method\r
        public function testApiMethodNotImplemented() {\r
                global $CONFIG;\r
                \r
@@ -60,6 +72,41 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest {
                $this->assertIdentical(sprintf(elgg_echo('APIException:MethodCallNotImplemented'), 'bad.method'), $obj->api[0]->message);\r
        }\r
 \r
+       public function testAuthenticateForApi() {\r
+               $this->registerFunction(true, false);\r
+               \r
+               $this->expectException('APIException');\r
+               authenticate_method('test');\r
+       }\r
+\r
+       public function testAuthenticateForUser() {\r
+               $this->registerFunction(false, true);\r
+               \r
+               $this->expectException('APIException');\r
+               authenticate_method('test');\r
+       }\r
+       \r
+       public function testAuthenticateMethod() {\r
+               $this->registerFunction(false, false);\r
+               // anonymous with no user authentication\r
+               $this->assertTrue(authenticate_method('test'));\r
+       }\r
+       \r
+// api_authenticate\r
+       public function testApiAuthenticate() {\r
+               $this->registerFunction(true, false);\r
+               \r
+               $this->assertFalse(api_authenticate());\r
+       }\r
+       \r
+// execute_method\r
+       public function testExecuteMethodNonCallable() {\r
+               $this->registerFunction();\r
+               \r
+               $this->expectException('APIException');\r
+               execute_method('test');\r
+       }\r
+       \r
        public function testVerifyParameters() {\r
                $this->registerFunction();\r
                \r
@@ -78,14 +125,8 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest {
        protected function registerFunction($api_auth = false, $user_auth = false) {\r
                $parameters = array('param1' => array('type' => 'int', 'required' => true),\r
                                                        'param2' => array('type' => 'bool', 'required' => false), );\r
-               $method['function'] = 'foo';\r
-               $method['parameters'] = $parameters;\r
-               $method['call_method'] = 'GET'; \r
-               $method['description'] = '';\r
-               $method['require_api_auth'] = $api_auth;\r
-               $method['require_user_auth'] = $user_auth;\r
 \r
-               expose_function('test', 'foo', $parameters);\r
+               expose_function('test', 'foo', $parameters, '', 'GET', $api_auth, $user_auth);\r
        }\r
        \r
 }\r
index 310f5cd091a05673d0f8498b65153754a8f63a18..6a692e55b2eb723ec3bd35cfba61d3980650c730 100644 (file)
@@ -134,6 +134,7 @@ $english = array(
        'SecurityException:AuthTokenExpired' => "Authentication token either missing, invalid or expired.",
        'CallException:InvalidCallMethod' => "%s must be called using '%s'",
        'APIException:MethodCallNotImplemented' => "Method call '%s' has not been implemented.",
+       'APIException:FunctionDoesNotExist' => "Function for method '%s' is not callable",
        'APIException:AlgorithmNotSupported' => "Algorithm '%s' is not supported or has been disabled.",
        'ConfigurationException:CacheDirNotSet' => "Cache directory 'cache_path' not set.",
        'APIException:NotGetOrPost' => "Request method must be GET or POST",