Add Facebook social login
This commit is contained in:
130
tests/Feature/Auth/TenantAdminFacebookControllerTest.php
Normal file
130
tests/Feature/Auth/TenantAdminFacebookControllerTest.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
use Laravel\Socialite\Two\User as SocialiteUser;
|
||||
use Mockery;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TenantAdminFacebookControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_redirect_stores_return_to_and_issues_facebook_redirect(): void
|
||||
{
|
||||
$driver = Mockery::mock();
|
||||
Socialite::shouldReceive('driver')->once()->with('facebook')->andReturn($driver);
|
||||
$driver->shouldReceive('scopes')->once()->with(['email'])->andReturnSelf();
|
||||
$driver->shouldReceive('fields')->once()->with(['name', 'email', 'first_name', 'last_name'])->andReturnSelf();
|
||||
$driver->shouldReceive('redirect')->once()->andReturn(new RedirectResponse('https://facebook.com/auth'));
|
||||
|
||||
$encodedReturn = rtrim(strtr(base64_encode(url('/test')), '+/', '-_'), '=');
|
||||
|
||||
$response = $this->get('/event-admin/auth/facebook?return_to='.$encodedReturn);
|
||||
|
||||
$response->assertRedirect('https://facebook.com/auth');
|
||||
$this->assertSame($encodedReturn, session('tenant_oauth_return_to'));
|
||||
}
|
||||
|
||||
public function test_callback_logs_in_tenant_admin_and_redirects_to_encoded_target(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'role' => 'tenant_admin',
|
||||
]);
|
||||
|
||||
$socialiteUser = tap(new SocialiteUser)->map([
|
||||
'id' => 'facebook-id-123',
|
||||
'name' => 'Facebook Tenant Admin',
|
||||
'email' => $user->email,
|
||||
]);
|
||||
|
||||
$driver = Mockery::mock();
|
||||
Socialite::shouldReceive('driver')->once()->with('facebook')->andReturn($driver);
|
||||
$driver->shouldReceive('user')->once()->andReturn($socialiteUser);
|
||||
|
||||
$targetUrl = url('/event-admin/dashboard?foo=bar');
|
||||
$encodedReturn = rtrim(strtr(base64_encode($targetUrl), '+/', '-_'), '=');
|
||||
|
||||
$this->withSession([
|
||||
'tenant_oauth_return_to' => $encodedReturn,
|
||||
]);
|
||||
|
||||
$response = $this->get('/event-admin/auth/facebook/callback');
|
||||
|
||||
$response->assertRedirect($targetUrl);
|
||||
$this->assertAuthenticatedAs($user);
|
||||
}
|
||||
|
||||
public function test_callback_ignores_intended_and_uses_admin_fallback(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'role' => 'tenant_admin',
|
||||
]);
|
||||
|
||||
$socialiteUser = tap(new SocialiteUser)->map([
|
||||
'id' => 'facebook-id-456',
|
||||
'name' => 'Facebook Tenant Admin',
|
||||
'email' => $user->email,
|
||||
]);
|
||||
|
||||
$driver = Mockery::mock();
|
||||
Socialite::shouldReceive('driver')->once()->with('facebook')->andReturn($driver);
|
||||
$driver->shouldReceive('user')->once()->andReturn($socialiteUser);
|
||||
|
||||
$this->withSession([
|
||||
'url.intended' => '/packages',
|
||||
]);
|
||||
|
||||
$response = $this->get('/event-admin/auth/facebook/callback');
|
||||
|
||||
$response->assertRedirect('/event-admin/dashboard');
|
||||
$this->assertAuthenticatedAs($user);
|
||||
}
|
||||
|
||||
public function test_callback_redirects_back_when_user_not_found(): void
|
||||
{
|
||||
$socialiteUser = tap(new SocialiteUser)->map([
|
||||
'id' => 'missing-user',
|
||||
'name' => 'Unknown User',
|
||||
'email' => 'unknown@example.com',
|
||||
]);
|
||||
|
||||
$driver = Mockery::mock();
|
||||
Socialite::shouldReceive('driver')->once()->with('facebook')->andReturn($driver);
|
||||
$driver->shouldReceive('user')->once()->andReturn($socialiteUser);
|
||||
|
||||
$response = $this->get('/event-admin/auth/facebook/callback');
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertStringContainsString('error=facebook_no_match', $response->headers->get('Location'));
|
||||
$this->assertFalse(Auth::check());
|
||||
}
|
||||
|
||||
public function test_callback_handles_socialite_failure(): void
|
||||
{
|
||||
$driver = Mockery::mock();
|
||||
Socialite::shouldReceive('driver')->once()->with('facebook')->andReturn($driver);
|
||||
$driver->shouldReceive('user')->once()->andThrow(new \RuntimeException('boom'));
|
||||
|
||||
$response = $this->get('/event-admin/auth/facebook/callback');
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertStringContainsString('error=facebook_failed', $response->headers->get('Location'));
|
||||
}
|
||||
}
|
||||
112
tests/Feature/CheckoutFacebookControllerTest.php
Normal file
112
tests/Feature/CheckoutFacebookControllerTest.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Package;
|
||||
use App\Models\User;
|
||||
use App\Support\CheckoutRoutes;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Laravel\Socialite\Contracts\Factory as SocialiteFactory;
|
||||
use Laravel\Socialite\Contracts\Provider as SocialiteProvider;
|
||||
use Laravel\Socialite\Contracts\User as SocialiteUserContract;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CheckoutFacebookControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_redirect_persists_package_context_and_delegates_to_facebook(): void
|
||||
{
|
||||
$package = Package::factory()->create();
|
||||
|
||||
$provider = Mockery::mock(SocialiteProvider::class);
|
||||
$provider->shouldReceive('scopes')->andReturnSelf();
|
||||
$provider->shouldReceive('fields')->andReturnSelf();
|
||||
$provider->shouldReceive('redirect')->once()->andReturn(redirect('/facebook/auth'));
|
||||
|
||||
$this->mock(SocialiteFactory::class, function ($mock) use ($provider) {
|
||||
$mock->shouldReceive('driver')->with('facebook')->andReturn($provider);
|
||||
});
|
||||
|
||||
$response = $this->get('/checkout/auth/facebook?package_id='.$package->id.'&locale=de');
|
||||
|
||||
$response->assertRedirect('/facebook/auth');
|
||||
$this->assertSame($package->id, session('checkout_facebook_payload.package_id'));
|
||||
}
|
||||
|
||||
public function test_callback_logs_in_existing_user_and_attaches_tenant(): void
|
||||
{
|
||||
$package = Package::factory()->create(['price' => 0]);
|
||||
$existingUser = User::factory()->create([
|
||||
'email' => 'checkout-facebook@example.com',
|
||||
'pending_purchase' => false,
|
||||
]);
|
||||
|
||||
$facebookUser = Mockery::mock(SocialiteUserContract::class);
|
||||
$facebookUser->shouldReceive('getEmail')->andReturn('checkout-facebook@example.com');
|
||||
$facebookUser->shouldReceive('getName')->andReturn('Checkout Facebook');
|
||||
$facebookUser->shouldReceive('getAvatar')->andReturn(null);
|
||||
$facebookUser->shouldReceive('getRaw')->andReturn([]);
|
||||
|
||||
$provider = Mockery::mock(SocialiteProvider::class);
|
||||
$provider->shouldReceive('user')->andReturn($facebookUser);
|
||||
|
||||
$this->mock(SocialiteFactory::class, function ($mock) use ($provider) {
|
||||
$mock->shouldReceive('driver')->with('facebook')->andReturn($provider);
|
||||
});
|
||||
|
||||
$response = $this
|
||||
->withSession([
|
||||
'checkout_facebook_payload' => ['package_id' => $package->id, 'locale' => 'de'],
|
||||
])
|
||||
->get('/checkout/auth/facebook/callback');
|
||||
|
||||
$response->assertRedirect(CheckoutRoutes::wizardUrl($package->id, 'de'));
|
||||
|
||||
$this->assertAuthenticatedAs($existingUser);
|
||||
|
||||
$user = auth()->user();
|
||||
$this->assertSame('checkout-facebook@example.com', $user->email);
|
||||
$this->assertTrue($user->pending_purchase);
|
||||
$this->assertNotNull($user->tenant);
|
||||
$this->assertDatabaseHas('tenant_packages', [
|
||||
'tenant_id' => $user->tenant_id,
|
||||
'package_id' => $package->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_callback_with_missing_email_flashes_error(): void
|
||||
{
|
||||
$package = Package::factory()->create();
|
||||
|
||||
$facebookUser = Mockery::mock(SocialiteUserContract::class);
|
||||
$facebookUser->shouldReceive('getEmail')->andReturn(null);
|
||||
$facebookUser->shouldReceive('getName')->andReturn('No Email');
|
||||
$facebookUser->shouldReceive('getAvatar')->andReturn(null);
|
||||
$facebookUser->shouldReceive('getRaw')->andReturn([]);
|
||||
|
||||
$provider = Mockery::mock(SocialiteProvider::class);
|
||||
$provider->shouldReceive('user')->andReturn($facebookUser);
|
||||
|
||||
$this->mock(SocialiteFactory::class, function ($mock) use ($provider) {
|
||||
$mock->shouldReceive('driver')->with('facebook')->andReturn($provider);
|
||||
});
|
||||
|
||||
$response = $this
|
||||
->withSession([
|
||||
'checkout_facebook_payload' => ['package_id' => $package->id, 'locale' => 'en'],
|
||||
])
|
||||
->get('/checkout/auth/facebook/callback');
|
||||
|
||||
$response->assertRedirect(CheckoutRoutes::wizardUrl($package->id, 'en'));
|
||||
$response->assertSessionHas('checkout_facebook_error');
|
||||
$this->assertGuest();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user