Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/Http/Controllers/UsageLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ public function getApplicationLogs(Request $request)
$this->validate($request, [
'fromDate' => 'sometimes|date',
'toDate' => 'sometimes|date',
'orderBy' => 'sometimes|string|in:name,username,estimatedUsers,requestCount',
'sort' => 'sometimes|string|in:asc,desc',
]);

try {
$orderBy = $request->query('orderBy', 'name');
$sort = strtolower($request->query('sort', 'asc')) === 'desc';
$apps = $this->applicationRepo->allDesc(['id', 'tenant_user_id', 'name', 'estimated_users_count']);

$usageLogs = collect([]);

foreach ($apps as $app) {
Expand All @@ -108,7 +112,7 @@ public function getApplicationLogs(Request $request)
'errors' => [],
], 500);
}

$usageLogs = $usageLogs->sortBy($orderBy, SORT_REGULAR, $sort)->values();
$paginated = $usageLogs->paginate(10)->toArray();

// TODO: Create custom paginator class
Expand Down Expand Up @@ -374,7 +378,7 @@ public function getTotals(Request $request)
$query = $usageLog->query();

if (isset($request->society)) {
$query->where('endpoint', 'v1/org/'.$request->society.'/whatnow');
$query->where('endpoint', config('app.api_version') . '/org/' .$request->society.'/whatnow');
}
if (isset($request->subnational)) {
$query->where('subnational', $request->subnational);
Expand Down
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,5 @@

'bucket_container' => env('AZURE_STORAGE_CONTAINER'),

'api_version' => 'v2',
];
16 changes: 12 additions & 4 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
|
*/

