|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GeneaLabs\LaravelSignInWithApple\Tests\Unit; |
| 4 | + |
| 5 | +use GeneaLabs\LaravelSignInWithApple\Http\Controllers\AppleSignInController; |
| 6 | +use GeneaLabs\LaravelSignInWithApple\Tests\UnitTestCase; |
| 7 | +use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken; |
| 8 | + |
| 9 | +class RouteRegistrationTest extends UnitTestCase |
| 10 | +{ |
| 11 | + public function testAppleRedirectRouteIsRegistered(): void |
| 12 | + { |
| 13 | + $routes = $this->app['router']->getRoutes(); |
| 14 | + |
| 15 | + $redirectRoute = $routes->getByName('apple.redirect'); |
| 16 | + |
| 17 | + $this->assertNotNull($redirectRoute); |
| 18 | + $this->assertEquals('apple/redirect', $redirectRoute->uri); |
| 19 | + $this->assertTrue(in_array('GET', $redirectRoute->methods)); |
| 20 | + } |
| 21 | + |
| 22 | + public function testAppleCallbackRouteIsRegistered(): void |
| 23 | + { |
| 24 | + $routes = $this->app['router']->getRoutes(); |
| 25 | + |
| 26 | + $callbackRoute = $routes->getByName('apple.callback'); |
| 27 | + |
| 28 | + $this->assertNotNull($callbackRoute); |
| 29 | + $this->assertEquals('apple/callback', $callbackRoute->uri); |
| 30 | + $this->assertTrue(in_array('POST', $callbackRoute->methods)); |
| 31 | + } |
| 32 | + |
| 33 | + public function testRedirectRouteUsesAppleSignInController(): void |
| 34 | + { |
| 35 | + $routes = $this->app['router']->getRoutes(); |
| 36 | + $route = $routes->getByName('apple.redirect'); |
| 37 | + |
| 38 | + $controller = $route->getAction('controller'); |
| 39 | + $this->assertStringContainsString('AppleSignInController', $controller); |
| 40 | + $this->assertStringContainsString('redirect', $controller); |
| 41 | + } |
| 42 | + |
| 43 | + public function testCallbackRouteUsesAppleSignInController(): void |
| 44 | + { |
| 45 | + $routes = $this->app['router']->getRoutes(); |
| 46 | + $route = $routes->getByName('apple.callback'); |
| 47 | + |
| 48 | + $controller = $route->getAction('controller'); |
| 49 | + $this->assertStringContainsString('AppleSignInController', $controller); |
| 50 | + $this->assertStringContainsString('callback', $controller); |
| 51 | + } |
| 52 | + |
| 53 | + public function testRoutesAreProtectedWithWebMiddleware(): void |
| 54 | + { |
| 55 | + $routes = $this->app['router']->getRoutes(); |
| 56 | + |
| 57 | + $redirectRoute = $routes->getByName('apple.redirect'); |
| 58 | + $callbackRoute = $routes->getByName('apple.callback'); |
| 59 | + |
| 60 | + $this->assertTrue(in_array('web', $redirectRoute->middleware())); |
| 61 | + $this->assertTrue(in_array('web', $callbackRoute->middleware())); |
| 62 | + } |
| 63 | + |
| 64 | + public function testCallbackRouteExcludesCsrfVerification(): void |
| 65 | + { |
| 66 | + $routes = $this->app['router']->getRoutes(); |
| 67 | + $callbackRoute = $routes->getByName('apple.callback'); |
| 68 | + |
| 69 | + $excludedMiddleware = $callbackRoute->excludedMiddleware(); |
| 70 | + $this->assertContains(VerifyCsrfToken::class, $excludedMiddleware); |
| 71 | + } |
| 72 | + |
| 73 | + public function testRoutesAreNotRegisteredWhenDisabled(): void |
| 74 | + { |
| 75 | + $this->app['config']->set('services.sign_in_with_apple.routes.enabled', false); |
| 76 | + |
| 77 | + // Clear routes and re-boot the service provider with routes disabled |
| 78 | + $this->app['router']->setRoutes(new \Illuminate\Routing\RouteCollection()); |
| 79 | + |
| 80 | + $provider = new \GeneaLabs\LaravelSignInWithApple\Providers\ServiceProvider($this->app); |
| 81 | + $provider->boot(); |
| 82 | + |
| 83 | + $routes = $this->app['router']->getRoutes(); |
| 84 | + $routes->refreshNameLookups(); |
| 85 | + |
| 86 | + $this->assertNull($routes->getByName('apple.redirect')); |
| 87 | + $this->assertNull($routes->getByName('apple.callback')); |
| 88 | + } |
| 89 | + |
| 90 | + public function testRedirectRoutePathCanBeCustomized(): void |
| 91 | + { |
| 92 | + $this->app['config']->set('services.sign_in_with_apple.routes.redirect_route', 'auth/apple/login'); |
| 93 | + |
| 94 | + $this->reloadRoutes(); |
| 95 | + |
| 96 | + $routes = $this->app['router']->getRoutes(); |
| 97 | + $redirectRoute = $routes->getByName('apple.redirect'); |
| 98 | + |
| 99 | + $this->assertNotNull($redirectRoute); |
| 100 | + $this->assertEquals('auth/apple/login', $redirectRoute->uri); |
| 101 | + } |
| 102 | + |
| 103 | + public function testCallbackRoutePathCanBeCustomized(): void |
| 104 | + { |
| 105 | + $this->app['config']->set('services.sign_in_with_apple.routes.callback_route', 'auth/apple/handle'); |
| 106 | + |
| 107 | + $this->reloadRoutes(); |
| 108 | + |
| 109 | + $routes = $this->app['router']->getRoutes(); |
| 110 | + $callbackRoute = $routes->getByName('apple.callback'); |
| 111 | + |
| 112 | + $this->assertNotNull($callbackRoute); |
| 113 | + $this->assertEquals('auth/apple/handle', $callbackRoute->uri); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Clear existing routes and reload the package route file. |
| 118 | + */ |
| 119 | + private function reloadRoutes(): void |
| 120 | + { |
| 121 | + $this->app['router']->setRoutes(new \Illuminate\Routing\RouteCollection()); |
| 122 | + |
| 123 | + require __DIR__ . '/../../routes/web.php'; |
| 124 | + |
| 125 | + $this->app['router']->getRoutes()->refreshNameLookups(); |
| 126 | + } |
| 127 | + |
| 128 | + public function testControllerCanBeOverriddenByApplication(): void |
| 129 | + { |
| 130 | + $this->app['router']->setRoutes(new \Illuminate\Routing\RouteCollection()); |
| 131 | + |
| 132 | + // Simulate an app overriding the callback route with a custom controller |
| 133 | + $this->app['router']->group(['middleware' => ['web']], function ($router) { |
| 134 | + $router->post('apple/callback', [\GeneaLabs\LaravelSignInWithApple\Tests\Fixtures\Http\Controllers\SiwaController::class, 'callback']) |
| 135 | + ->name('apple.callback'); |
| 136 | + }); |
| 137 | + |
| 138 | + $routes = $this->app['router']->getRoutes(); |
| 139 | + $routes->refreshNameLookups(); |
| 140 | + $callbackRoute = $routes->getByName('apple.callback'); |
| 141 | + |
| 142 | + $this->assertNotNull($callbackRoute); |
| 143 | + $controller = $callbackRoute->getAction('controller'); |
| 144 | + $this->assertStringContainsString('SiwaController', $controller); |
| 145 | + } |
| 146 | +} |
0 commit comments