#36 Add authentication for API; generate API key
This commit is contained in:
54
tests/ApiTest.php
Normal file
54
tests/ApiTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user