Change tests to use generated user

This commit is contained in:
Nabeel Shahzad
2018-01-04 21:05:26 -06:00
parent 265fb235f2
commit db3c60987e
8 changed files with 60 additions and 40 deletions
+1
View File
@@ -18,6 +18,7 @@ $factory->define(App\Models\User::class, function (Faker $faker)
'rank_id' => 1, 'rank_id' => 1,
'flights' => $faker->numberBetween(0, 1000), 'flights' => $faker->numberBetween(0, 1000),
'flight_time' => $faker->numberBetween(0, 10000), 'flight_time' => $faker->numberBetween(0, 10000),
'state' => UserState::ACTIVE,
'remember_token' => $faker->unique()->text(5), 'remember_token' => $faker->unique()->text(5),
]; ];
}); });
+1 -1
View File
@@ -42,7 +42,7 @@ class RestController
if($request instanceof Request) { if($request instanceof Request) {
if ($request->filled($field)) { if ($request->filled($field)) {
$fields[$field] = $request->get($field); $fields[$field] = $request->input($field);
} }
} else { } else {
if(array_key_exists($field, $request)) { if(array_key_exists($field, $request)) {
+38 -24
View File
@@ -48,7 +48,8 @@ class AcarsTest extends TestCase
protected function getPirep($pirep_id) protected function getPirep($pirep_id)
{ {
$resp = $this->withHeaders($this->apiHeaders()) $user = factory(App\Models\User::class)->create();
$resp = $this->withHeaders($this->headers($user))
->get('/api/pireps/' . $pirep_id); ->get('/api/pireps/' . $pirep_id);
$resp->assertStatus(200); $resp->assertStatus(200);
return $resp->json(); return $resp->json();
@@ -59,6 +60,8 @@ class AcarsTest extends TestCase
*/ */
public function testAcarsUpdates() public function testAcarsUpdates()
{ {
$user = factory(App\Models\User::class)->create();
$airport = factory(App\Models\Airport::class)->create(); $airport = factory(App\Models\Airport::class)->create();
$airline = factory(App\Models\Airline::class)->create(); $airline = factory(App\Models\Airline::class)->create();
$aircraft = factory(App\Models\Aircraft::class)->create(); $aircraft = factory(App\Models\Aircraft::class)->create();
@@ -75,7 +78,7 @@ class AcarsTest extends TestCase
'route' => 'POINTA POINTB', 'route' => 'POINTA POINTB',
]; ];
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); $response = $this->withHeaders($this->headers($user))->post($uri, $pirep);
$response->assertStatus(201); $response->assertStatus(201);
# Get the PIREP ID # Get the PIREP ID
@@ -92,7 +95,7 @@ class AcarsTest extends TestCase
# Test missing positions field # Test missing positions field
# Post an ACARS update # Post an ACARS update
$update = []; $update = [];
$response = $this->withHeaders($this->apiHeaders())->post($uri, $update); $response = $this->withHeaders($this->headers($user))->post($uri, $update);
$response->assertStatus(400); $response->assertStatus(400);
# Post an ACARS update # Post an ACARS update
@@ -100,7 +103,7 @@ class AcarsTest extends TestCase
unset($acars['id']); unset($acars['id']);
$update = ['positions' => [$acars]]; $update = ['positions' => [$acars]];
$response = $this->withHeaders($this->apiHeaders())->post($uri, $update); $response = $this->withHeaders($this->headers($user))->post($uri, $update);
$response->assertStatus(200)->assertJson(['count' => 1]); $response->assertStatus(200)->assertJson(['count' => 1]);
# Make sure PIREP state moved into ENROUTE # Make sure PIREP state moved into ENROUTE
@@ -108,7 +111,7 @@ class AcarsTest extends TestCase
$this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']); $this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']);
$this->assertEquals(PirepStatus::ENROUTE, $pirep['status']); $this->assertEquals(PirepStatus::ENROUTE, $pirep['status']);
$response = $this->withHeaders($this->apiHeaders())->get($uri); $response = $this->withHeaders($this->headers($user))->get($uri);
$response->assertStatus(200); $response->assertStatus(200);
$body = $response->json(); $body = $response->json();
@@ -123,10 +126,11 @@ class AcarsTest extends TestCase
*/ */
public function testMultipleAcarsPositionUpdates() public function testMultipleAcarsPositionUpdates()
{ {
$user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make()->toArray(); $pirep = factory(App\Models\Pirep::class)->make()->toArray();
$uri = '/api/pireps/prefile'; $uri = '/api/pireps/prefile';
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); $response = $this->withHeaders($this->headers($user))->post($uri, $pirep);
$response->assertStatus(201); $response->assertStatus(201);
$pirep_id = $response->json()['id']; $pirep_id = $response->json()['id'];
@@ -138,10 +142,10 @@ class AcarsTest extends TestCase
$acars = factory(App\Models\Acars::class, $acars_count)->make(['id'=>''])->toArray(); $acars = factory(App\Models\Acars::class, $acars_count)->make(['id'=>''])->toArray();
$update = ['positions' => $acars]; $update = ['positions' => $acars];
$response = $this->withHeaders($this->apiHeaders())->post($uri, $update); $response = $this->withHeaders($this->headers($user))->post($uri, $update);
$response->assertStatus(200)->assertJson(['count' => $acars_count]); $response->assertStatus(200)->assertJson(['count' => $acars_count]);
$response = $this->withHeaders($this->apiHeaders())->get($uri); $response = $this->withHeaders($this->headers($user))->get($uri);
$response->assertStatus(200)->assertJsonCount($acars_count); $response->assertStatus(200)->assertJsonCount($acars_count);
} }
@@ -150,8 +154,10 @@ class AcarsTest extends TestCase
*/ */
public function testNonExistentPirepGet() public function testNonExistentPirepGet()
{ {
$user = factory(App\Models\User::class)->create();
$uri = '/api/pireps/DOESNTEXIST/acars'; $uri = '/api/pireps/DOESNTEXIST/acars';
$response = $this->withHeaders($this->apiHeaders())->get($uri); $response = $this->withHeaders($this->headers($user))->get($uri);
$response->assertStatus(404); $response->assertStatus(404);
} }
@@ -160,9 +166,11 @@ class AcarsTest extends TestCase
*/ */
public function testNonExistentPirepStore() public function testNonExistentPirepStore()
{ {
$user = factory(App\Models\User::class)->create();
$uri = '/api/pireps/DOESNTEXIST/acars/position'; $uri = '/api/pireps/DOESNTEXIST/acars/position';
$acars = factory(App\Models\Acars::class)->make()->toArray(); $acars = factory(App\Models\Acars::class)->make()->toArray();
$response = $this->withHeaders($this->apiHeaders())->post($uri, $acars); $response = $this->withHeaders($this->headers($user))->post($uri, $acars);
$response->assertStatus(404); $response->assertStatus(404);
} }
@@ -171,10 +179,11 @@ class AcarsTest extends TestCase
*/ */
public function testAcarsIsoDate() public function testAcarsIsoDate()
{ {
$user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make()->toArray(); $pirep = factory(App\Models\Pirep::class)->make()->toArray();
$uri = '/api/pireps/prefile'; $uri = '/api/pireps/prefile';
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); $response = $this->withHeaders($this->headers($user))->post($uri, $pirep);
$pirep_id = $response->json()['id']; $pirep_id = $response->json()['id'];
$dt = date('c'); $dt = date('c');
@@ -184,7 +193,7 @@ class AcarsTest extends TestCase
])->toArray(); ])->toArray();
$update = ['positions' => [$acars]]; $update = ['positions' => [$acars]];
$response = $this->withHeaders($this->apiHeaders())->post($uri, $update); $response = $this->withHeaders($this->headers($user))->post($uri, $update);
$response->assertStatus(200); $response->assertStatus(200);
} }
@@ -193,15 +202,16 @@ class AcarsTest extends TestCase
*/ */
public function testAcarsInvalidRoutePost() public function testAcarsInvalidRoutePost()
{ {
$user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make()->toArray(); $pirep = factory(App\Models\Pirep::class)->make()->toArray();
$uri = '/api/pireps/prefile'; $uri = '/api/pireps/prefile';
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); $response = $this->withHeaders($this->headers($user))->post($uri, $pirep);
$pirep_id = $response->json()['id']; $pirep_id = $response->json()['id'];
$post_route = ['order' => 1, 'name' => 'NAVPOINT']; $post_route = ['order' => 1, 'name' => 'NAVPOINT'];
$uri = '/api/pireps/' . $pirep_id . '/route'; $uri = '/api/pireps/' . $pirep_id . '/route';
$response = $this->withHeaders($this->apiHeaders())->post($uri, $post_route); $response = $this->withHeaders($this->headers($user))->post($uri, $post_route);
$response->assertStatus(400); $response->assertStatus(400);
$post_route = [ $post_route = [
@@ -209,16 +219,17 @@ class AcarsTest extends TestCase
]; ];
$uri = '/api/pireps/' . $pirep_id . '/route'; $uri = '/api/pireps/' . $pirep_id . '/route';
$response = $this->withHeaders($this->apiHeaders())->post($uri, $post_route); $response = $this->withHeaders($this->headers($user))->post($uri, $post_route);
$response->assertStatus(400); $response->assertStatus(400);
} }
public function testAcarsLogPost() public function testAcarsLogPost()
{ {
$user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make()->toArray(); $pirep = factory(App\Models\Pirep::class)->make()->toArray();
$uri = '/api/pireps/prefile'; $uri = '/api/pireps/prefile';
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); $response = $this->withHeaders($this->headers($user))->post($uri, $pirep);
$pirep_id = $response->json()['id']; $pirep_id = $response->json()['id'];
$acars = factory(App\Models\Acars::class)->make(); $acars = factory(App\Models\Acars::class)->make();
@@ -229,7 +240,7 @@ class AcarsTest extends TestCase
]; ];
$uri = '/api/pireps/' . $pirep_id . '/acars/log'; $uri = '/api/pireps/' . $pirep_id . '/acars/log';
$response = $this->withHeaders($this->apiHeaders())->post($uri, $post_log); $response = $this->withHeaders($this->headers($user))->post($uri, $post_log);
$response->assertStatus(200); $response->assertStatus(200);
$body = $response->json(); $body = $response->json();
@@ -241,10 +252,11 @@ class AcarsTest extends TestCase
*/ */
public function testAcarsRoutePost() public function testAcarsRoutePost()
{ {
$user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make()->toArray(); $pirep = factory(App\Models\Pirep::class)->make()->toArray();
$uri = '/api/pireps/prefile'; $uri = '/api/pireps/prefile';
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); $response = $this->withHeaders($this->headers($user))->post($uri, $pirep);
$pirep_id = $response->json()['id']; $pirep_id = $response->json()['id'];
$order = 1; $order = 1;
@@ -264,7 +276,7 @@ class AcarsTest extends TestCase
} }
$uri = '/api/pireps/'.$pirep_id.'/route'; $uri = '/api/pireps/'.$pirep_id.'/route';
$response = $this->withHeaders($this->apiHeaders())->post($uri, ['route' => $post_route]); $response = $this->withHeaders($this->headers($user))->post($uri, ['route' => $post_route]);
$response->assertStatus(200)->assertJsonCount($route_count); $response->assertStatus(200)->assertJsonCount($route_count);
$body = $response->json(); $body = $response->json();
@@ -275,7 +287,7 @@ class AcarsTest extends TestCase
*/ */
$uri = '/api/pireps/' . $pirep_id . '/route'; $uri = '/api/pireps/' . $pirep_id . '/route';
$response = $this->withHeaders($this->apiHeaders())->get($uri); $response = $this->withHeaders($this->headers($user))->get($uri);
$response->assertStatus(200)->assertJsonCount($route_count); $response->assertStatus(200)->assertJsonCount($route_count);
$body = $response->json(); $body = $response->json();
$this->allPointsInRoute($post_route, $body); $this->allPointsInRoute($post_route, $body);
@@ -284,11 +296,11 @@ class AcarsTest extends TestCase
* Delete and then recheck * Delete and then recheck
*/ */
$uri = '/api/pireps/' . $pirep_id . '/route'; $uri = '/api/pireps/' . $pirep_id . '/route';
$response = $this->withHeaders($this->apiHeaders())->delete($uri); $response = $this->withHeaders($this->headers($user))->delete($uri);
$response->assertStatus(200); $response->assertStatus(200);
$uri = '/api/pireps/' . $pirep_id . '/route'; $uri = '/api/pireps/' . $pirep_id . '/route';
$response = $this->withHeaders($this->apiHeaders())->get($uri); $response = $this->withHeaders($this->headers($user))->get($uri);
$response->assertStatus(200)->assertJsonCount(0); $response->assertStatus(200)->assertJsonCount(0);
} }
@@ -297,6 +309,8 @@ class AcarsTest extends TestCase
*/ */
public function testDuplicatePirep() public function testDuplicatePirep()
{ {
$user = factory(App\Models\User::class)->create();
$uri = '/api/pireps/prefile'; $uri = '/api/pireps/prefile';
$user = factory(App\Models\User::class)->create(); $user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make([ $pirep = factory(App\Models\Pirep::class)->make([
@@ -305,11 +319,11 @@ class AcarsTest extends TestCase
'user_id' => $user->id, 'user_id' => $user->id,
])->toArray(); ])->toArray();
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); $response = $this->withHeaders($this->headers($user))->post($uri, $pirep);
$response->assertStatus(201); $response->assertStatus(201);
$pirep = $response->json(); $pirep = $response->json();
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); $response = $this->withHeaders($this->headers($user))->post($uri, $pirep);
$response->assertStatus(200); $response->assertStatus(200);
$body = $response->json(); $body = $response->json();
} }
+6 -2
View File
@@ -70,13 +70,17 @@ class ApiTest extends TestCase
*/ */
public function testAirportRequest() public function testAirportRequest()
{ {
$user = factory(App\Models\User::class)->create();
$airport = factory(App\Models\Airport::class)->create(); $airport = factory(App\Models\Airport::class)->create();
$response = $this->withHeaders($this->apiHeaders())->get('/api/airports/' . $airport->icao); $response = $this->withHeaders($this->headers($user))
->get('/api/airports/' . $airport->icao);
$response->assertStatus(200); $response->assertStatus(200);
$response->assertJson(['icao' => $airport->icao], true); $response->assertJson(['icao' => $airport->icao], true);
$this->withHeaders($this->apiHeaders())->get('/api/airports/UNK') $this->withHeaders($this->headers($user))
->get('/api/airports/UNK')
->assertStatus(404); ->assertStatus(404);
} }
} }
+2 -6
View File
@@ -61,9 +61,7 @@ class FlightTest extends TestCase
public function testBids() public function testBids()
{ {
$user = factory(User::class)->create(); $user = factory(User::class)->create();
$headers = [ $headers = $this->headers($user);
'x-api-key' => $user->api_key,
];
$flight = $this->addFlight(); $flight = $this->addFlight();
@@ -148,9 +146,7 @@ class FlightTest extends TestCase
public function testDeleteFlight() public function testDeleteFlight()
{ {
$user = factory(User::class)->create(); $user = factory(User::class)->create();
$headers = [ $headers = $this->headers($user);
'x-api-key' => $user->api_key,
];
$flight = $this->addFlight(); $flight = $this->addFlight();
+7 -4
View File
@@ -166,6 +166,7 @@ class PIREPTest extends TestCase
*/ */
public function testDuplicatePireps() public function testDuplicatePireps()
{ {
$user = factory(App\Models\User::class)->create();
$pirep = factory(Pirep::class)->create(); $pirep = factory(Pirep::class)->create();
# This should find itself... # This should find itself...
@@ -189,14 +190,16 @@ class PIREPTest extends TestCase
public function testCancelViaAPI() public function testCancelViaAPI()
{ {
$user = factory(App\Models\User::class)->create();
$pirep = factory(App\Models\Pirep::class)->make(['id'=>''])->toArray(); $pirep = factory(App\Models\Pirep::class)->make(['id'=>''])->toArray();
$uri = '/api/pireps/prefile'; $uri = '/api/pireps/prefile';
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); $response = $this->withHeaders($this->headers($user))->post($uri, $pirep);
$pirep_id = $response->json()['id']; $pirep_id = $response->json()['id'];
$uri = '/api/pireps/' . $pirep_id . '/acars/position'; $uri = '/api/pireps/' . $pirep_id . '/acars/position';
$acars = factory(App\Models\Acars::class)->make()->toArray(); $acars = factory(App\Models\Acars::class)->make()->toArray();
$response = $this->withHeaders($this->apiHeaders())->post($uri, [ $response = $this->withHeaders($this->headers($user))->post($uri, [
'positions' => [$acars] 'positions' => [$acars]
]); ]);
@@ -204,13 +207,13 @@ class PIREPTest extends TestCase
# Cancel it # Cancel it
$uri = '/api/pireps/' . $pirep_id . '/cancel'; $uri = '/api/pireps/' . $pirep_id . '/cancel';
$response = $this->withHeaders($this->apiHeaders())->delete($uri, $acars); $response = $this->withHeaders($this->headers($user))->delete($uri, $acars);
$response->assertStatus(200); $response->assertStatus(200);
# Should get a 400 when posting an ACARS update # Should get a 400 when posting an ACARS update
$uri = '/api/pireps/' . $pirep_id . '/acars/position'; $uri = '/api/pireps/' . $pirep_id . '/acars/position';
$acars = factory(App\Models\Acars::class)->make()->toArray(); $acars = factory(App\Models\Acars::class)->make()->toArray();
$response = $this->withHeaders($this->apiHeaders())->post($uri, $acars); $response = $this->withHeaders($this->headers($user))->post($uri, $acars);
$response->assertStatus(400); $response->assertStatus(400);
} }
} }
+4 -3
View File
@@ -27,11 +27,12 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
return self::$auth_headers; return self::$auth_headers;
} }
public function headers($api_key) public function headers($user)
{ {
return [ return [
'content-type' => 'application/json', #'accept' => 'application/json',
'x-api-key' => $api_key #'content-type' => 'application/json',
'x-api-key' => $user->api_key,
]; ];
} }
+1
View File
@@ -18,6 +18,7 @@ users:
home_airport_id: KAUS home_airport_id: KAUS
curr_airport_id: KAUS curr_airport_id: KAUS
rank_id: 1 rank_id: 1
state: 1
created_at: now created_at: now
updated_at: now updated_at: now