|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Logic\LicenceVerification; |
| 4 | +use App\Logic\NetifydLicenceRepository; |
| 5 | +use App\NetifydLicenceType; |
| 6 | +use Illuminate\Support\Facades\Cache; |
| 7 | +use Illuminate\Support\Facades\Http; |
| 8 | +use Mockery\MockInterface; |
| 9 | + |
| 10 | +use function Pest\Laravel\get; |
| 11 | +use function Pest\Laravel\partialMock; |
| 12 | +use function Pest\Laravel\withBasicAuth; |
| 13 | + |
| 14 | +it('cannot access enterprise licence without credentials', function () { |
| 15 | + get('/netifyd/enterprise/licence') |
| 16 | + ->assertUnauthorized() |
| 17 | + ->assertHeader('WWW-Authenticate', 'Basic'); |
| 18 | +}); |
| 19 | + |
| 20 | +it('can access enterprise licence with credentials', function () { |
| 21 | + partialMock(LicenceVerification::class, function (MockInterface $mock) { |
| 22 | + $mock->expects('enterpriseCheck') |
| 23 | + ->with('system-id', 'secret') |
| 24 | + ->andReturnTrue(); |
| 25 | + }); |
| 26 | + Cache::expects('has')->with(NetifydLicenceType::ENTERPRISE->cacheLabel())->andReturnTrue(); |
| 27 | + Cache::expects('get')->with(NetifydLicenceType::ENTERPRISE->cacheLabel())->andReturn(['license_key' => 'cache']); |
| 28 | + withBasicAuth('system-id', 'secret') |
| 29 | + ->get('/netifyd/enterprise/licence') |
| 30 | + ->assertOk() |
| 31 | + ->assertJson(['license_key' => 'cache']); |
| 32 | +}); |
| 33 | + |
| 34 | +it('serves correctly cache if present', function () { |
| 35 | + Cache::expects('has')->with(NetifydLicenceType::COMMUNITY->cacheLabel())->andReturnTrue(); |
| 36 | + Cache::expects('get')->with(NetifydLicenceType::COMMUNITY->cacheLabel())->andReturn(['license_key' => 'cached-license-key']); |
| 37 | + Http::preventStrayRequests(); |
| 38 | + Http::fake(); |
| 39 | + $response = get('/netifyd/community/licence'); |
| 40 | + $response->assertOk() |
| 41 | + ->assertJson([ |
| 42 | + 'license_key' => 'cached-license-key', |
| 43 | + ]); |
| 44 | +}); |
| 45 | + |
| 46 | +it('handles errors from netifyd server', function () { |
| 47 | + partialMock(NetifydLicenceRepository::class, function (MockInterface $mock) { |
| 48 | + $mock->expects('listLicences') |
| 49 | + ->andThrow(new Exception('Netifyd server error')); |
| 50 | + }); |
| 51 | + get('/netifyd/community/licence') |
| 52 | + ->assertInternalServerError() |
| 53 | + ->assertJson([ |
| 54 | + 'message' => 'Netifyd server error', |
| 55 | + ]); |
| 56 | +}); |
| 57 | + |
| 58 | +it('list licences', function () { |
| 59 | + $expiration = now()->addDays(2); |
| 60 | + $creation = now()->subDay(); |
| 61 | + $licence = [ |
| 62 | + 'issued_to' => NetifydLicenceType::COMMUNITY->label(), |
| 63 | + 'serial' => 'EXAMPLE-COMMUNITY-SERIAL', |
| 64 | + 'expire_at' => [ |
| 65 | + 'unix' => $expiration->unix(), |
| 66 | + ], |
| 67 | + 'created_at' => [ |
| 68 | + 'unix' => $creation->unix(), |
| 69 | + ], |
| 70 | + ]; |
| 71 | + partialMock(NetifydLicenceRepository::class, function (MockInterface $mock) use ($licence) { |
| 72 | + $mock->expects('listLicences') |
| 73 | + ->andReturn([ |
| 74 | + 'data' => [$licence], |
| 75 | + ]); |
| 76 | + }); |
| 77 | + Cache::expects('has')->with(NetifydLicenceType::COMMUNITY->cacheLabel())->andReturnFalse(); |
| 78 | + Cache::expects('put')->with(NetifydLicenceType::COMMUNITY->cacheLabel(), $licence, ($expiration->unix() - $creation->unix()) / 2); |
| 79 | + get('/netifyd/community/licence')->assertOk()->json($licence); |
| 80 | +}); |
| 81 | + |
| 82 | +it('licence not found', function () { |
| 83 | + partialMock(NetifydLicenceRepository::class, function (MockInterface $mock) { |
| 84 | + $mock->expects('listLicences') |
| 85 | + ->andReturn([ |
| 86 | + 'data' => [], |
| 87 | + ]); |
| 88 | + $mock->expects('createLicence') |
| 89 | + ->with(NetifydLicenceType::COMMUNITY) |
| 90 | + ->andreturn([]); |
| 91 | + }); |
| 92 | + get('/netifyd/community/licence'); |
| 93 | +}); |
| 94 | + |
| 95 | +it('cannot create new licence', function () { |
| 96 | + partialMock(NetifydLicenceRepository::class, function (MockInterface $mock) { |
| 97 | + $mock->expects('listLicences') |
| 98 | + ->andReturn([ |
| 99 | + 'data' => [], |
| 100 | + ]); |
| 101 | + $mock->expects('createLicence') |
| 102 | + ->with(NetifydLicenceType::COMMUNITY) |
| 103 | + ->andThrow(new Exception('Cannot create licence')); |
| 104 | + }); |
| 105 | + get('/netifyd/community/licence') |
| 106 | + ->assertInternalServerError() |
| 107 | + ->assertJson(['message' => 'Cannot create licence']); |
| 108 | +}); |
| 109 | + |
| 110 | +it('renews older licence', function () { |
| 111 | + $licence = [ |
| 112 | + 'issued_to' => NetifydLicenceType::COMMUNITY->label(), |
| 113 | + 'serial' => 'EXAMPLE-COMMUNITY-SERIAL', |
| 114 | + 'expire_at' => [ |
| 115 | + 'unix' => now()->addDay()->unix(), |
| 116 | + ], |
| 117 | + 'created_at' => [ |
| 118 | + 'unix' => now()->subDays(3)->unix(), |
| 119 | + ], |
| 120 | + ]; |
| 121 | + partialMock(NetifydLicenceRepository::class, function (MockInterface $mock) use ($licence) { |
| 122 | + $mock->expects('listLicences') |
| 123 | + ->andReturn([ |
| 124 | + 'data' => [ |
| 125 | + $licence, |
| 126 | + ], |
| 127 | + ]); |
| 128 | + $mock->expects('renewLicence') |
| 129 | + ->with(NetifydLicenceType::COMMUNITY, 'EXAMPLE-COMMUNITY-SERIAL') |
| 130 | + ->andReturn($licence); |
| 131 | + }); |
| 132 | + get('/netifyd/community/licence') |
| 133 | + ->assertOk(); |
| 134 | +}); |
| 135 | + |
| 136 | +it('cannot renew licence', function () { |
| 137 | + $licence = [ |
| 138 | + 'issued_to' => NetifydLicenceType::COMMUNITY->label(), |
| 139 | + 'serial' => 'EXAMPLE-COMMUNITY-SERIAL', |
| 140 | + 'expire_at' => [ |
| 141 | + 'unix' => now()->addDay()->unix(), |
| 142 | + ], |
| 143 | + 'created_at' => [ |
| 144 | + 'unix' => now()->subDays(3)->unix(), |
| 145 | + ], |
| 146 | + ]; |
| 147 | + partialMock(NetifydLicenceRepository::class, function (MockInterface $mock) use ($licence) { |
| 148 | + $mock->expects('listLicences') |
| 149 | + ->andReturn([ |
| 150 | + 'data' => [ |
| 151 | + $licence, |
| 152 | + ], |
| 153 | + ]); |
| 154 | + $mock->expects('renewLicence') |
| 155 | + ->with(NetifydLicenceType::COMMUNITY, 'EXAMPLE-COMMUNITY-SERIAL') |
| 156 | + ->andThrow(new Exception('Cannot renew licence')); |
| 157 | + }); |
| 158 | + get('/netifyd/community/licence') |
| 159 | + ->assertInternalServerError() |
| 160 | + ->assertJson(['message' => 'Cannot renew licence']); |
| 161 | +}); |
0 commit comments