Skip to content

Commit

Permalink
Add failure tests for createAccountOnFirstLogin feature
Browse files Browse the repository at this point in the history
  • Loading branch information
miguilimzero committed Nov 30, 2023
1 parent 98dbb12 commit 6ea7636
Showing 1 changed file with 104 additions and 1 deletion.
105 changes: 104 additions & 1 deletion tests/Feature/CreateAccountOnFirstLoginFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,107 @@
'provider_id' => $githubId,
'email' => '[email protected]',
]);
});
});


test('new users can register from random page', function (): void {
Config::set('socialstream.features', [
Features::createAccountOnFirstLogin(),
]);

$this->assertDatabaseEmpty('users');
$this->assertDatabaseEmpty('connected_accounts');

$user = (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);

$provider = Mockery::mock(GithubProvider::class);
$provider->shouldReceive('user')->once()->andReturn($user);

session()->put('socialstream.previous_url', '/random');

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

get('http://localhost/oauth/github/callback')
->assertRedirect(RouteServiceProvider::HOME);

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

test('new users cannot register from login page without feature enabled', function (): void {
$this->assertDatabaseEmpty('users');
$this->assertDatabaseEmpty('connected_accounts');

$user = (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);

$provider = Mockery::mock(GithubProvider::class);
$provider->shouldReceive('user')->once()->andReturn($user);

session()->put('socialstream.previous_url', route('login'));

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

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

$this->assertGuest();
$response->assertRedirect(route('login'));
$response->assertSessionHasErrors();
});

test('new users cannot register from random page without feature enabled', function (): void {
$this->assertDatabaseEmpty('users');
$this->assertDatabaseEmpty('connected_accounts');

$user = (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);

$provider = Mockery::mock(GithubProvider::class);
$provider->shouldReceive('user')->once()->andReturn($user);

session()->put('socialstream.previous_url', '/random');

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

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

$this->assertGuest();
$response->assertRedirect(route('login'));
$response->assertSessionHasErrors();
});

0 comments on commit 6ea7636

Please sign in to comment.