From 0e9060d0a6f36417de15b5736fbd98de4b787813 Mon Sep 17 00:00:00 2001 From: Matt Stauffer Date: Fri, 10 Feb 2023 18:46:58 -0500 Subject: [PATCH] Update `unlink` command to also `unsecure`, if necessary --- cli/Valet/Site.php | 15 ++++++++++++--- cli/app.php | 11 ++++++++++- tests/CliTest.php | 33 +++++++++++++++++++++++++++++++++ tests/SiteTest.php | 20 ++++++++++++++++++++ 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index 6ea0d14..5a48114 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -15,7 +15,7 @@ public function __construct(public Brew $brew, public Configuration $config, pub /** * Get the name of the site. */ - private function getRealSiteName(?string $name): string + private function getSiteLinkName(?string $name): string { if (! is_null($name)) { return $name; @@ -25,7 +25,7 @@ private function getRealSiteName(?string $name): string return $link; } - return basename(getcwd()); + throw new DomainException(basename(getcwd()).' is not linked.'); } /** @@ -42,6 +42,8 @@ private function getLinkNameByCurrentDir(): ?string if ($count > 1) { throw new DomainException("There are {$count} links related to the current directory, please specify the name: valet unlink ."); } + + return null; } /** @@ -283,7 +285,7 @@ public function getSites(string $path, Collection $certs): Collection */ public function unlink(?string $name = null): string { - $name = $this->getRealSiteName($name); + $name = $this->getSiteLinkName($name); if ($this->files->exists($path = $this->sitesPath($name))) { $this->files->unlink($path); @@ -433,6 +435,13 @@ public function secured(): array })->unique()->values()->all(); } + public function isSecured(string $site): bool + { + $tld = $this->config->read()['tld']; + + return in_array($site.'.'.$tld, $this->secured()); + } + /** * Secure the given host with TLS. * diff --git a/cli/app.php b/cli/app.php index 7bd4889..e3e2f22 100644 --- a/cli/app.php +++ b/cli/app.php @@ -217,7 +217,16 @@ function (ConsoleCommandEvent $event) { * Unlink a link from the Valet links directory. */ $app->command('unlink [name]', function (OutputInterface $output, $name) { - info('The ['.Site::unlink($name).'] symbolic link has been removed.'); + $name = Site::unlink($name); + info('The ['.$name.'] symbolic link has been removed.'); + + if (Site::isSecured($name)) { + info('Unsecuring '.$name.'...'); + + Site::unsecure(Site::domain($name)); + + Nginx::restart(); + } })->descriptions('Remove the specified Valet link'); /** diff --git a/tests/CliTest.php b/tests/CliTest.php index a509153..9bc40a0 100644 --- a/tests/CliTest.php +++ b/tests/CliTest.php @@ -269,6 +269,18 @@ public function test_unlink_command() { [$app, $tester] = $this->appAndTester(); + Site::link(getcwd(), basename(getcwd())); + + $tester->run(['command' => 'unlink']); + $tester->assertCommandIsSuccessful(); + + $this->assertEquals(0, Site::links()->count()); + } + + public function test_unlink_command_with_parameter() + { + [$app, $tester] = $this->appAndTester(); + Site::link(__DIR__.'/fixtures/Parked/Sites/my-best-site', 'tighten'); $tester->run(['command' => 'unlink', 'name' => 'tighten']); @@ -277,6 +289,27 @@ public function test_unlink_command() $this->assertEquals(0, Site::links()->count()); } + public function test_unlink_command_unsecures_as_well() + { + [$app, $tester] = $this->appAndTester(); + + Site::link(__DIR__.'/fixtures/Parked/Sites/my-best-site', 'tighten'); + + $site = Mockery::mock(RealSite::class); + $site->shouldReceive('domain')->with('tighten')->once()->andReturn('tighten.test'); + $site->shouldReceive('unlink')->with('tighten')->once()->andReturn('tighten'); + $site->shouldReceive('unsecure')->with('tighten.test')->once(); + $site->shouldReceive('isSecured')->with('tighten')->once()->andReturn(true); + swap(RealSite::class, $site); + + $nginx = Mockery::mock(Nginx::class); + $nginx->shouldReceive('restart')->once(); + swap(Nginx::class, $nginx); + + $tester->run(['command' => 'unlink', 'name' => 'tighten']); + $tester->assertCommandIsSuccessful(); + } + public function test_secure_command() { [$app, $tester] = $this->appAndTester(); diff --git a/tests/SiteTest.php b/tests/SiteTest.php index 3230d60..7c00970 100644 --- a/tests/SiteTest.php +++ b/tests/SiteTest.php @@ -900,6 +900,26 @@ public function test_it_returns_secured_sites() $this->assertSame(['helloworld.tld'], $sites); } + public function test_it_returns_true_if_a_site_is_secured() + { + $files = Mockery::mock(Filesystem::class); + $files->shouldReceive('scandir') + ->once() + ->andReturn(['helloworld.tld.crt', '.DS_Store']); + + $config = Mockery::mock(Configuration::class); + $config->shouldReceive('read') + ->once() + ->andReturn(['tld' => 'tld']); + + swap(Filesystem::class, $files); + swap(Configuration::class, $config); + + $site = resolve(Site::class); + + $this->assertTrue($site->isSecured('helloworld')); + } + public function test_it_can_read_valet_rc_files() { resolve(Configuration::class)->addPath(__DIR__.'/fixtures/Parked/Sites');