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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
<?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() {
try {
expose_function();
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'InvalidParameterException');
$this->assertIdentical($e->getMessage(), elgg_echo('InvalidParameterException:APIMethodOrFunctionNotSet'));
}
}
public function testExposeFunctionNoFunction() {
try {
expose_function('test');
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'InvalidParameterException');
$this->assertIdentical($e->getMessage(), elgg_echo('InvalidParameterException:APIMethodOrFunctionNotSet'));
}
}
public function testExposeFunctionBadParameters() {
try {
expose_function('test', 'test', 'BAD');
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'InvalidParameterException');
$this->assertIdentical($e->getMessage(), sprintf(elgg_echo('InvalidParameterException:APIParametersArrayStructure'), 'test'));
}
}
public function testExposeFunctionParametersBadArray() {
try {
expose_function('test', 'test', array('param1' => 'string'));
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'InvalidParameterException');
$this->assertIdentical($e->getMessage(), sprintf(elgg_echo('InvalidParameterException:APIParametersArrayStructure'), 'test'));
}
}
public function testExposeFunctionBadHttpMethod() {
try {
expose_function('test', 'test', null, '', 'BAD');
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'InvalidParameterException');
$this->assertIdentical($e->getMessage(), sprintf(elgg_echo('InvalidParameterException:UnrecognisedHttpMethod'), 'BAD', 'test'));
}
}
public function testExposeFunctionSuccess() {
global $API_METHODS;
// this is a general test but also tests specifically for setting 'required' correctly
$parameters = array('param1' => array('type' => 'int', 'required' => true),
'param2' => array('type' => 'bool'),
'param3' => array('type' => 'string', 'required' => false), );
$this->assertTrue(expose_function('test', 'foo', $parameters));
$parameters = array('param1' => array('type' => 'int', 'required' => true),
'param2' => array('type' => 'bool', 'required' => true),
'param3' => array('type' => 'string', 'required' => false), );
$method['function'] = 'foo';
$method['parameters'] = $parameters;
$method['call_method'] = 'GET';
$method['description'] = '';
$method['require_api_auth'] = false;
$method['require_user_auth'] = false;
$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 testAuthenticateMethodNotImplemented() {
try {
authenticate_method('BAD');
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'APIException');
$this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:MethodCallNotImplemented'), 'BAD'));
}
}
public function testAuthenticateMethodApiAuth() {
$this->registerFunction(true);
try {
authenticate_method('test');
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'APIException');
$this->assertIdentical($e->getMessage(), elgg_echo('APIException:APIAuthenticationFailed'));
}
}
public function testAuthenticateMethodUserAuth() {
$this->registerFunction(false, true);
try {
authenticate_method('test');
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'APIException');
$this->assertIdentical($e->getMessage(), elgg_echo('APIException:UserAuthenticationFailed'));
}
}
public function testAuthenticateMethod() {
$this->registerFunction(false, false);
// anonymous with no user authentication
$this->assertTrue(authenticate_method('test'));
}
// execute_method
public function testExecuteMethodNotImplemented() {
try {
execute_method('BAD');
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'APIException');
$this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:MethodCallNotImplemented'), 'BAD'));
}
}
public function testExecuteMethodNonCallable() {
expose_function('test', 'foo');
try {
execute_method('test');
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'APIException');
$this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:FunctionDoesNotExist'), 'test'));
}
}
public function testExecuteMethodWrongMethod() {
$this->registerFunction();
try {
// GET when it should be a POST
execute_method('test');
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'CallException');
$this->assertIdentical($e->getMessage(), sprintf(elgg_echo('CallException:InvalidCallMethod'), 'test', 'POST'));
}
}
// verify parameters
public function testVerifyParametersTypeNotSet() {
$params = array('param1' => array('required' => true));
expose_function('test', 'elgg_echo', $params);
try {
verify_parameters('test', array());
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'APIException');
$this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:InvalidParameter'), 'param1', 'test'));
}
}
public function testVerifyParametersMissing() {
$params = array('param1' => array('type' => 'int', 'required' => true));
expose_function('test', 'elgg_echo', $params);
try {
verify_parameters('test', array());
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'APIException');
$this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:MissingParameterInMethod'), 'param1', 'test'));
}
}
public function testVerifyParameters() {
$this->registerFunction();
$parameters = array('param1' => 0);
$this->assertTrue(verify_parameters('test', $parameters));
}
public function testSerialiseParameters() {
// int and bool
$this->registerFunction();
$parameters = array('param1' => 1, 'param2' => 0);
$s = serialise_parameters('test', $parameters);
$this->assertIdentical($s, ',1,false');
// string
$this->registerFunction(false, false, array('param1' => array('type' => 'string')));
$parameters = array('param1' => 'testing');
$s = serialise_parameters('test', $parameters);
$this->assertIdentical($s, ",'testing'");
// float
$this->registerFunction(false, false, array('param1' => array('type' => 'float')));
$parameters = array('param1' => 2.5);
$s = serialise_parameters('test', $parameters);
$this->assertIdentical($s, ',2.5');
// indexed array of strings
$this->registerFunction(false, false, array('param1' => array('type' => 'array')));
$parameters = array('param1' => array('one', 'two'));
$s = serialise_parameters('test', $parameters);
$this->assertIdentical($s, "array('0'=>'one','1'=>'two')");
// associative array of strings
$this->registerFunction(false, false, array('param1' => array('type' => 'array')));
$parameters = array('param1' => array('first' => 'one', 'second' => 'two'));
$s = serialise_parameters('test', $parameters);
$this->assertIdentical($s, "array('first'=>'one','second'=>'two')");
// indexed array of strings
$this->registerFunction(false, false, array('param1' => array('type' => 'array')));
$parameters = array('param1' => array(1, 2));
$s = serialise_parameters('test', $parameters);
$this->assertIdentical($s, "array('0'=>'1','1'=>'2')");
// test unknown type
$this->registerFunction(false, false, array('param1' => array('type' => 'bad')));
$parameters = array('param1' => 'test');
$this->expectException('APIException');
$s = serialise_parameters('test', $parameters);
}
// api key methods
public function testApiAuthenticate() {
$this->assertFalse(pam_authenticate(null, "api"));
}
public function testApiAuthKeyNoKey() {
try {
api_auth_key();
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'APIException');
$this->assertIdentical($e->getMessage(), elgg_echo('APIException:MissingAPIKey'));
}
}
public function testApiAuthKeyBadKey() {
global $CONFIG;
$CONFIG->input['api_key'] = 'BAD';
try {
api_auth_key();
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertIsA($e, 'APIException');
$this->assertIdentical($e->getMessage(), elgg_echo('APIException:BadAPIKey'));
}
}
protected function registerFunction($api_auth = false, $user_auth = false, $params = null) {
$parameters = array('param1' => array('type' => 'int', 'required' => true),
'param2' => array('type' => 'bool', 'required' => false), );
if ($params == null) {
$params = $parameters;
}
expose_function('test', 'elgg_echo', $params, '', 'POST', $api_auth, $user_auth);
}
}
|