From 3c4dc65219e0223fa598d2214f9931abce0fc4ee Mon Sep 17 00:00:00 2001 From: Jovert Lota Palonpon Date: Sun, 31 Mar 2019 11:30:00 +0800 Subject: [PATCH] Implemented API versioning #27 --- .../Auth/ForgotPasswordController.php | 2 +- .../{ => V1}/Auth/ResetPasswordController.php | 2 +- .../Api/{ => V1}/Auth/SessionsController.php | 2 +- .../Api/{ => V1}/UsersController.php | 2 +- resources/js/Backoffice.js | 6 ++-- resources/js/models/User.js | 8 ++--- resources/js/views/auth/SignIn.js | 4 +-- resources/js/views/auth/passwords/Request.js | 2 +- routes/api.php | 32 ++++++++++--------- .../Api/{ => V1}/Auth/SessionsTest.php | 14 ++++---- tests/Feature/Api/{ => V1}/BaseTest.php | 2 +- tests/Feature/Api/{ => V1}/UsersTest.php | 12 +++---- 12 files changed, 45 insertions(+), 43 deletions(-) rename app/Http/Controllers/Api/{ => V1}/Auth/ForgotPasswordController.php (97%) rename app/Http/Controllers/Api/{ => V1}/Auth/ResetPasswordController.php (91%) rename app/Http/Controllers/Api/{ => V1}/Auth/SessionsController.php (98%) rename app/Http/Controllers/Api/{ => V1}/UsersController.php (99%) rename tests/Feature/Api/{ => V1}/Auth/SessionsTest.php (80%) rename tests/Feature/Api/{ => V1}/BaseTest.php (92%) rename tests/Feature/Api/{ => V1}/UsersTest.php (89%) diff --git a/app/Http/Controllers/Api/Auth/ForgotPasswordController.php b/app/Http/Controllers/Api/V1/Auth/ForgotPasswordController.php similarity index 97% rename from app/Http/Controllers/Api/Auth/ForgotPasswordController.php rename to app/Http/Controllers/Api/V1/Auth/ForgotPasswordController.php index 7b12df3..4b85bc6 100755 --- a/app/Http/Controllers/Api/Auth/ForgotPasswordController.php +++ b/app/Http/Controllers/Api/V1/Auth/ForgotPasswordController.php @@ -1,6 +1,6 @@ name('api.')->group(function () { - Route::namespace('Auth')->name('auth.')->prefix('auth')->group(function () { - Route::post('identify', 'SessionsController@identify')->name('identify'); - Route::post('signin', 'SessionsController@signin')->name('signin'); + Route::namespace('V1')->name('v1.')->prefix('v1')->group(function () { + Route::namespace('Auth')->name('auth.')->prefix('auth')->group(function () { + Route::post('identify', 'SessionsController@identify')->name('identify'); + Route::post('signin', 'SessionsController@signin')->name('signin'); - Route::middleware('auth:api')->group(function () { - Route::post('signout', 'SessionsController@signout')->name('signout'); - Route::post('refresh', 'SessionsController@refresh')->name('refresh'); - Route::post('user', 'SessionsController@user')->name('user'); - }); + Route::middleware('auth:api')->group(function () { + Route::post('signout', 'SessionsController@signout')->name('signout'); + Route::post('refresh', 'SessionsController@refresh')->name('refresh'); + Route::post('user', 'SessionsController@user')->name('user'); + }); - Route::name('password.')->prefix('password')->group(function () { - Route::post('request', 'ForgotPasswordController@sendResetLinkEmail')->name('request'); - Route::post('reset/{token}', 'ResetPasswordController@reset')->name('reset'); + Route::name('password.')->prefix('password')->group(function () { + Route::post('request', 'ForgotPasswordController@sendResetLinkEmail')->name('request'); + Route::post('reset/{token}', 'ResetPasswordController@reset')->name('reset'); + }); }); - }); - Route::middleware('auth:api')->group(function () { - Route::resource('users', 'UsersController', ['except' => ['edit', 'create']]); - Route::patch('users/{user}/restore', 'UsersController@restore')->name('users.restore'); + Route::middleware('auth:api')->group(function () { + Route::resource('users', 'UsersController', ['except' => ['edit', 'create']]); + Route::patch('users/{user}/restore', 'UsersController@restore')->name('users.restore'); + }); }); }); diff --git a/tests/Feature/Api/Auth/SessionsTest.php b/tests/Feature/Api/V1/Auth/SessionsTest.php similarity index 80% rename from tests/Feature/Api/Auth/SessionsTest.php rename to tests/Feature/Api/V1/Auth/SessionsTest.php index 5f1ea81..1574f75 100644 --- a/tests/Feature/Api/Auth/SessionsTest.php +++ b/tests/Feature/Api/V1/Auth/SessionsTest.php @@ -1,9 +1,9 @@ $user->username, ]; - $this->post(route('api.auth.identify'), $payload) + $this->post(route('api.v1.auth.identify'), $payload) ->assertStatus(200) ->assertSee($user->email); } @@ -31,7 +31,7 @@ public function a_user_can_be_authenticated() 'password' => 'secret' ]; - $this->post(route('api.auth.signin'), $payload) + $this->post(route('api.v1.auth.signin'), $payload) ->assertStatus(200) ->assertJsonStructure([ 'auth_token', 'token_type', 'expires_in' @@ -43,7 +43,7 @@ public function a_user_can_view_itself() { $payload = array_merge($this->getDefaultPayload(), []); - $this->post(route('api.auth.user'), $payload) + $this->post(route('api.v1.auth.user'), $payload) ->assertStatus(200) ->assertJson(_test_user()->toArray()); } @@ -53,7 +53,7 @@ public function a_user_can_refresh_its_session() { $payload = array_merge($this->getDefaultPayload(), []); - $this->post(route('api.auth.refresh'), $payload) + $this->post(route('api.v1.auth.refresh'), $payload) ->assertStatus(200) ->assertJsonStructure([ 'auth_token', 'token_type', 'expires_in' @@ -68,7 +68,7 @@ public function a_user_can_signout() { $payload = array_merge($this->getDefaultPayload(), []); - $this->post(route('api.auth.signout'), $payload) + $this->post(route('api.v1.auth.signout'), $payload) ->assertStatus(200) ->assertJsonStructure([ 'message' diff --git a/tests/Feature/Api/BaseTest.php b/tests/Feature/Api/V1/BaseTest.php similarity index 92% rename from tests/Feature/Api/BaseTest.php rename to tests/Feature/Api/V1/BaseTest.php index cd35947..0ac5459 100644 --- a/tests/Feature/Api/BaseTest.php +++ b/tests/Feature/Api/V1/BaseTest.php @@ -1,6 +1,6 @@ getDefaultPayload(), []); - $this->get(route('api.users.index'), $payload)->assertStatus(200); + $this->get(route('api.v1.users.index'), $payload)->assertStatus(200); } /** @test */ @@ -37,7 +37,7 @@ public function a_user_can_create_a_user() // Assuming that the user is created through the test data, // It must return a 201 response status and then, // It must return a response body consisting our test data, if not all. - $this->post(route('api.users.store'), array_merge($attributes, $payload)) + $this->post(route('api.v1.users.store'), array_merge($attributes, $payload)) ->assertStatus(201) ->assertJson($attributes); @@ -59,7 +59,7 @@ public function a_user_can_view_a_user() // Assuming that a user is found, // It must return a 200 response status and then, // It must be found as is in the JSON response. - $this->get(route('api.users.show', $user), $payload) + $this->get(route('api.v1.users.show', $user), $payload) ->assertStatus(200) ->assertExactJson($user->toArray()); } @@ -78,7 +78,7 @@ public function a_user_can_delete_a_user() // Assuming that the API will delete one, // It must return a 200 response status and then, // It must equal this decremented counter. - $this->delete(route('api.users.destroy', $user)) + $this->delete(route('api.v1.users.destroy', $user)) ->assertStatus(200) ->assertJsonFragment([ 'total' => $decremented @@ -103,7 +103,7 @@ public function a_user_can_restore_a_user() // Assuming that the users count is decremented after deleting one, // It must return a 200 response status and then, // It must equal this incremented counter. - $this->patch(route('api.users.restore', $recoverableUser)) + $this->patch(route('api.v1.users.restore', $recoverableUser)) ->assertStatus(200) ->assertJsonFragment([ 'total' => $incremented