diff --git a/.run/AuthTest.it_check_non_existed_user_and_return_not_found.run.xml b/.run/AuthTest.it_check_non_existed_user_and_return_not_found.run.xml new file mode 100644 index 00000000..3efb6fe2 --- /dev/null +++ b/.run/AuthTest.it_check_non_existed_user_and_return_not_found.run.xml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/Actions/Fortify/CreateNewUser.php b/app/Actions/Fortify/CreateNewUser.php index 7acd3e72..caca33f8 100644 --- a/app/Actions/Fortify/CreateNewUser.php +++ b/app/Actions/Fortify/CreateNewUser.php @@ -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; } diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php index d70ca263..e7d9a851 100644 --- a/app/Http/Controllers/Auth/AuthController.php +++ b/app/Http/Controllers/Auth/AuthController.php @@ -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()); } } diff --git a/routes/api.php b/routes/api.php index 147da120..c4a5e45c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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 diff --git a/routes/user.php b/routes/user.php index 9469ea76..da6f85db 100644 --- a/routes/user.php +++ b/routes/user.php @@ -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']); diff --git a/tests/Feature/Accounts/AuthTest.php b/tests/Feature/Accounts/AuthTest.php index 567cd429..cb15f521 100644 --- a/tests/Feature/Accounts/AuthTest.php +++ b/tests/Feature/Accounts/AuthTest.php @@ -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 */