Fix for API key and start adding a few data factories

This commit is contained in:
Nabeel Shahzad
2017-12-12 20:14:01 -06:00
parent da5523e972
commit b5d2e1ba11
8 changed files with 140 additions and 16 deletions

View File

@@ -5,6 +5,8 @@
*/
class ApiTest extends TestCase
{
use \Tests\Traits\FixtureDataLoader;
public function setUp()
{
parent::setUp();
@@ -26,11 +28,11 @@ class ApiTest extends TestCase
->assertStatus(401);
// Test upper/lower case of Authorization header, etc
$this->withHeaders(self::$auth_headers)->get($uri)
$this->withHeaders($this->apiHeaders())->get($uri)
->assertStatus(200)
->assertJson(['icao' => 'KJFK'], true);
$this->withHeaders(['AUTHORIZATION' => 'testapikey'])->get($uri)
$this->withHeaders(['AUTHORIZATION' => 'testadminapikey'])->get($uri)
->assertStatus(200)
->assertJson(['icao' => 'KJFK'], true);
}
@@ -40,11 +42,11 @@ class ApiTest extends TestCase
*/
public function testAirportRequest()
{
$this->withHeaders(self::$auth_headers)->get('/api/airports/KJFK')
$this->withHeaders($this->apiHeaders())->get('/api/airports/KJFK')
->assertStatus(200)
->assertJson(['icao' => 'KJFK'], true);
$this->withHeaders(self::$auth_headers)->get('/api/airports/UNK')
$this->withHeaders($this->apiHeaders())->get('/api/airports/UNK')
->assertStatus(404);
}
}

View File

@@ -24,6 +24,7 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
public function __construct($name = null, array $data = [], $dataName = '') {
parent::__construct($name, $data, $dataName);
Eloquent::unguard();
}
protected function reset_db() {

View File

@@ -0,0 +1,81 @@
<?php
/**
*/
namespace Tests\Traits;
use App\Models\Flight;
use Faker\Factory;
trait FixtureDataLoader {
public static $airports = ['KAUS', 'KJFK', 'KSFO', 'OPKC', 'OMDB', 'KLGA'];
public function apiHeaders()
{
return [
'Authorization' => 'testadminapikey'
];
}
/**
* Create new ID of integer type
* @return integer
*/
protected function create_id_int()
{
return random_int(1, 10000);
}
/**
* Create a new ID
* @return mixed
*/
protected function create_id_hash()
{
$hashids = new Hashids('', 12);
$mt = str_replace('.', '', microtime(true));
$id = $hashids->encode($mt);
return $id;
}
/**
* Dynamically apply options to a model
* @param $model
* @param array $options
* @return mixed
*/
protected function apply_options($model, array $options)
{
foreach ($options as $key => $value) {
$model->{$key} = $value;
}
$model->save();
return $model;
}
/**
* Add a flight
* @param array $options
* @return mixed
*/
public function addFlight(array $options=[])
{
$faker = Factory::create();
$options = array_merge([
'id' => $this->create_id_hash(),
'flight_number' => $faker->numberBetween(),
'airline_id' => 1,
'dpt_airport_id' => $faker->randomElement(self::$airports),
'arr_airport_id' => $faker->randomElement(self::$airports),
], $options);
$flight = new Flight();
$flight = $this->apply_options($flight, $options);
$flight->subfleets()->syncWithoutDetaching([1]);
return $flight;
}
}