2017-12-12 12:05:22 +08:00
|
|
|
<?php
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
//use Swagger\Serializer;
|
2020-05-23 23:43:29 +08:00
|
|
|
use App\Models\Aircraft;
|
|
|
|
use App\Models\Airline;
|
|
|
|
use App\Models\Airport;
|
|
|
|
use App\Models\Enums\UserState;
|
|
|
|
use App\Models\Fare;
|
|
|
|
use App\Models\News;
|
|
|
|
use App\Models\Subfleet;
|
2018-01-01 23:32:04 +08:00
|
|
|
use App\Models\User;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Services\FareService;
|
2020-05-23 23:43:29 +08:00
|
|
|
use Exception;
|
|
|
|
use function random_int;
|
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
|
|
|
|
{
|
2019-05-12 23:50:38 +08:00
|
|
|
public function setUp(): void
|
2017-12-12 12:05:22 +08:00
|
|
|
{
|
|
|
|
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-02-09 01:07:21 +08:00
|
|
|
$uri = '/api/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));
|
2018-02-11 07:34:46 +08:00
|
|
|
$body = $response->json();
|
|
|
|
$response->assertStatus(200)->assertJson(['data' => ['id' => $user->id]]);
|
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-02-11 07:34:46 +08:00
|
|
|
->assertJson(['data' => ['id' => $user->id]]);
|
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-02-11 07:34:46 +08:00
|
|
|
->assertJson(['data' => ['id' => $user->id]]);
|
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-02-11 07:34:46 +08:00
|
|
|
->assertJson(['data' => ['id' => $user->id]]);
|
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-08-27 00:40:04 +08:00
|
|
|
'state' => UserState::PENDING,
|
2018-01-05 10:50:57 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$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-04-12 09:01:41 +08:00
|
|
|
/**
|
|
|
|
* Test getting the news from the API
|
|
|
|
*/
|
|
|
|
public function testGetNews(): void
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
factory(News::class)->create();
|
2018-04-12 09:01:41 +08:00
|
|
|
$response = $this->get('/api/news')->json();
|
|
|
|
|
|
|
|
$this->assertCount(1, $response['data']);
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->assertArrayHasKey('user', $response['data'][0]);
|
2018-04-12 09:01:41 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 02:50:08 +08:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2018-01-07 05:13:33 +08:00
|
|
|
public function testGetAirlines()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$size = random_int(5, 10);
|
|
|
|
$this->user = factory(User::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'airline_id' => 0,
|
2018-01-07 05:13:33 +08:00
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$airlines = factory(Airline::class, $size)->create();
|
2018-01-07 05:13:33 +08:00
|
|
|
|
2018-02-09 01:07:21 +08:00
|
|
|
$res = $this->get('/api/airlines');
|
2019-08-05 20:27:53 +08:00
|
|
|
$this->assertTrue($res->isOk());
|
2018-01-07 05:13:33 +08:00
|
|
|
|
|
|
|
$airline = $airlines->random();
|
2018-02-11 07:34:46 +08:00
|
|
|
$this->get('/api/airlines/'.$airline->id)
|
|
|
|
->assertJson(['data' => ['name' => $airline->name]]);
|
2018-01-07 05:13:33 +08:00
|
|
|
}
|
|
|
|
|
2019-09-13 23:21:40 +08:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function testPagination()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$size = random_int(5, 10);
|
|
|
|
$this->user = factory(User::class)->create([
|
2019-09-13 23:21:40 +08:00
|
|
|
'airline_id' => 0,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
factory(Airline::class, $size)->create();
|
2019-09-13 23:21:40 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Page 0 and page 1 should return the same thing
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Test pagination
|
|
|
|
$res = $this->get('/api/airlines?limit=1&page=0');
|
|
|
|
$this->assertTrue($res->isOk());
|
|
|
|
$body = $res->json('data');
|
|
|
|
|
|
|
|
$this->assertCount(1, $body);
|
|
|
|
|
|
|
|
$id_first = $body[0]['id'];
|
|
|
|
|
|
|
|
$res = $this->get('/api/airlines?limit=1&page=1');
|
|
|
|
$this->assertTrue($res->isOk());
|
|
|
|
$body = $res->json('data');
|
|
|
|
|
|
|
|
$id_second = $body[0]['id'];
|
|
|
|
|
|
|
|
$this->assertEquals($id_first, $id_second);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Page 2 should be different from page 1
|
|
|
|
*/
|
|
|
|
|
|
|
|
$res = $this->get('/api/airlines?limit=1&page=2');
|
|
|
|
$this->assertTrue($res->isOk());
|
|
|
|
$body = $res->json('data');
|
|
|
|
|
|
|
|
$id_third = $body[0]['id'];
|
|
|
|
|
|
|
|
$this->assertNotEquals($id_first, $id_third);
|
|
|
|
}
|
|
|
|
|
2017-12-12 12:05:22 +08:00
|
|
|
/**
|
|
|
|
* Make sure the airport data is returned
|
|
|
|
*/
|
|
|
|
public function testAirportRequest()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create();
|
|
|
|
$airport = factory(Airport::class)->create();
|
2017-12-13 11:50:55 +08:00
|
|
|
|
2018-08-27 00:40:04 +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);
|
2018-02-11 07:34:46 +08:00
|
|
|
$response->assertJson(['data' => ['icao' => $airport->icao]]);
|
2017-12-29 11:17:26 +08:00
|
|
|
|
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()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create();
|
|
|
|
factory(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-03-01 05:42:43 +08:00
|
|
|
->assertStatus(200);
|
2018-01-06 00:23:26 +08:00
|
|
|
|
|
|
|
$body = $response->json();
|
|
|
|
$this->assertHasKeys($body, ['data', 'links', 'meta']);
|
|
|
|
}
|
2018-01-06 00:45:34 +08:00
|
|
|
|
2018-01-06 05:30:35 +08:00
|
|
|
public function testGetAllAirportsHubs()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create();
|
|
|
|
factory(Airport::class, 10)->create();
|
|
|
|
factory(Airport::class)->create(['hub' => 1]);
|
2018-01-06 05:30:35 +08:00
|
|
|
|
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
|
2018-08-27 02:51:47 +08:00
|
|
|
*
|
2018-08-27 02:50:08 +08:00
|
|
|
* @throws Exception
|
2018-01-06 00:45:34 +08:00
|
|
|
*/
|
|
|
|
public function testGetSubfleets()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create();
|
2018-02-10 03:20:35 +08:00
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$subfleetA = factory(Subfleet::class)->create([
|
2018-02-10 03:20:35 +08:00
|
|
|
'airline_id' => $this->user->airline_id,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$subfleetB = factory(Subfleet::class)->create([
|
2018-02-10 03:20:35 +08:00
|
|
|
'airline_id' => $this->user->airline_id,
|
|
|
|
]);
|
2018-01-06 00:45:34 +08:00
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$subfleetA_size = random_int(2, 10);
|
|
|
|
$subfleetB_size = random_int(2, 10);
|
|
|
|
factory(Aircraft::class, $subfleetA_size)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'subfleet_id' => $subfleetA->id,
|
2018-01-06 00:45:34 +08:00
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
factory(Aircraft::class, $subfleetB_size)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'subfleet_id' => $subfleetB->id,
|
2018-01-06 00:45:34 +08:00
|
|
|
]);
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->get('/api/fleet');
|
2018-01-06 00:45:34 +08:00
|
|
|
$response->assertStatus(200);
|
2018-02-11 07:34:46 +08:00
|
|
|
$body = $response->json()['data'];
|
2018-01-06 00:45:34 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($body as $subfleet) {
|
|
|
|
if ($subfleet['id'] === $subfleetA->id) {
|
2018-01-06 00:45:34 +08:00
|
|
|
$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()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create();
|
2018-02-10 03:20:35 +08:00
|
|
|
|
2018-01-24 11:40:34 +08:00
|
|
|
$fare_svc = app(FareService::class);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$subfleet = factory(Subfleet::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'airline_id' => $this->user->airline_id,
|
2018-02-10 03:20:35 +08:00
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$fare = factory(Fare::class)->create();
|
2018-01-24 11:40:34 +08:00
|
|
|
|
|
|
|
$fare_svc->setForSubfleet($subfleet, $fare);
|
2020-05-23 23:43:29 +08:00
|
|
|
$aircraft = factory(Aircraft::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'subfleet_id' => $subfleet->id,
|
2018-01-06 03:00:45 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just try retrieving by ID
|
|
|
|
*/
|
2018-08-27 00:40:04 +08:00
|
|
|
$resp = $this->get('/api/fleet/aircraft/'.$aircraft->id);
|
2018-02-11 07:34:46 +08:00
|
|
|
$body = $resp->json()['data'];
|
2018-01-06 03:00:45 +08:00
|
|
|
$this->assertEquals($body['id'], $aircraft->id);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
$resp = $this->get('/api/fleet/aircraft/'.$aircraft->id.'?registration='.$aircraft->registration);
|
2018-02-11 07:34:46 +08:00
|
|
|
$body = $resp->json()['data'];
|
2018-01-06 03:00:45 +08:00
|
|
|
$this->assertEquals($body['id'], $aircraft->id);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
$resp = $this->get('/api/fleet/aircraft/'.$aircraft->id.'?icao='.$aircraft->icao);
|
2018-02-11 07:34:46 +08:00
|
|
|
$body = $resp->json()['data'];
|
2018-01-06 03:00:45 +08:00
|
|
|
$this->assertEquals($body['id'], $aircraft->id);
|
|
|
|
}
|
2018-01-07 05:41:23 +08:00
|
|
|
|
|
|
|
public function testGetAllSettings()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create();
|
2018-01-07 05:41:23 +08:00
|
|
|
$res = $this->get('/api/settings')->assertStatus(200);
|
|
|
|
$settings = $res->json();
|
|
|
|
}
|
2020-03-25 03:53:59 +08:00
|
|
|
|
|
|
|
public function testGetUser()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create([
|
2020-03-25 06:54:04 +08:00
|
|
|
'avatar' => '/assets/avatar.jpg',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$res = $this->get('/api/user')->assertStatus(200);
|
|
|
|
$user = $res->json('data');
|
|
|
|
$this->assertNotNull($user);
|
|
|
|
$this->assertTrue(strpos($user['avatar'], 'http') !== -1);
|
|
|
|
|
|
|
|
// Should go to gravatar
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create();
|
2020-03-25 06:54:04 +08:00
|
|
|
|
2020-03-25 03:53:59 +08:00
|
|
|
$res = $this->get('/api/user')->assertStatus(200);
|
|
|
|
$user = $res->json('data');
|
|
|
|
$this->assertNotNull($user);
|
2020-03-25 06:54:04 +08:00
|
|
|
$this->assertTrue(strpos($user['avatar'], 'gravatar') !== -1);
|
2020-03-25 03:53:59 +08:00
|
|
|
}
|
2017-12-12 12:05:22 +08:00
|
|
|
}
|