Skip to content

Commit

Permalink
Update acceptance test and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Sep 16, 2020
1 parent 83f4666 commit bb14b4c
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 30 deletions.
17 changes: 8 additions & 9 deletions core/Controller/OcsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
use OCP\IUserSession;

class OcsController extends \OCP\AppFramework\OCSController {
public const SELECT_MULTIPLE_STMT = 'SELECT `key`, `app`, `value` FROM `*PREFIX*privatedata` WHERE `user` = ? AND `app` = ? ';
public const SELECT_SINGLE_STMT = 'SELECT `key`, `app`, `value` FROM `*PREFIX*privatedata` WHERE `user` = ? AND `app` = ? AND `key` = ? ';
public const DELETE_STMT = 'DELETE FROM `*PREFIX*privatedata` WHERE `user` = ? AND `app` = ? AND `key` = ? ';

/** @var IDBConnection */
private $dbConnection;

Expand Down Expand Up @@ -85,6 +89,7 @@ public function checkPerson($login, $password) {
if ($login && $password) {
$user = $this->userManager->checkPassword($login, $password);
if ($user !== false) {
$xml = [];
$xml['person']['personid'] = $user->getUID();
return new Result($xml);
} else {
Expand Down Expand Up @@ -140,15 +145,11 @@ public function getAttribute($app, $key = null) {
$user = $this->userSession->getUser()->getUID();

if ($key === null) {
$q = $this->dbConnection->prepare(
'SELECT `key`, `app`, `value` FROM `*PREFIX*privatedata` WHERE `user` = ? AND `app` = ? '
);
$q = $this->dbConnection->prepare(self::SELECT_MULTIPLE_STMT);
$result = $q->execute([$user, $app]);
} else {
$key = $this->escape($key);
$q = $this->dbConnection->prepare(
'SELECT `key`, `app`, `value` FROM `*PREFIX*privatedata` WHERE `user` = ? AND `app` = ? AND `key` = ? '
);
$q = $this->dbConnection->prepare(self::SELECT_SINGLE_STMT);
$result = $q->execute([$user, $app, $key]);
}

Expand Down Expand Up @@ -220,9 +221,7 @@ public function deleteAttribute($app, $key) {
$user = $this->userSession->getUser()->getUID();

// delete in DB
$q = $this->dbConnection->prepare(
'DELETE FROM `*PREFIX*privatedata` WHERE `user` = ? AND `app` = ? AND `key` = ? '
);
$q = $this->dbConnection->prepare(self::DELETE_STMT);
$q->execute([$user, $app, $key]);

return new Result(null, 100);
Expand Down
195 changes: 195 additions & 0 deletions tests/Core/Controller/OcsControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php
/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2020, ownCloud GmbH
* @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 <http://www.gnu.org/licenses/>
*
*/

namespace Tests\Core\Controller;

use Doctrine\DBAL\Driver\Statement;
use OC\AppFramework\Http\Request;
use OC\Core\Controller\OcsController;
use OCP\IDBConnection;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

/**
* Class OcsControllerTest
*
* @package OC\Core\Controller
*/
class OcsControllerTest extends TestCase {
/** @var Request | MockObject */
private $request;

/** @var IDBConnection | MockObject */
private $dbConn;

/** @var IUserSession | MockObject */
private $userSession;

/** @var IUserManager | MockObject */
private $userManager;

/** @var OcsController | MockObject */
private $controller;

protected function setUp(): void {
parent::setUp();
$this->request = $this->createMock(IRequest::class);
$this->dbConn = $this->createMock(IDBConnection::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->controller = new OcsController(
'core',
$this->request,
$this->dbConn,
$this->userSession,
$this->userManager
);
}

public function testGetConfig() {
$hostname = 'host';
$this->request->expects($this->once())
->method('getServerHost')
->willReturn($hostname);
$config = $this->controller->getConfig();
$this->assertArrayHasKey('host', $config->getData());
$this->assertEquals($hostname, $config->getData()['host']);
}

public function checkPersonDataProvider() {
return [
['', '', false, 101],
['user', '', false, 101],
['', 'password', false, 101],
['user', 'password', false, 102],
['user', 'password', $this->getUserMock(), 100],
];
}

/**
* @dataProvider checkPersonDataProvider
*
* @param string $login
* @param string $password
* @param bool $checkPasswordSuccess
* @param int $expectedCode
*/
public function testCheckPerson($login, $password, $checkPasswordSuccess, $expectedCode) {
$this->userManager->method('checkPassword')
->willReturn($checkPasswordSuccess);
$result = $this->controller->checkPerson($login, $password);
$this->assertEquals($expectedCode, $result->getStatusCode());
}

public function getAttributeDataProvider() {
return [
['app', null],
['app', 'key']
];
}

/**
* @dataProvider getAttributeDataProvider
*
* @param string $app
* @param string|null $key
*/
public function testGetAttribute($app, $key) {
$user = $this->getUserMock();
$this->userSession->method('getUser')
->willReturn($user);

$stmt = $this->createMock(Statement::class);
$stmt->expects($this->once())
->method('execute');

$this->dbConn->expects($this->once())
->method('prepare')
->with(
$key === null ? OcsController::SELECT_MULTIPLE_STMT : OcsController::SELECT_SINGLE_STMT
)
->willReturn($stmt);
$result = $this->controller->getAttribute($app, $key);
$this->assertEquals(100, $result->getStatusCode());
}

public function testSetAttribute() {
$app = 'foo';
$key = 'bar';
$value = '42';
$user = $this->getUserMock();
$this->userSession->method('getUser')
->willReturn($user);
$this->dbConn->expects($this->once())
->method('upsert')
->with(
'*PREFIX*privatedata',
[
'value' => $value,
'user' => $user->getUID(),
'app' => $app,
'key' => $key
],
[
'user',
'app',
'key'
]
);
$this->request->expects($this->once())
->method('getParam')
->with('value')
->willReturn($value);
$result = $this->controller->setAttribute($app, $key);
$this->assertEquals(100, $result->getStatusCode());
}

public function testDeleteAttribute() {
$app = 'foo';
$key = 'bar';
$user = $this->getUserMock();
$this->userSession->method('getUser')
->willReturn($user);

$stmt = $this->createMock(Statement::class);
$stmt->expects($this->once())
->method('execute')
->with([$user->getUID(), $app, $key]);

$this->dbConn->expects($this->once())
->method('prepare')
->with(OcsController::DELETE_STMT)
->willReturn($stmt);
$result = $this->controller->deleteAttribute($app, $key);
$this->assertEquals(100, $result->getStatusCode());
}

protected function getUserMock() {
$userMock = $this->createMock(IUser::class);
$userMock->method('getUID')
->willReturn('foo');
return $userMock;
}
}
23 changes: 2 additions & 21 deletions tests/acceptance/features/apiAuth/cors.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,8 @@ Feature: CORS headers
| 2 | /cloud/groups | 997 | 401 |
| 1 | /cloud/users | 997 | 401 |
| 2 | /cloud/users | 997 | 401 |

#merge into previous scenario when fixed
@issue-34664
Scenario Outline: CORS headers should be returned when setting CORS domain sending Origin header
Given using OCS API version "<ocs_api_version>"
And user "Alice" has added "https://aphno.badal" to the list of personal CORS domains
When user "Alice" sends HTTP method "GET" to OCS API endpoint "<endpoint>" with headers
| header | value |
| Origin | https://aphno.badal |
Then the OCS status code should be "<ocs-code>"
And the HTTP status code should be "<http-code>"
Then the following headers should not be set
| header |
| Access-Control-Allow-Headers |
| Access-Control-Expose-Headers |
| Access-Control-Allow-Origin |
| Access-Control-Allow-Methods |
Examples:
| ocs_api_version | endpoint | ocs-code | http-code |
| 1 | /config | 100 | 200 |
| 2 | /config | 200 | 200 |
| 1 | /config | 100 | 200 |
| 2 | /config | 200 | 200 |

Scenario Outline: CORS headers should be returned when setting CORS domain sending Origin header (admin only endpoints)
Given using OCS API version "<ocs_api_version>"
Expand Down

0 comments on commit bb14b4c

Please sign in to comment.