ldc-crm/tests/Feature/ApiV2/ApiTest.php
2021-08-09 17:35:28 +02:00

60 lines
2 KiB
PHP

<?php
namespace Tests\Feature\ApiV2;
use App\Enums\ApiError;
use App\Enums\ApiStatus;
use App\Models\Service;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Hash;
use Tests\CreatesApplication;
class ApiTest extends ApiBase
{
use CreatesApplication;
use DatabaseTransactions;
public function testTimestampMiddleware()
{
$response = $this->callApi('get', route('apiV2.ping'));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$response = $this->json('get', route('apiV2.ping'), ['timestamp' => time()], [
'X-API-TIMESTAMP' => time() + 119
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$response = $this->json('get', route('apiV2.ping'), ['timestamp' => time()], [
'X-API-TIMESTAMP' => time() + 121
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
$json->assertFragment(['data' => [
'code' => ApiError::wrongTimeStamp()->value,
'message' => ApiError::wrongTimeStamp()->label
]]);
}
/**
* route: apiV2.authenticate
*/
public function testApiAuthenticate()
{
extract($this->getCustomer());
$response = $this->callApi(
'post',
route('apiV2.authenticate'),
['bearer_pwd' => $service->bearer_pwd],
['Authorization' => sprintf('Bearer %s', $service->bearer_token)]
);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$this->assertNotEmpty($json->json('data'));
$this->assertNotEmpty($json->json('data.api_token'));
$this->assertTrue(Hash::check($json->json('data.api_token'), Service::findOrFail($service->id)->api_token));
}
}