aboutsummaryrefslogtreecommitdiff
path: root/engine/tests/services/api.php
blob: 4c30195385a7099acaa13d82ba85e29c9e8eed0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
 * Elgg Test Services - General API and REST
 *
 * @package Elgg
 * @subpackage Test
 * @author Curverider Ltd
 * @link http://elgg.org/
 */
class ElggCoreServicesApiTest extends ElggCoreUnitTest {

	/**
	 * Called after each test method.
	 */
	public function tearDown() {
		global $API_METHODS;
		$this->swallowErrors();
		$API_METHODS = array();
	}
	
// expose_function
	public function testExposeFunctionNoMethod() {
		$this->expectException('InvalidParameterException');
		expose_function();
	}
	
	public function testExposeFunctionNoFunction() {
		$this->expectException('InvalidParameterException');
		expose_function('test');
	}
	
	public function testExposeFunctionBadParameters() {
		$this->expectException('InvalidParameterException');
		expose_function('test', 'test', 'BAD');
	}
	
	public function testExposeFunctionBadHttpMethod() {
		$this->expectException('InvalidParameterException');
		expose_function('test', 'test', null, '', 'BAD');
	}
	
	public function testExposeFunctionSuccess() {
		global $API_METHODS;
		$parameters = array('param1' => array('type' => 'int', 'required' => true));
		$method['function'] = 'foo';
		$method['parameters'] = $parameters;
		$method['call_method'] = 'GET'; 
		$method['description'] = '';
		$method['require_api_auth'] = false;
		$method['require_user_auth'] = false;

		$this->assertTrue(expose_function('test', 'foo', $parameters));
		$this->assertIdentical($method, $API_METHODS['test']);
	}

// unexpose_function
	public function testUnexposeFunction() {
		global $API_METHODS;
		
		$this->registerFunction();
		
		unexpose_function('test');
		$this->assertIdentical(array(), $API_METHODS);
	} 

// authenticate_method
	public function testApiMethodNotImplemented() {
		global $CONFIG;
		
		$results = send_api_get_call($CONFIG->wwwroot . 'pg/api/rest/json/', array('method' => 'bad.method'));
		$obj = json_decode($results);
		$this->assertIdentical(sprintf(elgg_echo('APIException:MethodCallNotImplemented'), 'bad.method'), $obj->api[0]->message);
	}

	public function testAuthenticateForApi() {
		$this->registerFunction(true, false);
		
		$this->expectException('APIException');
		authenticate_method('test');
	}

	public function testAuthenticateForUser() {
		$this->registerFunction(false, true);
		
		$this->expectException('APIException');
		authenticate_method('test');
	}
	
	public function testAuthenticateMethod() {
		$this->registerFunction(false, false);
		// anonymous with no user authentication
		$this->assertTrue(authenticate_method('test'));
	}
	
// api_authenticate
	public function testApiAuthenticate() {
		$this->registerFunction(true, false);
		
		$this->assertFalse(api_authenticate());
	}
	
// execute_method
	public function testExecuteMethodNonCallable() {
		$this->registerFunction();
		
		$this->expectException('APIException');
		execute_method('test');
	}
	
	public function testVerifyParameters() {
		$this->registerFunction();
		
		$parameters = array('param1' => 0);
		$this->assertTrue(verify_parameters('test', $parameters));
		
		$parameters = array('param2' => true);
		$this->expectException('APIException');
		$this->assertTrue(verify_parameters('test', $parameters));
	}
	
	public function testserialise_parameters() {
		
	}
	
	protected function registerFunction($api_auth = false, $user_auth = false) {
		$parameters = array('param1' => array('type' => 'int', 'required' => true),
							'param2' => array('type' => 'bool', 'required' => false), );

		expose_function('test', 'foo', $parameters, '', 'GET', $api_auth, $user_auth);
	}
	
}