phpvms/tests/ApiTest.php

334 lines
9.2 KiB
PHP
Raw Normal View History

<?php
namespace Tests;
2018-08-27 00:40:04 +08:00
//use Swagger\Serializer;
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;
use App\Models\User;
2018-02-21 12:33:09 +08:00
use App\Services\FareService;
use App\Support\Utils;
use Exception;
use function random_int;
2017-12-29 11:17:26 +08:00
/**
* Test API calls and authentication, etc
*/
class ApiTest extends TestCase
{
2019-05-12 23:50:38 +08:00
public function setUp(): void
{
parent::setUp();
$this->addData('base');
}
/**
* Ensure authentication against the API works
*/
public function testApiAuthentication()
{
$user = factory(User::class)->create();
$uri = '/api/user';
// Missing auth header
$res = $this->get($uri);
$res->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->get($uri, $this->headers($user));
$body = $response->json();
$response->assertStatus(200)->assertJson(['data' => ['id' => $user->id]]);
$this->withHeaders(['x-api-key' => $user->api_key])->get($uri)
->assertJson(['data' => ['id' => $user->id]]);
$this->withHeaders(['x-API-key' => $user->api_key])->get($uri)
->assertJson(['data' => ['id' => $user->id]]);
$this->withHeaders(['X-API-KEY' => $user->api_key])->get($uri)
->assertJson(['data' => ['id' => $user->id]]);
}
public function testApiDeniedOnInactiveUser()
{
$this->user = factory(User::class)->create([
2018-08-27 00:40:04 +08:00
'state' => UserState::PENDING,
]);
$uri = '/api/user';
$this->get($uri)->assertStatus(401);
}
2018-04-12 09:01:41 +08:00
/**
* Test getting the news from the API
*/
public function testGetNews(): void
{
factory(News::class)->create();
2018-04-12 09:01:41 +08:00
$response = $this->get('/api/news')->json();
$this->assertCount(1, $response['data']);
$this->assertArrayHasKey('user', $response['data'][0]);
2018-04-12 09:01:41 +08:00
}
2018-08-27 02:50:08 +08:00
/**
* @throws Exception
*/
public function testGetAirlines()
{
$size = random_int(5, 10);
$this->user = factory(User::class)->create([
2018-08-27 00:40:04 +08:00
'airline_id' => 0,
]);
$airlines = factory(Airline::class, $size)->create();
$res = $this->get('/api/airlines');
$this->assertTrue($res->isOk());
$airline = $airlines->random();
$this->get('/api/airlines/'.$airline->id)
->assertJson(['data' => ['name' => $airline->name]]);
}
/**
* @throws Exception
*/
public function testPagination()
{
$size = random_int(5, 10);
$this->user = factory(User::class)->create([
'airline_id' => 0,
]);
factory(Airline::class, $size)->create();
/*
* 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);
}
/**
* Make sure the airport data is returned
*/
public function testAirportRequest()
{
$this->user = factory(User::class)->create();
$airport = factory(Airport::class)->create();
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);
$response->assertJson(['data' => ['icao' => $airport->icao]]);
2017-12-29 11:17:26 +08:00
$this->get('/api/airports/UNK')->assertStatus(404);
}
/**
* Get all the airports, test the pagination
*/
public function testGetAllAirports()
{
$this->user = factory(User::class)->create();
factory(Airport::class, 70)->create();
$response = $this->get('/api/airports/')
2018-03-01 05:42:43 +08:00
->assertStatus(200);
$body = $response->json();
$this->assertHasKeys($body, ['data', 'links', 'meta']);
}
2018-01-06 05:30:35 +08:00
public function testGetAllAirportsHubs()
{
$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
$this->get('/api/airports/hubs')
2018-01-06 05:30:35 +08:00
->assertStatus(200)
->assertJsonCount(1, 'data');
}
/**
* Test getting the subfleets
2018-08-27 02:51:47 +08:00
*
2018-08-27 02:50:08 +08:00
* @throws Exception
*/
public function testGetSubfleets()
{
$this->user = factory(User::class)->create();
$subfleetA = factory(Subfleet::class)->create([
'airline_id' => $this->user->airline_id,
]);
$subfleetB = factory(Subfleet::class)->create([
'airline_id' => $this->user->airline_id,
]);
$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,
]);
factory(Aircraft::class, $subfleetB_size)->create([
2018-08-27 00:40:04 +08:00
'subfleet_id' => $subfleetB->id,
]);
$response = $this->get('/api/fleet');
$response->assertStatus(200);
$body = $response->json()['data'];
2018-08-27 00:40:04 +08:00
foreach ($body 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()
{
$this->user = factory(User::class)->create();
$fare_svc = app(FareService::class);
/** @var Subfleet $subfleet */
$subfleet = factory(Subfleet::class)->create([
2018-08-27 00:40:04 +08:00
'airline_id' => $this->user->airline_id,
]);
/** @var Fare $fare */
$fare = factory(Fare::class)->create();
$fare_svc->setForSubfleet($subfleet, $fare);
/** @var Aircraft $aircraft */
$aircraft = factory(Aircraft::class)->create([
2018-08-27 00:40:04 +08:00
'subfleet_id' => $subfleet->id,
]);
/**
* Just try retrieving by ID
*/
2018-08-27 00:40:04 +08:00
$resp = $this->get('/api/fleet/aircraft/'.$aircraft->id);
$body = $resp->json()['data'];
$this->assertEquals($body['id'], $aircraft->id);
$this->assertEquals($body['name'], $aircraft->name);
$this->assertEquals($body['mtow'], $aircraft->mtow);
$this->assertEquals($body['zfw'], $aircraft->zfw);
2018-08-27 00:40:04 +08:00
$resp = $this->get('/api/fleet/aircraft/'.$aircraft->id.'?registration='.$aircraft->registration);
$body = $resp->json()['data'];
$this->assertEquals($body['id'], $aircraft->id);
$this->assertEquals($body['name'], $aircraft->name);
$this->assertEquals($body['mtow'], $aircraft->mtow);
$this->assertEquals($body['zfw'], $aircraft->zfw);
2018-08-27 00:40:04 +08:00
$resp = $this->get('/api/fleet/aircraft/'.$aircraft->id.'?icao='.$aircraft->icao);
$body = $resp->json()['data'];
$this->assertEquals($body['id'], $aircraft->id);
$this->assertEquals($body['name'], $aircraft->name);
$this->assertEquals($body['mtow'], $aircraft->mtow);
$this->assertEquals($body['zfw'], $aircraft->zfw);
}
public function testGetAllSettings()
{
$this->user = factory(User::class)->create();
$res = $this->get('/api/settings')->assertStatus(200);
$settings = $res->json();
}
public function testGetUser()
{
$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
$this->user = factory(User::class)->create();
2020-03-25 06:54:04 +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);
}
/**
* Test that the web cron runs
*/
public function testWebCron()
{
$this->updateSetting('cron.random_id', '');
$this->get('/api/cron/sdf')->assertStatus(400);
$id = Utils::generateNewId(24);
$this->updateSetting('cron.random_id', $id);
$this->get('/api/cron/sdf')->assertStatus(400);
$res = $this->get('/api/cron/'.$id);
$res->assertStatus(200);
}
}