Skip to content

Commit

Permalink
Split feature tests into different files
Browse files Browse the repository at this point in the history
  • Loading branch information
miguilimzero committed Nov 30, 2023
1 parent 5059bff commit 98dbb12
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 120 deletions.
56 changes: 56 additions & 0 deletions tests/Feature/CreateAccountOnFirstLoginFeatureTest.php
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]',
]);
});
55 changes: 55 additions & 0 deletions tests/Feature/GenerateMissingEmailsFeatureTest.php
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,
]);
});
63 changes: 63 additions & 0 deletions tests/Feature/LoginOnRegistrationFeatureTest.php
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]',
]);
});
120 changes: 0 additions & 120 deletions tests/Feature/SocialstreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
});

0 comments on commit 98dbb12

Please sign in to comment.