aboutsummaryrefslogtreecommitdiffstats
path: root/apps/oauth2/tests
diff options
context:
space:
mode:
Diffstat (limited to 'apps/oauth2/tests')
-rw-r--r--apps/oauth2/tests/Controller/OauthApiControllerTest.php73
-rw-r--r--apps/oauth2/tests/Controller/SettingsControllerTest.php17
-rw-r--r--apps/oauth2/tests/Settings/AdminTest.php10
3 files changed, 72 insertions, 28 deletions
diff --git a/apps/oauth2/tests/Controller/OauthApiControllerTest.php b/apps/oauth2/tests/Controller/OauthApiControllerTest.php
index 8977f6a2b66..eb9311dbbc7 100644
--- a/apps/oauth2/tests/Controller/OauthApiControllerTest.php
+++ b/apps/oauth2/tests/Controller/OauthApiControllerTest.php
@@ -43,6 +43,7 @@ use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IRequest;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
+use Psr\Log\LoggerInterface;
use Test\TestCase;
/* We have to use this to add a property to the mocked request and avoid warnings about dynamic properties on PHP>=8.2 */
@@ -67,6 +68,8 @@ class OauthApiControllerTest extends TestCase {
private $time;
/** @var Throttler|\PHPUnit\Framework\MockObject\MockObject */
private $throttler;
+ /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
+ private $logger;
/** @var OauthApiController */
private $oauthApiController;
@@ -81,6 +84,7 @@ class OauthApiControllerTest extends TestCase {
$this->secureRandom = $this->createMock(ISecureRandom::class);
$this->time = $this->createMock(ITimeFactory::class);
$this->throttler = $this->createMock(Throttler::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
$this->oauthApiController = new OauthApiController(
'oauth2',
@@ -91,6 +95,7 @@ class OauthApiControllerTest extends TestCase {
$this->tokenProvider,
$this->secureRandom,
$this->time,
+ $this->logger,
$this->throttler
);
}
@@ -198,16 +203,21 @@ class OauthApiControllerTest extends TestCase {
$client = new Client();
$client->setClientIdentifier('clientId');
- $client->setSecret('clientSecret');
+ $client->setSecret('encryptedClientSecret');
$this->clientMapper->method('getByUid')
->with(42)
->willReturn($client);
- $this->crypto->method('decrypt')
- ->with(
- 'encryptedToken',
- 'validrefresh'
- )->willReturn('decryptedToken');
+ $this->crypto
+ ->method('decrypt')
+ ->with($this->callback(function (string $text) {
+ return $text === 'encryptedClientSecret' || $text === 'encryptedToken';
+ }))
+ ->willReturnCallback(function (string $text) {
+ return $text === 'encryptedClientSecret'
+ ? 'clientSecret'
+ : ($text === 'encryptedToken' ? 'decryptedToken' : '');
+ });
$this->tokenProvider->method('getTokenById')
->with(1337)
@@ -232,16 +242,21 @@ class OauthApiControllerTest extends TestCase {
$client = new Client();
$client->setClientIdentifier('clientId');
- $client->setSecret('clientSecret');
+ $client->setSecret('encryptedClientSecret');
$this->clientMapper->method('getByUid')
->with(42)
->willReturn($client);
- $this->crypto->method('decrypt')
- ->with(
- 'encryptedToken',
- 'validrefresh'
- )->willReturn('decryptedToken');
+ $this->crypto
+ ->method('decrypt')
+ ->with($this->callback(function (string $text) {
+ return $text === 'encryptedClientSecret' || $text === 'encryptedToken';
+ }))
+ ->willReturnCallback(function (string $text) {
+ return $text === 'encryptedClientSecret'
+ ? 'clientSecret'
+ : ($text === 'encryptedToken' ? 'decryptedToken' : '');
+ });
$appToken = new PublicKeyToken();
$appToken->setUid('userId');
@@ -324,16 +339,21 @@ class OauthApiControllerTest extends TestCase {
$client = new Client();
$client->setClientIdentifier('clientId');
- $client->setSecret('clientSecret');
+ $client->setSecret('encryptedClientSecret');
$this->clientMapper->method('getByUid')
->with(42)
->willReturn($client);
- $this->crypto->method('decrypt')
- ->with(
- 'encryptedToken',
- 'validrefresh'
- )->willReturn('decryptedToken');
+ $this->crypto
+ ->method('decrypt')
+ ->with($this->callback(function (string $text) {
+ return $text === 'encryptedClientSecret' || $text === 'encryptedToken';
+ }))
+ ->willReturnCallback(function (string $text) {
+ return $text === 'encryptedClientSecret'
+ ? 'clientSecret'
+ : ($text === 'encryptedToken' ? 'decryptedToken' : '');
+ });
$appToken = new PublicKeyToken();
$appToken->setUid('userId');
@@ -419,16 +439,21 @@ class OauthApiControllerTest extends TestCase {
$client = new Client();
$client->setClientIdentifier('clientId');
- $client->setSecret('clientSecret');
+ $client->setSecret('encryptedClientSecret');
$this->clientMapper->method('getByUid')
->with(42)
->willReturn($client);
- $this->crypto->method('decrypt')
- ->with(
- 'encryptedToken',
- 'validrefresh'
- )->willReturn('decryptedToken');
+ $this->crypto
+ ->method('decrypt')
+ ->with($this->callback(function (string $text) {
+ return $text === 'encryptedClientSecret' || $text === 'encryptedToken';
+ }))
+ ->willReturnCallback(function (string $text) {
+ return $text === 'encryptedClientSecret'
+ ? 'clientSecret'
+ : ($text === 'encryptedToken' ? 'decryptedToken' : '');
+ });
$appToken = new PublicKeyToken();
$appToken->setUid('userId');
diff --git a/apps/oauth2/tests/Controller/SettingsControllerTest.php b/apps/oauth2/tests/Controller/SettingsControllerTest.php
index e79d7cbe34e..817747599b7 100644
--- a/apps/oauth2/tests/Controller/SettingsControllerTest.php
+++ b/apps/oauth2/tests/Controller/SettingsControllerTest.php
@@ -38,6 +38,7 @@ use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
+use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use Test\TestCase;
@@ -61,6 +62,8 @@ class SettingsControllerTest extends TestCase {
private $settingsController;
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
private $l;
+ /** @var ICrypto|\PHPUnit\Framework\MockObject\MockObject */
+ private $crypto;
protected function setUp(): void {
parent::setUp();
@@ -71,6 +74,7 @@ class SettingsControllerTest extends TestCase {
$this->accessTokenMapper = $this->createMock(AccessTokenMapper::class);
$this->authTokenProvider = $this->createMock(IAuthTokenProvider::class);
$this->userManager = $this->createMock(IUserManager::class);
+ $this->crypto = $this->createMock(ICrypto::class);
$this->l = $this->createMock(IL10N::class);
$this->l->method('t')
->willReturnArgument(0);
@@ -82,7 +86,8 @@ class SettingsControllerTest extends TestCase {
$this->accessTokenMapper,
$this->l,
$this->authTokenProvider,
- $this->userManager
+ $this->userManager,
+ $this->crypto
);
}
@@ -96,6 +101,11 @@ class SettingsControllerTest extends TestCase {
'MySecret',
'MyClientIdentifier');
+ $this->crypto
+ ->expects($this->once())
+ ->method('encrypt')
+ ->willReturn('MyEncryptedSecret');
+
$client = new Client();
$client->setName('My Client Name');
$client->setRedirectUri('https://example.com/');
@@ -108,7 +118,7 @@ class SettingsControllerTest extends TestCase {
->with($this->callback(function (Client $c) {
return $c->getName() === 'My Client Name' &&
$c->getRedirectUri() === 'https://example.com/' &&
- $c->getSecret() === 'MySecret' &&
+ $c->getSecret() === 'MyEncryptedSecret' &&
$c->getClientIdentifier() === 'MyClientIdentifier';
}))->willReturnCallback(function (Client $c) {
$c->setId(42);
@@ -175,7 +185,8 @@ class SettingsControllerTest extends TestCase {
$this->accessTokenMapper,
$this->l,
$tokenProviderMock,
- $userManager
+ $userManager,
+ $this->crypto
);
$result = $settingsController->deleteClient(123);
diff --git a/apps/oauth2/tests/Settings/AdminTest.php b/apps/oauth2/tests/Settings/AdminTest.php
index fc5ebbb8365..fb19a9fc6d1 100644
--- a/apps/oauth2/tests/Settings/AdminTest.php
+++ b/apps/oauth2/tests/Settings/AdminTest.php
@@ -28,7 +28,9 @@ use OCA\OAuth2\Settings\Admin;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IURLGenerator;
+use OCP\Security\ICrypto;
use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
use Test\TestCase;
class AdminTest extends TestCase {
@@ -48,7 +50,13 @@ class AdminTest extends TestCase {
$this->initialState = $this->createMock(IInitialState::class);
$this->clientMapper = $this->createMock(ClientMapper::class);
- $this->admin = new Admin($this->initialState, $this->clientMapper, $this->createMock(IURLGenerator::class));
+ $this->admin = new Admin(
+ $this->initialState,
+ $this->clientMapper,
+ $this->createMock(IURLGenerator::class),
+ $this->createMock(ICrypto::class),
+ $this->createMock(LoggerInterface::class)
+ );
}
public function testGetForm() {