From 7349a110d631d5aabf62f85efbf7d1325b00b06a Mon Sep 17 00:00:00 2001 From: Bruno Francisco Date: Mon, 28 Aug 2023 10:31:23 +0100 Subject: [PATCH] Add support for security token - added support for passing `security token` - added tests to cover new `security token` - closes #4 --- src/DevqalyClient.php | 6 +++--- src/Events/BaseEvent.php | 6 +++++- src/Events/DatabaseTransactionEvent.php | 5 ----- tests/src/Events/BaseEventClassTest.php | 19 +++++++++++++------ .../Events/DatabaseTransactionEventTest.php | 6 ++++-- tests/src/Events/LogEventTest.php | 9 ++++++--- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/DevqalyClient.php b/src/DevqalyClient.php index 7fb7abc..3334e48 100755 --- a/src/DevqalyClient.php +++ b/src/DevqalyClient.php @@ -11,10 +11,10 @@ class DevqalyClient private LogEvent $logEvent; - public function __construct(?string $backendUrl, ?string $sourceIdentifier) + public function __construct(?string $backendUrl, ?string $sourceIdentifier, string $securityToken) { - $this->databaseEvent = new DatabaseTransactionEvent($backendUrl, $sourceIdentifier); - $this->logEvent = new LogEvent($backendUrl, $sourceIdentifier); + $this->databaseEvent = new DatabaseTransactionEvent($backendUrl, $sourceIdentifier, $securityToken); + $this->logEvent = new LogEvent($backendUrl, $sourceIdentifier, $securityToken); } public function createDatabaseEventTransaction(string $sessionId, string $sessionSecret, array $data): void diff --git a/src/Events/BaseEvent.php b/src/Events/BaseEvent.php index 632dfbd..3f4a46f 100644 --- a/src/Events/BaseEvent.php +++ b/src/Events/BaseEvent.php @@ -12,11 +12,14 @@ class BaseEvent private string $backendUrl; - public function __construct(?string $backendUrl, ?string $sourceIdentifier) + private string $securityToken; + + public function __construct(?string $backendUrl, ?string $sourceIdentifier, string $securityToken) { $this->handle = curl_init($backendUrl); $this->backendUrl = $backendUrl ?? 'https://api.devqaly.com/api'; $this->sourceIdentifier = $sourceIdentifier; + $this->securityToken = $securityToken; } public function setOption($name, $value): void @@ -46,6 +49,7 @@ public function generatePayload(array $data, string $type): array 'type' => $type, 'source' => $this->sourceIdentifier, 'clientUtcEventCreatedAt' => (new \DateTime('now', new \DateTimeZone('utc')))->format('Y-m-d\TH:i:s.u\Z'), + 'securityToken' => $this->securityToken ]; } diff --git a/src/Events/DatabaseTransactionEvent.php b/src/Events/DatabaseTransactionEvent.php index 88f75ec..e7123fa 100644 --- a/src/Events/DatabaseTransactionEvent.php +++ b/src/Events/DatabaseTransactionEvent.php @@ -6,11 +6,6 @@ class DatabaseTransactionEvent extends BaseEvent implements Event { const EVENT_TYPE = 'App\Models\Session\Event\EventDatabaseTransaction'; - public function __construct(?string $backendUrl, ?string $sourceIdentifier) - { - parent::__construct($backendUrl, $sourceIdentifier); - } - public function create(string $sessionId, string $sessionSecret, array $data): void { if (! isset($data['sql'])) { diff --git a/tests/src/Events/BaseEventClassTest.php b/tests/src/Events/BaseEventClassTest.php index cbf5d0b..1c1e473 100644 --- a/tests/src/Events/BaseEventClassTest.php +++ b/tests/src/Events/BaseEventClassTest.php @@ -5,8 +5,9 @@ it('should return the correct endpoint when calling `getCreateEventEndpoint` method', function () { $backendUrl = 'https://devqaly.test/api'; $sourceIdentifier = 'microservice-x'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = new BaseEvent($backendUrl, $sourceIdentifier); + $client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken); $sessionId = 'b79cb3ba-745e-5d9a-8903-4a02327a7e09'; @@ -18,8 +19,9 @@ it('should return correct payload when calling `generatePayload` method', function () { $backendUrl = 'https://devqaly.test/api'; $sourceIdentifier = 'microservice-x'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = new BaseEvent($backendUrl, $sourceIdentifier); + $client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken); $clickEventPayload = ['positionX' => 500, 'positionY' => 500]; $eventType = 'click-event'; @@ -33,6 +35,7 @@ ...$clickEventPayload, 'type' => $eventType, 'source' => $sourceIdentifier, + 'securityToken' => $securityToken, ]) ->and(DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $payload['clientUtcEventCreatedAt']) !== false) ->toBeTrue(); @@ -41,8 +44,9 @@ it('should allow to validate the session id when calling `validateSessionId` method', function () { $backendUrl = 'https://devqaly.test/api'; $sourceIdentifier = 'microservice-x'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = new BaseEvent($backendUrl, $sourceIdentifier); + $client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken); $sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423'; @@ -56,8 +60,9 @@ it('should throw error exception when calling `validateSessionId` method with invalid session id', function () { $backendUrl = 'https://devqaly.test/api'; $sourceIdentifier = 'microservice-x'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = new BaseEvent($backendUrl, $sourceIdentifier); + $client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken); $sessionId = ''; @@ -67,8 +72,9 @@ it('should allow to validate session secret when calling `validateSessionSecret` method', function () { $backendUrl = 'https://devqaly.test/api'; $sourceIdentifier = 'microservice-x'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = new BaseEvent($backendUrl, $sourceIdentifier); + $client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken); $sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423'; @@ -82,8 +88,9 @@ it('should throw error exception when calling `validateSessionSecret` method with invalid session secret', function () { $backendUrl = 'https://devqaly.test/api'; $sourceIdentifier = 'microservice-x'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = new BaseEvent($backendUrl, $sourceIdentifier); + $client = new BaseEvent($backendUrl, $sourceIdentifier, $securityToken); $sessionSecret = ''; diff --git a/tests/src/Events/DatabaseTransactionEventTest.php b/tests/src/Events/DatabaseTransactionEventTest.php index b0602fb..b393def 100644 --- a/tests/src/Events/DatabaseTransactionEventTest.php +++ b/tests/src/Events/DatabaseTransactionEventTest.php @@ -7,8 +7,9 @@ $sourceIdentifier = 'microservice-x'; $sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423'; $sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = new DatabaseTransactionEvent($backendUrl, $sourceIdentifier); + $client = new DatabaseTransactionEvent($backendUrl, $sourceIdentifier, $securityToken); $client->create($sessionId, $sessionSecret, []); })->throws(\Error::class, '`sql` must be set to create a database transaction event in $data'); @@ -18,8 +19,9 @@ $sourceIdentifier = 'microservice-x'; $sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423'; $sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = Mockery::mock(DatabaseTransactionEvent::class, [$backendUrl, $sourceIdentifier])->makePartial(); + $client = Mockery::mock(DatabaseTransactionEvent::class, [$backendUrl, $sourceIdentifier, $securityToken])->makePartial(); $endpoint = 'https://something.com'; diff --git a/tests/src/Events/LogEventTest.php b/tests/src/Events/LogEventTest.php index 91206bf..535d0f5 100644 --- a/tests/src/Events/LogEventTest.php +++ b/tests/src/Events/LogEventTest.php @@ -7,8 +7,9 @@ $sourceIdentifier = 'microservice-x'; $sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423'; $sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = new LogEvent($backendUrl, $sourceIdentifier); + $client = new LogEvent($backendUrl, $sourceIdentifier, $securityToken); $client->create($sessionId, $sessionSecret, ['log' => 'some log']); })->throws(\Error::class, '`level` must be set to create a log event in $data'); @@ -18,8 +19,9 @@ $sourceIdentifier = 'microservice-x'; $sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423'; $sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = new LogEvent($backendUrl, $sourceIdentifier); + $client = new LogEvent($backendUrl, $sourceIdentifier, $securityToken); $client->create($sessionId, $sessionSecret, ['level' => LogEvent::LOG_LEVEL_ALERT]); })->throws(\Error::class, '`log` must be set to create a log event in $data'); @@ -29,8 +31,9 @@ $sourceIdentifier = 'microservice-x'; $sessionId = 'c7622e14-21f8-40c2-b151-3a311816b423'; $sessionSecret = 'c7622e14-21f8-40c2-b151-3a311816b423'; + $securityToken = 'nJOFUgmcKDhzpMMbL6VqEzWbK7XOby8ZMqOWqYooTE1Xtd4Y3RQBidpeq42i'; - $client = Mockery::mock(LogEvent::class, [$backendUrl, $sourceIdentifier])->makePartial(); + $client = Mockery::mock(LogEvent::class, [$backendUrl, $sourceIdentifier, $securityToken])->makePartial(); $endpoint = 'https://something.com';