Refuse pirep prefile is user not allowed to use aircraft #170

This commit is contained in:
Nabeel Shahzad
2018-02-20 12:59:49 -06:00
parent 8393ae2851
commit 4e43223f3a
8 changed files with 137 additions and 42 deletions

View File

@@ -2,6 +2,7 @@
use App\Models\Enums\PirepState;
use App\Models\Enums\PirepStatus;
use Tests\TestData;
/**
* Test API calls and authentication, etc
@@ -88,11 +89,16 @@ class AcarsTest extends TestCase
*/
public function testAcarsUpdates()
{
$this->user = factory(App\Models\User::class)->create();
$subfleet = $this->createSubfleetWithAircraft(2);
$rank = $this->createRank(10, [$subfleet['subfleet']->id]);
$this->user = factory(App\Models\User::class)->create([
'rank_id' => $rank->id
]);
$airport = factory(App\Models\Airport::class)->create();
$airline = factory(App\Models\Airline::class)->create();
$aircraft = factory(App\Models\Aircraft::class)->create();
$aircraft = $subfleet['aircraft']->random();
$uri = '/api/pireps/prefile';
$pirep = [
@@ -217,13 +223,56 @@ class AcarsTest extends TestCase
$this->assertCount(1, $comments);
}
/**
* Test aircraft is allowed
*/
public function testAircraftAllowed()
{
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', true);
$airport = factory(App\Models\Airport::class)->create();
$airline = factory(App\Models\Airline::class)->create();
# Add subfleets and aircraft, but also add another set of subfleets
$subfleetA = $this->createSubfleetWithAircraft(1);
// User not allowed aircraft from this subfleet
$subfleetB = $this->createSubfleetWithAircraft(1);
$rank = $this->createRank(10, [$subfleetA['subfleet']->id]);
$this->user = factory(App\Models\User::class)->create([
'rank_id' => $rank->id,
]);
$uri = '/api/pireps/prefile';
$pirep = [
'airline_id' => $airline->id,
'aircraft_id' => $subfleetB['aircraft']->random()->id,
'dpt_airport_id' => $airport->icao,
'arr_airport_id' => $airport->icao,
'flight_number' => '6000',
'level' => 38000,
'planned_flight_time' => 120,
'route' => 'POINTA POINTB',
'source_name' => 'Unit test'
];
$response = $this->post($uri, $pirep);
$response->assertStatus(400);
// Try refiling with a valid aircraft
$pirep['aircraft_id'] = $subfleetA['aircraft']->random()->id;
$response = $this->post($uri, $pirep);
$response->assertStatus(201);
}
/**
* Test publishing multiple, batched updates
*/
public function testMultipleAcarsPositionUpdates()
{
$this->user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
$pirep = $this->createPirep()->toArray();
$uri = '/api/pireps/prefile';
$response = $this->post($uri, $pirep);
@@ -275,8 +324,7 @@ class AcarsTest extends TestCase
*/
public function testAcarsIsoDate()
{
$this->user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
$pirep = $this->createPirep()->toArray();
$uri = '/api/pireps/prefile';
$response = $this->post($uri, $pirep);
@@ -298,8 +346,7 @@ class AcarsTest extends TestCase
*/
public function testAcarsInvalidRoutePost()
{
$this->user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
$pirep = $this->createPirep()->toArray();
$uri = '/api/pireps/prefile';
$response = $this->post($uri, $pirep);
@@ -321,8 +368,7 @@ class AcarsTest extends TestCase
public function testAcarsLogPost()
{
$this->user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
$pirep = $this->createPirep()->toArray();
$uri = '/api/pireps/prefile';
$response = $this->post($uri, $pirep);
@@ -362,8 +408,7 @@ class AcarsTest extends TestCase
*/
public function testAcarsRoutePost()
{
$this->user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
$pirep = $this->createPirep()->toArray();
$uri = '/api/pireps/prefile';
$response = $this->post($uri, $pirep);
@@ -418,16 +463,9 @@ class AcarsTest extends TestCase
*/
public function testDuplicatePirep()
{
$this->user = factory(App\Models\User::class)->create();
$pirep = $this->createPirep()->toArray();
$uri = '/api/pireps/prefile';
$this->user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make([
'airline_id' => $this->user->airline_id,
'user_id' => $this->user->id,
])->toArray();
$response = $this->post($uri, $pirep);
$response->assertStatus(201);
$pirep_id = $response->json()['data']['id'];

View File

@@ -10,13 +10,15 @@ use App\Models\Enums\PirepState;
class PIREPTest extends TestCase
{
protected $pirepSvc;
protected $pirepSvc, $settingsRepo;
public function setUp()
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->addData('base');
$this->pirepSvc = app('App\Services\PIREPService');
$this->settingsRepo = app(SettingRepository::class);
}
protected function createNewRoute()
@@ -246,8 +248,7 @@ class PIREPTest extends TestCase
*/
public function testCancelViaAPI()
{
$this->user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make(['id'=>''])->toArray();
$pirep = $this->createPirep()->toArray();
$uri = '/api/pireps/prefile';
$response = $this->post($uri, $pirep);

View File

@@ -1,13 +1,15 @@
<?php
use App\Services\DatabaseService;
use Tests\TestData;
/**
* Class TestCase
*/
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
use TestData;
/**
* The base URL to use while testing the application.
*

View File

@@ -5,15 +5,34 @@
namespace Tests;
class TestData
trait TestData
{
/**
* Create a new PIREP with a proper subfleet/rank/user and an
* aircraft that the user is allowed to fly
* @return \App\Models\Pirep
*/
protected function createPirep()
{
$subfleet = $this->createSubfleetWithAircraft(2);
$rank = $this->createRank(10, [$subfleet['subfleet']->id]);
$this->user = factory(\App\Models\User::class)->create([
'rank_id' => $rank->id
]);
// Return a Pirep model
return factory(\App\Models\Pirep::class)->make([
'aircraft_id' => $subfleet['aircraft']->random()->id
]);
}
/**
* Create a rank and associate the given subfleet IDs with it
* @param int $hours
* @param array $subfleet_ids
* @return mixed
*/
public static function createRank($hours=0, $subfleet_ids=[])
public function createRank($hours=0, array $subfleet_ids)
{
$attrs = [];
@@ -32,9 +51,10 @@ class TestData
/**
* Create a subfleet with a number of aircraft assigned
* @param null $aircraft_count
* @param null $airport_id
* @return mixed
*/
public static function createSubfleetWithAircraft($aircraft_count = null, $airport_id=null)
public function createSubfleetWithAircraft($aircraft_count = null, $airport_id=null)
{
$subfleet = factory(\App\Models\Subfleet::class)->create();

View File

@@ -25,10 +25,10 @@ class UserTest extends TestCase
{
# Add subfleets and aircraft, but also add another
# set of subfleets
$subfleetA = TestData::createSubfleetWithAircraft();
TestData::createSubfleetWithAircraft();
$subfleetA = $this->createSubfleetWithAircraft();
$this->createSubfleetWithAircraft();
$rank = TestData::createRank(10, [$subfleetA['subfleet']->id]);
$rank = $this->createRank(10, [$subfleetA['subfleet']->id]);
$user = factory(App\Models\User::class)->create([
'rank_id' => $rank->id,
@@ -81,15 +81,15 @@ class UserTest extends TestCase
{
# Add subfleets and aircraft, but also add another
# set of subfleets
$subfleetA = TestData::createSubfleetWithAircraft();
$subfleetB = TestData::createSubfleetWithAircraft();
$subfleetA = $this->createSubfleetWithAircraft();
$subfleetB = $this->createSubfleetWithAircraft();
$added_aircraft = array_merge(
$subfleetA['aircraft']->pluck('id')->toArray(),
$subfleetB['aircraft']->pluck('id')->toArray()
);
$rank = TestData::createRank(10, [$subfleetA['subfleet']->id]);
$rank = $this->createRank(10, [$subfleetA['subfleet']->id]);
$user = factory(App\Models\User::class)->create([
'rank_id' => $rank->id,
@@ -134,10 +134,10 @@ class UserTest extends TestCase
# Add subfleets and aircraft, but also add another
# set of subfleets
$airport = factory(App\Models\Airport::class)->create();
$subfleetA = TestData::createSubfleetWithAircraft(2, $airport->id);
$subfleetB = TestData::createSubfleetWithAircraft(2);
$subfleetA = $this->createSubfleetWithAircraft(2, $airport->id);
$subfleetB = $this->createSubfleetWithAircraft(2);
$rank = TestData::createRank(10, [$subfleetA['subfleet']->id]);
$rank = $this->createRank(10, [$subfleetA['subfleet']->id]);
$user = factory(App\Models\User::class)->create([
'curr_airport_id' => $airport->id,
'rank_id' => $rank->id,