ldc-crm/tests/Feature/ApiV2/ApiTest.php
2021-08-02 11:46:08 +02:00

53 lines
1.7 KiB
PHP

<?php
namespace Tests\Feature\ApiV2;
use App\Enums\ApiStatus;
use App\Models\Service;
use Illuminate\Foundation\Testing\DatabaseTransactions;
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());
}
/**
* 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->assertEquals(Service::findOrFail($service->id)->api_token, $json->json('data.api_token'));
}
}