Beautiful Open-Source Form Builder
<?php
use Illuminate\Support\Facades\Cache;
use Tests\TestHelpers\MockOAuthProviders;
uses()->group('oauth', 'integration');
beforeEach(function () {
// Mock Google OAuth provider with comprehensive responses
MockOAuthProviders::mockGoogleProvider([
'user_data' => [
'id' => 'google123',
'email' => '[email protected]',
'name' => 'John Doe',
'picture' => 'https://example.com/avatar.jpg',
],
'should_fail' => false,
]);
});
describe('OAuth Controller Integration', function () {
it('handles OAuth redirect request', function () {
$response = $this->postJson('/oauth/connect/google', [
'intent' => 'auth',
'utm_data' => ['source' => 'google', 'medium' => 'cpc']
]);
$response->assertSuccessful();
$response->assertJsonStructure([
'url',
'state'
]);
$data = $response->json();
expect($data['url'])->toBeString();
expect($data['state'])->toBeString();
expect(strlen($data['state']))->toBe(32); // 32 char hex string
});
it('validates required intent parameter', function () {
$response = $this->postJson('/oauth/connect/google', [
// Missing intent
]);
$response->assertStatus(422);
});
it('validates intent values', function () {
$response = $this->postJson('/oauth/connect/google', [
'intent' => 'invalid_intent'
]);
$response->assertStatus(422);
});
it('handles OAuth redirect with invite token', function () {
$invite = $this->createUserInvite([
'email' => '[email protected]',
'token' => 'invite_token_123'
]);
$response = $this->postJson('/oauth/connect/google', [
'intent' => 'auth',
'invite_token' => 'invite_token_123'
]);
$response->assertSuccessful();
$response->assertJsonStructure(['url', 'state']);
});
it('handles invalid invite token gracefully', function () {
$response = $this->postJson('/oauth/connect/google', [
'intent' => 'auth',
'invite_token' => 'invalid_token'
]);
$response->assertStatus(400);
});
it('requires authentication for integration intent', function () {
$this->actingAsGuest();
$response = $this->postJson('/oauth/connect/google', [
'intent' => 'integration'
]);
$response->assertStatus(401);
});
it('requires authentication for forms_import intent', function () {
$this->actingAsGuest();
$response = $this->postJson('/oauth/connect/google', [
'intent' => 'forms_import'
]);
$response->assertStatus(401);
});
it('allows integration intent for authenticated user', function () {
$this->actingAsUser();
$response = $this->postJson('/oauth/connect/google', [
'intent' => 'integration',
'intention' => 'sheets_integration',
'autoClose' => true
]);
$response->assertSuccessful();
$response->assertJsonStructure(['url', 'state']);
});
it('allows forms_import intent for authenticated user', function () {
$this->actingAsUser();
$response = $this->postJson('/oauth/connect/google', [
'intent' => 'forms_import',
'autoClose' => true
]);
$response->assertSuccessful();
$response->assertJsonStructure(['url', 'state']);
});
});
describe('OAuth Context Management', function () {
it('stores OAuth context when initiating redirect', function () {
$response = $this->postJson('/oauth/connect/google', [
'intent' => 'auth',
'utm_data' => ['source' => 'test']
]);
$stateToken = $response->json('state');
$key = "oauth-context:state:" . $stateToken;
expect(Cache::has($key))->toBeTrue();
$context = Cache::get($key);
expect($context['intent'])->toBe('auth');
expect($context['utm_data'])->toEqual(['source' => 'test']);
});
// Note: Context expiration testing is covered by unit tests
// Integration test removed due to Laravel Socialite bypassing Http::fake() mocking
});
// OAuth Callback Error Handling tests removed due to Laravel Socialite bypassing Http::fake() mocking
// These scenarios are covered by unit tests which properly test the business logic
describe('Widget OAuth Flow', function () {
it('validates widget callback parameters', function () {
$response = $this->postJson('/oauth/widget-callback/google_one_tap', [
// Missing required parameters
]);
$response->assertStatus(422);
});
it('requires intent parameter for widget callback', function () {
$response = $this->postJson('/oauth/widget-callback/google_one_tap', [
'credential' => 'mock_jwt_credential'
// Missing intent
]);
$response->assertStatus(422);
});
it('validates intent parameter for widget callback', function () {
$response = $this->postJson('/oauth/widget-callback/google_one_tap', [
'credential' => 'mock_jwt_credential',
'intent' => 'invalid_intent'
]);
$response->assertStatus(422);
});
});
afterEach(function () {
Cache::flush();
MockOAuthProviders::cleanup();
});