Adds the user name as a separate field

This commit is contained in:
2026-03-19 11:57:27 +01:00
parent a046c017fa
commit 124c707634
9 changed files with 233 additions and 21 deletions

View File

@@ -5,6 +5,7 @@
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\User as SocialiteUser;
use Mockery;
@@ -31,13 +32,18 @@ public function test_callback_creates_new_user_and_logs_in(): void
$this->markTestSkipped('Skipped due to application bug: password field is NOT NULL but controller passes null');
}
public function test_callback_matches_existing_user_by_email(): void
public function test_callback_matches_existing_user_by_username(): void
{
$existingUser = User::factory()->create([
'email' => 'existing@example.com',
DB::table('users')->insert([
'username' => 'existing@example.com',
'email' => 'real@example.com',
'name' => 'Original Name',
'created_at' => now(),
'updated_at' => now(),
]);
$existingUser = User::where('username', 'existing@example.com')->first();
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getEmail')->andReturn('existing@example.com');
$socialiteUser->shouldReceive('getName')->andReturn('Updated Name');
@@ -45,6 +51,7 @@ public function test_callback_matches_existing_user_by_email(): void
$socialiteUser->shouldReceive('getAvatar')->andReturn(null);
$socialiteUser->shouldReceive('offsetExists')->andReturn(false);
$socialiteUser->user = [
'mail' => 'real@example.com',
'jobTitle' => null,
'department' => null,
'companyName' => null,
@@ -62,11 +69,12 @@ public function test_callback_matches_existing_user_by_email(): void
$this->get('/auth/callback')
->assertRedirect('/');
$this->assertEquals(1, User::where('email', 'existing@example.com')->count());
$this->assertEquals(1, User::where('username', 'existing@example.com')->count());
$existingUser->refresh();
$this->assertEquals('Updated Name', $existingUser->name);
$this->assertEquals('real@example.com', $existingUser->email);
$this->assertAuthenticatedAs($existingUser);
}
@@ -82,15 +90,18 @@ public function test_logout_logs_out_and_redirects_to_landing(): void
public function test_login_jonathan_works_in_testing_env(): void
{
User::factory()->create([
DB::table('users')->insert([
'username' => 'jonathan.van.rij@agerion.nl',
'email' => 'jonathan@blijnder.nl',
'name' => 'Jonathan',
'created_at' => now(),
'updated_at' => now(),
]);
$this->get('/login-for-testing')
->assertRedirect('/');
$user = User::where('email', 'jonathan@blijnder.nl')->first();
$user = User::where('username', 'jonathan.van.rij@agerion.nl')->first();
$this->assertAuthenticatedAs($user);
}