diff --git a/app/Commands/Browser.php b/app/Commands/Browser.php index ef5410d8..70a444d7 100644 --- a/app/Commands/Browser.php +++ b/app/Commands/Browser.php @@ -28,14 +28,16 @@ public function handle() $app = $this->resolvers()->application()->from($this->argument('application')); $environment = $this->resolvers()->environment()->withApplication($app)->from($this->argument('environment')); - if (! $environment->url) { + $url = $environment->siteUrl(); + + if (! $url) { warning('No site found for this environment.'); return self::FAILURE; } - openUrl($environment->url); + openUrl($url); - outro($environment->url); + outro($url); } } diff --git a/app/Dto/Environment.php b/app/Dto/Environment.php index 65c48482..42177465 100644 --- a/app/Dto/Environment.php +++ b/app/Dto/Environment.php @@ -47,6 +47,7 @@ public function __construct( public readonly ?string $currentDeploymentId = null, public readonly array $domainIds = [], public readonly ?string $primaryDomainId = null, + public readonly ?Domain $primaryDomain = null, public readonly ?string $databaseSchemaId = null, public readonly ?string $cacheId = null, public readonly ?string $websocketApplicationId = null, @@ -134,6 +135,12 @@ public static function createFromResponse(array $response): self if (isset($relationships['primaryDomain']['data']['id'])) { $transformed['primaryDomainId'] = $relationships['primaryDomain']['data']['id']; + + $domain = collect($included)->first(fn ($item) => $item['type'] === 'domains' && $item['id'] === $transformed['primaryDomainId']); + + if ($domain) { + $transformed['primaryDomain'] = Domain::createFromResponse(['data' => $domain]); + } } if (isset($relationships['database']['data']['id'])) { @@ -161,6 +168,22 @@ public static function createFromResponse(array $response): self return self::from($transformed); } + /** + * Until hostname and SSL both verify, the custom domain may not resolve or may serve a certificate error. + */ + public function siteUrl(): string + { + if ( + $this->primaryDomain?->name + && $this->primaryDomain->hostnameStatus === 'verified' + && $this->primaryDomain->sslStatus === 'verified' + ) { + return (string) str($this->primaryDomain->name)->start('https://'); + } + + return $this->url; + } + public function colorEnum(): ?EnvironmentColor { return $this->color ? EnvironmentColor::tryFrom($this->color) : null; diff --git a/app/Prompts/MonitorDeployments.php b/app/Prompts/MonitorDeployments.php index 56aa4e09..c778bc95 100644 --- a/app/Prompts/MonitorDeployments.php +++ b/app/Prompts/MonitorDeployments.php @@ -98,7 +98,7 @@ public function display(): void $keyPressListener = KeyPressListener::for($this)->listenForQuit()->on('o', function () { if ($this->lastDeployment) { - openUrl($this->environment->url); + openUrl($this->environment->siteUrl()); } }); diff --git a/tests/Feature/BrowserTest.php b/tests/Feature/BrowserTest.php new file mode 100644 index 00000000..4fe0515a --- /dev/null +++ b/tests/Feature/BrowserTest.php @@ -0,0 +1,106 @@ +mockGit = Mockery::mock(Git::class); + $this->mockGit->shouldReceive('isRepo')->andReturn(true)->byDefault(); + $this->mockGit->shouldReceive('hasRemote')->andReturn(true)->byDefault(); + $this->mockGit->shouldReceive('getRoot')->andReturn('/tmp/test-repo')->byDefault(); + $this->mockGit->shouldReceive('currentBranch')->andReturn('main')->byDefault(); + $this->app->instance(Git::class, $this->mockGit); + + $this->mockConfig = Mockery::mock(ConfigRepository::class); + $this->mockConfig->shouldReceive('apiTokens')->andReturn(collect(['test-api-token'])); + $this->app->instance(ConfigRepository::class, $this->mockConfig); + + Process::fake(); +}); + +afterEach(function () { + MockClient::destroyGlobal(); +}); + +function browserEnvironmentResponse(?string $hostnameStatus, string $sslStatus = 'verified'): array +{ + if ($hostnameStatus === null) { + return ['data' => createEnvironmentResponse(), 'included' => []]; + } + + return [ + 'data' => createEnvironmentResponse([ + 'relationships' => [ + 'primaryDomain' => ['data' => ['id' => 'dom-1', 'type' => 'domains']], + ], + ]), + 'included' => [[ + 'id' => 'dom-1', + 'type' => 'domains', + 'attributes' => [ + 'name' => 'example.com', + 'type' => 'root', + 'hostname_status' => $hostnameStatus, + 'ssl_status' => $sslStatus, + 'origin_status' => 'verified', + ], + ]], + ]; +} + +function mockBrowserApi(?string $hostnameStatus, string $sslStatus = 'verified'): void +{ + MockClient::global([ + GetOrganizationRequest::class => MockResponse::make(organizationResponse(), 200), + GetApplicationRequest::class => MockResponse::make(['data' => createApplicationResponse()], 200), + GetEnvironmentRequest::class => MockResponse::make(browserEnvironmentResponse($hostnameStatus, $sslStatus), 200), + ]); +} + +function openedUrl(string $url): void +{ + Process::assertRan(fn (PendingProcess $process) => in_array($url, (array) $process->command, true)); +} + +it('opens the verified primary domain', function () { + mockBrowserApi('verified'); + + $this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true]) + ->assertSuccessful(); + + openedUrl('https://example.com'); +}); + +it('falls back to the vanity domain while the primary domain is unverified', function () { + mockBrowserApi('pending'); + + $this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true]) + ->assertSuccessful(); + + openedUrl('https://my-app.cloud.laravel.com'); +}); + +it('falls back to the vanity domain while the certificate is pending', function () { + mockBrowserApi('verified', 'pending'); + + $this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true]) + ->assertSuccessful(); + + openedUrl('https://my-app.cloud.laravel.com'); +}); + +it('opens the vanity domain when there is no custom domain', function () { + mockBrowserApi(null); + + $this->artisan('browser', ['application' => 'app-123', 'environment' => 'env-1', '--no-interaction' => true]) + ->assertSuccessful(); + + openedUrl('https://my-app.cloud.laravel.com'); +});