| @ -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()); | |||
| } | |||
| } | |||
| @ -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')); | |||
| } | |||
| } | |||
| @ -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, | |||
| ]); | |||
| } | |||
| } | |||
| @ -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']); | |||
| } | |||
| } | |||