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

@@ -2,6 +2,7 @@
<profile version="1.0"> <profile version="1.0">
<option name="myName" value="Project Default" /> <option name="myName" value="Project Default" />
<inspection_tool class="AndroidLintTypos" enabled="false" level="WARNING" enabled_by_default="false" /> <inspection_tool class="AndroidLintTypos" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="ArgumentEqualsDefaultValueInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="ClassOverridesFieldOfSuperClassInspection" enabled="false" level="WARNING" enabled_by_default="false" /> <inspection_tool class="ClassOverridesFieldOfSuperClassInspection" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="ForgottenDebugOutputInspection" enabled="true" level="ERROR" enabled_by_default="true"> <inspection_tool class="ForgottenDebugOutputInspection" enabled="true" level="ERROR" enabled_by_default="true">
<option name="configuration"> <option name="configuration">

View File

@@ -1,5 +1,8 @@
<?php <?php
$factory->define(App\Models\Airport::class, function (Faker\Generator $faker) {
use Faker\Generator as Faker;
$factory->define(App\Models\Airport::class, function (Faker $faker) {
return [ return [
'code' => 'Y', 'code' => 'Y',
'name' => 'Economy', 'name' => 'Economy',

View File

@@ -0,0 +1,30 @@
<?php
use Faker\Generator as Faker;
# Match the list available in tests/data/*.yml
$airlinesAvailable = [1];
$airportsAvailable = [
'KJFK',
'KAUS',
'EGLL',
];
$factory->define(App\Models\Flight::class, function (Faker $faker) use ($airportsAvailable, $airlinesAvailable) {
return [
'id' => $faker->sha1,
'flight_number' => $faker->numberBetween(),
'airline_id' => $faker->randomElement($airlinesAvailable),
'dpt_airport_id' => $faker->randomElement($airportsAvailable),
'arr_airport_id' => $faker->randomElement($airportsAvailable),
'route' => $faker->text(),
'dpt_time' => $faker->time(),
'arr_time' => $faker->time(),
'flight_time' => $faker->randomFloat(2),
'has_bid' => false,
'active' => true,
'created_at' => $faker->dateTimeBetween('-1 week', 'now'),
];
});

View File

@@ -1,12 +1 @@
<?php <?php
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
});

View File

@@ -0,0 +1,17 @@
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\User::class, function (Faker $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'api_key' => $faker->sha1,
'flights' => $faker->numberBetween(0, 1000),
'flight_time' => $faker->numberBetween(0, 10000),
'remember_token' => str_random(10),
];
});

View File

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

View File

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