Browse Source

Adiciona testes para login de colaboradores e corrige componentes

usuarios_separados
Guilherme Capanema 6 years ago
parent
commit
b5da9ac287
12 changed files with 345 additions and 17 deletions
  1. +2
    -2
      app/Http/Controllers/Collaborators/Auth/LogoutController.php
  2. +2
    -2
      app/Http/Livewire/Collaborators/Auth/Login.php
  3. +1
    -1
      app/Http/Livewire/Collaborators/Auth/Passwords/Email.php
  4. +3
    -3
      app/Http/Livewire/Collaborators/Auth/Passwords/Reset.php
  5. +5
    -1
      app/Http/Middleware/Authenticate.php
  6. +5
    -1
      app/Http/Middleware/RedirectIfAuthenticated.php
  7. +0
    -6
      resources/views/livewire/collaborators/auth/login.blade.php
  8. +1
    -1
      routes/web.php
  9. +107
    -0
      tests/Feature/Collaborators/Auth/LoginTest.php
  10. +34
    -0
      tests/Feature/Collaborators/Auth/LogoutTest.php
  11. +53
    -0
      tests/Feature/Collaborators/Auth/Passwords/EmailTest.php
  12. +132
    -0
      tests/Feature/Collaborators/Auth/Passwords/ResetTest.php

+ 2
- 2
app/Http/Controllers/Collaborators/Auth/LogoutController.php View File

@ -11,8 +11,8 @@ class LogoutController extends Controller
{ {
public function __invoke(): RedirectResponse public function __invoke(): RedirectResponse
{ {
Auth::logout();
Auth::guard('collaborators')->logout();
return redirect(route('home'));
return redirect(route('collaborators.home'));
} }
} }

+ 2
- 2
app/Http/Livewire/Collaborators/Auth/Login.php View File

@ -24,13 +24,13 @@ class Login extends Component
'password' => ['required'], 'password' => ['required'],
]); ]);
if (!Auth::attempt($credentials, $this->remember)) {
if (!Auth::guard('collaborators')->attempt($credentials, $this->remember)) {
$this->addError('email', trans('auth.failed')); $this->addError('email', trans('auth.failed'));
return; return;
} }
redirect(route('home'));
redirect(route('collaborators.home'));
} }
public function render() public function render()


+ 1
- 1
app/Http/Livewire/Collaborators/Auth/Passwords/Email.php View File

@ -37,7 +37,7 @@ class Email extends Component
*/ */
public function broker() public function broker()
{ {
return Password::broker();
return Password::broker('collaborators');
} }
public function render() public function render()


+ 3
- 3
app/Http/Livewire/Collaborators/Auth/Passwords/Reset.php View File

@ -59,7 +59,7 @@ class Reset extends Component
if ($response == Password::PASSWORD_RESET) { if ($response == Password::PASSWORD_RESET) {
session()->flash(trans($response)); session()->flash(trans($response));
return redirect(route('home'));
return redirect(route('collaborators.home'));
} }
$this->addError('email', trans($response)); $this->addError('email', trans($response));
@ -72,7 +72,7 @@ class Reset extends Component
*/ */
public function broker() public function broker()
{ {
return Password::broker();
return Password::broker('collaborators');
} }
/** /**
@ -82,7 +82,7 @@ class Reset extends Component
*/ */
protected function guard() protected function guard()
{ {
return Auth::guard();
return Auth::guard('collaborators');
} }
public function render() public function render()


+ 5
- 1
app/Http/Middleware/Authenticate.php View File

@ -15,7 +15,11 @@ class Authenticate extends Middleware
protected function redirectTo($request) protected function redirectTo($request)
{ {
if (! $request->expectsJson()) { if (! $request->expectsJson()) {
return route('login');
if ($request->is('admin/*')) {
return route('collaborators.login');
} else {
return route('login');
}
} }
} }
} }

+ 5
- 1
app/Http/Middleware/RedirectIfAuthenticated.php View File

