2013-11-14 00:40:09 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Tom Needham <tom@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 07:27:21 +00:00
|
|
|
namespace Test;
|
|
|
|
|
2017-10-24 13:26:53 +00:00
|
|
|
use OCP\IRequest;
|
|
|
|
|
2016-05-19 07:27:21 +00:00
|
|
|
class APITest extends \Test\TestCase {
|
2014-01-14 16:11:04 +00:00
|
|
|
|
2013-11-14 00:40:09 +00:00
|
|
|
// Helps build a response variable
|
2014-02-19 08:31:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $message
|
|
|
|
*/
|
2014-01-14 16:11:04 +00:00
|
|
|
function buildResponse($shipped, $data, $code, $message=null) {
|
2017-09-21 15:56:00 +00:00
|
|
|
$resp = new \OC\OCS\Result($data, $code, $message);
|
2015-08-10 19:33:50 +00:00
|
|
|
$resp->addHeader('KEY', 'VALUE');
|
|
|
|
return [
|
2013-11-14 00:40:09 +00:00
|
|
|
'shipped' => $shipped,
|
2015-08-10 19:33:50 +00:00
|
|
|
'response' => $resp,
|
2014-12-02 12:51:36 +00:00
|
|
|
'app' => $this->getUniqueID('testapp_'),
|
2015-08-10 19:33:50 +00:00
|
|
|
];
|
2013-11-14 00:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate details of the result
|
2014-02-19 08:31:54 +00:00
|
|
|
|
|
|
|
/**
|
2017-09-21 15:56:00 +00:00
|
|
|
* @param \OC\OCS\Result $result
|
2014-02-19 08:31:54 +00:00
|
|
|
*/
|
2013-11-14 14:37:30 +00:00
|
|
|
function checkResult($result, $success) {
|
2013-11-14 00:40:09 +00:00
|
|
|
// Check response is of correct type
|
2017-09-21 15:56:00 +00:00
|
|
|
$this->assertInstanceOf(\OC\OCS\Result::class, $result);
|
2013-11-14 14:37:30 +00:00
|
|
|
// Check if it succeeded
|
2017-09-21 15:56:00 +00:00
|
|
|
/** @var $result \OC\OCS\Result */
|
2013-11-14 00:40:09 +00:00
|
|
|
$this->assertEquals($success, $result->succeeded());
|
|
|
|
}
|
|
|
|
|
2015-10-12 21:39:16 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function versionDataScriptNameProvider() {
|
|
|
|
return [
|
|
|
|
// Valid script name
|
|
|
|
[
|
|
|
|
'/master/ocs/v2.php',
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
|
|
|
|
// Invalid script names
|
|
|
|
[
|
|
|
|
'/master/ocs/v2.php/someInvalidPathName',
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'/master/ocs/v1.php',
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'',
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider versionDataScriptNameProvider
|
|
|
|
* @param string $scriptName
|
|
|
|
* @param bool $expected
|
|
|
|
*/
|
|
|
|
public function testIsV2($scriptName, $expected) {
|
2017-10-24 13:26:53 +00:00
|
|
|
$request = $this->getMockBuilder(IRequest::class)
|
2015-10-12 21:39:16 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getScriptName')
|
|
|
|
->will($this->returnValue($scriptName));
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->invokePrivate(new \OC_API, 'isV2', [$request]));
|
|
|
|
}
|
|
|
|
|
2013-11-14 14:37:30 +00:00
|
|
|
function dataProviderTestOneResult() {
|
2015-08-10 19:33:50 +00:00
|
|
|
return [
|
|
|
|
[100, true],
|
|
|
|
[101, false],
|
|
|
|
[997, false],
|
|
|
|
];
|
2013-11-14 14:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataProviderTestOneResult
|
|
|
|
*
|
|
|
|
* @param $statusCode
|
|
|
|
* @param $succeeded
|
|
|
|
*/
|
|
|
|
public function testOneResult($statusCode, $succeeded) {
|
|
|
|
// Setup some data arrays
|
2015-08-10 19:33:50 +00:00
|
|
|
$data1 = [
|
|
|
|
'users' => [
|
|
|
|
'tom' => [
|
2013-11-14 14:37:30 +00:00
|
|
|
'key' => 'value',
|
2015-08-10 19:33:50 +00:00
|
|
|
],
|
|
|
|
'frank' => [
|
2013-11-14 14:37:30 +00:00
|
|
|
'key' => 'value',
|
2015-08-10 19:33:50 +00:00
|
|
|
],
|
|
|
|
]];
|
2013-11-14 14:37:30 +00:00
|
|
|
|
|
|
|
// Test merging one success result
|
|
|
|
$response = $this->buildResponse(true, $data1, $statusCode);
|
2016-05-19 07:27:21 +00:00
|
|
|
$result = \OC_API::mergeResponses([$response]);
|
2013-11-14 14:37:30 +00:00
|
|
|
$this->assertEquals($response['response'], $result);
|
|
|
|
$this->checkResult($result, $succeeded);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataProviderTestMergeResponses() {
|
2015-08-10 19:33:50 +00:00
|
|
|
return [
|
2013-11-14 14:37:30 +00:00
|
|
|
// Two shipped success results
|
2015-08-10 19:33:50 +00:00
|
|
|
[true, 100, true, 100, true],
|
2013-11-14 14:37:30 +00:00
|
|
|
// Two shipped results, one success and one failure
|
2015-08-10 19:33:50 +00:00
|
|
|
[true, 100, true, 998, false],
|
2013-11-14 14:37:30 +00:00
|
|
|
// Two shipped results, both failure
|
2015-08-10 19:33:50 +00:00
|
|
|
[true, 997, true, 998, false],
|
2013-11-14 14:37:30 +00:00
|
|
|
// Two third party success results
|
2015-08-10 19:33:50 +00:00
|
|
|
[false, 100, false, 100, true],
|
2013-11-14 14:37:30 +00:00
|
|
|
// Two third party results, one success and one failure
|
2015-08-10 19:33:50 +00:00
|
|
|
[false, 100, false, 998, false],
|
2013-11-14 14:37:30 +00:00
|
|
|
// Two third party results, both failure
|
2015-08-10 19:33:50 +00:00
|
|
|
[false, 997, false, 998, false],
|
2013-11-14 14:37:30 +00:00
|
|
|
// One of each, both success
|
2015-08-10 19:33:50 +00:00
|
|
|
[false, 100, true, 100, true],
|
|
|
|
[true, 100, false, 100, true],
|
2013-11-14 14:37:30 +00:00
|
|
|
// One of each, both failure
|
2015-08-10 19:33:50 +00:00
|
|
|
[false, 997, true, 998, false],
|
2013-11-14 14:37:30 +00:00
|
|
|
// One of each, shipped success
|
2015-08-10 19:33:50 +00:00
|
|
|
[false, 997, true, 100, true],
|
2013-11-14 14:37:30 +00:00
|
|
|
// One of each, third party success
|
2015-08-10 19:33:50 +00:00
|
|
|
[false, 100, true, 998, false],
|
|
|
|
];
|
2013-11-14 14:37:30 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @dataProvider dataProviderTestMergeResponses
|
|
|
|
*
|
|
|
|
* Test the merging of multiple responses
|
|
|
|
* @param $statusCode1
|
|
|
|
* @param $statusCode2
|
|
|
|
* @param $succeeded
|
|
|
|
*/
|
|
|
|
public function testMultipleMergeResponses($shipped1, $statusCode1, $shipped2, $statusCode2, $succeeded){
|
2013-11-14 00:40:09 +00:00
|
|
|
// Tests that app responses are merged correctly
|
|
|
|
// Setup some data arrays
|
|
|
|
$data1 = array(
|
|
|
|
'users' => array(
|
|
|
|
'tom' => array(
|
|
|
|
'key' => 'value',
|
2013-11-14 14:37:30 +00:00
|
|
|
),
|
2013-11-14 00:40:09 +00:00
|
|
|
'frank' => array(
|
|
|
|
'key' => 'value',
|
2013-11-14 14:37:30 +00:00
|
|
|
),
|
2013-11-14 00:40:09 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$data2 = array(
|
|
|
|
'users' => array(
|
|
|
|
'tom' => array(
|
|
|
|
'key' => 'newvalue',
|
2013-11-14 14:37:30 +00:00
|
|
|
),
|
2013-11-14 00:40:09 +00:00
|
|
|
'jan' => array(
|
|
|
|
'key' => 'value',
|
2013-11-14 14:37:30 +00:00
|
|
|
),
|
2013-11-14 00:40:09 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
// Two shipped success results
|
2016-05-19 07:27:21 +00:00
|
|
|
$result = \OC_API::mergeResponses(array(
|
2014-01-14 16:11:04 +00:00
|
|
|
$this->buildResponse($shipped1, $data1, $statusCode1, "message1"),
|
|
|
|
$this->buildResponse($shipped2, $data2, $statusCode2, "message2"),
|
2013-11-14 14:37:30 +00:00
|
|
|
));
|
|
|
|
$this->checkResult($result, $succeeded);
|
2013-11-14 00:40:09 +00:00
|
|
|
$resultData = $result->getData();
|
2014-01-14 16:11:04 +00:00
|
|
|
$resultMeta = $result->getMeta();
|
2015-08-10 19:33:50 +00:00
|
|
|
$resultHeaders = $result->getHeaders();
|
2014-01-14 16:11:04 +00:00
|
|
|
$resultStatusCode = $result->getStatusCode();
|
|
|
|
|
2013-11-14 00:40:09 +00:00
|
|
|
$this->assertArrayHasKey('jan', $resultData['users']);
|
2015-08-10 19:33:50 +00:00
|
|
|
$this->assertArrayHasKey('KEY', $resultHeaders);
|
2014-01-14 16:11:04 +00:00
|
|
|
|
|
|
|
// check if the returned status message matches the selected status code
|
|
|
|
if ($resultStatusCode === 997) {
|
|
|
|
$this->assertEquals('message1', $resultMeta['message']);
|
|
|
|
} elseif ($resultStatusCode === 998) {
|
|
|
|
$this->assertEquals('message2', $resultMeta['message']);
|
|
|
|
} elseif ($resultStatusCode === 100) {
|
|
|
|
$this->assertEquals(null, $resultMeta['message']);
|
|
|
|
}
|
|
|
|
|
2013-11-14 00:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|