create(); $response = $this->actingAs($user)->get(route('user.index')); $response->assertStatus(200); } public function test_guest_can_not_view_profile() { $response = $this->get(route('user.index')); $response->assertRedirect(route('auth.login')); } public function test_user_can_update_username() { $user = User::factory()->create(); $response = $this->actingAs($user) ->post(route('user.update'), [ 'username' => 'AnotherUsername', 'current_password' => null, 'password' => null, 'password_confirmation' => null ]); $response->assertRedirect(route('user.index')); $this->assertDatabaseHas('users', [ 'id' => $user->id, 'username' => 'AnotherUsername', 'password' => $user->password ]); } public function test_user_can_update_password() { $fakeHash = '$2y$04$wiUTB.6ldFQ3TmxdSyizEubuKubDA45L/Bv0zlZ1.uoMcnm.ftIaK'; $user = User::factory()->create(); // Mock Hash::make() to return a fake password hash. Hash::shouldReceive('make') ->once() ->with('newpassword') ->andReturn($fakeHash); // Also have to mock Hash::check() as i can not get // partial mocks to work for facades. Hash::shouldReceive('check') ->with('password', $user->password) ->andReturn(true); $response = $this->actingAs($user) ->post(route('user.update'), [ 'username' => $user->username, 'current_password' => 'password', 'password' => 'newpassword', 'password_confirmation' => 'newpassword' ]); $response->assertRedirect(route('user.index')); $this->assertDatabaseHas('users', [ 'id' => $user->id, 'username' => $user->username, 'password' => $fakeHash ]); } public function test_guest_can_not_update() { $response = $this->post(route('user.update'), [ 'current_password' => 'password', 'password' => 'newpassword', 'password_confirmation' => 'newpassword' ]); $response->assertRedirect(route('auth.login')); } }