once()->with('google')->andReturn($driver); $driver->shouldReceive('scopes')->once()->with(['openid', 'profile', 'email'])->andReturnSelf(); $driver->shouldReceive('with')->once()->with(['prompt' => 'select_account'])->andReturnSelf(); $driver->shouldReceive('redirect')->once()->andReturn(new RedirectResponse('https://accounts.google.com')); $encodedReturn = rtrim(strtr(base64_encode('http://localhost/test'), '+/', '-_'), '='); $response = $this->get('/event-admin/auth/google?return_to='.$encodedReturn); $response->assertRedirect('https://accounts.google.com'); $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' => 'google-id-123', 'name' => 'Google Tenant Admin', 'email' => $user->email, ]); $driver = Mockery::mock(); Socialite::shouldReceive('driver')->once()->with('google')->andReturn($driver); $driver->shouldReceive('user')->once()->andReturn($socialiteUser); $targetUrl = 'http://localhost:8000/api/v1/oauth/authorize?foo=bar'; $encodedReturn = rtrim(strtr(base64_encode($targetUrl), '+/', '-_'), '='); $this->withSession([ 'tenant_oauth_return_to' => $encodedReturn, ]); $response = $this->get('/event-admin/auth/google/callback'); $response->assertRedirect($targetUrl); $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('google')->andReturn($driver); $driver->shouldReceive('user')->once()->andReturn($socialiteUser); $response = $this->get('/event-admin/auth/google/callback'); $response->assertRedirect(); $this->assertStringContainsString('error=google_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('google')->andReturn($driver); $driver->shouldReceive('user')->once()->andThrow(new \RuntimeException('boom')); $response = $this->get('/event-admin/auth/google/callback'); $response->assertRedirect(); $this->assertStringContainsString('error=google_failed', $response->headers->get('Location')); } }