|
| 1 | +<?php |
| 2 | + |
| 3 | +use CoyoteCert\CoyoteCert; |
| 4 | +use CoyoteCert\Enums\KeyType; |
| 5 | +use CoyoteCert\Provider\Pebble; |
| 6 | +use CoyoteCert\Storage\InMemoryStorage; |
| 7 | +use CoyoteCert\Storage\StoredCertificate; |
| 8 | +use Tests\Integration\Helpers\NoOpHttp01Handler; |
| 9 | + |
| 10 | +$pebbleUrl = getenv('PEBBLE_URL') ?: 'https://localhost:14000/dir'; |
| 11 | + |
| 12 | +function pebble(): Pebble |
| 13 | +{ |
| 14 | + return new Pebble(url: getenv('PEBBLE_URL') ?: 'https://localhost:14000/dir', verifyTls: false); |
| 15 | +} |
| 16 | + |
| 17 | +it('issues a certificate with default key types (RSA account, EC_P256 cert)', function () use ($pebbleUrl) { |
| 18 | + $cert = CoyoteCert::with(pebble()) |
| 19 | + ->storage(new InMemoryStorage()) |
| 20 | + ->domains('test.example.com') |
| 21 | + ->challenge(new NoOpHttp01Handler()) |
| 22 | + ->skipLocalTest() |
| 23 | + ->issue(); |
| 24 | + |
| 25 | + expect($cert)->toBeInstanceOf(StoredCertificate::class); |
| 26 | + expect($cert->certificate)->toContain('-----BEGIN CERTIFICATE-----'); |
| 27 | + expect($cert->privateKey)->toContain('-----BEGIN'); |
| 28 | + expect($cert->fullchain)->toContain('-----BEGIN CERTIFICATE-----'); |
| 29 | + expect($cert->domains)->toBe(['test.example.com']); |
| 30 | + expect(openssl_x509_parse($cert->certificate))->toBeArray(); |
| 31 | +})->skip(!getenv('PEBBLE_URL'), 'Set PEBBLE_URL to run Pebble integration tests'); |
| 32 | + |
| 33 | +it('issues a certificate with an EC P-256 account key', function () { |
| 34 | + $cert = CoyoteCert::with(pebble()) |
| 35 | + ->storage(new InMemoryStorage()) |
| 36 | + ->domains('ec-account.example.com') |
| 37 | + ->accountKeyType(KeyType::EC_P256) |
| 38 | + ->challenge(new NoOpHttp01Handler()) |
| 39 | + ->skipLocalTest() |
| 40 | + ->issue(); |
| 41 | + |
| 42 | + expect($cert->certificate)->toContain('-----BEGIN CERTIFICATE-----'); |
| 43 | +})->skip(!getenv('PEBBLE_URL'), 'Set PEBBLE_URL to run Pebble integration tests'); |
| 44 | + |
| 45 | +it('reuses an existing account on a second issue', function () { |
| 46 | + $storage = new InMemoryStorage(); |
| 47 | + |
| 48 | + $first = CoyoteCert::with(pebble()) |
| 49 | + ->storage($storage) |
| 50 | + ->domains('reuse.example.com') |
| 51 | + ->challenge(new NoOpHttp01Handler()) |
| 52 | + ->skipLocalTest() |
| 53 | + ->issue(); |
| 54 | + |
| 55 | + // Second call reuses the account key already in $storage |
| 56 | + $second = CoyoteCert::with(pebble()) |
| 57 | + ->storage($storage) |
| 58 | + ->domains('reuse.example.com') |
| 59 | + ->challenge(new NoOpHttp01Handler()) |
| 60 | + ->skipLocalTest() |
| 61 | + ->issue(); |
| 62 | + |
| 63 | + expect($first->certificate)->not->toBe($second->certificate); |
| 64 | + expect($second->certificate)->toContain('-----BEGIN CERTIFICATE-----'); |
| 65 | +})->skip(!getenv('PEBBLE_URL'), 'Set PEBBLE_URL to run Pebble integration tests'); |
| 66 | + |
| 67 | +it('issueOrRenew returns the cached cert when still valid', function () { |
| 68 | + $storage = new InMemoryStorage(); |
| 69 | + |
| 70 | + $issued = CoyoteCert::with(pebble()) |
| 71 | + ->storage($storage) |
| 72 | + ->domains('cached.example.com') |
| 73 | + ->challenge(new NoOpHttp01Handler()) |
| 74 | + ->skipLocalTest() |
| 75 | + ->issue(); |
| 76 | + |
| 77 | + $returned = CoyoteCert::with(pebble()) |
| 78 | + ->storage($storage) |
| 79 | + ->domains('cached.example.com') |
| 80 | + ->challenge(new NoOpHttp01Handler()) |
| 81 | + ->skipLocalTest() |
| 82 | + ->issueOrRenew(daysBeforeExpiry: 1); // very low threshold — cert won't expire this soon |
| 83 | + |
| 84 | + expect($returned->certificate)->toBe($issued->certificate); |
| 85 | +})->skip(!getenv('PEBBLE_URL'), 'Set PEBBLE_URL to run Pebble integration tests'); |
| 86 | + |
| 87 | +it('issues a certificate with the shortlived profile', function () { |
| 88 | + $cert = CoyoteCert::with(pebble()) |
| 89 | + ->storage(new InMemoryStorage()) |
| 90 | + ->domains('profile.example.com') |
| 91 | + ->profile('shortlived') |
| 92 | + ->challenge(new NoOpHttp01Handler()) |
| 93 | + ->skipLocalTest() |
| 94 | + ->issue(); |
| 95 | + |
| 96 | + expect($cert->certificate)->toContain('-----BEGIN CERTIFICATE-----'); |
| 97 | +})->skip(!getenv('PEBBLE_URL'), 'Set PEBBLE_URL to run Pebble integration tests'); |
0 commit comments