Skip to content

Commit

Permalink
Refuse pirep prefile is user not allowed to use aircraft #170
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Feb 20, 2018
1 parent 8393ae2 commit 4e43223
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 42 deletions.
21 changes: 18 additions & 3 deletions app/Http/Controllers/Api/PirepController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Requests\Acars\EventRequest;
use App\Http\Requests\Acars\UpdateRequest;
use App\Models\Enums\PirepSource;
use App\Services\UserService;
use Auth;
use Log;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -40,7 +41,8 @@ class PirepController extends RestController
protected $acarsRepo,
$geoSvc,
$pirepRepo,
$pirepSvc;
$pirepSvc,
$userSvc;

/**
* PirepController constructor.
Expand All @@ -53,12 +55,14 @@ public function __construct(
AcarsRepository $acarsRepo,
GeoService $geoSvc,
PirepRepository $pirepRepo,
PIREPService $pirepSvc
PIREPService $pirepSvc,
UserService $userSvc
) {
$this->acarsRepo = $acarsRepo;
$this->geoSvc = $geoSvc;
$this->pirepRepo = $pirepRepo;
$this->pirepSvc = $pirepSvc;
$this->userSvc = $userSvc;
}

/**
Expand Down Expand Up @@ -111,19 +115,30 @@ protected function updateFields($pirep, Request $request)
*
* @param PrefileRequest $request
* @return PirepResource
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
public function prefile(PrefileRequest $request)
{
Log::info('PIREP Prefile, user '.Auth::id(), $request->post());

$user = Auth::user();

$attrs = $request->post();
$attrs['user_id'] = Auth::id();
$attrs['user_id'] = $user->id;
$attrs['source'] = PirepSource::ACARS;
$attrs['state'] = PirepState::IN_PROGRESS;
$attrs['status'] = PirepStatus::PREFILE;

$pirep = new Pirep($attrs);

# See if this user is allowed to fly this aircraft
if(setting('pireps.restrict_aircraft_to_rank', false)) {
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
if (!$can_use_ac) {
throw new BadRequestHttpException('User is not allowed to fly this aircraft');
}
}

# Find if there's a duplicate, if so, let's work on that
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
if($dupe_pirep !== false) {
Expand Down
5 changes: 3 additions & 2 deletions app/Models/Aircraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class Aircraft extends BaseModel
* @var array
*/
protected $casts = [
'zfw' => 'float',
'active' => 'boolean',
'subfleet_id' => 'integer',
'zfw' => 'float',
'active' => 'boolean',
];

/**
Expand Down
20 changes: 19 additions & 1 deletion app/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Services;

use App\Repositories\AircraftRepository;
use App\Repositories\SubfleetRepository;
use Illuminate\Support\Collection;
use Log;
Expand All @@ -16,15 +17,17 @@

class UserService extends BaseService
{
protected $subfleetRepo;
protected $aircraftRepo, $subfleetRepo;

/**
* UserService constructor.
* @param SubfleetRepository $subfleetRepo
*/
public function __construct(
AircraftRepository $aircraftRepo,
SubfleetRepository $subfleetRepo
) {
$this->aircraftRepo = $aircraftRepo;
$this->subfleetRepo = $subfleetRepo;
}

Expand Down Expand Up @@ -83,6 +86,21 @@ public function getAllowableSubfleets($user)
return $subfleets->with('aircraft')->get();
}

/**
* Return a bool if a user is allowed to fly the current aircraft
* @param $user
* @param $aircraft_id
* @return bool
*/
public function aircraftAllowed($user, $aircraft_id)
{
$aircraft = $this->aircraftRepo->find($aircraft_id, ['subfleet_id']);
$subfleets = $this->getAllowableSubfleets($user);
$subfleet_ids = $subfleets->pluck('id')->toArray();

return \in_array($aircraft->subfleet_id, $subfleet_ids, true);
}

/**
* Change the user's state. PENDING to ACCEPTED, etc
* Send out an email
Expand Down
78 changes: 58 additions & 20 deletions tests/AcarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Models\Enums\PirepState;
use App\Models\Enums\PirepStatus;
use Tests\TestData;

/**
* Test API calls and authentication, etc
Expand Down Expand Up @@ -88,11 +89,16 @@ public function testPrefileErrors()
*/
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 = [
Expand Down Expand Up @@ -217,13 +223,56 @@ public function testAcarsUpdates()
$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);
Expand Down Expand Up @@ -275,8 +324,7 @@ public function testNonExistentPirepStore()
*/
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);
Expand All @@ -298,8 +346,7 @@ public function testAcarsIsoDate()
*/
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);
Expand All @@ -321,8 +368,7 @@ public function testAcarsInvalidRoutePost()

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);
Expand Down Expand Up @@ -362,8 +408,7 @@ public function testAcarsLogPost()
*/
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);
Expand Down Expand Up @@ -418,16 +463,9 @@ public function testAcarsRoutePost()
*/
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'];
Expand Down
7 changes: 4 additions & 3 deletions tests/PIREPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

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()
Expand Down Expand Up @@ -246,8 +248,7 @@ public function testDuplicatePireps()
*/
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);
Expand Down
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down
26 changes: 23 additions & 3 deletions tests/TestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -32,9 +51,10 @@ public static function createRank($hours=0, $subfleet_ids=[])
/**
* 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();

Expand Down
Loading

0 comments on commit 4e43223

Please sign in to comment.