From 906b6b7337b7cd8922c0b1287f25bf03fff5c471 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Mon, 13 Apr 2015 09:43:45 +0200 Subject: Prevent php message: "Trying to get property of non-object at /xxx/lib/private/ocsclient.php#282" --- tests/lib/ocsclienttest.php | 979 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 979 insertions(+) create mode 100644 tests/lib/ocsclienttest.php (limited to 'tests/lib/ocsclienttest.php') diff --git a/tests/lib/ocsclienttest.php b/tests/lib/ocsclienttest.php new file mode 100644 index 00000000000..f4bf1536291 --- /dev/null +++ b/tests/lib/ocsclienttest.php @@ -0,0 +1,979 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +use OC\OCSClient; +use OCP\Http\Client\IClientService; +use OCP\IConfig; +use OCP\ILogger; + +/** + * Class OCSClientTest + */ +class OCSClientTest extends \Test\TestCase { + /** @var OCSClient */ + private $ocsClient; + /** @var IConfig */ + private $config; + /** @var IClientService */ + private $clientService; + /** @var ILogger */ + private $logger; + + public function setUp() { + parent::setUp(); + + $this->config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + $this->clientService = $this->getMock('\OCP\Http\Client\IClientService'); + $this->logger = $this->getMock('\OCP\ILogger'); + + $this->ocsClient = new OCSClient( + $this->clientService, + $this->config, + $this->logger + ); + } + + public function testIsAppStoreEnabledSuccess() { + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->assertTrue($this->ocsClient->isAppStoreEnabled()); + } + + public function testIsAppStoreEnabledFail() { + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(false)); + $this->assertFalse($this->ocsClient->isAppStoreEnabled()); + } + + public function testGetAppStoreUrl() { + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + $this->assertSame('https://api.owncloud.com/v1', Test_Helper::invokePrivate($this->ocsClient, 'getAppStoreUrl')); + } + + public function testGetCategoriesDisabledAppStore() { + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(false)); + $this->assertNull($this->ocsClient->getCategories()); + } + + public function testGetCategoriesExceptionClient() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/categories', + [ + 'timeout' => 5, + ] + ) + ->will($this->throwException(new \Exception('TheErrorMessage'))); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with( + 'Could not get categories: TheErrorMessage', + [ + 'app' => 'core', + ] + ); + + $this->assertNull($this->ocsClient->getCategories()); + } + + public function testGetCategoriesParseError() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $response = $this->getMock('\OCP\Http\Client\IResponse'); + $response + ->expects($this->once()) + ->method('getBody') + ->will($this->returnValue('MyInvalidXml')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/categories', + [ + 'timeout' => 5, + ] + ) + ->will($this->returnValue($response)); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with( + 'Could not get categories, content was no valid XML', + [ + 'app' => 'core', + ] + ); + + $this->assertNull($this->ocsClient->getCategories()); + } + + public function testGetCategoriesSuccessful() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $response = $this->getMock('\OCP\Http\Client\IResponse'); + $response + ->expects($this->once()) + ->method('getBody') + ->will($this->returnValue(' + + + ok + 100 + + 6 + + + + 920 + ownCloud Multimedia + + + 921 + ownCloud PIM + + + 922 + ownCloud Productivity + + + 923 + ownCloud Game + + + 924 + ownCloud Tool + + + 925 + ownCloud other + + + + ')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/categories', + [ + 'timeout' => 5, + ] + ) + ->will($this->returnValue($response)); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $expected = [ + 920 => 'ownCloud Multimedia', + 921 => 'ownCloud PIM', + 922 => 'ownCloud Productivity', + 923 => 'ownCloud Game', + 924 => 'ownCloud Tool', + 925 => 'ownCloud other', + ]; + $this->assertSame($expected, $this->ocsClient->getCategories()); + } + + public function testGetApplicationsDisabledAppStore() { + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(false)); + $this->assertSame([], $this->ocsClient->getApplications([], 1, 'approved')); + } + + public function testGetApplicationsExceptionClient() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/data', + [ + 'timeout' => 5, + 'query' => [ + 'version' => implode('x', \OC_Util::getVersion()), + 'filter' => 'approved', + 'categories' => '815x1337', + 'sortmode' => 'new', + 'page' => 1, + 'pagesize' => 100, + 'approved' => 'approved', + ], + ] + ) + ->will($this->throwException(new \Exception('TheErrorMessage'))); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with( + 'Could not get applications: TheErrorMessage', + [ + 'app' => 'core', + ] + ); + + $this->assertSame([], $this->ocsClient->getApplications([815, 1337], 1, 'approved')); + } + + public function testGetApplicationsParseError() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $response = $this->getMock('\OCP\Http\Client\IResponse'); + $response + ->expects($this->once()) + ->method('getBody') + ->will($this->returnValue('MyInvalidXml')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/data', + [ + 'timeout' => 5, + 'query' => [ + 'version' => implode('x', \OC_Util::getVersion()), + 'filter' => 'approved', + 'categories' => '815x1337', + 'sortmode' => 'new', + 'page' => 1, + 'pagesize' => 100, + 'approved' => 'approved', + ], + ] + ) + ->will($this->returnValue($response)); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with( + 'Could not get applications, content was no valid XML', + [ + 'app' => 'core', + ] + ); + + $this->assertSame([], $this->ocsClient->getApplications([815, 1337], 1, 'approved')); + } + + public function testGetApplicationsSuccessful() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $response = $this->getMock('\OCP\Http\Client\IResponse'); + $response + ->expects($this->once()) + ->method('getBody') + ->will($this->returnValue(' + + + ok + 100 + + 2 + 100 + + + + 168707 + Calendar 8.0 + 0.6.4 + + 2015-02-09T15:23:56+01:00 + 2015-01-26T04:35:19+01:00 + 921 + ownCloud PIM + + owncloud + http://opendesktop.org/usermanager/search.php?username=owncloud + 5393 + 60 + Calendar App for ownCloud + 7 + 10 + 16 + 0 + 1 + AGPL + + https://apps.owncloud.com/content/show.php?content=168707 + + 0 + 0 + http://apps.owncloud.com/content/download.php?content=168707&id=1 + + + + + + 885 + + + 168708 + Contacts 8.0 + 0.3.0.18 + + 2015-02-09T15:18:58+01:00 + 2015-01-26T04:45:17+01:00 + 921 + ownCloud PIM + + owncloud + http://opendesktop.org/usermanager/search.php?username=owncloud + 4237 + 58 + + 3 + 6 + 16 + 200 + 1 + AGPL + + https://apps.owncloud.com/content/show.php?content=168708 + + 0 + 0 + http://apps.owncloud.com/content/download.php?content=168708&id=1 + + + + + + 1409 + + + ')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/data', + [ + 'timeout' => 5, + 'query' => [ + 'version' => implode('x', \OC_Util::getVersion()), + 'filter' => 'approved', + 'categories' => '815x1337', + 'sortmode' => 'new', + 'page' => 1, + 'pagesize' => 100, + 'approved' => 'approved', + ], + ] + ) + ->will($this->returnValue($response)); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $expected = [ + [ + 'id' => '168707', + 'name' => 'Calendar 8.0', + 'label' => 'recommended', + 'version' => '0.6.4', + 'type' => '921', + 'typename' => 'ownCloud PIM', + 'personid' => 'owncloud', + 'license' => 'AGPL', + 'detailpage' => 'https://apps.owncloud.com/content/show.php?content=168707', + 'preview' => '', + 'preview-full' => '', + 'changed' => 1423491836, + 'description' => 'Calendar App for ownCloud', + 'score' => '60', + 'downloads' => 5393, + 'level' => 0, + 'profilepage' => 'http://opendesktop.org/usermanager/search.php?username=owncloud', + ], + [ + 'id' => '168708', + 'name' => 'Contacts 8.0', + 'label' => 'recommended', + 'version' => '0.3.0.18', + 'type' => '921', + 'typename' => 'ownCloud PIM', + 'personid' => 'owncloud', + 'license' => 'AGPL', + 'detailpage' => 'https://apps.owncloud.com/content/show.php?content=168708', + 'preview' => '', + 'preview-full' => '', + 'changed' => 1423491538, + 'description' => '', + 'score' => '58', + 'downloads' => 4237, + 'level' => 200, + 'profilepage' => 'http://opendesktop.org/usermanager/search.php?username=owncloud', + ], + ]; + $this->assertEquals($expected, $this->ocsClient->getApplications([815, 1337], 1, 'approved')); + } + + public function tesGetApplicationDisabledAppStore() { + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(false)); + $this->assertNull($this->ocsClient->getApplication('MyId')); + } + + public function testGetApplicationExceptionClient() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/data/MyId', + [ + 'timeout' => 5, + ] + ) + ->will($this->throwException(new \Exception('TheErrorMessage'))); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with( + 'Could not get application: TheErrorMessage', + [ + 'app' => 'core', + ] + ); + + $this->assertNull($this->ocsClient->getApplication('MyId')); + } + + public function testGetApplicationParseError() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $response = $this->getMock('\OCP\Http\Client\IResponse'); + $response + ->expects($this->once()) + ->method('getBody') + ->will($this->returnValue('MyInvalidXml')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/data/MyId', + [ + 'timeout' => 5, + ] + ) + ->will($this->returnValue($response)); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with( + 'Could not get application, content was no valid XML', + [ + 'app' => 'core', + ] + ); + + $this->assertNull($this->ocsClient->getApplication('MyId')); + } + + public function testGetApplicationSuccessful() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $response = $this->getMock('\OCP\Http\Client\IResponse'); + $response + ->expects($this->once()) + ->method('getBody') + ->will($this->returnValue(' + + + ok + 100 + + + + + 166053 + Versioning + 0.0.1 + + 925 + ownCloud other + + owncloud + http://opendesktop.org/usermanager/search.php?username=owncloud + 2014-07-07T16:34:40+02:00 + 2014-07-07T16:34:40+02:00 + 140 + 50 + Placeholder for future updates + + + + + + + + + + + + + + + + + + + + + + + + 16 + AGPL + + 0 + http://apps.owncloud.com/content/show.php?content=166053 + 0 + http://apps.owncloud.com/content/show.php?action=fan&content=166053 + 0 + http://apps.owncloud.com/content/show.php?action=knowledgebase&content=166053 + ownCloud 7 + + + + + + + + + + https://apps.owncloud.com/content/show.php?content=166053 + + 0 + http://apps.owncloud.com/content/download.php?content=166053&id=1 + + + + + + 1 + + + + ')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/data/MyId', + [ + 'timeout' => 5, + ] + ) + ->will($this->returnValue($response)); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $expected = [ + 'id' => 166053, + 'name' => 'Versioning', + 'version' => '0.0.1', + 'type' => '925', + 'label' => 'recommended', + 'typename' => 'ownCloud other', + 'personid' => 'owncloud', + 'profilepage' => 'http://opendesktop.org/usermanager/search.php?username=owncloud', + 'detailpage' => 'https://apps.owncloud.com/content/show.php?content=166053', + 'preview1' => '', + 'preview2' => '', + 'preview3' => '', + 'changed' => 1404743680, + 'description' => 'Placeholder for future updates', + 'score' => 50, + ]; + $this->assertSame($expected, $this->ocsClient->getApplication('MyId')); + } + public function testGetApplicationEmptyXml() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $response = $this->getMock('\OCP\Http\Client\IResponse'); + $response + ->expects($this->once()) + ->method('getBody') + ->will($this->returnValue(' + + + ok + 100 + + + + ')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/data/MyId', + [ + 'timeout' => 5, + ] + ) + ->will($this->returnValue($response)); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $this->assertSame(null, $this->ocsClient->getApplication('MyId')); + } + + public function testGetApplicationDownloadDisabledAppStore() { + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(false)); + $this->assertNull($this->ocsClient->getApplicationDownload('MyId')); + } + + public function testGetApplicationDownloadExceptionClient() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/download/MyId/1', + [ + 'timeout' => 5, + ] + ) + ->will($this->throwException(new \Exception('TheErrorMessage'))); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with( + 'Could not get application download URL: TheErrorMessage', + [ + 'app' => 'core', + ] + ); + + $this->assertNull($this->ocsClient->getApplicationDownload('MyId')); + } + + public function testGetApplicationDownloadParseError() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $response = $this->getMock('\OCP\Http\Client\IResponse'); + $response + ->expects($this->once()) + ->method('getBody') + ->will($this->returnValue('MyInvalidXml')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/download/MyId/1', + [ + 'timeout' => 5, + ] + ) + ->will($this->returnValue($response)); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with( + 'Could not get application download URL, content was no valid XML', + [ + 'app' => 'core', + ] + ); + + $this->assertNull($this->ocsClient->getApplicationDownload('MyId')); + } + + public function testGetApplicationDownloadUrlSuccessful() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('appstoreenabled', true) + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('appstoreurl', 'https://api.owncloud.com/v1') + ->will($this->returnValue('https://api.owncloud.com/v1')); + + $response = $this->getMock('\OCP\Http\Client\IResponse'); + $response + ->expects($this->once()) + ->method('getBody') + ->will($this->returnValue(' + + + ok + 100 + + + + + https://apps.owncloud.com/CONTENT/content-files/166052-files_trashbin.zip + application/zip + + + + + + + + ')); + + $client = $this->getMock('\OCP\Http\Client\IClient'); + $client + ->expects($this->once()) + ->method('get') + ->with( + 'https://api.owncloud.com/v1/content/download/MyId/1', + [ + 'timeout' => 5, + ] + ) + ->will($this->returnValue($response)); + + $this->clientService + ->expects($this->once()) + ->method('newClient') + ->will($this->returnValue($client)); + + $expected = [ + 'downloadlink' => 'https://apps.owncloud.com/CONTENT/content-files/166052-files_trashbin.zip', + ]; + $this->assertSame($expected, $this->ocsClient->getApplicationDownload('MyId')); + } +} -- cgit v1.2.3