131 lines
4.6 KiB
PHP
131 lines
4.6 KiB
PHP
<?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'));
|
|
}
|
|
}
|