2017-12-12 12:05:22 +08:00
|
|
|
<?php
|
|
|
|
|
2017-12-29 11:17:26 +08:00
|
|
|
#use Swagger\Serializer;
|
2018-01-01 23:32:04 +08:00
|
|
|
use App\Models\User;
|
2017-12-29 11:17:26 +08:00
|
|
|
|
2017-12-12 12:05:22 +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()
|
|
|
|
{
|
2018-01-01 23:32:04 +08:00
|
|
|
$user = factory(User::class)->create();
|
2017-12-13 11:50:55 +08:00
|
|
|
|
2018-01-07 05:13:33 +08:00
|
|
|
$uri = $this->u('/user');
|
2017-12-12 12:05:22 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2017-12-12 12:05:22 +08:00
|
|
|
// Test upper/lower case of Authorization header, etc
|
2018-01-07 05:13:33 +08:00
|
|
|
$response = $this->get($uri, $this->headers($user));
|
|
|
|
$response->assertStatus(200)->assertJson(['id' => $user->id], true);
|
2017-12-12 12:05:22 +08:00
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
$this->withHeaders(['x-api-key' => $user->api_key])->get($uri)
|
2018-01-07 05:13:33 +08:00
|
|
|
->assertJson(['id' => $user->id], true);
|
2017-12-31 02:42:45 +08:00
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
$this->withHeaders(['x-API-key' => $user->api_key])->get($uri)
|
2018-01-07 05:13:33 +08:00
|
|
|
->assertJson(['id' => $user->id], true);
|
2017-12-31 02:42:45 +08:00
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
$this->withHeaders(['X-API-KEY' => $user->api_key])->get($uri)
|
2018-01-07 05:13:33 +08:00
|
|
|
->assertJson(['id' => $user->id], true);
|
2017-12-12 12:05:22 +08:00
|
|
|
}
|
|
|
|
|
2018-01-05 10:50:57 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testApiDeniedOnInactiveUser()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(User::class)->create([
|
2018-01-05 10:50:57 +08:00
|
|
|
'state' => UserState::PENDING
|
|
|
|
]);
|
|
|
|
|
|
|
|
$uri = '/api/user';
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->get($uri)->assertStatus(401);
|
2018-01-05 10:50:57 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 05:13:33 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testGetAirlines()
|
|
|
|
{
|
|
|
|
$size = \random_int(5, 10);
|
|
|
|
$this->user = factory(App\Models\User::class)->create([
|
|
|
|
'airline_id' => 0
|
|
|
|
]);
|
|
|
|
|
|
|
|
$airlines = factory(App\Models\Airline::class, $size)->create();
|
|
|
|
|
|
|
|
$res = $this->get($this->u('/airlines'));
|
|
|
|
$body = $res->json();
|
|
|
|
|
|
|
|
$this->assertCount($size, $body['data']);
|
|
|
|
|
|
|
|
$airline = $airlines->random();
|
|
|
|
$this->get('/api/airlines/'.$airline->id)->assertJson(['name' => $airline->name]);
|
|
|
|
}
|
|
|
|
|
2017-12-12 12:05:22 +08:00
|
|
|
/**
|
|
|
|
* Make sure the airport data is returned
|
|
|
|
*/
|
|
|
|
public function testAirportRequest()
|
|
|
|
{
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
2017-12-13 11:50:55 +08:00
|
|
|
$airport = factory(App\Models\Airport::class)->create();
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->get('/api/airports/' . $airport->icao);
|
2018-01-05 11:05:26 +08:00
|
|
|
|
2017-12-29 11:17:26 +08:00
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJson(['icao' => $airport->icao], true);
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->get('/api/airports/UNK')->assertStatus(404);
|
2017-12-12 12:05:22 +08:00
|
|
|
}
|
2018-01-06 00:23:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the airports, test the pagination
|
|
|
|
*/
|
|
|
|
public function testGetAllAirports()
|
|
|
|
{
|
2018-01-06 05:30:35 +08:00
|
|
|
factory(App\Models\Airport::class, 70)->create();
|
2018-01-06 00:23:26 +08:00
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->get('/api/airports/')
|
2018-01-06 00:23:26 +08:00
|
|
|
->assertStatus(200)
|
|
|
|
->assertJsonCount(50, 'data');
|
|
|
|
|
|
|
|
$body = $response->json();
|
|
|
|
|
|
|
|
$this->assertHasKeys($body, ['data', 'links', 'meta']);
|
|
|
|
|
|
|
|
$last_page = $body['meta']['last_page'];
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->get('/api/airports?page=' . $last_page)
|
2018-01-06 00:23:26 +08:00
|
|
|
->assertStatus(200)
|
|
|
|
->assertJsonCount(20, 'data');
|
|
|
|
}
|
2018-01-06 00:45:34 +08:00
|
|
|
|
2018-01-06 05:30:35 +08:00
|
|
|
public function testGetAllAirportsHubs()
|
|
|
|
{
|
|
|
|
factory(App\Models\Airport::class, 10)->create();
|
|
|
|
factory(App\Models\Airport::class)->create(['hub' => 1]);
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->get('/api/airports/hubs')
|
2018-01-06 05:30:35 +08:00
|
|
|
->assertStatus(200)
|
|
|
|
->assertJsonCount(1, 'data');
|
|
|
|
}
|
|
|
|
|
2018-01-06 00:45:34 +08:00
|
|
|
/**
|
|
|
|
* Test getting the subfleets
|
|
|
|
*/
|
|
|
|
public function testGetSubfleets()
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
]);
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->get('/api/fleet');
|
2018-01-06 00:45:34 +08:00
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
}
|
2018-01-06 03:00:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting an aircraft
|
|
|
|
*/
|
|
|
|
public function testGetAircraft()
|
|
|
|
{
|
|
|
|
$subfleet = factory(App\Models\Subfleet::class)->create();
|
|
|
|
$aircraft = factory(App\Models\Aircraft::class)->create([
|
|
|
|
'subfleet_id' => $subfleet->id
|
|
|
|
]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just try retrieving by ID
|
|
|
|
*/
|
2018-01-07 02:07:22 +08:00
|
|
|
$resp = $this->get('/api/fleet/aircraft/' . $aircraft->id);
|
2018-01-06 03:00:45 +08:00
|
|
|
$body = $resp->json();
|
|
|
|
$this->assertEquals($body['id'], $aircraft->id);
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$resp = $this->get('/api/fleet/aircraft/' . $aircraft->id . '?registration=' . $aircraft->registration);
|
2018-01-06 03:00:45 +08:00
|
|
|
$body = $resp->json();
|
|
|
|
$this->assertEquals($body['id'], $aircraft->id);
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$resp = $this->get('/api/fleet/aircraft/' . $aircraft->id . '?tail_number=' . $aircraft->registration);
|
2018-01-06 03:00:45 +08:00
|
|
|
$body = $resp->json();
|
|
|
|
$this->assertEquals($body['id'], $aircraft->id);
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$resp = $this->get('/api/fleet/aircraft/' . $aircraft->id . '?icao=' . $aircraft->icao);
|
2018-01-06 03:00:45 +08:00
|
|
|
$body = $resp->json();
|
|
|
|
$this->assertEquals($body['id'], $aircraft->id);
|
|
|
|
}
|
2018-01-07 05:41:23 +08:00
|
|
|
|
|
|
|
public function testGetAllSettings()
|
|
|
|
{
|
|
|
|
$this->user = factory(App\Models\User::class)->create();
|
|
|
|
$res = $this->get('/api/settings')->assertStatus(200);
|
|
|
|
$settings = $res->json();
|
|
|
|
}
|
2017-12-12 12:05:22 +08:00
|
|
|
}
|