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

View File

@ -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),
];
});

View File

@ -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)) {

View File

@ -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();
}

View File

@ -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);
}
}

View File

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

View File

@ -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);
}
}

View File

@ -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,
];
}

View File

@ -18,6 +18,7 @@ users:
home_airport_id: KAUS
curr_airport_id: KAUS
rank_id: 1
state: 1
created_at: now
updated_at: now