Major refactoring and cleanup of ACARS/Pirep API

This commit is contained in:
Nabeel Shahzad
2018-01-28 11:12:13 -06:00
parent 77d0d2bcd0
commit 653ff2a104
16 changed files with 387 additions and 231 deletions

View File

@@ -47,7 +47,6 @@ class AcarsTest extends TestCase
protected function getPirep($pirep_id)
{
$this->user = factory(App\Models\User::class)->create();
$resp = $this ->get('/api/pireps/' . $pirep_id);
$resp->assertStatus(200);
return $resp->json();
@@ -116,6 +115,7 @@ class AcarsTest extends TestCase
$this->assertHasKeys($body, ['airline', 'arr_airport', 'dpt_airport', 'position']);
$this->assertNotNull($pirep_id);
$this->assertEquals($body['user_id'], $this->user->id);
# Check the PIREP state and status
$pirep = $this->getPirep($pirep_id);
@@ -131,13 +131,20 @@ class AcarsTest extends TestCase
$response->assertStatus(400);
# Post an ACARS update
$acars = factory(App\Models\Acars::class)->make()->toArray();
unset($acars['id']);
$acars = factory(App\Models\Acars::class)->make([
'id' => null
])->toArray();
$update = ['positions' => [$acars]];
$response = $this->post($uri, $update);
$response->assertStatus(200)->assertJson(['count' => 1]);
# Read that if the ACARS record posted
$acars_data = $this->get($uri)->json()[0];
$this->assertEquals($acars['lat'], $acars_data['lat']);
$this->assertEquals($acars['lon'], $acars_data['lon']);
$this->assertEquals($acars['log'], $acars_data['log']);
# Make sure PIREP state moved into ENROUTE
$pirep = $this->getPirep($pirep_id);
$this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']);
@@ -161,7 +168,7 @@ class AcarsTest extends TestCase
$response->assertStatus(400); // invalid flight time
$response = $this->post($uri, ['flight_time' => '130']);
$response->assertStatus(200); // invalid flight time
$response->assertStatus(200);
# Add a comment
$uri = '/api/pireps/'.$pirep_id.'/comments';

View File

@@ -199,6 +199,9 @@ class PIREPTest extends TestCase
$this->assertFalse($dupe_pirep);
}
/**
*
*/
public function testCancelViaAPI()
{
$this->user = factory(App\Models\User::class)->create();
@@ -223,7 +226,10 @@ class PIREPTest extends TestCase
# Should get a 400 when posting an ACARS update
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
$acars = factory(App\Models\Acars::class)->make()->toArray();
$acars = factory(App\Models\Acars::class)->make([
'pirep_id' => null
])->toArray();
$response = $this->post($uri, $acars);
$response->assertStatus(400);
}

View File

@@ -1,13 +1,11 @@
<?php
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
use App\Services\DatabaseService;
/**
* Class TestCase
*/
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
@@ -32,11 +30,22 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
return self::$auth_headers;
}
public function headers($user)
/**
* @param $user
* @param array $headers
* @return array
*/
public function headers($user=null, array $headers = []): array
{
return [
'x-api-key' => $user->api_key,
];
if($user !== null) {
$headers['x-api-key'] = $user->api_key;
} else {
if($this->user !== null) {
$headers['x-api-key'] = $this->user->api_key;
}
}
return $headers;
}
/**
@@ -81,7 +90,7 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
public function addData($file)
{
$svc = app('\App\Services\DatabaseService');
$svc = app(DatabaseService::class);
$file_path = base_path('tests/data/' . $file . '.yml');
try {
$svc->seed_from_yaml_file($file_path);
@@ -91,7 +100,6 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
public function fillableFields(\Illuminate\Database\Eloquent\Model $model)
{
//$klass = new $model();
return $model->fillable;
}
@@ -116,18 +124,9 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
*/
public function get($uri, array $headers=[], $user=null): \Illuminate\Foundation\Testing\TestResponse
{
if($this->user !== null) {
$headers = $this->headers($this->user);
}
if($user !== null) {
$headers['x-api-key'] = $user->api_key;
}
$req = parent::get($uri, $headers);
$req = parent::get($uri, $this->headers($user, $headers));
if($req->isClientError() || $req->isServerError()) {
Log::error('Error on '.$uri);
Log::error($req->json());
Log::error('GET Error: ' . $uri, $req->json());
}
return $req;
@@ -138,17 +137,17 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
* @param string $uri
* @param array $data
* @param array $headers
* @param null $user
* @return \Illuminate\Foundation\Testing\TestResponse
*/
public function post($uri, array $data = [], array $headers = [])
public function post($uri, array $data = [], array $headers = [], $user=null)
{
if (empty($headers)) {
if ($this->user !== null) {
$headers = $this->headers($this->user);
}
$req = parent::post($uri, $data, $this->headers($user, $headers));
if ($req->isClientError() || $req->isServerError()) {
Log::error('POST Error: ' . $uri, $req->json());
}
return parent::post($uri, $data, $headers);
return $req;
}
/**
@@ -156,16 +155,16 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
* @param string $uri
* @param array $data
* @param array $headers
* @param null $user
* @return \Illuminate\Foundation\Testing\TestResponse
*/
public function delete($uri, array $data = [], array $headers = [])
public function delete($uri, array $data = [], array $headers = [], $user=null)
{
if (empty($headers)) {
if ($this->user !== null) {
$headers = $this->headers($this->user);
}
$req = parent::delete($uri, $data, $this->headers($user, $headers));
if ($req->isClientError() || $req->isServerError()) {
Log::error('DELETE Error: ' . $uri, $req->json());
}
return parent::delete($uri, $data, $headers);
return $req;
}
}