mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Implementation of the backend for the OAuth2 clients management
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\unit\modules\oauth\models;
|
||||
|
||||
use api\modules\oauth\models\ApplicationType;
|
||||
use common\models\OauthClient;
|
||||
use tests\codeception\api\unit\TestCase;
|
||||
|
||||
class ApplicationTypeTest extends TestCase {
|
||||
|
||||
public function testApplyToClient(): void {
|
||||
$model = new ApplicationType();
|
||||
$model->name = 'Application name';
|
||||
$model->websiteUrl = 'http://example.com';
|
||||
$model->redirectUri = 'http://example.com/oauth/ely';
|
||||
$model->description = 'Application description.';
|
||||
|
||||
$client = new OauthClient();
|
||||
|
||||
$model->applyToClient($client);
|
||||
|
||||
$this->assertSame('Application name', $client->name);
|
||||
$this->assertSame('Application description.', $client->description);
|
||||
$this->assertSame('http://example.com/oauth/ely', $client->redirect_uri);
|
||||
$this->assertSame('http://example.com', $client->website_url);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\unit\modules\oauth\models;
|
||||
|
||||
use api\modules\oauth\models\BaseOauthClientType;
|
||||
use common\models\OauthClient;
|
||||
use tests\codeception\api\unit\TestCase;
|
||||
|
||||
class BaseOauthClientTypeTest extends TestCase {
|
||||
|
||||
public function testApplyTyClient(): void {
|
||||
$client = new OauthClient();
|
||||
|
||||
/** @var BaseOauthClientType|\Mockery\MockInterface $form */
|
||||
$form = mock(BaseOauthClientType::class);
|
||||
$form->makePartial();
|
||||
$form->name = 'Application name';
|
||||
$form->websiteUrl = 'http://example.com';
|
||||
|
||||
$form->applyToClient($client);
|
||||
$this->assertSame('Application name', $client->name);
|
||||
$this->assertSame('http://example.com', $client->website_url);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\unit\modules\oauth\models;
|
||||
|
||||
use api\modules\oauth\models\MinecraftServerType;
|
||||
use common\models\OauthClient;
|
||||
use tests\codeception\api\unit\TestCase;
|
||||
|
||||
class MinecraftServerTypeTest extends TestCase {
|
||||
|
||||
public function testApplyToClient(): void {
|
||||
$model = new MinecraftServerType();
|
||||
$model->name = 'Server name';
|
||||
$model->websiteUrl = 'http://example.com';
|
||||
$model->minecraftServerIp = 'localhost:12345';
|
||||
|
||||
$client = new OauthClient();
|
||||
|
||||
$model->applyToClient($client);
|
||||
|
||||
$this->assertSame('Server name', $client->name);
|
||||
$this->assertSame('http://example.com', $client->website_url);
|
||||
$this->assertSame('localhost:12345', $client->minecraft_server_ip);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\unit\modules\oauth\models;
|
||||
|
||||
use api\modules\oauth\models\ApplicationType;
|
||||
use api\modules\oauth\models\MinecraftServerType;
|
||||
use api\modules\oauth\models\OauthClientFormFactory;
|
||||
use common\models\OauthClient;
|
||||
use tests\codeception\api\unit\TestCase;
|
||||
|
||||
class OauthClientFormFactoryTest extends TestCase {
|
||||
|
||||
public function testCreate() {
|
||||
$client = new OauthClient();
|
||||
$client->type = OauthClient::TYPE_APPLICATION;
|
||||
$client->name = 'Application name';
|
||||
$client->description = 'Application description.';
|
||||
$client->website_url = 'http://example.com';
|
||||
$client->redirect_uri = 'http://example.com/oauth/ely';
|
||||
/** @var ApplicationType $requestForm */
|
||||
$requestForm = OauthClientFormFactory::create($client);
|
||||
$this->assertInstanceOf(ApplicationType::class, $requestForm);
|
||||
$this->assertSame('Application name', $requestForm->name);
|
||||
$this->assertSame('Application description.', $requestForm->description);
|
||||
$this->assertSame('http://example.com', $requestForm->websiteUrl);
|
||||
$this->assertSame('http://example.com/oauth/ely', $requestForm->redirectUri);
|
||||
|
||||
$client = new OauthClient();
|
||||
$client->type = OauthClient::TYPE_MINECRAFT_SERVER;
|
||||
$client->name = 'Server name';
|
||||
$client->website_url = 'http://example.com';
|
||||
$client->minecraft_server_ip = 'localhost:12345';
|
||||
/** @var MinecraftServerType $requestForm */
|
||||
$requestForm = OauthClientFormFactory::create($client);
|
||||
$this->assertInstanceOf(MinecraftServerType::class, $requestForm);
|
||||
$this->assertSame('Server name', $requestForm->name);
|
||||
$this->assertSame('http://example.com', $requestForm->websiteUrl);
|
||||
$this->assertSame('localhost:12345', $requestForm->minecraftServerIp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \api\modules\oauth\exceptions\UnsupportedOauthClientType
|
||||
*/
|
||||
public function testCreateUnknownType() {
|
||||
$client = new OauthClient();
|
||||
$client->type = 'unknown-type';
|
||||
OauthClientFormFactory::create($client);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\unit\modules\oauth\models;
|
||||
|
||||
use api\modules\oauth\models\OauthClientForm;
|
||||
use api\modules\oauth\models\OauthClientTypeForm;
|
||||
use common\models\OauthClient;
|
||||
use common\tasks\ClearOauthSessions;
|
||||
use tests\codeception\api\unit\TestCase;
|
||||
|
||||
class OauthClientFormTest extends TestCase {
|
||||
|
||||
public function testSave() {
|
||||
/** @var OauthClient|\Mockery\MockInterface $client */
|
||||
$client = mock(OauthClient::class . '[save]');
|
||||
$client->shouldReceive('save')->andReturn(true);
|
||||
$client->account_id = 1;
|
||||
$client->type = OauthClient::TYPE_APPLICATION;
|
||||
$client->name = 'Test application';
|
||||
|
||||
/** @var OauthClientForm|\Mockery\MockInterface $form */
|
||||
$form = mock(OauthClientForm::class . '[isClientExists]', [$client]);
|
||||
$form->shouldAllowMockingProtectedMethods();
|
||||
$form->shouldReceive('isClientExists')
|
||||
->times(3)
|
||||
->andReturnValues([true, true, false]);
|
||||
|
||||
/** @var OauthClientTypeForm|\Mockery\MockInterface $requestType */
|
||||
$requestType = mock(OauthClientTypeForm::class);
|
||||
$requestType->shouldReceive('validate')->once()->andReturn(true);
|
||||
$requestType->shouldReceive('applyToClient')->once()->withArgs([$client]);
|
||||
|
||||
$this->assertTrue($form->save($requestType));
|
||||
$this->assertSame('test-application2', $client->id);
|
||||
$this->assertNotNull($client->secret);
|
||||
$this->assertSame(64, mb_strlen($client->secret));
|
||||
}
|
||||
|
||||
public function testSaveUpdateExistsModel() {
|
||||
/** @var OauthClient|\Mockery\MockInterface $client */
|
||||
$client = mock(OauthClient::class . '[save]');
|
||||
$client->shouldReceive('save')->andReturn(true);
|
||||
$client->setIsNewRecord(false);
|
||||
$client->id = 'application-id';
|
||||
$client->secret = 'application_secret';
|
||||
$client->account_id = 1;
|
||||
$client->type = OauthClient::TYPE_APPLICATION;
|
||||
$client->name = 'Application name';
|
||||
$client->description = 'Application description';
|
||||
$client->redirect_uri = 'http://example.com/oauth/ely';
|
||||
$client->website_url = 'http://example.com';
|
||||
|
||||
/** @var OauthClientForm|\Mockery\MockInterface $form */
|
||||
$form = mock(OauthClientForm::class . '[isClientExists]', [$client]);
|
||||
$form->shouldAllowMockingProtectedMethods();
|
||||
$form->shouldReceive('isClientExists')->andReturn(false);
|
||||
|
||||
$request = new class implements OauthClientTypeForm {
|
||||
|
||||
public function load($data): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getValidationErrors(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function applyToClient(OauthClient $client): void {
|
||||
$client->name = 'New name';
|
||||
$client->description = 'New description.';
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
$this->assertTrue($form->save($request));
|
||||
$this->assertSame('application-id', $client->id);
|
||||
$this->assertSame('application_secret', $client->secret);
|
||||
$this->assertSame('New name', $client->name);
|
||||
$this->assertSame('New description.', $client->description);
|
||||
$this->assertSame('http://example.com/oauth/ely', $client->redirect_uri);
|
||||
$this->assertSame('http://example.com', $client->website_url);
|
||||
}
|
||||
|
||||
public function testDelete() {
|
||||
/** @var OauthClient|\Mockery\MockInterface $client */
|
||||
$client = mock(OauthClient::class . '[save]');
|
||||
$client->id = 'mocked-id';
|
||||
$client->type = OauthClient::TYPE_APPLICATION;
|
||||
$client->shouldReceive('save')->andReturn(true);
|
||||
|
||||
$form = new OauthClientForm($client);
|
||||
$this->assertTrue($form->delete());
|
||||
$this->assertTrue($form->getClient()->is_deleted);
|
||||
/** @var ClearOauthSessions $job */
|
||||
$job = $this->tester->grabLastQueuedJob();
|
||||
$this->assertInstanceOf(ClearOauthSessions::class, $job);
|
||||
$this->assertSame('mocked-id', $job->clientId);
|
||||
$this->assertNull($job->notSince);
|
||||
}
|
||||
|
||||
public function testReset() {
|
||||
/** @var OauthClient|\Mockery\MockInterface $client */
|
||||
$client = mock(OauthClient::class . '[save]');
|
||||
$client->id = 'mocked-id';
|
||||
$client->secret = 'initial_secret';
|
||||
$client->type = OauthClient::TYPE_APPLICATION;
|
||||
$client->shouldReceive('save')->andReturn(true);
|
||||
|
||||
$form = new OauthClientForm($client);
|
||||
$this->assertTrue($form->reset());
|
||||
$this->assertSame('initial_secret', $form->getClient()->secret);
|
||||
/** @var ClearOauthSessions $job */
|
||||
$job = $this->tester->grabLastQueuedJob();
|
||||
$this->assertInstanceOf(ClearOauthSessions::class, $job);
|
||||
$this->assertSame('mocked-id', $job->clientId);
|
||||
$this->assertEquals(time(), $job->notSince, '', 2);
|
||||
}
|
||||
|
||||
public function testResetWithSecret() {
|
||||
/** @var OauthClient|\Mockery\MockInterface $client */
|
||||
$client = mock(OauthClient::class . '[save]');
|
||||
$client->id = 'mocked-id';
|
||||
$client->secret = 'initial_secret';
|
||||
$client->type = OauthClient::TYPE_APPLICATION;
|
||||
$client->shouldReceive('save')->andReturn(true);
|
||||
|
||||
$form = new OauthClientForm($client);
|
||||
$this->assertTrue($form->reset(true));
|
||||
$this->assertNotSame('initial_secret', $form->getClient()->secret);
|
||||
/** @var ClearOauthSessions $job */
|
||||
$job = $this->tester->grabLastQueuedJob();
|
||||
$this->assertInstanceOf(ClearOauthSessions::class, $job);
|
||||
$this->assertSame('mocked-id', $job->clientId);
|
||||
$this->assertEquals(time(), $job->notSince, '', 2);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user