From 5ed0f0443c6e5e6a87078078db3e67e101e035d9 Mon Sep 17 00:00:00 2001 From: Enver Cigal Date: Wed, 20 Apr 2022 10:58:51 +0300 Subject: [PATCH] Initial commit --- .gitignore | 4 + LICENSE | 21 + README.md | 159 +++++++ composer.json | 48 ++ phpunit.xml | 22 + src/Auth.php | 42 ++ src/Drivers/AbstractDriver.php | 27 ++ .../Permission/Enums/ConsentSourceTypes.php | 20 + .../Permission/Enums/PermissionTypes.php | 10 + .../Permission/Enums/RecipientTypes.php | 9 + src/Drivers/Permission/Enums/SourceTypes.php | 9 + src/Drivers/Permission/Enums/StatusTypes.php | 9 + .../Permission/Models/InterfaceModel.php | 8 + src/Drivers/Permission/Models/Permission.php | 158 +++++++ .../Permission/Models/PermissionList.php | 30 ++ src/Drivers/Permission/PermissionDriver.php | 74 +++ src/Enums/Config.php | 23 + src/IysManager.php | 44 ++ tests/AuthTest.php | 68 +++ tests/PermissionDriverTest.php | 436 ++++++++++++++++++ tests/PermissionListModelTest.php | 75 +++ tests/PermissionModelTest.php | 71 +++ tests/TestCase.php | 29 ++ 23 files changed, 1396 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 src/Auth.php create mode 100644 src/Drivers/AbstractDriver.php create mode 100644 src/Drivers/Permission/Enums/ConsentSourceTypes.php create mode 100644 src/Drivers/Permission/Enums/PermissionTypes.php create mode 100644 src/Drivers/Permission/Enums/RecipientTypes.php create mode 100644 src/Drivers/Permission/Enums/SourceTypes.php create mode 100644 src/Drivers/Permission/Enums/StatusTypes.php create mode 100644 src/Drivers/Permission/Models/InterfaceModel.php create mode 100644 src/Drivers/Permission/Models/Permission.php create mode 100644 src/Drivers/Permission/Models/PermissionList.php create mode 100644 src/Drivers/Permission/PermissionDriver.php create mode 100644 src/Enums/Config.php create mode 100644 src/IysManager.php create mode 100644 tests/AuthTest.php create mode 100644 tests/PermissionDriverTest.php create mode 100644 tests/PermissionListModelTest.php create mode 100644 tests/PermissionModelTest.php create mode 100644 tests/TestCase.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..698b642 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor/ +/.idea/ +.phpunit.result.cache +composer.lock \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f983bf4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Macellan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..437e121 --- /dev/null +++ b/README.md @@ -0,0 +1,159 @@ +## Contents + +- [Installation](#installation) + - [Setting up the Iys service](#setting-up-the-IYS-service) +- [Enums](#enums) +- [Usage](#usage) +- [Testing](#testing) +- [License](#license) + +## Installation + +You can install this package via composer: + +``` bash +composer require macellan/iys +``` + +### Setting up the IYS service + +Add your IYS login to your config/services.php: + +```php +// config/services.php +... + 'iys' => [ + 'username' => env('IYS_USERNAME', ''), + 'password' => env('IYS_PASSWORD', ''), + 'iys_code' => env('IYS_CODE', ''), + 'brand_code' => env('IYS_BRAND_CODE', ''), + 'url' => env('IYS_URL', ''), + ], +... +``` + +## Enums + +IYS consent source types: + +```php +enum ConsentSourceTypes: string +{ + case PHYSICAL = 'HS_FIZIKSEL_ORTAM'; + case WET_SIGNATURE = 'HS_ISLAK_IMZA'; + case WEB = 'HS_WEB'; + case CALL_CENTER = 'HS_CAGRI_MERKEZI'; + case SOCIAL_MEDIA = 'HS_SOSYAL_MEDYA'; + case EMAIL = 'HS_EPOSTA'; + case MESSAGE = 'HS_MESAJ'; + case MOBILE = 'HS_MOBIL'; + case HS_EORTAM = 'HS_EORTAM'; + case ACTIVITY = 'HS_ETKINLIK'; + case HS_2015 = 'HS_2015'; + case HS_ATM = 'HS_ATM'; + case HS_DECISION = 'HS_KARAR'; +} +``` + +IYS permission types: + +```php +enum PermissionTypes: string +{ + case CALL = 'ARAMA'; + case MESSAGE = 'MESAJ'; + case EMAIL = 'EPOSTA'; +} +``` + +IYS recipient types: + +```php +enum RecipientTypes: string +{ + case INDIVIDUAL = 'BIREYSEL'; + case TRADER = 'TACIR'; +} +``` + +IYS source types: + +```php +enum SourceTypes: string +{ + case HS = 'HS'; + case IYS = 'IYS'; +} +``` + +IYS status types: + +```php +enum StatusTypes: string +{ + case APPROVE = 'ONAY'; + case REJECT = 'RET'; +} +``` + +## Usage + +**NOTE:** +Authentication token is generated per UserManager instance. Expire time is two hour. + +With the usage in this example, you can submit a single release. This method works with the "Permission" model + +```php + IysManager::make()->createPermissionDriver()->sendSinglePermission( + Permission::make() + ->setConsentDate('2022-02-10 09:50:02') + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipient('example@.com') + ->setRecipientType(RecipientTypes::INDIVIDUAL) + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL) + ); +``` + +You can send permissions by filling out the permission list model. The permission list model is the permission model array. + +```php + $permissionList = PermissionList::make() + ->addPermission($permission + ->setConsentDate('2022-02-10 09:50:02') + ->setRecipient('example1@.com') + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL)) + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipientType(RecipientTypes::INDIVIDUAL); + ->addPermission($permission + ->setConsentDate('2022-02-10 09:50:03') + ->setRecipient('example2@.com') + ->setStatus(StatusTypes::REJECT) + ->setType(PermissionTypes::MESSAGE)); + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipientType(RecipientTypes::INDIVIDUAL); + + IysManager::make()->createPermissionDriver()->sendPermissions($permissionList); +``` + +You can get send permission information with request id + +```php + IysManager::make()->createPermissionDriver()->getPermissionsStatus('request_id'); +``` +You can get changed permission by IYS + +```php + IysManager::make()->createPermissionDriver()->getChangedPermissions(); +``` + +## Testing + +``` bash +composer test +``` + +## License + +The MIT License (MIT). Please see [License File](LICENSE) for more information. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0c7f14d --- /dev/null +++ b/composer.json @@ -0,0 +1,48 @@ +{ + "name": "macellan/iys", + "description": "Iys library for Laravel", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Enver Cigal", + "email": "enver.cigal@gmail.com" + } + ], + "require": { + "php": ">=8.1", + "ext-json": "*", + "guzzlehttp/guzzle": "^7.2|^7.0.1", + "illuminate/http": "^8.0 || ^9.0", + "illuminate/support": "^8.0 || ^9.0" + }, + "require-dev": { + "mockery/mockery": "^1.4.4", + "orchestra/testbench": "^7.1", + "phpunit/phpunit": "^9.5.10", + "squizlabs/php_codesniffer": "^3.6" + }, + "autoload": { + "psr-4": { + "Macellan\\Iys\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Macellan\\Iys\\Tests\\": "tests" + } + }, + "scripts": { + "test": "vendor/bin/phpunit", + "check-style": "phpcs -p --standard=PSR12 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", + "fix-style": "phpcbf -p --standard=PSR12 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests" + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": false + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..c8548fc --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,22 @@ + + + + + ./tests/ + + + + + + + \ No newline at end of file diff --git a/src/Auth.php b/src/Auth.php new file mode 100644 index 0000000..92852df --- /dev/null +++ b/src/Auth.php @@ -0,0 +1,42 @@ +username = $config[Config::USERNAME->value]; + + $this->password = $config[Config::PASSWORD->value]; + + $this->endPoint = $config[Config::URL->value] . '/oauth2/token'; + } + + /** + * @throws RequestException + */ + public function login() + { + return Http::timeout($this->timeout) + ->asJson()->acceptJson() + ->post($this->endPoint, [ + 'username' => $this->username, + 'password' => $this->password, + 'grant_type' => 'password', + ]) + ->throw(); + } +} diff --git a/src/Drivers/AbstractDriver.php b/src/Drivers/AbstractDriver.php new file mode 100644 index 0000000..5591f01 --- /dev/null +++ b/src/Drivers/AbstractDriver.php @@ -0,0 +1,27 @@ +branchCode = $config[Config::BRAND_CODE->value]; + + $this->iysCode = $config[Config::IYS_CODE->value]; + + $this->baseUrl = $config[Config::URL->value]; + + $this->bearer = $bearer; + } +} diff --git a/src/Drivers/Permission/Enums/ConsentSourceTypes.php b/src/Drivers/Permission/Enums/ConsentSourceTypes.php new file mode 100644 index 0000000..10b2aac --- /dev/null +++ b/src/Drivers/Permission/Enums/ConsentSourceTypes.php @@ -0,0 +1,20 @@ +consentDate = $consentDate; + + return $this; + } + + /** + * @param ConsentSourceTypes $source + * @return Permission + */ + public function setSource(ConsentSourceTypes $source): self + { + $this->source = $source; + + return $this; + } + + /** + * @param string $recipient + * @return Permission + */ + public function setRecipient(string $recipient): self + { + $this->recipient = $recipient; + + return $this; + } + + /** + * @param StatusTypes $status + * @return Permission + */ + public function setStatus(StatusTypes $status): self + { + $this->status = $status; + + return $this; + } + + /** + * @param RecipientTypes $recipientType + * @return Permission + */ + public function setRecipientType(RecipientTypes $recipientType): self + { + $this->recipientType = $recipientType; + + return $this; + } + + /** + * @param PermissionTypes $type + * @return Permission + */ + public function setType(PermissionTypes $type): self + { + $this->type = $type; + + return $this; + } + + /** + * @param int $retailerCode + * @return Permission + */ + public function setRetailerCode(int $retailerCode): self + { + $this->retailerCode = $retailerCode; + + return $this; + } + + /** + * @param array $retailerAccess + * @return Permission + */ + public function setRetailerAccess(array $retailerAccess): self + { + $this->retailerAccess = $retailerAccess; + + return $this; + } + + /** + * @return array + */ + #[ArrayShape( + [ + 'consentDate' => "string", + 'source' => "string", + 'recipient' => "string", + 'recipientType' => "string", + 'status' => "string", + 'type' => "string", + 'retailerAccess' => "array|null", + 'retailerCode' => "int|null" + ] + )] + public function toArray(): array + { + $array = [ + 'consentDate' => $this->consentDate, + 'source' => $this->source->value, + 'recipient' => $this->recipient, + 'recipientType' => $this->recipientType->value, + 'status' => $this->status->value, + 'type' => $this->type->value, + ]; + + if ($this->retailerCode) { + $array['retailerCode'] = $this->retailerCode; + } + + if ($this->retailerAccess) { + $array['retailerAccess'] = $this->retailerAccess; + } + + return $array; + } + + public static function make(): Permission + { + return new static(); + } +} diff --git a/src/Drivers/Permission/Models/PermissionList.php b/src/Drivers/Permission/Models/PermissionList.php new file mode 100644 index 0000000..96f4df4 --- /dev/null +++ b/src/Drivers/Permission/Models/PermissionList.php @@ -0,0 +1,30 @@ +permissions[] = $singlePermission->toArray(); + + return $this; + } + + public function isEmpty(): bool + { + return empty($this->permissions); + } + + public function toArray(): array + { + return $this->permissions; + } + + public static function make(): PermissionList + { + return new static(); + } +} diff --git a/src/Drivers/Permission/PermissionDriver.php b/src/Drivers/Permission/PermissionDriver.php new file mode 100644 index 0000000..4da55c9 --- /dev/null +++ b/src/Drivers/Permission/PermissionDriver.php @@ -0,0 +1,74 @@ +timeout) + ->withToken($this->bearer)->asJson()->acceptJson() + ->post($this->buildUrl('consents'), $permission->toArray()) + ->throw(); + } + + /** + * @throws RequestException + */ + public function sendPermissions(PermissionList $permissionList) + { + return Http::timeout($this->timeout) + ->withToken($this->bearer)->asJson()->acceptJson() + ->post($this->buildUrl('consents/request'), $permissionList->toArray()) + ->throw(); + } + + /** + * @throws RequestException + */ + public function getPermissionsStatus(string $requestId) + { + return Http::timeout($this->timeout) + ->withToken($this->bearer)->asJson()->acceptJson() + ->get($this->buildUrl("consents/request/$requestId")) + ->throw(); + } + + /** + * @throws RequestException + */ + public function getChangedPermissions(?string $after = null, SourceTypes $source = SourceTypes::IYS, int $limit = 999) + { + $query = [ + 'source' => $source->value, + 'limit' => $limit, + ]; + + if ($after) { + $query['after'] = $after; + } + + return Http::timeout($this->timeout) + ->withToken($this->bearer)->asJson()->acceptJson() + ->get($this->buildUrl('consents/changes?' . Arr::query($query))) + ->throw(); + } + + private function buildUrl(string $api): string + { + return $this->baseUrl . "/sps/$this->iysCode/brands/$this->branchCode/$api"; + } +} diff --git a/src/Enums/Config.php b/src/Enums/Config.php new file mode 100644 index 0000000..5bd2709 --- /dev/null +++ b/src/Enums/Config.php @@ -0,0 +1,23 @@ +value, + self::PASSWORD->value, + self::IYS_CODE->value, + self::BRAND_CODE->value, + self::URL->value, + ]; + } +} diff --git a/src/IysManager.php b/src/IysManager.php new file mode 100644 index 0000000..1006085 --- /dev/null +++ b/src/IysManager.php @@ -0,0 +1,44 @@ +config = config('services.iys')) { + throw new Exception('Iys configuration not found.'); + } + + if ($diff = array_diff(Config::toArray(), array_keys($this->config))) { + throw new Exception(implode(',', $diff) . ' configuration parameter not found.'); + } + + $result = (new Auth($this->config))->login(); + + $this->bearer = $result->json('access_token') ?? null; + } + + public function createPermissionDriver(): PermissionDriver + { + return new PermissionDriver($this->config, $this->bearer); + } + + public static function make(): IysManager + { + return new static(); + } +} diff --git a/tests/AuthTest.php b/tests/AuthTest.php new file mode 100644 index 0000000..16f12c7 --- /dev/null +++ b/tests/AuthTest.php @@ -0,0 +1,68 @@ + Http::response([ + 'access_token' => 'eyJhbGciOiJSUzI1NiIsInR5c', + 'refresh_token' => 'eyJhbGciOiJSUzI1NiIsInR5c', + 'expires_in' => 7200, + 'refresh_expires_in' => 14400, + 'token_type' => 'bearer', + ]), + ]); + + $config = [ + 'username' => $this->username, + 'password' => $this->password, + 'url' => $this->url, + ]; + + (new Auth($config))->login(); + + Http::assertSent(function (Request $request) use ($endpoint) { + return $request->url() == $this->url . $endpoint && + $request['username'] == $this->username && + $request['password'] == $this->password && + $request['grant_type'] == 'password'; + }); + } + + /** + * @return void + * @throws RequestException + */ + public function test_throws_an_exception() + { + Http::fake([ + '/oauth2/token' => Http::response([ + "error" => "invalid_request", + "error_description" => "Giriş yöntemi belirtilmemiş." + ], 400), + ]); + + $this->expectException(RequestException::class); + + $config = [ + 'username' => $this->username, + 'password' => $this->password, + 'url' => $this->url, + ]; + + (new Auth($config))->login(); + } +} diff --git a/tests/PermissionDriverTest.php b/tests/PermissionDriverTest.php new file mode 100644 index 0000000..9f6dc44 --- /dev/null +++ b/tests/PermissionDriverTest.php @@ -0,0 +1,436 @@ + Http::response([ + 'access_token' => '162cc155429fdccd7f2be8e9482aa322993ff210c2923fb84e8ed0fb1902ae21', + 'refresh_token' => '2020-08-06 15:50:23', + 'expires_in' => 7200, + 'refresh_expires_in' => 14400, + 'token_type' => 'bearer', + ]), + ]); + } + + /** + * @return void + * @throws RequestException + */ + public function test_can_send_permission() + { + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents"; + + $recipient = 'test@example.com'; + + $consentDate = '2020-07-08 07:07:07'; + + $this->createHttpFakeToken(); + + Http::fake([ + $endpoint => Http::response([ + 'transaction_id' => '162cc155429fdccd7f2be8e9482aa322993ff210c2923fb84e8ed0fb1902ae21', + 'creationDate' => '2020-08-06 15:50:23', + ]), + ]); + + IysManager::make()->createPermissionDriver()->sendSinglePermission( + Permission::make() + ->setConsentDate($consentDate) + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipient($recipient) + ->setRecipientType(RecipientTypes::INDIVIDUAL) + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL) + ); + + Http::assertSent(function (Request $request) use ($endpoint, $recipient, $consentDate) { + return $request->url() == $this->url . $endpoint && + $request['consentDate'] == $consentDate && + $request['source'] == ConsentSourceTypes::MOBILE->value && + $request['recipient'] == $recipient && + $request['recipientType'] == RecipientTypes::INDIVIDUAL->value && + $request['status'] == StatusTypes::APPROVE->value && + $request['type'] == PermissionTypes::EMAIL->value; + }); + } + + /** + * @return void + * @throws RequestException + */ + public function test_throw_exception_send_permission() + { + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents"; + + $recipient = 'test@example.com'; + + $consentDate = '2020-07-08 07:07:07'; + + $this->createHttpFakeToken(); + + Http::fake([ + $endpoint => Http::response([ + "errors" => [ + [ + "message" => "Eksik veya geçersiz jeton!", + "code" => "H351", + ] + ] + ], 401), + ]); + + $this->expectException(RequestException::class); + + IysManager::make()->createPermissionDriver()->sendSinglePermission( + Permission::make() + ->setConsentDate($consentDate) + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipient($recipient) + ->setRecipientType(RecipientTypes::INDIVIDUAL) + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL) + ); + } + + /** + * @return void + * @throws RequestException + */ + public function test_can_send_permissions() + { + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/request"; + + Http::fake([ + $endpoint => Http::response( + [ + 'requestId' => '5980a44b06f333c25b1ah61bkceb95fa', + 'subRequests' => [ + 'type' => 'EPOSTA', + 'source' => 'HS_MOBIL', + 'recipient' => 'test@example1.com', + 'status' => 'ONAY', + 'consentDate' => '2020-07-08 07:07:07', + 'recipientType' => 'BIREYSEL', + 'subRequestId' => '3e141ad8-cdd5-49f2-a4b9-28ed76bc9677', + 'creationDate' => '2020-08-21 14:45:24', + ], + [ + 'type' => 'MESAJ', + 'source' => 'HS_MOBIL', + 'recipient' => 'test@example1.com', + 'status' => 'RET', + 'consentDate' => '2020-07-08 07:07:07', + 'recipientType' => 'BIREYSEL', + 'subRequestId' => '3e141ad8-cdd5-49f2-a4b9-28ed76bc9677', + 'creationDate' => '2020-08-21 14:45:24', + + ], + ] + ) + ]); + + $recipientFirst = 'test@example1.com'; + + $recipientSecond = 'test@example1.com'; + + $consentDate = '2020-07-08 07:07:07'; + + $this->createHttpFakeToken(); + + + $permission = Permission::make() + ->setConsentDate($consentDate) + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipientType(RecipientTypes::INDIVIDUAL); + + $permissionList = PermissionList::make() + ->addPermission($permission + ->setRecipient($recipientFirst) + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL)) + ->addPermission($permission + ->setRecipient($recipientSecond) + ->setStatus(StatusTypes::REJECT) + ->setType(PermissionTypes::MESSAGE)); + + IysManager::make()->createPermissionDriver()->sendPermissions($permissionList); + + Http::assertSent(function (Request $request) use ($endpoint, $recipientFirst, $recipientSecond, $consentDate) { + return $request->url() == $this->url . $endpoint && + $request[0]['consentDate'] == $consentDate && + $request[0]['recipient'] == $recipientFirst && + $request[0]['source'] == ConsentSourceTypes::MOBILE->value && + $request[0]['recipientType'] == RecipientTypes::INDIVIDUAL->value && + $request[0]['status'] == StatusTypes::APPROVE->value && + $request[0]['type'] == PermissionTypes::EMAIL->value && + $request[1]['consentDate'] == $consentDate && + $request[1]['recipient'] == $recipientSecond && + $request[1]['source'] == ConsentSourceTypes::MOBILE->value && + $request[1]['recipientType'] == RecipientTypes::INDIVIDUAL->value && + $request[1]['status'] == StatusTypes::REJECT->value && + $request[1]['type'] == PermissionTypes::MESSAGE->value; + }); + } + + /** + * @return void + * @throws RequestException + */ + public function test_throw_exception_send_permissions() + { + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/request"; + + $this->createHttpFakeToken(); + + Http::fake([ + $endpoint => Http::response([ + "errors" => [ + [ + "code" => "H124", + "message" => "Saniyede kabul edilen istek limitine ulaşıldı. Lütfen daha sonra + tekrar deneyiniz." + ] + ] + ], 429), + ]); + + $recipientFirst = 'test@example1.com'; + + $recipientSecond = 'test@example1.com'; + + $consentDate = '2020-07-08 07:07:07'; + + $permission = Permission::make() + ->setConsentDate($consentDate) + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipientType(RecipientTypes::INDIVIDUAL); + + $permissionList = PermissionList::make() + ->addPermission($permission + ->setRecipient($recipientFirst) + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL)) + ->addPermission($permission + ->setRecipient($recipientSecond) + ->setStatus(StatusTypes::REJECT) + ->setType(PermissionTypes::MESSAGE)); + + $this->expectException(RequestException::class); + + IysManager::make()->createPermissionDriver()->sendPermissions($permissionList); + } + + /** + * @return void + * @throws RequestException + */ + public function test_get_status_permissions() + { + $requestId = '5980a44b06f333c25b1ah61bkceb95fa'; + + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/request/$requestId"; + + Http::fake([ + $endpoint => Http::response( + [ + [ + "subRequestId" => "cf15b6bf-1b84-4e59-b5bb-bda95de5c1cf", + "index" => 0, + "status" => "failure", + "error" => [ + "code" => "H175", + "message" => "İlk izin kayıt işlemlerinde RET durum (status) bilgisi + içeren izinler kaydedilmez.", + ] + ], + [ + "requestId" => "548014a006fb33c6wb1a0a1b0vebs5ae", + "subRequestId" => "2666eb07-cbc9-4069-bc6e-4eeb1a48991f", + "index" => 1, + "transactionId" => "432cc155429fdccd7f2be8e9482aa322993ff210c2923fb84e8ed0fb1902ae21", + "status" => "success", + "creationDate" => "2020-08-21 14:45:24", + ], + [ + "index" => 2, + "status" => "enqueue", + "subrequestId" => "7ed0f89d-15c7-457f-a16a-d689ef5dca62", + ], + ] + ) + ]); + + + $this->createHttpFakeToken(); + + IysManager::make()->createPermissionDriver()->getPermissionsStatus($requestId); + + Http::assertSent(function (Request $request) use ($endpoint) { + return $request->url() == $this->url . $endpoint; + }); + } + + /** + * @return void + * @throws RequestException + */ + public function test_throw_exception_get_status_permissions() + { + $requestId = '5980a44b06f333c25b1ah61bkceb95fa'; + + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/request/$requestId"; + + $this->createHttpFakeToken(); + + Http::fake([ + $endpoint => Http::response([ + "errors" => [ + [ + "code" => "H103", + "message" => "Sunucuda arama yapılamadı.", + ] + ], + ], 500), + ]); + + $this->expectException(RequestException::class); + + IysManager::make()->createPermissionDriver()->getChangedPermissions(); + } + + /** + * @return void + * @throws RequestException + */ + public function test_get_changed_permissions() + { + $query = Arr::query([ + 'after' => null, + 'source' => SourceTypes::IYS->value, + 'limit' => 999, + ]); + + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/changes?$query"; + + $this->createHttpFakeToken(); + + Http::fake([ + $endpoint => Http::response( + [ + 'after' => '1597740617_632cc155429fdccd7f2be8e9482aa322993ff210c2923fb84e8ed0fb1902ae21', + 'list' => [ + 'consentDate' => '2018-02-10 09:50:02', + 'creationDate' => '2020-02-10 09:50:02', + 'source' => 'HS_WEB', + 'recipient' => 'ornek@adiniz.com', + 'recipientType' => 'BIREYSEL', + 'status' => 'ONAY', + 'type' => 'EPOSTA', + 'transactionId' => '532cc155429fdccd7f2be8e9482aa322993ff210c2923fb84e8ed0fb1902ae21', + 'citizenName' => 'Test isim', + ], + ] + )]); + + IysManager::make()->createPermissionDriver()->getChangedPermissions(); + + Http::assertSent(function (Request $request) use ($endpoint) { + return $request->url() == $this->url . $endpoint; + }); + } + + /** + * @throws RequestException + */ + public function test_get_changed_permissions_with_options() + { + $limit = 50; + + $after = '1597740617_632cc155429fdccd7f2be8e9482aa322993ff210c2923fb84e8ed0fb1902ae22'; + + $query = Arr::query([ + 'source' => SourceTypes::HS->value, + 'limit' => $limit, + 'after' => $after, + ]); + + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/changes?$query"; + + $this->createHttpFakeToken(); + + Http::fake([ + $endpoint => Http::response( + [ + 'after' => '1597740617_632cc155429fdccd7f2be8e9482aa322993ff210c2923fb84e8ed0fb1902ae21', + 'list' => [ + 'consentDate' => '2018-02-10 09:50:02', + 'creationDate' => '2020-02-10 09:50:02', + 'source' => 'HS_WEB', + 'recipient' => 'ornek@adiniz.com', + 'recipientType' => 'BIREYSEL', + 'status' => 'ONAY', + 'type' => 'EPOSTA', + 'transactionId' => '532cc155429fdccd7f2be8e9482aa322993ff210c2923fb84e8ed0fb1902ae21', + 'citizenName' => 'Test isim', + ], + ] + )]); + + IysManager::make()->createPermissionDriver()->getChangedPermissions($after, SourceTypes::HS, $limit); + + Http::assertSent(function (Request $request) use ($endpoint) { + return $request->url() == $this->url . $endpoint; + }); + } + + /** + * @return void + * @throws RequestException + */ + public function test_throw_exception_get_changed_permissions() + { + $query = Arr::query([ + 'after' => null, + 'source' => SourceTypes::IYS->value, + 'limit' => 999, + ]); + + $endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/changes?$query"; + + $this->createHttpFakeToken(); + + Http::fake([ + $endpoint => Http::response([ + "errors" => [ + [ + "location" => [], + "code" => "H168", + "message" => "Alıcı listesi (recipients) bulunamadı.", + ] + ] + ], 422), + ]); + + $this->expectException(RequestException::class); + + IysManager::make()->createPermissionDriver()->getChangedPermissions(); + } +} diff --git a/tests/PermissionListModelTest.php b/tests/PermissionListModelTest.php new file mode 100644 index 0000000..ab6575d --- /dev/null +++ b/tests/PermissionListModelTest.php @@ -0,0 +1,75 @@ +setConsentDate($consentDateFirst) + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipientType(RecipientTypes::INDIVIDUAL) + ->setRecipient($recipientFirst) + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL); + + + $recipientSecond = 'test@example1.com'; + + $consentDateSecond = '2020-09-01 07:06:07'; + + $retailerCode = 55550127; + + $retailerAccess = [ + 58795354, + 30143398, + ]; + + $permissionSecond = Permission::make() + ->setConsentDate($consentDateSecond) + ->setSource(ConsentSourceTypes::EMAIL) + ->setRecipientType(RecipientTypes::TRADER) + ->setRecipient($recipientSecond) + ->setStatus(StatusTypes::REJECT) + ->setType(PermissionTypes::CALL) + ->setRetailerCode($retailerCode) + ->setRetailerAccess($retailerAccess); + + $permissionList = PermissionList::make() + ->addPermission($permissionFirst) + ->addPermission($permissionSecond); + + $this->assertEquals([ + [ + 'consentDate' => $consentDateFirst, + 'source' => ConsentSourceTypes::MOBILE->value, + 'recipient' => $recipientFirst, + 'recipientType' => RecipientTypes::INDIVIDUAL->value, + 'status' => StatusTypes::APPROVE->value, + 'type' => PermissionTypes::EMAIL->value, + ], + [ + 'consentDate' => $consentDateSecond, + 'source' => ConsentSourceTypes::EMAIL->value, + 'recipient' => $recipientSecond, + 'recipientType' => RecipientTypes::TRADER->value, + 'status' => StatusTypes::REJECT->value, + 'type' => PermissionTypes::CALL->value, + 'retailerCode' => $retailerCode, + 'retailerAccess' => $retailerAccess, + ] + ], $permissionList->toArray()); + } +} diff --git a/tests/PermissionModelTest.php b/tests/PermissionModelTest.php new file mode 100644 index 0000000..3015f23 --- /dev/null +++ b/tests/PermissionModelTest.php @@ -0,0 +1,71 @@ +setConsentDate($consentDate) + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipient($recipient) + ->setRecipientType(RecipientTypes::INDIVIDUAL) + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL); + + $this->assertEquals([ + 'consentDate' => $consentDate, + 'source' => ConsentSourceTypes::MOBILE->value, + 'recipient' => $recipient, + 'recipientType' => RecipientTypes::INDIVIDUAL->value, + 'status' => StatusTypes::APPROVE->value, + 'type' => PermissionTypes::EMAIL->value, + ], $permission->toArray()); + } + + public function test_create_optional_parameters() + { + $recipient = 'test@example.com'; + + $consentDate = '2020-07-08 07:07:07'; + + $retailerCode = 55550127; + + $retailerAccess = [ + 58795354, + 30143398, + ]; + + $permission = Permission::make() + ->setConsentDate($consentDate) + ->setSource(ConsentSourceTypes::MOBILE) + ->setRecipient($recipient) + ->setRecipientType(RecipientTypes::INDIVIDUAL) + ->setStatus(StatusTypes::APPROVE) + ->setType(PermissionTypes::EMAIL) + ->setRetailerCode($retailerCode) + ->setRetailerAccess($retailerAccess); + + $this->assertEquals([ + 'consentDate' => $consentDate, + 'source' => ConsentSourceTypes::MOBILE->value, + 'recipient' => $recipient, + 'recipientType' => RecipientTypes::INDIVIDUAL->value, + 'status' => StatusTypes::APPROVE->value, + 'type' => PermissionTypes::EMAIL->value, + 'retailerCode' => $retailerCode, + 'retailerAccess' => $retailerAccess, + ], $permission->toArray()); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..d4020de --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,29 @@ + $this->username, + 'services.iys.password' => $this->password, + 'services.iys.brand_code' => $this->brandCode, + 'services.iys.iys_code' => $this->iysCode, + 'services.iys.url' => $this->url, + ]); + } +}