diff --git a/app/Http/Controllers/Api/FlightController.php b/app/Http/Controllers/Api/FlightController.php index e5d8d122..e7dbb774 100644 --- a/app/Http/Controllers/Api/FlightController.php +++ b/app/Http/Controllers/Api/FlightController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Api; use Illuminate\Http\Request; +use Prettus\Repository\Criteria\RequestCriteria; use App\Repositories\FlightRepository; use App\Http\Resources\Flight as FlightResource; @@ -31,7 +32,9 @@ class FlightController extends RestController public function search(Request $request) { try { - $flights = $this->flightRepo->searchCriteria($request)->paginate(); + $this->flightRepo->searchCriteria($request); + $this->flightRepo->pushCriteria(new RequestCriteria($request)); + $flights = $this->flightRepo->paginate(); } catch (RepositoryException $e) { return response($e, 503); } diff --git a/tests/AcarsTest.php b/tests/AcarsTest.php index 67a4f07d..d8318cf8 100644 --- a/tests/AcarsTest.php +++ b/tests/AcarsTest.php @@ -48,8 +48,8 @@ class AcarsTest extends TestCase protected function getPirep($pirep_id) { - $user = factory(App\Models\User::class)->create(); - $resp = $this->withHeaders($this->headers($user)) + $this->user = factory(App\Models\User::class)->create(); + $resp = $this ->get('/api/pireps/' . $pirep_id); $resp->assertStatus(200); return $resp->json(); @@ -60,7 +60,7 @@ class AcarsTest extends TestCase */ public function testAcarsUpdates() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $airport = factory(App\Models\Airport::class)->create(); $airline = factory(App\Models\Airline::class)->create(); @@ -78,7 +78,7 @@ class AcarsTest extends TestCase 'route' => 'POINTA POINTB', ]; - $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); + $response = $this->post($uri, $pirep); $response->assertStatus(201); # Get the PIREP ID @@ -98,7 +98,7 @@ class AcarsTest extends TestCase # Test missing positions field # Post an ACARS update $update = []; - $response = $this->withHeaders($this->headers($user))->post($uri, $update); + $response = $this->post($uri, $update); $response->assertStatus(400); # Post an ACARS update @@ -106,7 +106,7 @@ class AcarsTest extends TestCase unset($acars['id']); $update = ['positions' => [$acars]]; - $response = $this->withHeaders($this->headers($user))->post($uri, $update); + $response = $this->post($uri, $update); $response->assertStatus(200)->assertJson(['count' => 1]); # Make sure PIREP state moved into ENROUTE @@ -114,7 +114,7 @@ class AcarsTest extends TestCase $this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']); $this->assertEquals(PirepStatus::ENROUTE, $pirep['status']); - $response = $this->withHeaders($this->headers($user))->get($uri); + $response = $this->get($uri); $response->assertStatus(200); $body = $response->json(); @@ -129,11 +129,11 @@ class AcarsTest extends TestCase */ public function testMultipleAcarsPositionUpdates() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make()->toArray(); $uri = '/api/pireps/prefile'; - $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); + $response = $this->post($uri, $pirep); $response->assertStatus(201); $pirep_id = $response->json()['id']; @@ -145,10 +145,10 @@ class AcarsTest extends TestCase $acars = factory(App\Models\Acars::class, $acars_count)->make(['id'=>''])->toArray(); $update = ['positions' => $acars]; - $response = $this->withHeaders($this->headers($user))->post($uri, $update); + $response = $this->post($uri, $update); $response->assertStatus(200)->assertJson(['count' => $acars_count]); - $response = $this->withHeaders($this->headers($user))->get($uri); + $response = $this->get($uri); $response->assertStatus(200)->assertJsonCount($acars_count); } @@ -157,10 +157,10 @@ class AcarsTest extends TestCase */ public function testNonExistentPirepGet() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $uri = '/api/pireps/DOESNTEXIST/acars'; - $response = $this->withHeaders($this->headers($user))->get($uri); + $response = $this->get($uri); $response->assertStatus(404); } @@ -169,11 +169,11 @@ class AcarsTest extends TestCase */ public function testNonExistentPirepStore() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $uri = '/api/pireps/DOESNTEXIST/acars/position'; $acars = factory(App\Models\Acars::class)->make()->toArray(); - $response = $this->withHeaders($this->headers($user))->post($uri, $acars); + $response = $this->post($uri, $acars); $response->assertStatus(404); } @@ -182,11 +182,11 @@ class AcarsTest extends TestCase */ public function testAcarsIsoDate() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make()->toArray(); $uri = '/api/pireps/prefile'; - $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); + $response = $this->post($uri, $pirep); $pirep_id = $response->json()['id']; $dt = date('c'); @@ -196,7 +196,7 @@ class AcarsTest extends TestCase ])->toArray(); $update = ['positions' => [$acars]]; - $response = $this->withHeaders($this->headers($user))->post($uri, $update); + $response = $this->post($uri, $update); $response->assertStatus(200); } @@ -205,16 +205,16 @@ class AcarsTest extends TestCase */ public function testAcarsInvalidRoutePost() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make()->toArray(); $uri = '/api/pireps/prefile'; - $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); + $response = $this->post($uri, $pirep); $pirep_id = $response->json()['id']; $post_route = ['order' => 1, 'name' => 'NAVPOINT']; $uri = '/api/pireps/' . $pirep_id . '/route'; - $response = $this->withHeaders($this->headers($user))->post($uri, $post_route); + $response = $this->post($uri, $post_route); $response->assertStatus(400); $post_route = [ @@ -222,17 +222,17 @@ class AcarsTest extends TestCase ]; $uri = '/api/pireps/' . $pirep_id . '/route'; - $response = $this->withHeaders($this->headers($user))->post($uri, $post_route); + $response = $this->post($uri, $post_route); $response->assertStatus(400); } public function testAcarsLogPost() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make()->toArray(); $uri = '/api/pireps/prefile'; - $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); + $response = $this->post($uri, $pirep); $pirep_id = $response->json()['id']; $acars = factory(App\Models\Acars::class)->make(); @@ -243,7 +243,7 @@ class AcarsTest extends TestCase ]; $uri = '/api/pireps/' . $pirep_id . '/acars/log'; - $response = $this->withHeaders($this->headers($user))->post($uri, $post_log); + $response = $this->post($uri, $post_log); $response->assertStatus(200); $body = $response->json(); @@ -255,11 +255,11 @@ class AcarsTest extends TestCase */ public function testAcarsRoutePost() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make()->toArray(); $uri = '/api/pireps/prefile'; - $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); + $response = $this->post($uri, $pirep); $pirep_id = $response->json()['id']; $order = 1; @@ -279,7 +279,7 @@ class AcarsTest extends TestCase } $uri = '/api/pireps/'.$pirep_id.'/route'; - $response = $this->withHeaders($this->headers($user))->post($uri, ['route' => $post_route]); + $response = $this->post($uri, ['route' => $post_route]); $response->assertStatus(200)->assertJsonCount($route_count); $body = $response->json(); @@ -290,7 +290,7 @@ class AcarsTest extends TestCase */ $uri = '/api/pireps/' . $pirep_id . '/route'; - $response = $this->withHeaders($this->headers($user))->get($uri); + $response = $this->get($uri); $response->assertStatus(200)->assertJsonCount($route_count); $body = $response->json(); $this->allPointsInRoute($post_route, $body); @@ -299,11 +299,11 @@ class AcarsTest extends TestCase * Delete and then recheck */ $uri = '/api/pireps/' . $pirep_id . '/route'; - $response = $this->withHeaders($this->headers($user))->delete($uri); + $response = $this->delete($uri); $response->assertStatus(200); $uri = '/api/pireps/' . $pirep_id . '/route'; - $response = $this->withHeaders($this->headers($user))->get($uri); + $response = $this->get($uri); $response->assertStatus(200)->assertJsonCount(0); } @@ -312,21 +312,21 @@ class AcarsTest extends TestCase */ public function testDuplicatePirep() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $uri = '/api/pireps/prefile'; - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make([ 'id' => '', - 'airline_id' => $user->airline_id, - 'user_id' => $user->id, + 'airline_id' => $this->user->airline_id, + 'user_id' => $this->user->id, ])->toArray(); - $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); + $response = $this->post($uri, $pirep); $response->assertStatus(201); $pirep = $response->json(); - $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); + $response = $this->post($uri, $pirep); $response->assertStatus(200); $body = $response->json(); } diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 5df63c35..f3b5562a 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -56,13 +56,12 @@ class ApiTest extends TestCase */ public function testApiDeniedOnInactiveUser() { - $user = factory(User::class)->create([ + $this->user = factory(User::class)->create([ 'state' => UserState::PENDING ]); $uri = '/api/user'; - $this->withHeaders(['x-api-key' => $user->api_key])->get($uri) - ->assertStatus(401); + $this->get($uri)->assertStatus(401); } /** @@ -70,18 +69,15 @@ class ApiTest extends TestCase */ public function testAirportRequest() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $airport = factory(App\Models\Airport::class)->create(); - $response = $this->withHeaders($this->headers($user)) - ->get('/api/airports/' . $airport->icao); + $response = $this->get('/api/airports/' . $airport->icao); $response->assertStatus(200); $response->assertJson(['icao' => $airport->icao], true); - $this->withHeaders($this->headers($user)) - ->get('/api/airports/UNK') - ->assertStatus(404); + $this->get('/api/airports/UNK')->assertStatus(404); } /** @@ -89,10 +85,9 @@ class ApiTest extends TestCase */ public function testGetAllAirports() { - $user = factory(App\Models\User::class)->create(); factory(App\Models\Airport::class, 70)->create(); - $response = $this->user_get($user, '/api/airports/') + $response = $this->get('/api/airports/') ->assertStatus(200) ->assertJsonCount(50, 'data'); @@ -101,19 +96,17 @@ class ApiTest extends TestCase $this->assertHasKeys($body, ['data', 'links', 'meta']); $last_page = $body['meta']['last_page']; - $this->user_get($user, '/api/airports?page='.$last_page) + $this->get('/api/airports?page=' . $last_page) ->assertStatus(200) ->assertJsonCount(20, 'data'); } public function testGetAllAirportsHubs() { - $user = factory(App\Models\User::class)->create(); - factory(App\Models\Airport::class, 10)->create(); factory(App\Models\Airport::class)->create(['hub' => 1]); - $this->user_get($user, '/api/airports/hubs') + $this->get('/api/airports/hubs') ->assertStatus(200) ->assertJsonCount(1, 'data'); } @@ -123,7 +116,6 @@ class ApiTest extends TestCase */ public function testGetSubfleets() { - $user = factory(App\Models\User::class)->create(); $subfleetA = factory(App\Models\Subfleet::class)->create(); $subfleetB = factory(App\Models\Subfleet::class)->create(); @@ -137,7 +129,7 @@ class ApiTest extends TestCase 'subfleet_id' => $subfleetB->id ]); - $response = $this->user_get($user, '/api/fleet'); + $response = $this->get('/api/fleet'); $response->assertStatus(200); $body = $response->json(); @@ -157,7 +149,6 @@ class ApiTest extends TestCase */ public function testGetAircraft() { - $user = factory(App\Models\User::class)->create(); $subfleet = factory(App\Models\Subfleet::class)->create(); $aircraft = factory(App\Models\Aircraft::class)->create([ 'subfleet_id' => $subfleet->id @@ -166,22 +157,19 @@ class ApiTest extends TestCase /** * Just try retrieving by ID */ - $resp = $this->user_get($user, '/api/fleet/aircraft/'. $aircraft->id); + $resp = $this->get('/api/fleet/aircraft/' . $aircraft->id); $body = $resp->json(); $this->assertEquals($body['id'], $aircraft->id); - $resp = $this->user_get($user, - '/api/fleet/aircraft/'.$aircraft->id.'?registration='.$aircraft->registration); + $resp = $this->get('/api/fleet/aircraft/' . $aircraft->id . '?registration=' . $aircraft->registration); $body = $resp->json(); $this->assertEquals($body['id'], $aircraft->id); - $resp = $this->user_get($user, - '/api/fleet/aircraft/' . $aircraft->id . '?tail_number=' . $aircraft->registration); + $resp = $this->get('/api/fleet/aircraft/' . $aircraft->id . '?tail_number=' . $aircraft->registration); $body = $resp->json(); $this->assertEquals($body['id'], $aircraft->id); - $resp = $this->user_get($user, - '/api/fleet/aircraft/' . $aircraft->id . '?icao=' . $aircraft->icao); + $resp = $this->get('/api/fleet/aircraft/' . $aircraft->id . '?icao=' . $aircraft->icao); $body = $resp->json(); $this->assertEquals($body['id'], $aircraft->id); } diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 053a710e..4e17c9ec 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -29,7 +29,7 @@ class FlightTest extends TestCase { $flight = $this->addFlight(); - $req = $this->get('/api/flights/'.$flight->id, self::$auth_headers); + $req = $this->get('/api/flights/' . $flight->id); $req->assertStatus(200); $body = $req->json(); @@ -50,10 +50,22 @@ class FlightTest extends TestCase # search specifically for a flight ID $query = 'flight_id=' . $flight->id; - $req = $this->get('/api/flights/search?' . $query, self::$auth_headers); + $req = $this->get('/api/flights/search?' . $query); $req->assertStatus(200); } + public function testFlightSearchApi() + { + $flights = factory(App\Models\Flight::class, 100)->create(); + $flight = $flights->random(); + + $query = 'flight_id=' . $flight->id; + $req = $this->get('/api/flights/search?' . $query); + $body = $req->json(); + + $this->assertEquals($flight->id, $body['data'][0]['id']); + } + /** * Add/remove a bid, test the API, etc * @throws \App\Services\Exception diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index 49958160..8bf7083b 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -167,7 +167,9 @@ class PIREPTest extends TestCase public function testDuplicatePireps() { $user = factory(App\Models\User::class)->create(); - $pirep = factory(Pirep::class)->create(); + $pirep = factory(Pirep::class)->create([ + 'user_id' => $user->id + ]); # This should find itself... $dupe_pirep = $this->pirepSvc->findDuplicate($pirep); @@ -190,16 +192,16 @@ class PIREPTest extends TestCase public function testCancelViaAPI() { - $user = factory(App\Models\User::class)->create(); + $this->user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make(['id'=>''])->toArray(); $uri = '/api/pireps/prefile'; - $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); + $response = $this->post($uri, $pirep); $pirep_id = $response->json()['id']; $uri = '/api/pireps/' . $pirep_id . '/acars/position'; $acars = factory(App\Models\Acars::class)->make()->toArray(); - $response = $this->withHeaders($this->headers($user))->post($uri, [ + $response = $this->post($uri, [ 'positions' => [$acars] ]); @@ -207,13 +209,13 @@ class PIREPTest extends TestCase # Cancel it $uri = '/api/pireps/' . $pirep_id . '/cancel'; - $response = $this->withHeaders($this->headers($user))->delete($uri, $acars); + $response = $this->delete($uri, $acars); $response->assertStatus(200); # Should get a 400 when posting an ACARS update $uri = '/api/pireps/' . $pirep_id . '/acars/position'; $acars = factory(App\Models\Acars::class)->make()->toArray(); - $response = $this->withHeaders($this->headers($user))->post($uri, $acars); + $response = $this->post($uri, $acars); $response->assertStatus(400); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 7a5f88e7..7e431b38 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,5 +1,6 @@ 'testadminapikey' ]; @@ -30,8 +33,6 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase public function headers($user) { return [ - #'accept' => 'application/json', - #'content-type' => 'application/json', 'x-api-key' => $user->api_key, ]; } @@ -59,7 +60,6 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase { $app = require __DIR__.'/../bootstrap/app.php'; $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); - //$app['config']->set('database.default','testing'); return $app; } @@ -97,13 +97,55 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase } /** - * Shortcut for a get call with a user - * @param \App\Models\User $user + * Override the GET call to inject the user API key * @param string $uri + * @param array $headers * @return \Illuminate\Foundation\Testing\TestResponse */ - public function user_get($user, $uri) + public function get($uri, array $headers=[]): \Illuminate\Foundation\Testing\TestResponse { - return $this->withHeaders($this->headers($user))->get($uri); + if(empty($headers)) { + if($this->user !== null) { + $headers = $this->headers($this->user); + } + } + + return parent::get($uri, $headers); + } + + /** + * Override the POST calls to inject the user API key + * @param string $uri + * @param array $data + * @param array $headers + * @return \Illuminate\Foundation\Testing\TestResponse + */ + public function post($uri, array $data = [], array $headers = []) + { + if (empty($headers)) { + if ($this->user !== null) { + $headers = $this->headers($this->user); + } + } + + return parent::post($uri, $data, $headers); + } + + /** + * Override the DELETE calls to inject the user API key + * @param string $uri + * @param array $data + * @param array $headers + * @return \Illuminate\Foundation\Testing\TestResponse + */ + public function delete($uri, array $data = [], array $headers = []) + { + if (empty($headers)) { + if ($this->user !== null) { + $headers = $this->headers($this->user); + } + } + + return parent::delete($uri, $data, $headers); } }