|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Spatie\UptimeMonitor\Test\Integration\Models\Traits; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | +use Exception; |
| 7 | +use Illuminate\Support\Facades\Event; |
| 8 | +use Spatie\SslCertificate\Downloader; |
| 9 | +use Spatie\SslCertificate\SslCertificate; |
| 10 | +use Spatie\UptimeMonitor\Events\CertificateCheckFailed; |
| 11 | +use Spatie\UptimeMonitor\Events\CertificateCheckSucceeded; |
| 12 | +use Spatie\UptimeMonitor\Events\CertificateExpiresSoon; |
| 13 | +use Spatie\UptimeMonitor\Models\Enums\CertificateStatus; |
| 14 | +use Spatie\UptimeMonitor\Models\Monitor; |
| 15 | +use Spatie\UptimeMonitor\Test\TestCase; |
| 16 | + |
| 17 | +class SupportsCertificateCheckTest extends TestCase |
| 18 | +{ |
| 19 | + protected Monitor $monitor; |
| 20 | + protected int $daysBeforeExpiration; |
| 21 | + protected SslCertificate $certificate; |
| 22 | + protected const DOMAIN = 'https://google.com'; |
| 23 | + |
| 24 | + public function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + Event::fake(); |
| 29 | + |
| 30 | + $this->monitor = Monitor::factory()->create([ |
| 31 | + 'certificate_check_enabled' => true, |
| 32 | + 'certificate_status' => CertificateStatus::NOT_YET_CHECKED, |
| 33 | + 'url' => self::DOMAIN, |
| 34 | + ]); |
| 35 | + |
| 36 | + $this->certificate = Downloader::downloadCertificateFromUrl(self::DOMAIN); |
| 37 | + $this->daysBeforeExpiration = config( |
| 38 | + 'uptime-monitor.certificate_check.fire_expiring_soon_event_if_certificate_expires_within_days' |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + /** @test */ |
| 43 | + public function it_can_set_valid_certificate_not_within_expiration_range() |
| 44 | + { |
| 45 | + // Collect |
| 46 | + Carbon::setTestNow($this->certificate->expirationDate()->subDays($this->daysBeforeExpiration + 1)); |
| 47 | + |
| 48 | + // Act |
| 49 | + $this->monitor->setCertificate($this->certificate); |
| 50 | + |
| 51 | + // Assert |
| 52 | + $this->monitor->fresh(); |
| 53 | + $this->assertSame(CertificateStatus::VALID, $this->monitor->certificate_status); |
| 54 | + $this->assertSame($this->certificate->getIssuer(), $this->monitor->certificate_issuer); |
| 55 | + $this->assertTrue($this->monitor->certificate_expiration_date->isSameDay($this->certificate->expirationDate())); |
| 56 | + Event::assertDispatched(CertificateCheckSucceeded::class); |
| 57 | + Event::assertNotDispatched(CertificateCheckFailed::class); |
| 58 | + Event::assertNotDispatched(CertificateExpiresSoon::class); |
| 59 | + } |
| 60 | + |
| 61 | + /** @test */ |
| 62 | + public function it_can_set_valid_certificate_within_expiration_range() |
| 63 | + { |
| 64 | + // Collect |
| 65 | + Carbon::setTestNow($this->certificate->expirationDate()->subDays($this->daysBeforeExpiration - 1)); |
| 66 | + |
| 67 | + // Act |
| 68 | + $this->monitor->setCertificate($this->certificate); |
| 69 | + |
| 70 | + // Assert |
| 71 | + $this->monitor->fresh(); |
| 72 | + $this->assertSame(CertificateStatus::VALID, $this->monitor->certificate_status); |
| 73 | + $this->assertSame($this->certificate->getIssuer(), $this->monitor->certificate_issuer); |
| 74 | + $this->assertTrue($this->monitor->certificate_expiration_date->isSameDay($this->certificate->expirationDate())); |
| 75 | + Event::assertDispatched(CertificateCheckSucceeded::class); |
| 76 | + Event::assertDispatched(CertificateExpiresSoon::class); |
| 77 | + Event::assertNotDispatched(CertificateCheckFailed::class); |
| 78 | + } |
| 79 | + |
| 80 | + /** @test */ |
| 81 | + public function it_can_set_invalid_certificate() |
| 82 | + { |
| 83 | + // Collect |
| 84 | + Carbon::setTestNow($this->certificate->expirationDate()->addDay()); |
| 85 | + |
| 86 | + // Act |
| 87 | + $this->monitor->setCertificate($this->certificate); |
| 88 | + |
| 89 | + // Assert |
| 90 | + $this->monitor->fresh(); |
| 91 | + $this->assertSame(CertificateStatus::INVALID, $this->monitor->certificate_status); |
| 92 | + $this->assertSame('The certificate has expired', $this->monitor->certificate_check_failure_reason); |
| 93 | + $this->assertSame($this->certificate->getIssuer(), $this->monitor->certificate_issuer); |
| 94 | + $this->assertTrue($this->monitor->certificate_expiration_date->isSameDay($this->certificate->expirationDate())); |
| 95 | + Event::assertDispatched(CertificateCheckFailed::class); |
| 96 | + Event::assertNotDispatched(CertificateCheckSucceeded::class); |
| 97 | + Event::assertNotDispatched(CertificateExpiresSoon::class); |
| 98 | + } |
| 99 | + |
| 100 | + /** @test */ |
| 101 | + public function it_can_set_certificate_exception() |
| 102 | + { |
| 103 | + // Collect |
| 104 | + $exception = new Exception('Certificate check failed'); |
| 105 | + |
| 106 | + // Act |
| 107 | + $this->monitor->setCertificateException($exception); |
| 108 | + |
| 109 | + // Assert |
| 110 | + $this->monitor->fresh(); |
| 111 | + $this->assertSame(CertificateStatus::INVALID, $this->monitor->certificate_status); |
| 112 | + $this->assertSame('Certificate check failed', $this->monitor->certificate_check_failure_reason); |
| 113 | + $this->assertSame('', $this->monitor->certificate_issuer); |
| 114 | + $this->assertNull($this->monitor->certificate_expiration_date); |
| 115 | + Event::assertDispatched(CertificateCheckFailed::class); |
| 116 | + } |
| 117 | +} |
0 commit comments