Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
joelbutcher committed Apr 2, 2024
1 parent ed43f61 commit 496ca44
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/Feature/SocialstreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,54 @@ public function generate(string $provider): RedirectResponse
'email' => '[email protected]',
]);
});

test('linking and already linked provider fails', function () {
// Arrange - existing user with account linked
$this->actingAs(User::create([
'name' => 'Joel Butcher',
'email' => '[email protected]',
'password' => Hash::make('password'),
]));

$provider = mock(GithubProvider::class);
$provider->shouldReceive('user')->andReturn((new SocialiteUser())
->map([
'id' => $githubId = fake()->numerify('########'),
'nickname' => 'joel',
'name' => 'Joel',
'email' => '[email protected]',
'avatar' => null,
'avatar_original' => null,
])
->setToken('user-token')
->setRefreshToken('refresh-token')
->setExpiresIn(3600));

Socialite::shouldReceive('driver')->with('github')->andReturn($provider);

get('http://localhost/oauth/github/callback');

$this->assertAuthenticated();
$this->assertDatabaseHas('connected_accounts', [
'provider' => 'github',
'provider_id' => $githubId,
'email' => '[email protected]',
]);

// Arrange - new user
$this->actingAs(User::create([
'name' => 'Joel Butcher',
'email' => '[email protected]',
'password' => Hash::make('password'),
]));

// Act
get('http://localhost/oauth/github/callback')
->assertRedirectToRoute('profile.show')
->assertSessionHas([
'flash' => [
'banner' => 'It looks like this GitHub account is used by another user. Please log in.',
'bannerStyle' => 'danger'
]
]);
});

0 comments on commit 496ca44

Please sign in to comment.