Skip to content

Commit

Permalink
fix bug where aircraft restrictions aren't respected in flight calls #…
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Feb 9, 2018
1 parent 71189e4 commit f6b2102
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

***

Expand Down
56 changes: 52 additions & 4 deletions app/Http/Controllers/Api/FlightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
}
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion app/Http/Resources/Flight.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions app/Repositories/SettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 6 additions & 9 deletions tests/FlightTest.php
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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);


}
}
65 changes: 65 additions & 0 deletions tests/UserTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Models\Setting;
use App\Services\UserService;
use App\Repositories\SettingRepository;

Expand Down Expand Up @@ -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.