Permalink
Browse files

fix bug where aircraft restrictions aren't respected in flight calls #…

  • Loading branch information...
nabeelio committed Feb 9, 2018
1 parent 71189e4 commit f6b2102e4827da6177eb4eee0c3ce0d38eb78ce3
View
@@ -15,6 +15,8 @@
- PIREP fields being set when filing manually is working
- Field for the rank's image changed to string input
- API: Fixed typo from `subfleet` to `subfleets` in the `/api/flights` call(s)
- API: Subfleets returned in the flight calls respect the `pireps.restrict_aircraft_to_rank` setting
***
@@ -2,20 +2,52 @@
namespace App\Http\Controllers\Api;
use Auth;
use Illuminate\Http\Request;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException;
use App\Services\UserService;
use App\Repositories\FlightRepository;
use App\Http\Resources\Flight as FlightResource;
use Prettus\Repository\Exceptions\RepositoryException;
/**
* Class FlightController
* @package App\Http\Controllers\Api
*/
class FlightController extends RestController
{
protected $flightRepo;
protected $flightRepo, $userSvc;
public function __construct(FlightRepository $flightRepo) {
public function __construct(
FlightRepository $flightRepo,
UserService $userSvc
) {
$this->flightRepo = $flightRepo;
$this->userSvc = $userSvc;
}
/**
* Filter out subfleets to only include aircraft that a user has access to
* @param $user
* @param $flight
* @return mixed
*/
public function filterSubfleets($user, $flight)
{
if(setting('pireps.restrict_aircraft_to_rank', false) === false) {
return $flight;
}
$allowed_subfleets = $this->userSvc->getAllowableSubfleets($user)->pluck('id');
$flight->subfleets = $flight->subfleets->filter(
function($subfleet, $item) use ($allowed_subfleets) {
if ($allowed_subfleets->contains($subfleet->id)) {
return true;
}
});
return $flight;
}
/**
@@ -27,12 +59,23 @@ public function index(Request $request)
->orderBy('flight_number', 'asc')
->paginate(50);
$user = Auth::user();
foreach($flights as $flight) {
$this->filterSubfleets($user, $flight);
}
return FlightResource::collection($flights);
}
/**
* @param $id
* @return FlightResource
*/
public function get($id)
{
$flight = $this->flightRepo->find($id);
$this->filterSubfleets(Auth::user(), $flight);
FlightResource::withoutWrapping();
return new FlightResource($flight);
}
@@ -51,6 +94,11 @@ public function search(Request $request)
return response($e, 503);
}
$user = Auth::user();
foreach ($flights as $flight) {
$this->filterSubfleets($user, $flight);
}
return FlightResource::collection($flights);
}
}
@@ -11,7 +11,7 @@ public function toArray($request)
$flight = parent::toArray($request);
$flight['airline'] = new Airline($this->airline);
$flight['subfleet'] = Subfleet::collection($this->subfleets);
$flight['subfleets'] = Subfleet::collection($this->subfleets);
return $flight;
}
@@ -67,6 +67,14 @@ public function retrieve($key)
}
}
/**
* @alias store($key,$value)
*/
public function save($key, $value)
{
return $this->store($key, $value);
}
/**
* Update an existing setting with a new value. Doesn't create
* a new setting
View
@@ -1,18 +1,22 @@
<?php
use App\Services\FlightService;
use App\Models\Flight;
use App\Models\User;
use App\Models\UserBid;
use App\Repositories\SettingRepository;
use App\Services\FlightService;
class FlightTest extends TestCase
{
protected $flightSvc, $settingsRepo;
public function setUp()
{
parent::setUp();
$this->addData('base');
$this->flightSvc = app(FlightService::class);
$this->settingsRepo = app(SettingRepository::class);
}
public function addFlight($user)
@@ -168,7 +172,7 @@ public function testBids()
*/
public function testMultipleBidsSingleFlight()
{
setting('bids.disable_flight_on_bid', true);
$this->settingsRepo->store('bids.disable_flight_on_bid', true);
$user1 = factory(User::class)->create();
$user2 = factory(User::class)->create([
@@ -224,11 +228,4 @@ public function testDeleteFlight()
$body = $req->json();
$this->assertEquals(0, sizeof($body));
}
public function testRestrictedFlights()
{
setting('bids.disable_flight_on_bid', true);
}
}
View
@@ -1,5 +1,6 @@
<?php
use App\Models\Setting;
use App\Services\UserService;
use App\Repositories\SettingRepository;
@@ -121,4 +122,68 @@ public function testGetAllAircraft()
$this->assertEquals($added_aircraft, $aircraft_from_api);
}
/**
* Flip the setting for getting all of the user's aircraft restricted
* by rank. Make sure that they're all returned. Create two subfleets,
* assign only one of them to the user's rank. When calling the api
* to retrieve the flight, only subfleetA should be showing
*/
public function testGetAircraftAllowedFromFlight()
{
# Add subfleets and aircraft, but also add another
# set of subfleets
$subfleetA = TestData::createSubfleetWithAircraft(2);
$subfleetB = TestData::createSubfleetWithAircraft(2);
$rank = TestData::createRank(10, [$subfleetA['subfleet']->id]);
$user = factory(App\Models\User::class)->create(['rank_id' => $rank->id,]);
$flight = factory(App\Models\Flight::class)->create(['airline_id' => $user->airline_id]);
$flight->subfleets()->syncWithoutDetaching([
$subfleetA['subfleet']->id,
$subfleetB['subfleet']->id
]);
/*
* Now we can do some actual tests
*/
/*
* Do some sanity checks first
*/
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false);
$response = $this->get('/api/flights/' . $flight->id, [], $user);
$response->assertStatus(200);
$this->assertCount(2, $response->json()['subfleets']);
/*
* Now make sure it's filtered out
*/
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', true);
/**
* Make sure it's filtered out from the single flight call
*/
$response = $this->get('/api/flights/' . $flight->id, [], $user);
$response->assertStatus(200);
$this->assertCount(1, $response->json()['subfleets']);
/**
* Make sure it's filtered out from the flight list
*/
$response = $this->get('/api/flights', [], $user);
$response->assertStatus(200);
$body = $response->json();
$this->assertCount(1, $body['data'][0]['subfleets']);
/**
* Filtered from search?
*/
$response = $this->get('/api/flights/search?flight_id=' . $flight->id, [], $user);
$response->assertStatus(200);
$body = $response->json();
$this->assertCount(1, $body['data'][0]['subfleets']);
}
}

0 comments on commit f6b2102

Please sign in to comment.