Skip to content

Commit

Permalink
Cleanup tests to automatically inject proper headers when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Jan 6, 2018
1 parent 60256ab commit 46a411e
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 78 deletions.
5 changes: 4 additions & 1 deletion app/Http/Controllers/Api/FlightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,7 +32,9 @@ public function get($id)
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);
}
Expand Down
74 changes: 37 additions & 37 deletions tests/AcarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ protected function allPointsInRoute($route, $points, $addtl_fields=[])

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();
Expand All @@ -60,7 +60,7 @@ protected function getPirep($pirep_id)
*/
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();
Expand All @@ -78,7 +78,7 @@ public function testAcarsUpdates()
'route' => 'POINTA POINTB',
];

$response = $this->withHeaders($this->headers($user))->post($uri, $pirep);
$response = $this->post($uri, $pirep);
$response->assertStatus(201);

# Get the PIREP ID
Expand All @@ -98,23 +98,23 @@ public function testAcarsUpdates()
# 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
$acars = factory(App\Models\Acars::class)->make()->toArray();
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
$pirep = $this->getPirep($pirep_id);
$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();

Expand All @@ -129,11 +129,11 @@ public function testAcarsUpdates()
*/
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'];
Expand All @@ -145,10 +145,10 @@ public function testMultipleAcarsPositionUpdates()
$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);
}

Expand All @@ -157,10 +157,10 @@ public function testMultipleAcarsPositionUpdates()
*/
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);
}

Expand All @@ -169,11 +169,11 @@ public function testNonExistentPirepGet()
*/
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);
}

Expand All @@ -182,11 +182,11 @@ public function testNonExistentPirepStore()
*/
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');
Expand All @@ -196,7 +196,7 @@ public function testAcarsIsoDate()
])->toArray();

$update = ['positions' => [$acars]];
$response = $this->withHeaders($this->headers($user))->post($uri, $update);
$response = $this->post($uri, $update);
$response->assertStatus(200);
}

Expand All @@ -205,34 +205,34 @@ public function testAcarsIsoDate()
*/
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 = [
['order' => 1, 'name' => 'NAVPOINT', 'lat' => 'notanumber', 'lon' => 34.11]
];

$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();
Expand All @@ -243,7 +243,7 @@ public function testAcarsLogPost()
];

$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();

Expand All @@ -255,11 +255,11 @@ public function testAcarsLogPost()
*/
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;
Expand All @@ -279,7 +279,7 @@ public function testAcarsRoutePost()
}

$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();
Expand All @@ -290,7 +290,7 @@ public function testAcarsRoutePost()
*/

$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);
Expand All @@ -299,11 +299,11 @@ public function testAcarsRoutePost()
* 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);
}

Expand All @@ -312,21 +312,21 @@ public function testAcarsRoutePost()
*/
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();
}
Expand Down
38 changes: 13 additions & 25 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,38 @@ public function testApiAuthentication()
*/
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);
}

/**
* Make sure the airport data is returned
*/
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);
}

/**
* Get all the airports, test the pagination
*/
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');

Expand All @@ -101,19 +96,17 @@ public function testGetAllAirports()
$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');
}
Expand All @@ -123,7 +116,6 @@ public function testGetAllAirportsHubs()
*/
public function testGetSubfleets()
{
$user = factory(App\Models\User::class)->create();
$subfleetA = factory(App\Models\Subfleet::class)->create();
$subfleetB = factory(App\Models\Subfleet::class)->create();

Expand All @@ -137,7 +129,7 @@ public function testGetSubfleets()
'subfleet_id' => $subfleetB->id
]);

$response = $this->user_get($user, '/api/fleet');
$response = $this->get('/api/fleet');
$response->assertStatus(200);
$body = $response->json();

Expand All @@ -157,7 +149,6 @@ public function testGetSubfleets()
*/
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
Expand All @@ -166,22 +157,19 @@ public function testGetAircraft()
/**
* 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);
}
Expand Down
Loading

0 comments on commit 46a411e

Please sign in to comment.