change namespace and cleanup code

This commit is contained in:
2022-05-14 18:21:55 +02:00
parent 76edd961b7
commit 377dc53037
46 changed files with 400 additions and 360 deletions

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Bitinflow\Accounts\Tests;
use Bitinflow\Accounts\Tests\TestCases\ApiTestCase;
/**
* @author René Preuß <rene@preuss.io>
*/
class ApiOauthTest extends ApiTestCase
{
public function testGetOauthToken(): void
{
$this->registerResult($result = $this->getClient()->retrievingToken('client_credentials', [
'scope' => '',
]));
$this->assertTrue($result->success());
$this->assertNotEmpty($result->data()->access_token);
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Bitinflow\Accounts\Tests;
use Bitinflow\Accounts\Result;
use Bitinflow\Accounts\Tests\TestCases\ApiTestCase;
/**
* @author René Preuß <rene@preuss.io>
*/
class ApiSshKeysTest extends ApiTestCase
{
public function testGetSshKeyByUserId(): void
{
$this->registerResult($result = $this->getClient()->getSshKeysByUserId(38));
$this->assertEquals('rene.preuss@check24.de', $result->shift()->name);
$this->assertGreaterThanOrEqual(2, $result->count());
}
public function testSshKeyManagement(): void
{
$customName = 'Hello World!';
$publicKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEA3H7sYVrVCwwYIuRm3on3S9n/BCd2mBJrgCk6xTerbNmt0RyUZ+RtGsK6UYkgnRR2WWq9/Pv2s3RXJXPxbsIEYmKCcTdLUvDk56x9385cIVUX4w016mpe/8lyu+mIdqWYKsJMoab0oReCDX8Y9qBcaffDh8AgmYVN+86gXgoP1ITe9BDYrFiR6U571VyLDVN3OYOYPMF3/L9f0knMfM0T4LrS8oi6faVBCxZHRoBGtGmsTBkE0KwplYQFN2aa4Mxab+rTUFmJr3LYEcJF0J8wNJ3eEDFNOR0254jrjbGGAXGsc+cxJoNzech+GBkRMKMpNU0lds6VxP0ZB25VfzjEmQ== René Preuß';
$this->getClient()->withToken($this->getToken());
$this->registerResult($result = $this->getClient()->createSshKey($publicKey, $customName));
$this->assertInstanceOf(Result::class, $result);
$this->assertEquals('6b:fa:33:da:6c:3a:08:05:6f:71:8b:d8:ed:06:37:b6', $result->data()->fingerprint);
$this->assertEquals($customName, $result->data()->name);
$keyId = $result->data()->id;
$this->getClient()->withToken($this->getToken());
$this->registerResult($result = $this->getClient()->deleteSshKey($keyId));
$this->assertInstanceOf(Result::class, $result);
$this->assertTrue($result->success());
}
}

View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Bitinflow\Accounts\Tests;
use Bitinflow\Accounts\Enums\Scope;
use Bitinflow\Accounts\Tests\TestCases\ApiTestCase;
use Illuminate\Support\Str;
/**
* @author René Preuß <rene@preuss.io>
*/
class ApiUsersTest extends ApiTestCase
{
public function testGetAuthedUser(): void
{
$this->getClient()->withToken($this->getToken());
$this->registerResult($result = $this->getClient()->getAuthedUser());
$this->assertTrue($result->success());
$this->assertEquals('rene@preuss.io', $result->data()->email);
}
public function testEmailAvailabilityNonExisting(): void
{
$this->getClient()->withToken($this->getToken());
$this->registerResult($result = $this->getClient()->isEmailExisting('rene+non-existing@preuss.io'));
$this->assertTrue(!$result->success());
}
public function testEmailAvailabilityExisting(): void
{
$this->getClient()->withToken($this->getToken());
$this->registerResult($result = $this->getClient()->isEmailExisting('rene@preuss.io'));
$this->assertTrue($result->success());
}
public function testCreateUser(): void
{
$testEmailAddress = $this->createRandomEmail();
$this->registerResult($result = $this->getClient()->retrievingToken('client_credentials', [
'scope' => Scope::USERS_CREATE,
]));
$this->getClient()->withToken($result->data()->access_token);
$this->registerResult($result = $this->getClient()->createUser([
'first_name' => 'René',
'last_name' => 'Preuß',
'email' => $testEmailAddress,
'password' => 'Password1',
'password_confirmation' => 'Password1',
'terms_accepted' => true,
]));
$this->assertTrue($result->success(), $result->error());
$this->assertEquals($testEmailAddress, $result->data()->email);
}
private function createRandomEmail(): string
{
return sprintf('rene+unittest.%s@bitinflow.com', Str::random());
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Bitinflow\Accounts\Tests;
use Bitinflow\Accounts\BitinflowAccounts;
use Bitinflow\Accounts\Facades\BitinflowAccounts as BitinflowAccountsFacade;
use Bitinflow\Accounts\Tests\TestCases\TestCase;
/**
* @author René Preuß <rene@preuss.io>
*/
class ServiceInstantiationTest extends TestCase
{
public function testInstance(): void
{
$this->assertInstanceOf(BitinflowAccounts::class, app(BitinflowAccounts::class));
}
public function testFacade(): void
{
$this->assertInstanceOf(BitinflowAccounts::class, BitinflowAccountsFacade::getFacadeRoot());
}
}

View File

@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Bitinflow\Accounts\Tests\TestCases;
use Bitinflow\Accounts\BitinflowAccounts;
use Bitinflow\Accounts\Result;
/**
* @author René Preuß <rene@preuss.io>
*/
abstract class ApiTestCase extends TestCase
{
protected static $rateLimitRemaining = null;
protected function setUp(): void
{
parent::setUp();
if ($this->getBaseUrl()) {
BitinflowAccounts::setBaseUrl($this->getBaseUrl());
}
if (!$this->getClientId()) {
$this->markTestSkipped('No Client-ID given');
}
if (self::$rateLimitRemaining !== null && self::$rateLimitRemaining < 3) {
$this->markTestSkipped('Rate Limit exceeded (' . self::$rateLimitRemaining . ')');
}
$this->getClient()->setClientId($this->getClientId());
}
protected function registerResult(Result $result): Result
{
self::$rateLimitRemaining = $result->rateLimit('remaining');
return $result;
}
protected function getBaseUrl()
{
return getenv('BASE_URL');
}
protected function getClientId()
{
return getenv('CLIENT_ID');
}
protected function getClientSecret()
{
return getenv('CLIENT_KEY');
}
protected function getToken()
{
return getenv('CLIENT_ACCESS_TOKEN');
}
public function getClient(): BitinflowAccounts
{
return app(BitinflowAccounts::class);
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Bitinflow\Accounts\Tests\TestCases;
use Bitinflow\Accounts\BitinflowAccounts;
use Bitinflow\Accounts\Providers\BitinflowAccountsServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;
/**
* @author René Preuß <rene@preuss.io>
*/
abstract class TestCase extends BaseTestCase
{
protected function getPackageProviders($app)
{
return [
BitinflowAccountsServiceProvider::class,
];
}
protected function getPackageAliases($app)
{
return [
'BitinflowAccounts' => BitinflowAccounts::class,
];
}
}