@ -19,7 +19,11 @@ class RedirectIfAuthenticated
public function handle($request, Closure $next, $guard = null) public function handle($request, Closure $next, $guard = null)
{ {
if (Auth::guard($guard)->check()) { if (Auth::guard($guard)->check()) {
return redirect(route('home'));
if ($guard === 'collaborators') {
return redirect(route('collaborators.home'));
} else {
return redirect(route('home'));
}
} }
return $next($request); return $next($request);


+ 0
- 6
resources/views/livewire/collaborators/auth/login.blade.php View File

@ -7,12 +7,6 @@
<h2 class="mt-6 text-3xl font-extrabold text-center text-gray-900 leading-9"> <h2 class="mt-6 text-3xl font-extrabold text-center text-gray-900 leading-9">
{{ __('auth.login') }} {{ __('auth.login') }}
</h2> </h2>
<p class="mt-2 text-sm text-center text-gray-600 leading-5 max-w">
Ou
<a href="{{ route('register') }}" class="font-medium text-green-600 hover:text-green-500 lowercase focus:outline-none focus:underline transition ease-in-out duration-150">
{{ __('auth.register') }}
</a>
</p>
</div> </div>
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-md"> <div class="mt-8 sm:mx-auto sm:w-full sm:max-w-md">


+ 1
- 1
routes/web.php View File

@ -29,7 +29,7 @@ Route::middleware('auth')->group(function () {
}); });
Route::prefix('admin')->name('collaborators.')->group(function () { Route::prefix('admin')->name('collaborators.')->group(function () {
Route::middleware('guest')->group(function () {
Route::middleware('guest:collaborators')->group(function () {
Route::view('login', 'collaborators.auth.login')->name('login'); Route::view('login', 'collaborators.auth.login')->name('login');
}); });


+ 107
- 0
tests/Feature/Collaborators/Auth/LoginTest.php View File

@ -0,0 +1,107 @@
<?php
namespace Tests\Feature\Collaborators\Auth;
use App\Collaborator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Livewire\Livewire;
use Tests\TestCase;
class LoginTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function can_view_login_page()
{
$this->get(route('collaborators.login'))
->assertSuccessful()
->assertSeeLivewire('collaborators.auth.login');
}
/** @test */
public function is_redirected_if_already_logged_in()
{
$collaborator = factory(Collaborator::class)->create();
$this->be($collaborator, 'collaborators');
$this->get(route('collaborators.login'))
->assertRedirect(route('collaborators.home'));
}
/** @test */
public function a_collaborator_can_login()
{
$collaborator = $this->factoryWithoutObservers(Collaborator::class)->create(['password' => Hash::make('password')]);
Livewire::test('collaborators.auth.login')
->set('email', $collaborator->email)
->set('password', 'password')
->call('authenticate');
$this->assertAuthenticatedAs($collaborator, 'collaborators');
}
/** @test */
public function is_redirected_to_the_home_page_after_login()
{
$collaborator = $this->factoryWithoutObservers(Collaborator::class)->create(['password' => Hash::make('password')]);
Livewire::test('collaborators.auth.login')
->set('email', $collaborator->email)
->set('password', 'password')
->call('authenticate')
->assertRedirect(route('collaborators.home'));
}
/** @test */
public function email_is_required()
{
$collaborator = factory(Collaborator::class)->create(['password' => Hash::make('password')]);
Livewire::test('collaborators.auth.login')
->set('password', 'password')
->call('authenticate')
->assertHasErrors(['email' => 'required']);
}
/** @test */
public function email_must_be_valid_email()
{
$collaborator = factory(Collaborator::class)->create(['password' => Hash::make('password')]);
Livewire::test('collaborators.auth.login')
->set('email', 'invalid-email')
->set('password', 'password')
->call('authenticate')
->assertHasErrors(['email' => 'email']);
}
/** @test */
public function password_is_required()
{
$collaborator = factory(Collaborator::class)->create(['password' => Hash::make('password')]);
Livewire::test('collaborators.auth.login')
->set('email', $collaborator->email)
->call('authenticate')
->assertHasErrors(['password' => 'required']);
}
/** @test */
public function bad_login_attempt_shows_message()
{
$collaborator = factory(Collaborator::class)->create();
Livewire::test('collaborators.auth.login')
->set('email', $collaborator->email)
->set('password', 'bad-password')
->call('authenticate')
->assertHasErrors('email');
$this->assertFalse(Auth::check());
}
}

+ 34
- 0
tests/Feature/Collaborators/Auth/LogoutTest.php View File

@ -0,0 +1,34 @@
<?php
namespace Tests\Feature\Collaborators\Auth;
use App\Collaborator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;
class LogoutTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function an_authenticated_collaborator_can_log_out()
{
$collaborator = factory(Collaborator::class)->create();
$this->be($collaborator, 'collaborators');
$this->post(route('collaborators.logout'))
->assertRedirect(route('collaborators.home'));
$this->assertFalse(Auth::check('collaborators'));
}
/** @test */
public function an_unauthenticated_collaborator_can_not_log_out()
{
$this->post(route('collaborators.logout'))
->assertRedirect(route('collaborators.login'));
$this->assertFalse(Auth::check('collaborators'));
}
}

+ 53
- 0
tests/Feature/Collaborators/Auth/Passwords/EmailTest.php View File

@ -0,0 +1,53 @@
<?php
namespace Tests\Feature\Collaborators\Auth\Passwords;
use App\Collaborator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class EmailTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function can_view_password_request_page()
{
$this->get(route('collaborators.password.request'))
->assertSuccessful()
->assertSeeLivewire('collaborators.auth.passwords.email');
}
/** @test */
public function a_collaborator_must_enter_an_email_address()
{
Livewire::test('collaborators.auth.passwords.email')
->call('sendResetPasswordLink')
->assertHasErrors(['email' => 'required']);
}
/** @test */
public function a_collaborator_must_enter_a_valid_email_address()
{
Livewire::test('collaborators.auth.passwords.email')
->set('email', 'email')
->call('sendResetPasswordLink')
->assertHasErrors(['email' => 'email']);
}
/** @test */
public function a_collaborator_who_enters_a_valid_email_address_will_get_sent_an_email()
{
$collaborator = factory(Collaborator::class)->create();
Livewire::test('collaborators.auth.passwords.email')
->set('email', $collaborator->email)
->call('sendResetPasswordLink')
->assertNotSet('emailSentMessage', false);
$this->assertDatabaseHas('collaborator_password_resets', [
'email' => $collaborator->email,
]);
}
}

+ 132
- 0
tests/Feature/Collaborators/Auth/Passwords/ResetTest.php View File

@ -0,0 +1,132 @@
<?php
namespace Tests\Feature\Collaborators\Auth\Passwords;
use App\Collaborator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Livewire\Livewire;
use Tests\TestCase;
class ResetTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function can_view_password_reset_page()
{
$collaborator = factory(Collaborator::class)->create();
$token = Str::random(16);
DB::table('collaborator_password_resets')->insert([
'email' => $collaborator->email,
'token' => Hash::make($token),
'created_at' => Carbon::now(),
]);
$this->get(route('collaborators.password.reset', [
'email' => $collaborator->email,
'token' => $token,
]))
->assertSuccessful()
->assertSeeLivewire('collaborators.auth.passwords.reset');
}
/** @test */
public function can_reset_password()
{
$collaborator = factory(Collaborator::class)->create();
$token = Str::random(16);
DB::table('collaborator_password_resets')->insert([
'email' => $collaborator->email,
'token' => Hash::make($token),
'created_at' => Carbon::now(),
]);
Livewire::test('collaborators.auth.passwords.reset', [
'token' => $token,
])
->set('email', $collaborator->email)
->set('password', 'new-password')
->set('passwordConfirmation', 'new-password')
->call('resetPassword');
$this->assertTrue(Auth::guard('collaborators')->attempt([
'email' => $collaborator->email,
'password' => 'new-password',
]));
}
/** @test */
public function token_is_required()
{
Livewire::test('collaborators.auth.passwords.reset', [
'token' => null,
])
->call('resetPassword')
->assertHasErrors(['token' => 'required']);
}
/** @test */
public function email_is_required()
{
Livewire::test('collaborators.auth.passwords.reset', [
'token' => Str::random(16),
])
->set('email', null)
->call('resetPassword')
->assertHasErrors(['email' => 'required']);
}
/** @test */
public function email_is_valid_email()
{
Livewire::test('collaborators.auth.passwords.reset', [
'token' => Str::random(16),
])
->set('email', 'email')
->call('resetPassword')
->assertHasErrors(['email' => 'email']);
}
/** @test */
function password_is_required()
{
Livewire::test('collaborators.auth.passwords.reset', [
'token' => Str::random(16),
])
->set('password', '')
->call('resetPassword')
->assertHasErrors(['password' => 'required']);
}
/** @test */
function password_is_minimum_of_eight_characters()
{
Livewire::test('collaborators.auth.passwords.reset', [
'token' => Str::random(16),
])
->set('password', 'secret')
->call('resetPassword')
->assertHasErrors(['password' => 'min']);
}
/** @test */
function password_matches_password_confirmation()
{
Livewire::test('collaborators.auth.passwords.reset', [
'token' => Str::random(16),
])
->set('password', 'new-password')
->set('passwordConfirmation', 'not-new-password')
->call('resetPassword')
->assertHasErrors(['password' => 'same']);
}
}