This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
106 lines
3.1 KiB
PHP
106 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\CRM\ApiV2;
|
|
|
|
use App\Enums\ApiStatus;
|
|
use App\Lib\Helper;
|
|
use App\Models\Customer;
|
|
use App\Models\CustomerLogin;
|
|
use App\Models\Service;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
|
use Illuminate\Testing\AssertableJsonString;
|
|
use Illuminate\Testing\TestResponse;
|
|
use Tests\CreatesApplication;
|
|
|
|
class ApiBase extends BaseTestCase
|
|
{
|
|
use CreatesApplication;
|
|
use DatabaseTransactions;
|
|
|
|
/**
|
|
* Make assertion that json response has status.
|
|
*
|
|
* @param AssertableJsonString $json
|
|
* @param ApiStatus $status
|
|
* @return void
|
|
*/
|
|
protected function assertApiResponseStatusIs(AssertableJsonString $json, ApiStatus $status)
|
|
{
|
|
$json->assertFragment(['status' => $status->value]);
|
|
}
|
|
|
|
/**
|
|
* Raw Function to call api
|
|
*
|
|
* @param string $method
|
|
* @param string $uri
|
|
* @param array $data
|
|
* @param array $headers
|
|
* @return \Illuminate\Testing\TestResponse
|
|
*/
|
|
protected function callApi(string $method, string $uri, array $data = [], array $headers = []): TestResponse
|
|
{
|
|
if (!isset($data['timestamp'])) {
|
|
$data['timestamp'] = \time();
|
|
}
|
|
return $this->json($method, $uri, $data, $headers);
|
|
}
|
|
|
|
/**
|
|
* Raw Function to call api with Token
|
|
*
|
|
* @param Service $service
|
|
* @param string $method
|
|
* @param string $uri
|
|
* @param array $data
|
|
* @param array $headers
|
|
* @return \Illuminate\Testing\TestResponse
|
|
*/
|
|
protected function callApiWithToken(
|
|
Service $service,
|
|
string $method,
|
|
string $uri,
|
|
array $data = [],
|
|
array $headers = []
|
|
): TestResponse {
|
|
$data['bearer_token'] = $service->bearer_token;
|
|
$data['timestamp'] = \gmdate('Y-m-d H:i:s');
|
|
|
|
$data['signature'] = Helper::signRequest(
|
|
$service->bearer_pwd,
|
|
$uri,
|
|
$method,
|
|
$data
|
|
);
|
|
|
|
return $this->callApi($method, $uri, $data, $headers);
|
|
}
|
|
|
|
/**
|
|
* Get Customer models with given status (active or not)
|
|
*
|
|
* @param boolean $status The customer status for a brand.
|
|
* @param boolean $random Randomize the customer to get.
|
|
* @return array
|
|
*/
|
|
protected function getCustomer(bool $status = true, bool $random = true): array
|
|
{
|
|
$login = CustomerLogin::whereStatus($status);
|
|
if ($random) {
|
|
$login = $login->inRandomOrder();
|
|
}
|
|
/** @var \App\Models\CustomerLogin $login */
|
|
$login = $login->firstOrFail();
|
|
/** @var \App\Models\Brand $brand */
|
|
$brand = $login->brand;
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = $login->customer;
|
|
/** @var \App\Models\Service $service */
|
|
$service = $brand->services()->firstOrFail();
|
|
/** @var \App\Models\CustomerInfo $info */
|
|
$info = $customer->info($brand, Customer::FIRST_OR_FAIL);
|
|
|
|
return compact('customer', 'service', 'login', 'brand', 'info');
|
|
}
|
|
}
|