mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2025-02-23 14:59:07 +08:00
Add API for fetch current user
This commit is contained in:
parent
8a2aaccabe
commit
fd70a7182f
@ -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();
|
||||
|
@ -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');
|
||||
});
|
||||
|
||||
|
@ -4,5 +4,8 @@ mod types;
|
||||
#[cfg(test)]
|
||||
mod auth;
|
||||
|
||||
#[cfg(test)]
|
||||
mod user;
|
||||
|
||||
#[cfg(test)]
|
||||
mod players;
|
||||
|
32
tests/Api/tests/user.rs
Normal file
32
tests/Api/tests/user.rs
Normal file
@ -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::<JsonBody<User>>()
|
||||
.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);
|
||||
}
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user