mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-05 18:23:48 +00:00
added it_check_if_user_exist_and_return_name_with_avatar, it_check_non_existed_user_and_return_not_found test
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="AuthTest.it_check_non_existed_user_and_return_not_found" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
|
||||
<CommandLine>
|
||||
<PhpTestInterpreterSettings>
|
||||
<option name="interpreterName" value="PHP 7.4" />
|
||||
</PhpTestInterpreterSettings>
|
||||
</CommandLine>
|
||||
<TestRunner class="Tests\Feature\Accounts\AuthTest" configuration_file="$PROJECT_DIR$/phpunit.xml" file="$PROJECT_DIR$/tests/Feature/Accounts/AuthTest.php" method="it_check_non_existed_user_and_return_not_found" scope="Method" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Actions\Fortify;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use App\Models\UserSettings;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
@@ -15,14 +17,22 @@ class CreateNewUser implements CreatesNewUsers
|
||||
/**
|
||||
* Validate and create a newly registered user.
|
||||
*
|
||||
* @param array $input
|
||||
* @param array $input
|
||||
* @return \App\Models\User
|
||||
*/
|
||||
public function create(array $input)
|
||||
{
|
||||
$settings = Setting::whereIn('name', ['storage_default', 'registration'])
|
||||
->pluck('value', 'name');
|
||||
|
||||
// Check if account registration is enabled
|
||||
if (!intval($settings['registration'])) {
|
||||
abort(401);
|
||||
}
|
||||
|
||||
Validator::make($input, [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => [
|
||||
'required',
|
||||
'string',
|
||||
'email',
|
||||
@@ -33,13 +43,20 @@ class CreateNewUser implements CreatesNewUsers
|
||||
])->validate();
|
||||
|
||||
$user = User::create([
|
||||
'email' => $input['email'],
|
||||
'email' => $input['email'],
|
||||
'password' => Hash::make($input['password']),
|
||||
]);
|
||||
|
||||
$user->settings()->create([
|
||||
'name' => $input['name']
|
||||
]);
|
||||
UserSettings::unguard();
|
||||
|
||||
$user
|
||||
->settings()
|
||||
->create([
|
||||
'name' => $input['name'],
|
||||
'storage_capacity' => $settings['storage_default'],
|
||||
]);
|
||||
|
||||
UserSettings::reguard();
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
@@ -24,100 +24,16 @@ class AuthController extends Controller
|
||||
public function check_account(CheckAccountRequest $request)
|
||||
{
|
||||
// Get User
|
||||
$user = User::where('email', $request->input('email'))->select(['name', 'avatar'])->first();
|
||||
$user = User::whereEmail($request->email)
|
||||
->first();
|
||||
|
||||
// Return user info
|
||||
if ($user) return [
|
||||
'name' => $user->name,
|
||||
'avatar' => $user->avatar,
|
||||
if (! $user) {
|
||||
return response(__('vuefilemanager.user_not_fount'), 404);
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $user->settings->name,
|
||||
'avatar' => $user->settings->avatar,
|
||||
];
|
||||
|
||||
// Abort with 404, user not found
|
||||
return abort('404', __('vuefilemanager.user_not_fount'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Login user
|
||||
*
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function login(Request $request)
|
||||
{
|
||||
$response = Route::dispatch(self::make_login_request($request));
|
||||
|
||||
if ($response->isSuccessful()) {
|
||||
|
||||
$data = json_decode($response->content(), true);
|
||||
|
||||
return response('Login Successfull!', 200)->cookie('access_token', $data['access_token'], 43200);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register user
|
||||
*
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function register(Request $request)
|
||||
{
|
||||
$settings = Setting::whereIn('name', ['storage_default', 'registration'])->pluck('value', 'name');
|
||||
|
||||
// Check if account registration is enabled
|
||||
if (! intval($settings['registration'])) abort(401);
|
||||
|
||||
// Validate request
|
||||
$request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
|
||||
'password' => ['required', 'string', 'min:6', 'confirmed'],
|
||||
]);
|
||||
|
||||
// Create user
|
||||
$user = User::create([
|
||||
'name' => $request->name,
|
||||
'email' => $request->email,
|
||||
'password' => Hash::make($request->password),
|
||||
]);
|
||||
|
||||
// Create settings
|
||||
UserSettings::forceCreate([
|
||||
'user_id' => $user->id,
|
||||
'storage_capacity' => $settings['storage_default'],
|
||||
]);
|
||||
|
||||
$response = Route::dispatch(self::make_login_request($request));
|
||||
|
||||
if ($response->isSuccessful()) {
|
||||
|
||||
$data = json_decode($response->content(), true);
|
||||
|
||||
return response('Register Successfull!', 200)->cookie('access_token', $data['access_token'], 43200);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make login request for get access token
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Request
|
||||
*/
|
||||
private static function make_login_request($request)
|
||||
{
|
||||
$request->request->add([
|
||||
'grant_type' => 'password',
|
||||
'client_id' => config('services.passport.client_id'),
|
||||
'client_secret' => config('services.passport.client_secret'),
|
||||
'username' => $request->email,
|
||||
'password' => $request->password,
|
||||
'scope' => 'master',
|
||||
]);
|
||||
|
||||
return Request::create(url('/oauth/token'), 'POST', $request->all());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,11 +12,9 @@ use App\Http\Controllers\General\PricingController;
|
||||
use App\Http\Controllers\Sharing\FileSharingController;
|
||||
|
||||
// Pages
|
||||
Route::get('/content', [AppFunctionsController::class, 'get_setting_columns']);
|
||||
Route::post('/contact', [AppFunctionsController::class, 'contact_form']);
|
||||
Route::get('/page/{page}', [AppFunctionsController::class, 'get_page']);
|
||||
Route::get('/content', [AppFunctionsController::class, 'get_setting_columns']);
|
||||
|
||||
// Stripe
|
||||
Route::get('/pricing', [PricingController::class, 'index']);
|
||||
|
||||
// Password
|
||||
|
||||
@@ -5,7 +5,6 @@ use App\Http\Controllers\User\AccountController;
|
||||
use App\Http\Controllers\User\PaymentMethodsController;
|
||||
use App\Http\Controllers\User\SubscriptionController;
|
||||
|
||||
// TODO: testy
|
||||
Route::post('/check', [AuthController::class, 'check_account']);
|
||||
Route::post('/register', [AuthController::class, 'register']);
|
||||
Route::post('/login', [AuthController::class, 'login']);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Tests\Feature\Accounts;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
@@ -38,6 +39,22 @@ class AuthTest extends TestCase
|
||||
*/
|
||||
public function it_register_user()
|
||||
{
|
||||
collect([
|
||||
[
|
||||
'name' => 'storage_default',
|
||||
'value' => 12,
|
||||
],
|
||||
[
|
||||
'name' => 'registration',
|
||||
'value' => 1,
|
||||
],
|
||||
])->each(function ($setting) {
|
||||
Setting::create([
|
||||
'name' => $setting['name'],
|
||||
'value' => $setting['value'],
|
||||
]);
|
||||
});
|
||||
|
||||
$this->postJson('/register', [
|
||||
'email' => 'john@doe.com',
|
||||
'password' => 'SecretPassword',
|
||||
@@ -46,17 +63,41 @@ class AuthTest extends TestCase
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'email' => 'john@doe.com',
|
||||
'email' => 'john@doe.com',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('user_settings', [
|
||||
'name' => 'John Doe',
|
||||
'name' => 'John Doe',
|
||||
'storage_capacity' => 12,
|
||||
]);
|
||||
|
||||
Storage::disk('local')
|
||||
->assertExists('files/' . User::first()->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_check_if_user_exist_and_return_name_with_avatar()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create(['email' => 'john@doe.com']);
|
||||
|
||||
$this->postJson('/api/user/check', [
|
||||
'email' => $user->email,
|
||||
])->assertStatus(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_check_non_existed_user_and_return_not_found()
|
||||
{
|
||||
$this->postJson('/api/user/check', [
|
||||
'email' => 'jane@doe.com',
|
||||
])->assertStatus(404);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user