phpvms/tests/ApiTest.php

177 lines
5.3 KiB
PHP
Raw Normal View History

<?php
2017-12-29 11:17:26 +08:00
#use Swagger\Serializer;
use App\Models\User;
2017-12-29 11:17:26 +08:00
/**
* Test API calls and authentication, etc
*/
class ApiTest extends TestCase
{
public function setUp()
{
parent::setUp();
$this->addData('base');
}
/**
* Ensure authentication against the API works
*/
public function testApiAuthentication()
{
$user = factory(User::class)->create();
2018-01-03 01:06:04 +08:00
$pirep = factory(App\Models\Pirep::class)->create();
2018-01-03 01:06:04 +08:00
$uri = '/api/pireps/' . $pirep->id;
// Missing auth header
$this->get($uri)->assertStatus(401);
// Test invalid API key
$this->withHeaders(['Authorization' => 'invalidKey'])->get($uri)
->assertStatus(401);
2017-12-31 03:02:22 +08:00
$this->withHeaders(['Authorization' => ''])->get($uri)
->assertStatus(401);
// Test upper/lower case of Authorization header, etc
$response = $this->withHeaders($this->apiHeaders())->get($uri);
$response->assertStatus(200)->assertJson(['id' => $pirep->id], true);
$this->withHeaders(['x-api-key' => $user->api_key])->get($uri)
->assertStatus(200)
2018-01-03 01:06:04 +08:00
->assertJson(['id' => $pirep->id], true);
$this->withHeaders(['x-API-key' => $user->api_key])->get($uri)
->assertStatus(200)
2018-01-03 01:06:04 +08:00
->assertJson(['id' => $pirep->id], true);
$this->withHeaders(['X-API-KEY' => $user->api_key])->get($uri)
->assertStatus(200)
2018-01-03 01:06:04 +08:00
->assertJson(['id' => $pirep->id], true);
}
/**
*
*/
public function testApiDeniedOnInactiveUser()
{
$user = factory(User::class)->create([
'state' => UserState::PENDING
]);
$uri = '/api/user';
$this->withHeaders(['x-api-key' => $user->api_key])->get($uri)
->assertStatus(401);
}
/**
* Make sure the airport data is returned
*/
public function testAirportRequest()
{
2018-01-05 11:05:26 +08:00
$user = factory(App\Models\User::class)->create();
$airport = factory(App\Models\Airport::class)->create();
2018-01-05 11:05:26 +08:00
$response = $this->withHeaders($this->headers($user))
->get('/api/airports/' . $airport->icao);
2017-12-29 11:17:26 +08:00
$response->assertStatus(200);
$response->assertJson(['icao' => $airport->icao], true);
2018-01-05 11:05:26 +08:00
$this->withHeaders($this->headers($user))
->get('/api/airports/UNK')
->assertStatus(404);
}
/**
* Get all the airports, test the pagination
*/
public function testGetAllAirports()
{
$user = factory(App\Models\User::class)->create();
factory(App\Models\Airport::class, 120)->create();
$response = $this->user_get($user, '/api/airports/')
->assertStatus(200)
->assertJsonCount(50, 'data');
$body = $response->json();
$this->assertHasKeys($body, ['data', 'links', 'meta']);
$last_page = $body['meta']['last_page'];
$this->user_get($user, '/api/airports?page='.$last_page)
->assertStatus(200)
->assertJsonCount(20, 'data');
}
/**
* Test getting the subfleets
*/
public function testGetSubfleets()
{
$user = factory(App\Models\User::class)->create();
$subfleetA = factory(App\Models\Subfleet::class)->create();
$subfleetB = factory(App\Models\Subfleet::class)->create();
$subfleetA_size = \random_int(2, 10);
$subfleetB_size = \random_int(2, 10);
factory(App\Models\Aircraft::class, $subfleetA_size)->create([
'subfleet_id' => $subfleetA->id
]);
factory(App\Models\Aircraft::class, $subfleetB_size)->create([
'subfleet_id' => $subfleetB->id
]);
$response = $this->user_get($user, '/api/fleet');
$response->assertStatus(200);
$body = $response->json();
foreach($body['data'] as $subfleet) {
if($subfleet['id'] === $subfleetA->id) {
$size = $subfleetA_size;
} else {
$size = $subfleetB_size;
}
$this->assertCount($size, $subfleet['aircraft']);
}
}
/**
* Test getting an aircraft
*/
public function testGetAircraft()
{
$user = factory(App\Models\User::class)->create();
$subfleet = factory(App\Models\Subfleet::class)->create();
$aircraft = factory(App\Models\Aircraft::class)->create([
'subfleet_id' => $subfleet->id
]);
/**
* Just try retrieving by ID
*/
$resp = $this->user_get($user, '/api/fleet/aircraft/'. $aircraft->id);
$body = $resp->json();
$this->assertEquals($body['id'], $aircraft->id);
$resp = $this->user_get($user,
'/api/fleet/aircraft/'.$aircraft->id.'?registration='.$aircraft->registration);
$body = $resp->json();
$this->assertEquals($body['id'], $aircraft->id);
$resp = $this->user_get($user,
'/api/fleet/aircraft/' . $aircraft->id . '?tail_number=' . $aircraft->registration);
$body = $resp->json();
$this->assertEquals($body['id'], $aircraft->id);
$resp = $this->user_get($user,
'/api/fleet/aircraft/' . $aircraft->id . '?icao=' . $aircraft->icao);
$body = $resp->json();
$this->assertEquals($body['id'], $aircraft->id);
}
}