-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split feature tests into different files
- Loading branch information
1 parent
5059bff
commit 98dbb12
Showing
4 changed files
with
174 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace JoelButcher\Socialstream\Tests\Feature; | ||
|
||
use App\Providers\RouteServiceProvider; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Facades\Config; | ||
use JoelButcher\Socialstream\Features; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Laravel\Socialite\Two\GithubProvider; | ||
use Laravel\Socialite\Two\User as SocialiteUser; | ||
use Mockery; | ||
|
||
use function Pest\Laravel\get; | ||
|
||
uses(RefreshDatabase::class); | ||
|
||
test('new users can register from login 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', route('login')); | ||
|
||
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]', | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace JoelButcher\Socialstream\Tests\Feature; | ||
|
||
use App\Models\User; | ||
use App\Providers\RouteServiceProvider; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Facades\Config; | ||
use JoelButcher\Socialstream\Features; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Laravel\Socialite\Two\GithubProvider; | ||
use Laravel\Socialite\Two\User as SocialiteUser; | ||
use Mockery; | ||
|
||
use function Pest\Laravel\get; | ||
|
||
uses(RefreshDatabase::class); | ||
|
||
it('generates missing emails', function (): void { | ||
Config::set('socialstream.features', [ | ||
Features::generateMissingEmails(), | ||
]); | ||
|
||
$user = (new SocialiteUser()) | ||
->map([ | ||
'id' => $githubId = fake()->numerify('########'), | ||
'nickname' => 'joel', | ||
'name' => 'Joel', | ||
'avatar' => null, | ||
'avatar_original' => null, | ||
]) | ||
->setToken('user-token') | ||
->setRefreshToken('refresh-token') | ||
->setExpiresIn(3600); | ||
|
||
$provider = Mockery::mock(GithubProvider::class); | ||
$provider->shouldReceive('user')->once()->andReturn($user); | ||
|
||
Socialite::shouldReceive('driver')->once()->with('github')->andReturn($provider); | ||
|
||
session()->put('socialstream.previous_url', route('register')); | ||
|
||
get('http://localhost/oauth/github/callback') | ||
->assertRedirect(RouteServiceProvider::HOME); | ||
|
||
$user = User::first(); | ||
|
||
$this->assertAuthenticated(); | ||
$this->assertEquals("$githubId@github", $user->email); | ||
$this->assertDatabaseHas('connected_accounts', [ | ||
'provider' => 'github', | ||
'provider_id' => $githubId, | ||
'email' => $user->email, | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace JoelButcher\Socialstream\Tests\Feature; | ||
|
||
use App\Models\User; | ||
use App\Providers\RouteServiceProvider; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Hash; | ||
use JoelButcher\Socialstream\Features; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Laravel\Socialite\Two\GithubProvider; | ||
use Laravel\Socialite\Two\User as SocialiteUser; | ||
use Mockery; | ||
|
||
use function Pest\Laravel\get; | ||
|
||
uses(RefreshDatabase::class); | ||
|
||
test('users can login on registration', function (): void { | ||
Config::set('socialstream.features', [ | ||
Features::loginOnRegistration(), | ||
]); | ||
|
||
User::create([ | ||
'name' => 'Joel Butcher', | ||
'email' => '[email protected]', | ||
'password' => Hash::make('password'), | ||
]); | ||
|
||
$this->assertDatabaseHas('users', ['email' => '[email protected]']); | ||
$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); | ||
|
||
Socialite::shouldReceive('driver')->once()->with('github')->andReturn($provider); | ||
|
||
session()->put('socialstream.previous_url', route('register')); | ||
|
||
get('http://localhost/oauth/github/callback') | ||
->assertRedirect(RouteServiceProvider::HOME); | ||
|
||
$this->assertAuthenticated(); | ||
$this->assertDatabaseHas('connected_accounts', [ | ||
'provider' => 'github', | ||
'provider_id' => $githubId, | ||
'email' => '[email protected]', | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,123 +188,3 @@ public function generate(string $provider): RedirectResponse | |
'email' => '[email protected]', | ||
]); | ||
}); | ||
|
||
test('new users can register from login 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); | ||
|
||
Socialite::shouldReceive('driver')->once()->with('github')->andReturn($provider); | ||
|
||
get('http://localhost/oauth/github/callback') | ||
->assertRedirect(RouteServiceProvider::HOME); | ||
|
||
$this->assertAuthenticated(); | ||
$this->assertDatabaseHas('connected_accounts', [ | ||
'provider' => 'github', | ||
'provider_id' => $githubId, | ||
'email' => '[email protected]', | ||
]); | ||
}); | ||
|
||
test('users can login on registration', function (): void { | ||
Config::set('socialstream.features', [ | ||
Features::loginOnRegistration(), | ||
]); | ||
|
||
User::create([ | ||
'name' => 'Joel Butcher', | ||
'email' => '[email protected]', | ||
'password' => Hash::make('password'), | ||
]); | ||
|
||
$this->assertDatabaseHas('users', ['email' => '[email protected]']); | ||
$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); | ||
|
||
Socialite::shouldReceive('driver')->once()->with('github')->andReturn($provider); | ||
|
||
session()->put('socialstream.previous_url', route('register')); | ||
|
||
get('http://localhost/oauth/github/callback') | ||
->assertRedirect(RouteServiceProvider::HOME); | ||
|
||
$this->assertAuthenticated(); | ||
$this->assertDatabaseHas('connected_accounts', [ | ||
'provider' => 'github', | ||
'provider_id' => $githubId, | ||
'email' => '[email protected]', | ||
]); | ||
}); | ||
|
||
it('generates missing emails', function (): void { | ||
Config::set('socialstream.features', [ | ||
Features::generateMissingEmails(), | ||
]); | ||
|
||
$user = (new SocialiteUser()) | ||
->map([ | ||
'id' => $githubId = fake()->numerify('########'), | ||
'nickname' => 'joel', | ||
'name' => 'Joel', | ||
'avatar' => null, | ||
'avatar_original' => null, | ||
]) | ||
->setToken('user-token') | ||
->setRefreshToken('refresh-token') | ||
->setExpiresIn(3600); | ||
|
||
$provider = Mockery::mock(GithubProvider::class); | ||
$provider->shouldReceive('user')->once()->andReturn($user); | ||
|
||
Socialite::shouldReceive('driver')->once()->with('github')->andReturn($provider); | ||
|
||
session()->put('socialstream.previous_url', route('register')); | ||
|
||
get('http://localhost/oauth/github/callback') | ||
->assertRedirect(RouteServiceProvider::HOME); | ||
|
||
$user = User::first(); | ||
|
||
$this->assertAuthenticated(); | ||
$this->assertEquals("$githubId@github", $user->email); | ||
$this->assertDatabaseHas('connected_accounts', [ | ||
'provider' => 'github', | ||
'provider_id' => $githubId, | ||
'email' => $user->email, | ||
]); | ||
}); |