Route::group(['prefix' => 'v1'], function () {
Route::group(['prefix' => config('app.api_version')], function () {
// Unauthenticated endpoints
Route::get('alerts/rss', 'AlertController@getRss');
Route::get('alerts/{identifier}', 'AlertController@getByIdentifier');
Expand All @@ -22,14 +22,14 @@
Route::get('org/{code}/alerts/rss', 'AlertController@getRssByOrg');
});

Route::group(['middleware' => 'BasicAuth', 'prefix' => 'v1'], function () {
Route::group(['middleware' => 'BasicAuth', 'prefix' => config('app.api_version')], function () {
// Alert management
Route::post('alerts', 'AlertController@post');
});

Route::group([
'middleware' => 'ApiAuth',
'prefix' => 'v1',
'prefix' => config('app.api_version'),
], function () {
// Endpoints requiring API key authentication
Route::get('org/', 'OrganisationController@getAll');
Expand All @@ -40,7 +40,7 @@

Route::group([
'middleware' => 'BasicAuth',
'prefix' => 'v1',
'prefix' => config('app.api_version'),
], function () {
Route::get('/subnationals/{country_code}', 'RegionController@getAllForOrganisation');
Route::get('/subnationals/{country_code}/{code}', 'RegionController@getForCountryCode');
Expand Down Expand Up @@ -88,3 +88,11 @@
Route::get('/health', function () {
return response()->json(['status' => 'ok']); // Or a more detailed status
});

Route::prefix('v1')->group(function () {
Route::any('{any}', function () {
return response()->json([
'error' => 'API version v1 is no longer supported. Please use /v2/.'
], 410);
})->where('any', '.*');
});
2 changes: 1 addition & 1 deletion storage/api-docs/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"servers": [
{
"url": "https://api.whatnow.jazusoft.com/v1",
"url": "https://api.whatnow.jazusoft.com/v2",
"description": "API Base URL"
}
],
Expand Down
22 changes: 11 additions & 11 deletions tests/Feature/AlertFeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private function _postAlert($alert = null)
$alert = factory(\App\Models\Alert::class)->make();
}

return $this->json('POST', '/v1/alerts', [
return $this->json('POST', '/' . config('app.api_version') . '/alerts', [
'country' => $alert->country_code,
'language' => $alert->language_code,
'event' => $alert->event,
Expand Down Expand Up @@ -56,7 +56,7 @@ public function test_rss_feed_returns_alerts_filtered_by_severity()

$alert = Alert::find($alert->json('id'));

$response = $this->call('GET', '/v1/alerts/rss', [
$response = $this->call('GET', '/' . config('app.api_version') . '/alerts/rss', [
'severity' => $alert->severity,
]);

Expand All @@ -68,7 +68,7 @@ public function test_rss_feed_returns_alerts_filtered_by_severity()
$this->assertEquals($alert->getPublicUrl(), (string)$item[0]);

// Load the xml from disk
$path = storage_path('app/public/v1/alerts/cap12/') . $alert->getXmlPath();
$path = storage_path('app/public/' . config('app.api_version') . '/alerts/cap12/') . $alert->getXmlPath();
$this->assertFileExists($path);

$cap = simplexml_load_file($path);
Expand All @@ -86,7 +86,7 @@ public function test_rss_feed_returns_alerts_filtered_by_event_type()

$response = $this->call(
'GET',
'/v1/org/' . strtolower($alert->organisation->country_code) . '/alerts/rss',
'/' . config('app.api_version') . '/org/' . strtolower($alert->organisation->country_code) . '/alerts/rss',
['eventType' => $alert->event]
);

Expand All @@ -104,7 +104,7 @@ public function test_json_feed_returns_alerts_filtered_by_event_type()

$response = $this->call(
'GET',
'/v1/org/' . strtolower($alert->organisation->country_code) . '/alerts',
'/' . config('app.api_version') . '/org/' . strtolower($alert->organisation->country_code) . '/alerts',
['eventType' => $alert->event]
);

Expand All @@ -115,7 +115,7 @@ public function test_json_feed_returns_alerts_filtered_by_event_type()

public function test_json_feed_returns_alerts_filtered_by_severity()
{
$response = $this->call('GET', '/v1/alerts', [
$response = $this->call('GET', '/' . config('app.api_version') . '/alerts', [
'severity' => 'extreme',
]);

Expand All @@ -134,7 +134,7 @@ public function test_rss_item_link_self_reference_permalink_is_true()

$alert = Alert::find($alert->json('id'));

$response = $this->call('GET', '/v1/alerts/rss');
$response = $this->call('GET', '/' . config('app.api_version') . '/alerts/rss');

$xml = new SimpleXMLElement($response->content());
$item = $xml->xpath('//rss/channel/item[1]/guid/@isPermaLink');
Expand All @@ -158,7 +158,7 @@ public function test_active_filter_returns_only_active_alerts()

$response = $this->call(
'GET',
'/v1/alerts/rss',
'/' . config('app.api_version') . '/alerts/rss',
['active' => 'true']
);

Expand Down Expand Up @@ -187,7 +187,7 @@ public function test_feed_returns_active_and_inactive_alerts_when_active_filter_

$response = $this->call(
'GET',
'/v1/alerts/rss'
'/' . config('app.api_version') . '/alerts/rss'
);

$xml = new SimpleXMLElement($response->content());
Expand All @@ -211,7 +211,7 @@ public function test_date_filter_return_alerts_sent_within_specified_range()
// Request last 24 hrs of alerts
$response = $this->call(
'GET',
'/v1/alerts/rss',
'/' . config('app.api_version') . '/alerts/rss',
[
'startTime' => (new DateTime('now'))->sub(new DateInterval('PT24H'))->format('c'),
'endTime' => (new DateTime('now'))->format('c'),
Expand All @@ -236,7 +236,7 @@ public function test_date_filter_excludes_alerts_sent_outside_specified_range()
// Request alerts up to 24hrs old
$response = $this->call(
'GET',
'/v1/alerts/rss',
'/' . config('app.api_version') . '/alerts/rss',
[
'startTime' => (new DateTime('now'))->sub(new DateInterval('PT24H'))->format('c'),
'endTime' => (new DateTime('now'))->format('c'),
Expand Down
12 changes: 6 additions & 6 deletions tests/Feature/AlertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private function _postAlert($alert = null)
$alert = factory(\App\Models\Alert::class)->make();
}

return $this->json('POST', '/v1/alerts', [
return $this->json('POST', '/' . config('app.api_version') . '/alerts', [
'country' => $alert->country_code,
'language' => $alert->language_code,
'event' => $alert->event,
Expand Down Expand Up @@ -93,7 +93,7 @@ public function test_it_generates_a_valid_cap_file()

$alert = Alert::find($alert->json('id'));

$path = storage_path('app/public') . '/v1/alerts/cap12/' . $alert->getXmlPath();
$path = storage_path('app/public') . '/' . config('app.api_version') . '/alerts/cap12/' . $alert->getXmlPath();

$this->assertFileExists($path);

Expand All @@ -111,7 +111,7 @@ public function test_it_appears_in_rss_feed()

$alert = Alert::find($alert->json('id'));

$response = $this->call('GET', '/v1/alerts/rss');
$response = $this->call('GET', '/' . config('app.api_version') . '/alerts/rss');
$this->assertEquals(200, $response->status());

$xml = new SimpleXMLElement($response->content());
Expand All @@ -126,7 +126,7 @@ public function test_it_appears_in_country_feed()

$alert = Alert::find($alert->json('id'));

$response = $this->call('GET', '/v1/org/' . strtolower($alert->organisation->country_code) . '/alerts/rss');
$response = $this->call('GET', '/' . config('app.api_version') . '/org/' . strtolower($alert->organisation->country_code) . '/alerts/rss');
$this->assertEquals(200, $response->status());

$xml = new SimpleXMLElement($response->content());
Expand All @@ -141,7 +141,7 @@ public function test_it_appears_in_json_feed()

$alert = Alert::find($alert->json('id'));

$response = $this->call('GET', '/v1/alerts');
$response = $this->call('GET', '/' . config('app.api_version') . '/alerts');

$response->assertStatus(200);

Expand All @@ -156,7 +156,7 @@ public function test_it_is_returned_by_identifier()

$alert = Alert::find($alert->json('id'));

$response = $this->call('GET', '/v1/alerts/' . $alert->getCapIdentifier());
$response = $this->call('GET', '/' . config('app.api_version') . '/alerts/' . $alert->getCapIdentifier());

$response->assertStatus(200);

Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function test_it_lists_apps()
],
];

$response = $this->json('GET', '/v1/apps?userId=999');
$response = $this->json('GET', '/' . config('app.api_version') . '/apps?userId=999');

$response->assertStatus(200);

Expand Down Expand Up @@ -71,7 +71,7 @@ public function test_it_gets_app()
],
];

$response = $this->json('GET', '/v1/apps/'.$app->id);
$response = $this->json('GET', '/' . config('app.api_version') . '/apps/'.$app->id);

$response->assertStatus(200);

Expand All @@ -91,7 +91,7 @@ public function test_it_deletes_app()
'key' => '1234567890',
]);

$this->json('DELETE', '/v1/apps/'.$app->id)->assertStatus(200);
$this->json('DELETE', '/' . config('app.api_version') . '/apps/'.$app->id)->assertStatus(200);

$this->assertDatabaseHas('applications', [
'id' => $app->id,
Expand All @@ -106,7 +106,7 @@ public function test_it_creates_app()
{
$this->markTestSkipped('Bug Request POST parameters lost when unit testing https://github.com/laravel/lumen-framework/issues/559 upgrade to Lumen 5.4.2+');

$this->json('POST', '/v1/apps/', [
$this->json('POST', '/' . config('app.api_version') . '/apps/', [
'userId' => '55',
'name' => 'Hello',
])->assertStatus(200);
Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/RegionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testCreateRegionForOrganisation()
],
];

$response = $this->json('POST', '/v1/subnationals', $data);
$response = $this->json('POST', '/' . config('app.api_version') . '/subnationals', $data);

//dd($response);

Expand Down Expand Up @@ -98,7 +98,7 @@ public function testUpdateRegion()

$this->assertEquals(0, $matched);

$this->json('PUT', "/v1/subnationals/subnational/{$region->id}", $data)
$this->json('PUT', '/' . config('app.api_version') . '/subnationals/subnational/{$region->id}', $data)
->assertStatus(201);

$matched = RegionTranslation::where('region_id', '=', $region->id)
Expand All @@ -111,13 +111,13 @@ public function testUpdateRegion()

public function testGetRegionsForOrganisation()
{
$this->json('GET', '/v1/subnationals/USA')
$this->json('GET', '/' . config('app.api_version') . '/subnationals/USA')
->assertStatus(200);
}

public function testGetLaguageSpecificRegionsForOrganisation()
{
$this->json('GET', '/v1/subnationals/USA/es')
$this->json('GET', '/' . config('app.api_version') . '/subnationals/USA/es')
->assertStatus(200);
}

Expand All @@ -139,7 +139,7 @@ public function testDeleteRegionForOrganisation()
],
]);

$this->json('DELETE', "/v1/subnationals/subnational/{$region->id}")
$this->json('DELETE', '/' . config('app.api_version') . '/subnationals/subnational/{$region->id}')
->assertStatus(202);
}
}
Loading
Loading