API User details and logout

This commit is contained in:
Bubka 2019-05-29 11:04:12 +02:00
parent 24588e5d26
commit bf029bc741
3 changed files with 83 additions and 12 deletions

View File

@ -11,7 +11,10 @@
class UserController extends Controller
{
/**
* log a user in
* @return [type] [description]
*/
public function login()
{
$credentials = [
@ -30,6 +33,25 @@ public function login()
}
/**
* log out current user
* @param Request $request
* @return json
*/
public function logout()
{
$accessToken = Auth::user()->token();
$accessToken->revoke();
return response()->json(['success' => 'signed out']);
}
/**
* register new user
* @param Request $request [description]
* @return json
*/
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
@ -53,8 +75,12 @@ public function register(Request $request)
}
/**
* get detailed information about a user
* @return [type] [description]
*/
public function getDetails()
{
return response()->json(['success' => Auth::user()]);
return response()->json(Auth::user(), 200);
}
}

View File

@ -16,11 +16,9 @@
Route::post('login', 'UserController@login');
Route::post('register', 'UserController@register');
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::group(['middleware' => 'auth:api'], function(){
Route::post('logout', 'UserController@logout');
Route::get('user', 'UserController@getDetails');
Route::apiResource('twofaccounts', 'TwoFAccountController');
Route::get('twofaccounts/{twofaccount}/totp', 'TwoFAccountController@generateTOTP')->name('twofaccounts.generateTOTP');
Route::delete('twofaccounts/force/{id}', 'TwoFAccountController@forceDestroy')->name('twofaccounts.forceDestroy');

View File

@ -3,10 +3,12 @@
namespace Tests\Unit;
use Tests\TestCase;
use App\User;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth;
class UserTest extends TestCase
{
@ -41,9 +43,10 @@ public function testUserCreation()
'password' => 'test',
]);
$response->assertStatus(200)->assertJsonStructure([
'success' => ['token', 'name']
]);
$response->assertStatus(200)
->assertJsonStructure([
'success' => ['token', 'name']
]);
}
@ -59,9 +62,53 @@ public function testUserLogin()
'password' => 'test'
]);
$response->assertStatus(200)->assertJsonStructure([
'success' => ['token']
]);
$response->assertStatus(200)
->assertJsonStructure([
'success' => ['token']
]);
}
/**
* test User logout via API
*
* @return void
*/
public function testUserLogout()
{
$user = ['email' => 'test@test.com',
'password' => 'test'
];
Auth::attempt($user);
$token = Auth::user()->createToken('testToken')->accessToken;
$headers = ['Authorization' => "Bearer $token"];
$response = $this->json('POST', '/api/logout', [], $headers)
->assertStatus(200)
->assertJson([
'success' => 'signed out',
]);
}
/**
* test User logout via API
*
* @return void
*/
public function testGetUserDetails()
{
$user = \App\User::find(1);
$response = $this->actingAs($user, 'api')
->json('GET', '/api/user')
->assertStatus(200)
->assertJsonFragment([
'id' => 1,
'name' => 'testLogin',
'email' => 'test@test.com',
]);
}
}