Skip to content

Commit 6feb2b9

Browse files
committed
Some tests for the remote cloud api
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 52dcb96 commit 6feb2b9

6 files changed

Lines changed: 222 additions & 4 deletions

File tree

build/integration/config/behat.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,18 @@ default:
8585
- admin
8686
- admin
8787
regular_user_password: what_for
88-
88+
remoteapi:
89+
paths:
90+
- %paths.base%/../remoteapi_features
91+
contexts:
92+
- FeatureContext:
93+
baseUrl: http://localhost:8080/ocs/
94+
admin:
95+
- admin
96+
- admin
97+
regular_user_password: 123456
98+
- RemoteContext:
99+
remote: http://localhost:8080
89100
extensions:
90101
jarnaiz\JUnitFormatter\JUnitFormatterExtension:
91102
filename: report.xml

build/integration/features/bootstrap/BasicStructure.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ trait BasicStructure {
5858
/** @var string */
5959
private $requestToken;
6060

61+
protected $adminUser;
62+
protected $regularUser;
63+
protected $localBaseUrl;
64+
protected $remoteBaseUrl;
65+
6166
public function __construct($baseUrl, $admin, $regular_user_password) {
6267

6368
// Initialize your context here
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
4+
*
5+
* @license GNU AGPL version 3 or any later version
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
use Behat\Behat\Context\Context;
23+
24+
require __DIR__ . '/../../vendor/autoload.php';
25+
require __DIR__ . '/../../../../lib/base.php';
26+
27+
/**
28+
* Remote context.
29+
*/
30+
class RemoteContext implements Context {
31+
/** @var \OC\Remote\Instance */
32+
protected $remoteInstance;
33+
34+
/** @var \OC\Remote\Credentials */
35+
protected $credentails;
36+
37+
/** @var \OC\Remote\User */
38+
protected $userResult;
39+
40+
protected $remoteUrl;
41+
42+
protected $lastException;
43+
44+
public function __construct($remote) {
45+
$this->remoteUrl = $remote;
46+
}
47+
48+
protected function getApiClient() {
49+
return new \OC\Remote\Api\OCS($this->remoteInstance, $this->credentails, \OC::$server->getHTTPClientService());
50+
}
51+
52+
/**
53+
* @Given /^using remote server "(REMOTE|NON_EXISTING)"$/
54+
*
55+
* @param string $remoteServer "NON_EXISTING" or "REMOTE"
56+
*/
57+
public function selectRemoteInstance($remoteServer) {
58+
if ($remoteServer == "REMOTE") {
59+
$baseUri = $this->remoteUrl;
60+
} else {
61+
$baseUri = 'nonexistingnextcloudserver.local';
62+
}
63+
$this->lastException = null;
64+
try {
65+
$this->remoteInstance = new \OC\Remote\Instance($baseUri, \OC::$server->getMemCacheFactory()->createLocal(), \OC::$server->getHTTPClientService());
66+
// trigger the status request
67+
$this->remoteInstance->getProtocol();
68+
} catch (\Exception $e) {
69+
$this->lastException = $e;
70+
}
71+
}
72+
73+
/**
74+
* @Then /^the remote version should be "([^"]*)"$/
75+
* @param string $version
76+
*/
77+
public function theRemoteVersionShouldBe($version) {
78+
if ($version === '__current_version__') {
79+
$version = \OC::$server->getConfig()->getSystemValue('version', '0.0.0.0');
80+
}
81+
82+
PHPUnit_Framework_Assert::assertEquals($version, $this->remoteInstance->getVersion());
83+
}
84+
85+
/**
86+
* @Then /^the remote protocol should be "([^"]*)"$/
87+
* @param string $protocol
88+
*/
89+
public function theRemoteProtocolShouldBe($protocol) {
90+
PHPUnit_Framework_Assert::assertEquals($protocol, $this->remoteInstance->getProtocol());
91+
}
92+
93+
/**
94+
* @Given /^using credentials "([^"]*)", "([^"]*)"/
95+
* @param string $user
96+
* @param string $password
97+
*/
98+
public function usingCredentials($user, $password) {
99+
$this->credentails = new \OC\Remote\Credentials($user, $password);
100+
}
101+
102+
/**
103+
* @When /^getting the remote user info for "([^"]*)"$/
104+
* @param string $user
105+
*/
106+
public function remoteUserInfo($user) {
107+
$this->lastException = null;
108+
try {
109+
$this->userResult = $this->getApiClient()->getUser($user);
110+
} catch (\Exception $e) {
111+
$this->lastException = $e;
112+
}
113+
}
114+
115+
/**
116+
* @Then /^the remote user should have userid "([^"]*)"$/
117+
* @param string $user
118+
*/
119+
public function remoteUserId($user) {
120+
PHPUnit_Framework_Assert::assertEquals($user, $this->userResult->getUserId());
121+
}
122+
123+
/**
124+
* @Then /^the request should throw a "([^"]*)"$/
125+
* @param string $class
126+
*/
127+
public function lastError($class) {
128+
PHPUnit_Framework_Assert::assertEquals($class, get_class($this->lastException));
129+
}
130+
131+
/**
132+
* @Then /^the capability "([^"]*)" is "([^"]*)"$/
133+
* @param string $key
134+
* @param string $value
135+
*/
136+
public function hasCapability($key, $value) {
137+
$capabilities = $this->getApiClient()->getCapabilities();
138+
$current = $capabilities;
139+
$parts = explode('.', $key);
140+
foreach ($parts as $part) {
141+
if ($current !== null) {
142+
$current = isset($current[$part]) ? $current[$part] : null;
143+
}
144+
}
145+
PHPUnit_Framework_Assert::assertEquals($value, $current);
146+
}
147+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Feature: remote
2+
3+
Scenario: Get status of remote server
4+
Given using remote server "REMOTE"
5+
Then the remote version should be "__current_version__"
6+
And the remote protocol should be "http"
7+
8+
Scenario: Get status of a non existing server
9+
Given using remote server "NON_EXISTING"
10+
Then the request should throw a "OC\Remote\Api\NotFoundException"
11+
12+
Scenario: Get user info for a remote user
13+
Given using remote server "REMOTE"
14+
And user "user0" exists
15+
And using credentials "user0", "123456"
16+
When getting the remote user info for "user0"
17+
Then the remote user should have userid "user0"
18+
19+
Scenario: Get user info for a non existing remote user
20+
Given using remote server "REMOTE"
21+
And user "user0" exists
22+
And using credentials "user0", "123456"
23+
When getting the remote user info for "user_non_existing"
24+
Then the request should throw a "OC\Remote\Api\NotFoundException"
25+
26+
Scenario: Get user info with invalid credentials
27+
Given using remote server "REMOTE"
28+
And user "user0" exists
29+
And using credentials "user0", "invalid"
30+
When getting the remote user info for "user0"
31+
Then the request should throw a "OC\ForbiddenException"
32+
33+
Scenario: Get capability of remote server
34+
Given using remote server "REMOTE"
35+
And user "user0" exists
36+
And using credentials "user0", "invalid"
37+
Then the capability "theming.name" is "Nextcloud"

lib/private/Remote/Api/OCS.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace OC\Remote\Api;
2323

2424

25+
use GuzzleHttp\Exception\ClientException;
2526
use OC\ForbiddenException;
2627
use OC\Remote\User;
2728
use OCP\API;
@@ -39,8 +40,18 @@ class OCS extends ApiBase {
3940
* @throws \Exception
4041
*/
4142
protected function request($method, $url, array $body = [], array $query = [], array $headers = []) {
42-
$response = json_decode(parent::request($method, '/ocs/v2.php/' . $url, $body, $query, $headers), true);
43-
if (!isset($result['ocs']) || !isset($result['ocs']['meta'])) {
43+
try {
44+
$response = json_decode(parent::request($method, '/ocs/v2.php/' . $url, $body, $query, $headers), true);
45+
} catch (ClientException $e) {
46+
if ($e->getResponse()->getStatusCode() === 404) {
47+
throw new NotFoundException();
48+
} else if ($e->getResponse()->getStatusCode() === 403 || $e->getResponse()->getStatusCode() === 401) {
49+
throw new ForbiddenException();
50+
} else {
51+
throw $e;
52+
}
53+
}
54+
if (!isset($response['ocs']) || !isset($response['ocs']['meta'])) {
4455
throw new \Exception('Invalid ocs response');
4556
}
4657
if ($response['ocs']['meta']['statuscode'] === API::RESPOND_UNAUTHORISED) {
@@ -60,7 +71,11 @@ public function getUser($userId) {
6071
return new User($this->request('get', 'cloud/users/' . $userId));
6172
}
6273

74+
/**
75+
* @return array The capabilities in the form of [$appId => [$capability => $value]]
76+
*/
6377
public function getCapabilities() {
64-
return $this->request('get', 'cloud/capabilities');
78+
$result = $this->request('get', 'cloud/capabilities');
79+
return $result['capabilities'];
6580
}
6681
}

lib/private/Remote/Instance.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace OC\Remote;
2323

24+
use OC\Remote\Api\NotFoundException;
2425
use OCP\Http\Client\IClientService;
2526
use OCP\ICache;
2627

@@ -111,6 +112,8 @@ private function getStatus() {
111112
if ($status) {
112113
$this->cache->set($key, $status, 5 * 60);
113114
$this->status = $status;
115+
} else {
116+
throw new NotFoundException('Remote server not found at address ' . $this->url);
114117
}
115118
}
116119
return $status;

0 commit comments

Comments
 (0)