Implemented oauth session revocation notification.

Reworked webhooks notifications constructors
This commit is contained in:
ErickSkrauch
2020-10-01 01:40:28 +03:00
parent b904d5d314
commit 5fc97fdd7a
13 changed files with 283 additions and 137 deletions

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\notifications;
use Codeception\Test\Unit;
use common\models\Account;
use common\notifications\AccountDeletedNotification;
/**
* @covers \common\notifications\AccountDeletedNotification
*/
class AccountDeletedNotificationTest extends Unit {
public function testGetPayloads(): void {
$account = new Account();
$account->id = 123;
$account->username = 'mock-username';
$account->uuid = 'afc8dc7a-4bbf-4d3a-8699-68890088cf84';
$account->email = 'mock@ely.by';
$account->created_at = 1531008814;
$account->deleted_at = 1601501781;
$notification = new AccountDeletedNotification($account);
$this->assertSame('account.deletion', $notification::getType());
$this->assertSame([
'id' => 123,
'uuid' => 'afc8dc7a-4bbf-4d3a-8699-68890088cf84',
'username' => 'mock-username',
'email' => 'mock@ely.by',
'registered' => '2018-07-08T00:13:34+00:00',
'deleted' => '2020-09-30T21:36:21+00:00',
], $notification->getPayloads());
}
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\notifications;
use Codeception\Test\Unit;
use common\models\Account;
use common\notifications\AccountEditNotification;
/**
* @covers \common\notifications\AccountEditNotification
*/
class AccountEditNotificationTest extends Unit {
public function testGetPayloads(): void {
$account = new Account();
$account->id = 123;
$account->username = 'mock-username';
$account->uuid = 'afc8dc7a-4bbf-4d3a-8699-68890088cf84';
$account->email = 'mock@ely.by';
$account->lang = 'en';
$account->status = Account::STATUS_ACTIVE;
$account->created_at = 1531008814;
$changedAttributes = [
'username' => 'old-username',
'uuid' => 'e05d33e9-ff91-4d26-9f5c-8250f802a87a',
'email' => 'old-email@ely.by',
'status' => 0,
];
$notification = new AccountEditNotification($account, $changedAttributes);
$this->assertSame('account.edit', $notification::getType());
$this->assertSame([
'id' => 123,
'uuid' => 'afc8dc7a-4bbf-4d3a-8699-68890088cf84',
'username' => 'mock-username',
'email' => 'mock@ely.by',
'lang' => 'en',
'isActive' => true,
'isDeleted' => false,
'registered' => '2018-07-08T00:13:34+00:00',
'changedAttributes' => $changedAttributes,
], $notification->getPayloads());
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\notifications;
use Codeception\Test\Unit;
use common\models\OauthSession;
use common\notifications\OAuthSessionRevokedNotification;
/**
* @covers \common\notifications\OAuthSessionRevokedNotification
*/
class OAuthSessionRevokedNotificationTest extends Unit {
public function testGetPayloads(): void {
$oauthSession = new OauthSession();
$oauthSession->account_id = 1;
$oauthSession->client_id = 'mock-client';
$oauthSession->revoked_at = 1601504074;
$notification = new OAuthSessionRevokedNotification($oauthSession);
$this->assertSame('oauth2.session_revoked', $notification::getType());
$this->assertSame([
'accountId' => 1,
'clientId' => 'mock-client',
'revoked' => '2020-09-30T22:14:34+00:00',
], $notification->getPayloads());
}
}

View File

@@ -3,7 +3,7 @@ declare(strict_types=1);
namespace common\tests\unit\tasks;
use common\models\Account;
use common\notifications\NotificationInterface;
use common\tasks\CreateWebHooksDeliveries;
use common\tasks\DeliveryWebHook;
use common\tests\fixtures;
@@ -21,67 +21,39 @@ class CreateWebHooksDeliveriesTest extends TestCase {
];
}
public function testCreateAccountEdit() {
$account = new Account();
$account->id = 123;
$account->username = 'mock-username';
$account->uuid = 'afc8dc7a-4bbf-4d3a-8699-68890088cf84';
$account->email = 'mock@ely.by';
$account->lang = 'en';
$account->status = Account::STATUS_ACTIVE;
$account->created_at = 1531008814;
$changedAttributes = [
'username' => 'old-username',
'uuid' => 'e05d33e9-ff91-4d26-9f5c-8250f802a87a',
'email' => 'old-email@ely.by',
'status' => 0,
];
$result = CreateWebHooksDeliveries::createAccountEdit($account, $changedAttributes);
$this->assertSame('account.edit', $result->type);
$this->assertEmpty(array_diff_assoc([
'id' => 123,
'uuid' => 'afc8dc7a-4bbf-4d3a-8699-68890088cf84',
'username' => 'mock-username',
'email' => 'mock@ely.by',
'lang' => 'en',
'isActive' => true,
'registered' => '2018-07-08T00:13:34+00:00',
], $result->payloads));
$this->assertSame($changedAttributes, $result->payloads['changedAttributes']);
}
public function testExecute() {
$task = new CreateWebHooksDeliveries('account.edit', [
'id' => 123,
'uuid' => 'afc8dc7a-4bbf-4d3a-8699-68890088cf84',
'username' => 'mock-username',
'email' => 'mock@ely.by',
'lang' => 'en',
'isActive' => true,
'registered' => '2018-07-08T00:13:34+00:00',
'changedAttributes' => [
'username' => 'old-username',
'uuid' => 'e05d33e9-ff91-4d26-9f5c-8250f802a87a',
'email' => 'old-email@ely.by',
'status' => 0,
],
]);
$task->execute($this->createMock(Queue::class));
/** @var DeliveryWebHook[] $tasks */
$tasks = $this->tester->grabQueueJobs();
$this->assertCount(2, $tasks);
$notification = new class implements NotificationInterface {
public static function getType(): string {
return 'account.edit';
}
$this->assertInstanceOf(DeliveryWebHook::class, $tasks[0]);
$this->assertSame($task->type, $tasks[0]->type);
$this->assertSame($task->payloads, $tasks[0]->payloads);
$this->assertSame('http://localhost:80/webhooks/ely', $tasks[0]->url);
$this->assertSame('my-secret', $tasks[0]->secret);
public function getPayloads(): array {
return ['key' => 'value'];
}
};
$this->assertInstanceOf(DeliveryWebHook::class, $tasks[1]);
$this->assertSame($task->type, $tasks[1]->type);
$this->assertSame($task->payloads, $tasks[1]->payloads);
$this->assertSame('http://localhost:81/webhooks/ely', $tasks[1]->url);
$this->assertNull($tasks[1]->secret);
$queue = $this->createMock(Queue::class);
$queue->expects($this->exactly(2))->method('push')->withConsecutive(
[$this->callback(function(DeliveryWebHook $task): bool {
$this->assertSame('account.edit', $task->type);
$this->assertSame(['key' => 'value'], $task->payloads);
$this->assertSame('http://localhost:80/webhooks/ely', $task->url);
$this->assertSame('my-secret', $task->secret);
return true;
})],
[$this->callback(function(DeliveryWebHook $task): bool {
$this->assertSame('account.edit', $task->type);
$this->assertSame(['key' => 'value'], $task->payloads);
$this->assertSame('http://localhost:81/webhooks/ely', $task->url);
$this->assertNull($task->secret);
return true;
})],
);
$task = new CreateWebHooksDeliveries($notification);
$task->execute($queue);
}
}