Browse Source

Refactor user registration and login testing and lang usage

pull/1/head
Nafies Luthfi 8 years ago
parent
commit
ff4812c690
  1. 35
      app/Http/Controllers/AuthController.php
  2. 1
      resources/lang/id/auth.php
  3. 19
      tests/Feature/Auth/MemberLoginTest.php
  4. 38
      tests/Feature/Auth/MemberRegistrationTest.php

35
app/Http/Controllers/AuthController.php

@ -3,7 +3,6 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Entities\Users\User; use App\Entities\Users\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\Accounts\ChangePasswordRequest; use App\Http\Requests\Accounts\ChangePasswordRequest;
use App\Http\Requests\Accounts\LoginRequest; use App\Http\Requests\Accounts\LoginRequest;
use App\Http\Requests\Accounts\RegisterRequest; use App\Http\Requests\Accounts\RegisterRequest;
@ -13,10 +12,9 @@ use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker; use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Foundation\Auth\ResetsPasswords; use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Foundation\Auth\ThrottlesLogins; use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Http\Request;
class AuthController extends Controller {
class AuthController extends Controller
{
use ResetsPasswords, ThrottlesLogins; use ResetsPasswords, ThrottlesLogins;
private $user; private $user;
@ -44,17 +42,16 @@ class AuthController extends Controller {
return view('auth.login'); return view('auth.login');
} }
public function postLogin(LoginRequest $req)
public function postLogin(LoginRequest $request)
{ {
$loginData = $req->only('email','password');
$loginData = $request->only('email', 'password');
if (Auth::attempt($loginData, $req->has('remember')))
{
flash()->success('Selamat datang kembali ' . Auth::user()->name . '.');
if (Auth::attempt($loginData, $request->has('remember'))) {
flash()->success(trans('auth.welcome', ['name' => Auth::user()->name]));
return redirect()->intended('home'); return redirect()->intended('home');
} }
flash()->error('Mohon maaf, anda tidak dapat login, cek kembali email/password anda!');
flash()->error(trans('auth.failed'));
return redirect()->back()->withInput(); return redirect()->back()->withInput();
} }
@ -70,21 +67,20 @@ class AuthController extends Controller {
return view('auth.register'); return view('auth.register');
} }
public function postRegister(RegisterRequest $req)
public function postRegister(RegisterRequest $request)
{ {
$registerData = $req->only('name','email','password');
$registerData = $request->only('name', 'email', 'password');
$user = User::create($registerData); $user = User::create($registerData);
$user->assignRole('customer'); $user->assignRole('customer');
Auth::login($user); Auth::login($user);
flash()->success('Selamat datang ' . $user->name . '.');
flash()->success(trans('auth.welcome', ['name' => $user->name]));
return redirect()->route('home'); return redirect()->route('home');
} }
public function getActivate($code) public function getActivate($code)
{ {
} }
public function getChangePassword() public function getChangePassword()
@ -92,12 +88,11 @@ class AuthController extends Controller {
return view('auth.change-password'); return view('auth.change-password');
} }
public function postChangePassword(ChangePasswordRequest $req)
public function postChangePassword(ChangePasswordRequest $request)
{ {
$input = $req->except('_token');
$input = $request->except('_token');
if (app('hash')->check($input['old_password'], $this->user->password))
{
if (app('hash')->check($input['old_password'], $this->user->password)) {
$this->user->password = $input['password']; $this->user->password = $input['password'];
$this->user->save(); $this->user->save();
@ -115,9 +110,9 @@ class AuthController extends Controller {
return view('auth.profile', compact('user')); return view('auth.profile', compact('user'));
} }
public function patchProfile(UpdateProfileRequest $req)
public function patchProfile(UpdateProfileRequest $request)
{ {
$this->user->name = $req->get('name');
$this->user->name = $request->get('name');
$this->user->save(); $this->user->save();
flash()->success('Profil berhasil diupdate.'); flash()->success('Profil berhasil diupdate.');

1
resources/lang/id/auth.php

@ -33,4 +33,5 @@ return [
'send_reset_password_link' => 'Kirim Link Reset Password', 'send_reset_password_link' => 'Kirim Link Reset Password',
'old_password_failed' => 'Password lama tidak cocok!', 'old_password_failed' => 'Password lama tidak cocok!',
'welcome' => 'Selamat datang kembali :name.', 'welcome' => 'Selamat datang kembali :name.',
'logged_out' => 'Anda telah logout.',
]; ];

19
tests/Feature/Auth/MemberLoginTest.php

@ -8,20 +8,25 @@ use Tests\TestCase;
class MemberLoginTest extends TestCase class MemberLoginTest extends TestCase
{ {
/** @test */ /** @test */
public function member_register_and_login_successfully()
public function user_can_login_and_logout()
{ {
$user = factory(User::class)->create(['name' => 'Nama Member', 'email' => 'email@mail.com']); $user = factory(User::class)->create(['name' => 'Nama Member', 'email' => 'email@mail.com']);
$user->assignRole('customer'); $user->assignRole('customer');
$this->visit(route('auth.login')); $this->visit(route('auth.login'));
$this->type('email@mail.com', 'email');
$this->type('member', 'password');
$this->press(trans('auth.login'));
$this->submitForm(trans('auth.login'), [
'email' => 'email@mail.com',
'password' => 'member',
]);
$this->seePageIs(route('home')); $this->seePageIs(route('home'));
$this->see('Selamat datang kembali Nama Member.');
$this->see(trans('auth.welcome', ['name' => $user->name]));
$this->click(trans('auth.logout')); $this->click(trans('auth.logout'));
$this->seePageIs(route('auth.login')); $this->seePageIs(route('auth.login'));
$this->see('Anda telah logout.');
$this->see(trans('auth.logged_out'));
} }
/** @test */ /** @test */
@ -32,6 +37,6 @@ class MemberLoginTest extends TestCase
$this->type('password.112', 'password'); $this->type('password.112', 'password');
$this->press(trans('auth.login')); $this->press(trans('auth.login'));
$this->seePageIs(route('auth.login')); $this->seePageIs(route('auth.login'));
$this->see('Mohon maaf, anda tidak dapat login');
$this->see(trans('auth.failed'));
} }
} }

38
tests/Feature/Auth/MemberRegistrationTest.php

@ -10,22 +10,27 @@ class MemberRegistrationTest extends TestCase
public function registration_validation() public function registration_validation()
{ {
$this->visit(route('auth.register')); $this->visit(route('auth.register'));
$this->type('', 'name');
$this->type('member@app.dev', 'email');
$this->type('', 'password');
$this->type('', 'password_confirmation');
$this->press(trans('auth.register'));
$this->submitForm(trans('auth.register'), [
'name' => '',
'email' => 'member@app.dev',
'password' => '',
'password_confirmation' => '',
]);
$this->seePageIs(route('auth.register')); $this->seePageIs(route('auth.register'));
$this->see('Nama harus diisi.'); $this->see('Nama harus diisi.');
$this->see('Email ini sudah terdaftar.'); $this->see('Email ini sudah terdaftar.');
$this->see('Password harus diisi.'); $this->see('Password harus diisi.');
$this->see('Konfirmasi password harus diisi.'); $this->see('Konfirmasi password harus diisi.');
$this->type('Nama Member', 'name');
$this->type('email', 'email');
$this->type('password', 'password');
$this->type('password..', 'password_confirmation');
$this->press(trans('auth.register'));
$this->submitForm(trans('auth.register'), [
'name' => 'Nama Member',
'email' => 'email',
'password' => 'password',
'password_confirmation' => 'password..',
]);
$this->seePageIs(route('auth.register')); $this->seePageIs(route('auth.register'));
$this->see('Email tidak valid.'); $this->see('Email tidak valid.');
$this->see('Konfirmasi password tidak sesuai.'); $this->see('Konfirmasi password tidak sesuai.');
@ -35,12 +40,13 @@ class MemberRegistrationTest extends TestCase
public function member_register_successfully() public function member_register_successfully()
{ {
$this->visit(route('auth.register')); $this->visit(route('auth.register'));
$this->type('Nama Member', 'name');
$this->type('email@mail.com', 'email');
$this->type('password.111', 'password');
$this->type('password.111', 'password_confirmation');
$this->press(trans('auth.register'));
$this->submitForm(trans('auth.register'), [
'name' => 'Nama Member',
'email' => 'email@mail.com',
'password' => 'password.111',
'password_confirmation' => 'password.111',
]);
$this->seePageIs(route('home')); $this->seePageIs(route('home'));
$this->see('Selamat datang Nama Member.');
$this->see(trans('auth.welcome', ['name' => 'Nama Member']));
} }
} }
Loading…
Cancel
Save