Skip to content

Commit

Permalink
Merge pull request #3 from JaZo/bugfix/check-expired-certificates
Browse files Browse the repository at this point in the history
Properly mark check as failed for expired certificates
  • Loading branch information
VictoRD11 authored Oct 12, 2023
2 parents 41720dd + 7e428d9 commit 7040cc6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/SslCertificationExpiredCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace VictoRD11\SslCertificationHealthCheck;

use Carbon\Carbon;
use Spatie\Health\Checks\Check;
use Spatie\Health\Checks\Result;
use Spatie\SslCertificate\SslCertificate;
Expand Down Expand Up @@ -60,8 +61,8 @@ public function run(): Result
throw InvalidUrl::make();
}

$certificate = SslCertificate::createForHostName($this->url);
$daysUntilExpired = $certificate->expirationDate()->diffInDays();
$certificate = SslCertificate::createForHostName($this->url, 30, false);
$daysUntilExpired = Carbon::now()->diffInDays($certificate->expirationDate(), false);

$result = Result::make()
->meta(['days_until_expired' => $daysUntilExpired])
Expand Down
12 changes: 10 additions & 2 deletions tests/SslCertificationExpiredCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
expect($result->status)->toBe(Status::ok());
});

it('certification is expired with warning', function () {
it('certification will expire with warning', function () {
$result = SslCertificationExpiredCheck::new()
->url('google.com')
->warnWhenSslCertificationExpiringDay(9999)
Expand All @@ -25,11 +25,19 @@
expect($result->status)->toBe(Status::warning());
});

it('certification is expired with fail', function () {
it('certification will expire with fail', function () {
$result = SslCertificationExpiredCheck::new()
->url('google.com')
->failWhenSslCertificationExpiringDay(9999)
->run();

expect($result->status)->toBe(Status::failed());
});

it('certification is expired with fail', function () {
$result = SslCertificationExpiredCheck::new()
->url('expired.badssl.com')
->run();

expect($result->status)->toBe(Status::failed());
});

0 comments on commit 7040cc6

Please sign in to comment.