#36 Add authentication for API; generate API key

This commit is contained in:
Nabeel Shahzad
2017-12-11 22:05:22 -06:00
parent 8a6bba0d0b
commit 0f18e60fd9
12 changed files with 173 additions and 18 deletions

54
tests/ApiTest.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
/**
* Test API calls and authentication, etc
*/
class ApiTest extends TestCase
{
protected static $headers = [
'Authorization' => 'testapikey'
];
public function setUp()
{
parent::setUp();
$this->addData('base');
}
/**
* Ensure authentication against the API works
*/
public function testApiAuthentication()
{
$uri = '/api/airports/kjfk';
// Missing auth header
$this->get($uri)->assertStatus(401);
// Test invalid API key
$this->withHeaders(['Authorization' => 'invalidKey'])->get($uri)
->assertStatus(401);
// Test upper/lower case of Authorization header, etc
$this->withHeaders(self::$headers)->get($uri)
->assertStatus(200)
->assertJson(['icao' => 'KJFK'], true);
$this->withHeaders(['AUTHORIZATION' => 'testapikey'])->get($uri)
->assertStatus(200)
->assertJson(['icao' => 'KJFK'], true);
}
/**
* Make sure the airport data is returned
*/
public function testAirportRequest()
{
$this->withHeaders(self::$headers)->get('/api/airports/KJFK')
->assertStatus(200)
->assertJson(['icao' => 'KJFK'], true);
$this->withHeaders(self::$headers)->get('/api/airports/UNK')
->assertStatus(404);
}
}

View File

@@ -1,19 +1,19 @@
airports:
- id: 1
- id: KAUS
iata: AUS
icao: KAUS
name: Austin-Bergstrom
location: Austin, Texas, USA
lat: 30.1945278
lon: -97.6698889
- id: 2
- id: KJFK
iata: JFK
icao: KJFK
name: John F Kennedy
location: New York, New York, USA
lat: 40.6399257
lon: -73.7786950
- id: 3
- id: EGLL
iata: LHR
icao: EGLL
name: London Heathrow

View File

@@ -13,6 +13,7 @@ users:
name: Admin User
email: admin@phpvms.net
password: admin
apikey: testapikey
rank_id: 1
created_at: now
updated_at: now
@@ -42,14 +43,14 @@ ranks:
auto_promote: 0
airports:
- id: 1
- id: KAUS
icao: KAUS
name: Austin-Bergstrom
location: Austin, Texas, USA
lat: 30.1945278
lon: -97.6698889
fuel_jeta_cost: 100
- id: 2
- id: KJFK
icao: KJFK
name: John F Kennedy
location: New York, New York, USA