From fd70a7182f1553281694601823d71d33d07c87b6 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sat, 27 Apr 2019 23:10:21 +0800 Subject: [PATCH] Add API for fetch current user --- app/Http/Controllers/UserController.php | 5 ++++ routes/api.php | 1 + tests/Api/tests/main.rs | 3 +++ tests/Api/tests/user.rs | 32 +++++++++++++++++++++++++ tests/UserControllerTest.php | 16 +++++++++++++ 5 files changed, 57 insertions(+) create mode 100644 tests/Api/tests/user.rs diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 0bbc4c91..00ece4e8 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -27,6 +27,11 @@ class UserController extends Controller })->only(['index', 'profile']); } + public function user() + { + return json('', 0, auth()->user()->makeHidden(['password', 'ip', 'remember_token'])->toArray()); + } + public function index() { $user = Auth::user(); diff --git a/routes/api.php b/routes/api.php index 4ab06c96..215bbf50 100644 --- a/routes/api.php +++ b/routes/api.php @@ -7,6 +7,7 @@ Route::prefix('auth')->group(function () { }); Route::prefix('user')->middleware('auth:jwt,oauth')->group(function () { + Route::get('', 'UserController@user'); Route::put('sign', 'UserController@sign'); }); diff --git a/tests/Api/tests/main.rs b/tests/Api/tests/main.rs index dd6a236a..5202bcde 100644 --- a/tests/Api/tests/main.rs +++ b/tests/Api/tests/main.rs @@ -4,5 +4,8 @@ mod types; #[cfg(test)] mod auth; +#[cfg(test)] +mod user; + #[cfg(test)] mod players; diff --git a/tests/Api/tests/user.rs b/tests/Api/tests/user.rs new file mode 100644 index 00000000..3e5b66bb --- /dev/null +++ b/tests/Api/tests/user.rs @@ -0,0 +1,32 @@ +use crate::auth::login; +use crate::types::JsonBody; +use serde::Deserialize; + +#[derive(Deserialize)] +struct User { + pub uid: u32, + pub email: String, + pub nickname: String, + pub avatar: u32, + pub score: u32, +} + +#[test] +fn fetch_user_info() { + let client = reqwest::Client::new(); + let body = client + .get("http://127.0.0.1:32123/api/user") + .header("Authorization", login()) + .send() + .unwrap() + .json::>() + .unwrap(); + assert!(body.is_success()); + + let user = body.data().unwrap(); + assert_eq!(user.uid, 1); + assert_eq!(user.email, String::from("ibara.mayaka@api.test")); + assert_eq!(user.nickname, String::from("hyouka")); + assert_eq!(user.avatar, 0); + assert_eq!(user.score, 1000); +} diff --git a/tests/UserControllerTest.php b/tests/UserControllerTest.php index 6967f3f2..158b5b93 100644 --- a/tests/UserControllerTest.php +++ b/tests/UserControllerTest.php @@ -14,6 +14,22 @@ class UserControllerTest extends TestCase { use DatabaseTransactions; + public function testUser() + { + $user = factory(User::class)->create(); + $this->actingAs($user, 'jwt') + ->get('/api/user') + ->assertJson([ + 'code' => 0, + 'data' => [ + 'uid' => $user->uid, + 'email' => $user->email, + 'nickname' => $user->nickname, + 'score' => $user->score, + ] + ]); + } + public function testIndex() { $user = factory(User::class)->create();