|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Support\Facades\Config; |
| 4 | +use Illuminate\Support\Facades\Session; |
| 5 | +use Laravel\WorkOS\Http\Requests\AuthKitLoginRequest; |
| 6 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + Config::set('services.workos.client_id', 'test_client_id'); |
| 10 | + Config::set('services.workos.secret', 'test_secret'); |
| 11 | + Config::set('services.workos.redirect_url', 'https://laravel.com/authenticate'); |
| 12 | + |
| 13 | + $this->request = AuthKitLoginRequest::create('/', 'GET'); |
| 14 | + $this->request->setLaravelSession(app('session.store')); |
| 15 | +}); |
| 16 | + |
| 17 | +it('redirects to WorkOS without screen hint', function () { |
| 18 | + $response = $this->request->redirect(); |
| 19 | + |
| 20 | + expect($response)->toBeInstanceOf(RedirectResponse::class); |
| 21 | + |
| 22 | + expect($response->headers->get('Location'))->toContain('https://api.workos.com/user_management/authorize') |
| 23 | + ->toContain('client_id=test_client_id') |
| 24 | + ->toContain('response_type=code') |
| 25 | + ->toContain('redirect_uri='.urlencode('https://laravel.com/authenticate')) |
| 26 | + ->toContain('provider=authkit') |
| 27 | + ->not->toContain('screen_hint'); |
| 28 | +}); |
| 29 | + |
| 30 | +it('redirects to WorkOS with sign-in screen hint', function () { |
| 31 | + $response = $this->request->redirect(['screenHint' => 'sign-in']); |
| 32 | + |
| 33 | + expect($response)->toBeInstanceOf(RedirectResponse::class); |
| 34 | + |
| 35 | + expect($response->headers->get('Location')) |
| 36 | + ->toContain('https://api.workos.com/user_management/authorize') |
| 37 | + ->toContain('screen_hint=sign-in'); |
| 38 | +}); |
| 39 | + |
| 40 | +it('redirects to WorkOS with sign-up screen hint', function () { |
| 41 | + $response = $this->request->redirect(['screenHint' => 'sign-up']); |
| 42 | + |
| 43 | + expect($response)->toBeInstanceOf(RedirectResponse::class); |
| 44 | + |
| 45 | + expect($response->headers->get('Location')) |
| 46 | + ->toContain('https://api.workos.com/user_management/authorize') |
| 47 | + ->toContain('screen_hint=sign-up'); |
| 48 | +}); |
| 49 | + |
| 50 | +it('stores state in session', function () { |
| 51 | + $response = $this->request->redirect(); |
| 52 | + |
| 53 | + $sessionState = Session::get('state'); |
| 54 | + expect($sessionState)->not->toBeNull(); |
| 55 | + |
| 56 | + $decodedState = json_decode($sessionState, true); |
| 57 | + |
| 58 | + expect($decodedState) |
| 59 | + ->toHaveKey('state') |
| 60 | + ->toHaveKey('previous_url') |
| 61 | + ->and($decodedState['state'])->toHaveLength(20); |
| 62 | +}); |
| 63 | + |
| 64 | +it('includes state in the authorization URL', function () { |
| 65 | + $response = $this->request->redirect(); |
| 66 | + |
| 67 | + $location = $response->headers->get('Location'); |
| 68 | + expect($location)->toContain('state='); |
| 69 | + |
| 70 | + parse_str(parse_url($location, PHP_URL_QUERY), $queryParams); |
| 71 | + |
| 72 | + expect($queryParams)->toHaveKey('state'); |
| 73 | + |
| 74 | + $sessionState = json_decode(Session::get('state'), true); |
| 75 | + $urlState = json_decode($queryParams['state'], true); |
| 76 | + |
| 77 | + expect($urlState)->toBe($sessionState); |
| 78 | +}); |
| 79 | + |
| 80 | +it('passes all parameters correctly to getAuthorizationUrl', function () { |
| 81 | + $response = $this->request->redirect(['screenHint' => 'sign-up']); |
| 82 | + |
| 83 | + expect($response->headers->get('Location'))->not->toBeNull() |
| 84 | + ->toBeString() |
| 85 | + ->toContain('https://api.workos.com/user_management/authorize') |
| 86 | + ->toContain('client_id=test_client_id') |
| 87 | + ->toContain('response_type=code') |
| 88 | + ->toContain('redirect_uri='.urlencode('https://laravel.com/authenticate')) |
| 89 | + ->toContain('provider=authkit') |
| 90 | + ->toContain('screen_hint=sign-up') |
| 91 | + ->toContain('state='); |
| 92 | +}); |
| 93 | + |
| 94 | +it('supports domain hint parameter', function () { |
| 95 | + $response = $this->request->redirect(['domainHint' => 'laravel.com']); |
| 96 | + |
| 97 | + expect($response->headers->get('Location')) |
| 98 | + ->toContain('domain_hint=laravel.com'); |
| 99 | +}); |
| 100 | + |
| 101 | +it('supports login hint parameter', function () { |
| 102 | + $response = $this-> request-> redirect([ 'loginHint' => '[email protected]']); |
| 103 | + |
| 104 | + expect($response->headers->get('Location')) |
| 105 | + -> toContain( 'login_hint='. urlencode( '[email protected]')); |
| 106 | +}); |
| 107 | + |
| 108 | +it('supports multiple parameters at once', function () { |
| 109 | + $response = $this->request->redirect([ |
| 110 | + 'screenHint' => 'sign-in', |
| 111 | + 'domainHint' => 'laravel.com', |
| 112 | + 'loginHint' => '[email protected]', |
| 113 | + ]); |
| 114 | + |
| 115 | + expect($response->headers->get('Location'))->toContain('screen_hint=sign-in') |
| 116 | + ->toContain('domain_hint=laravel.com') |
| 117 | + -> toContain( 'login_hint='. urlencode( '[email protected]')); |
| 118 | +}); |
0 commit comments