From db3c60987efce78c37b0b6b4834436c135a0054c Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Thu, 4 Jan 2018 21:05:26 -0600 Subject: [PATCH] Change tests to use generated user --- app/Database/factories/UserFactory.php | 1 + app/Http/Controllers/Api/RestController.php | 2 +- tests/AcarsTest.php | 62 +++++++++++++-------- tests/ApiTest.php | 8 ++- tests/FlightTest.php | 8 +-- tests/PIREPTest.php | 11 ++-- tests/TestCase.php | 7 ++- tests/data/base.yml | 1 + 8 files changed, 60 insertions(+), 40 deletions(-) diff --git a/app/Database/factories/UserFactory.php b/app/Database/factories/UserFactory.php index fb704e7e..498dd266 100644 --- a/app/Database/factories/UserFactory.php +++ b/app/Database/factories/UserFactory.php @@ -18,6 +18,7 @@ $factory->define(App\Models\User::class, function (Faker $faker) 'rank_id' => 1, 'flights' => $faker->numberBetween(0, 1000), 'flight_time' => $faker->numberBetween(0, 10000), + 'state' => UserState::ACTIVE, 'remember_token' => $faker->unique()->text(5), ]; }); diff --git a/app/Http/Controllers/Api/RestController.php b/app/Http/Controllers/Api/RestController.php index 85331494..4bf41a1c 100644 --- a/app/Http/Controllers/Api/RestController.php +++ b/app/Http/Controllers/Api/RestController.php @@ -42,7 +42,7 @@ class RestController if($request instanceof Request) { if ($request->filled($field)) { - $fields[$field] = $request->get($field); + $fields[$field] = $request->input($field); } } else { if(array_key_exists($field, $request)) { diff --git a/tests/AcarsTest.php b/tests/AcarsTest.php index 2e27b16a..bdd6e336 100644 --- a/tests/AcarsTest.php +++ b/tests/AcarsTest.php @@ -48,7 +48,8 @@ class AcarsTest extends TestCase 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); $resp->assertStatus(200); return $resp->json(); @@ -59,6 +60,8 @@ class AcarsTest extends TestCase */ public function testAcarsUpdates() { + $user = factory(App\Models\User::class)->create(); + $airport = factory(App\Models\Airport::class)->create(); $airline = factory(App\Models\Airline::class)->create(); $aircraft = factory(App\Models\Aircraft::class)->create(); @@ -75,7 +78,7 @@ class AcarsTest extends TestCase 'route' => 'POINTA POINTB', ]; - $response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); + $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); $response->assertStatus(201); # Get the PIREP ID @@ -92,7 +95,7 @@ class AcarsTest extends TestCase # Test missing positions field # Post an ACARS update $update = []; - $response = $this->withHeaders($this->apiHeaders())->post($uri, $update); + $response = $this->withHeaders($this->headers($user))->post($uri, $update); $response->assertStatus(400); # Post an ACARS update @@ -100,7 +103,7 @@ class AcarsTest extends TestCase unset($acars['id']); $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]); # Make sure PIREP state moved into ENROUTE @@ -108,7 +111,7 @@ class AcarsTest extends TestCase $this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']); $this->assertEquals(PirepStatus::ENROUTE, $pirep['status']); - $response = $this->withHeaders($this->apiHeaders())->get($uri); + $response = $this->withHeaders($this->headers($user))->get($uri); $response->assertStatus(200); $body = $response->json(); @@ -123,10 +126,11 @@ class AcarsTest extends TestCase */ public function testMultipleAcarsPositionUpdates() { + $user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make()->toArray(); $uri = '/api/pireps/prefile'; - $response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); + $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); $response->assertStatus(201); $pirep_id = $response->json()['id']; @@ -138,10 +142,10 @@ class AcarsTest extends TestCase $acars = factory(App\Models\Acars::class, $acars_count)->make(['id'=>''])->toArray(); $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 = $this->withHeaders($this->apiHeaders())->get($uri); + $response = $this->withHeaders($this->headers($user))->get($uri); $response->assertStatus(200)->assertJsonCount($acars_count); } @@ -150,8 +154,10 @@ class AcarsTest extends TestCase */ public function testNonExistentPirepGet() { + $user = factory(App\Models\User::class)->create(); + $uri = '/api/pireps/DOESNTEXIST/acars'; - $response = $this->withHeaders($this->apiHeaders())->get($uri); + $response = $this->withHeaders($this->headers($user))->get($uri); $response->assertStatus(404); } @@ -160,9 +166,11 @@ class AcarsTest extends TestCase */ public function testNonExistentPirepStore() { + $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->apiHeaders())->post($uri, $acars); + $response = $this->withHeaders($this->headers($user))->post($uri, $acars); $response->assertStatus(404); } @@ -171,10 +179,11 @@ class AcarsTest extends TestCase */ public function testAcarsIsoDate() { + $user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make()->toArray(); $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']; $dt = date('c'); @@ -184,7 +193,7 @@ class AcarsTest extends TestCase ])->toArray(); $update = ['positions' => [$acars]]; - $response = $this->withHeaders($this->apiHeaders())->post($uri, $update); + $response = $this->withHeaders($this->headers($user))->post($uri, $update); $response->assertStatus(200); } @@ -193,15 +202,16 @@ class AcarsTest extends TestCase */ public function testAcarsInvalidRoutePost() { + $user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make()->toArray(); $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']; $post_route = ['order' => 1, 'name' => 'NAVPOINT']; $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); $post_route = [ @@ -209,16 +219,17 @@ class AcarsTest extends TestCase ]; $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); } public function testAcarsLogPost() { + $user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make()->toArray(); $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']; $acars = factory(App\Models\Acars::class)->make(); @@ -229,7 +240,7 @@ class AcarsTest extends TestCase ]; $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); $body = $response->json(); @@ -241,10 +252,11 @@ class AcarsTest extends TestCase */ public function testAcarsRoutePost() { + $user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make()->toArray(); $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']; $order = 1; @@ -264,7 +276,7 @@ class AcarsTest extends TestCase } $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); $body = $response->json(); @@ -275,7 +287,7 @@ class AcarsTest extends TestCase */ $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); $body = $response->json(); $this->allPointsInRoute($post_route, $body); @@ -284,11 +296,11 @@ class AcarsTest extends TestCase * Delete and then recheck */ $uri = '/api/pireps/' . $pirep_id . '/route'; - $response = $this->withHeaders($this->apiHeaders())->delete($uri); + $response = $this->withHeaders($this->headers($user))->delete($uri); $response->assertStatus(200); $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); } @@ -297,6 +309,8 @@ class AcarsTest extends TestCase */ public function testDuplicatePirep() { + $user = factory(App\Models\User::class)->create(); + $uri = '/api/pireps/prefile'; $user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make([ @@ -305,11 +319,11 @@ class AcarsTest extends TestCase 'user_id' => $user->id, ])->toArray(); - $response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); + $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); $response->assertStatus(201); $pirep = $response->json(); - $response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); + $response = $this->withHeaders($this->headers($user))->post($uri, $pirep); $response->assertStatus(200); $body = $response->json(); } diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 576c2712..c85dc0c8 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -70,13 +70,17 @@ class ApiTest extends TestCase */ public function testAirportRequest() { + $user = factory(App\Models\User::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->assertJson(['icao' => $airport->icao], true); - $this->withHeaders($this->apiHeaders())->get('/api/airports/UNK') + $this->withHeaders($this->headers($user)) + ->get('/api/airports/UNK') ->assertStatus(404); } } diff --git a/tests/FlightTest.php b/tests/FlightTest.php index abb8bcc0..053a710e 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -61,9 +61,7 @@ class FlightTest extends TestCase public function testBids() { $user = factory(User::class)->create(); - $headers = [ - 'x-api-key' => $user->api_key, - ]; + $headers = $this->headers($user); $flight = $this->addFlight(); @@ -148,9 +146,7 @@ class FlightTest extends TestCase public function testDeleteFlight() { $user = factory(User::class)->create(); - $headers = [ - 'x-api-key' => $user->api_key, - ]; + $headers = $this->headers($user); $flight = $this->addFlight(); diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index 24ccc620..49958160 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -166,6 +166,7 @@ class PIREPTest extends TestCase */ public function testDuplicatePireps() { + $user = factory(App\Models\User::class)->create(); $pirep = factory(Pirep::class)->create(); # This should find itself... @@ -189,14 +190,16 @@ class PIREPTest extends TestCase public function testCancelViaAPI() { + $user = factory(App\Models\User::class)->create(); $pirep = factory(App\Models\Pirep::class)->make(['id'=>''])->toArray(); + $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']; $uri = '/api/pireps/' . $pirep_id . '/acars/position'; $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] ]); @@ -204,13 +207,13 @@ class PIREPTest extends TestCase # Cancel it $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); # 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->apiHeaders())->post($uri, $acars); + $response = $this->withHeaders($this->headers($user))->post($uri, $acars); $response->assertStatus(400); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 12051001..875e2fe3 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -27,11 +27,12 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase return self::$auth_headers; } - public function headers($api_key) + public function headers($user) { return [ - 'content-type' => 'application/json', - 'x-api-key' => $api_key + #'accept' => 'application/json', + #'content-type' => 'application/json', + 'x-api-key' => $user->api_key, ]; } diff --git a/tests/data/base.yml b/tests/data/base.yml index b539ad92..050eeb3b 100644 --- a/tests/data/base.yml +++ b/tests/data/base.yml @@ -18,6 +18,7 @@ users: home_airport_id: KAUS curr_airport_id: KAUS rank_id: 1 + state: 1 created_at: now updated_at: now