|
22 | 22 | ]); |
23 | 23 |
|
24 | 24 | it('can access free license without credentials', function () { |
25 | | - Cache::expects('has')->with(NetifydLicenseType::COMMUNITY->cacheLabel())->andReturnTrue(); |
26 | | - Cache::expects('get')->with(NetifydLicenseType::COMMUNITY->cacheLabel())->andReturn(['license_key' => 'cache']); |
| 25 | + $fakeLicense = [ |
| 26 | + 'issued_to' => NetifydLicenseType::COMMUNITY->label(), |
| 27 | + 'expire_at' => [ |
| 28 | + 'unix' => now()->addDays(2)->unix(), |
| 29 | + ], |
| 30 | + ]; |
| 31 | + Cache::expects('get')->with(NetifydLicenseType::COMMUNITY->cacheLabel())->andReturn($fakeLicense); |
27 | 32 | get('/api/netifyd/license') |
28 | 33 | ->assertOk() |
29 | | - ->assertJson(['license_key' => 'cache']); |
| 34 | + ->assertJson($fakeLicense); |
30 | 35 | }); |
31 | 36 | }); |
32 | 37 |
|
|
41 | 46 | }); |
42 | 47 |
|
43 | 48 | it('can access enterprise license with credentials', function () { |
44 | | - Cache::expects('has')->with(NetifydLicenseType::ENTERPRISE->cacheLabel())->andReturnTrue(); |
45 | | - Cache::expects('get')->with(NetifydLicenseType::ENTERPRISE->cacheLabel())->andReturn(['license_key' => 'cache']); |
| 49 | + $fakeLicense = [ |
| 50 | + 'issued_to' => NetifydLicenseType::ENTERPRISE->label(), |
| 51 | + 'expire_at' => [ |
| 52 | + 'unix' => now()->addDays(2)->unix(), |
| 53 | + ], |
| 54 | + ]; |
| 55 | + Cache::expects('get')->with(NetifydLicenseType::ENTERPRISE->cacheLabel())->andReturn($fakeLicense); |
46 | 56 | withBasicAuth('', '') |
47 | 57 | ->get('/api/netifyd/community/license') |
48 | 58 | ->assertOk() |
49 | | - ->assertJson(['license_key' => 'cache']); |
| 59 | + ->assertJson($fakeLicense); |
50 | 60 | }); |
51 | 61 |
|
52 | | - it('serves correctly cache if present', function () { |
53 | | - Cache::expects('has')->with(NetifydLicenseType::ENTERPRISE->cacheLabel())->andReturnTrue(); |
54 | | - Cache::expects('get')->with(NetifydLicenseType::ENTERPRISE->cacheLabel())->andReturn(['license_key' => 'cached-license-key']); |
| 62 | + it('serves correctly cache if present and not expired', function () { |
| 63 | + $fakeLicense = [ |
| 64 | + 'issued_to' => NetifydLicenseType::ENTERPRISE->label(), |
| 65 | + 'expire_at' => [ |
| 66 | + 'unix' => now()->addDays(2)->unix(), |
| 67 | + ], |
| 68 | + ]; |
| 69 | + Cache::expects('get')->with(NetifydLicenseType::ENTERPRISE->cacheLabel())->andReturn($fakeLicense); |
55 | 70 | Http::preventStrayRequests(); |
56 | 71 | Http::fake(); |
57 | 72 | withBasicAuth('system-id', 'secret') |
58 | 73 | ->get('/api/netifyd/community/license') |
59 | 74 | ->assertOk() |
60 | | - ->assertJson([ |
61 | | - 'license_key' => 'cached-license-key', |
62 | | - ]); |
| 75 | + ->assertJson($fakeLicense); |
| 76 | + }); |
| 77 | + |
| 78 | + it('throws away the cache if license is expired', function () { |
| 79 | + $fakeLicense = [ |
| 80 | + 'issued_to' => NetifydLicenseType::ENTERPRISE->label(), |
| 81 | + 'expire_at' => [ |
| 82 | + 'unix' => now()->subDay()->unix(), |
| 83 | + ], |
| 84 | + ]; |
| 85 | + Cache::expects('get')->with(NetifydLicenseType::ENTERPRISE->cacheLabel())->andReturn($fakeLicense); |
| 86 | + Cache::expects('forget')->with(NetifydLicenseType::ENTERPRISE->cacheLabel()); |
| 87 | + $license = [ |
| 88 | + 'issued_to' => NetifydLicenseType::ENTERPRISE->label(), |
| 89 | + 'serial' => 'EXAMPLE-ENTERPRISE-SERIAL', |
| 90 | + 'expire_at' => [ |
| 91 | + 'unix' => now()->addDays(2)->unix(), |
| 92 | + ], |
| 93 | + ]; |
| 94 | + Cache::expects('put')->withSomeOfArgs(NetifydLicenseType::ENTERPRISE->cacheLabel(), $license); |
| 95 | + partialMock(NetifydLicenseRepository::class, function (MockInterface $mock) use ($license) { |
| 96 | + $mock->expects('listLicenses') |
| 97 | + ->andReturn([$license]); |
| 98 | + }); |
| 99 | + withBasicAuth('system-id', 'secret') |
| 100 | + ->get('/api/netifyd/community/license') |
| 101 | + ->assertOk() |
| 102 | + ->json($license); |
63 | 103 | }); |
64 | 104 |
|
65 | 105 | it('handles errors from netifyd server', function () { |
|
82 | 122 | 'expire_at' => [ |
83 | 123 | 'unix' => now()->addDays(2)->unix(), |
84 | 124 | ], |
85 | | - 'created_at' => [ |
86 | | - 'unix' => now()->subDay()->unix(), |
87 | | - ], |
88 | 125 | ]; |
89 | 126 | partialMock(NetifydLicenseRepository::class, function (MockInterface $mock) use ($license) { |
90 | 127 | $mock->expects('listLicenses') |
91 | 128 | ->andReturn([$license]); |
92 | 129 | }); |
93 | | - Cache::expects('has')->with(NetifydLicenseType::ENTERPRISE->cacheLabel())->andReturnFalse(); |
| 130 | + Cache::expects('get')->with(NetifydLicenseType::ENTERPRISE->cacheLabel())->andReturnNull(); |
94 | 131 | Cache::expects('put')->withSomeOfArgs(NetifydLicenseType::ENTERPRISE->cacheLabel(), $license); |
95 | 132 | withBasicAuth('system-id', 'secret') |
96 | 133 | ->get('/api/netifyd/community/license') |
|
130 | 167 | 'expire_at' => [ |
131 | 168 | 'unix' => now()->addDay()->unix(), |
132 | 169 | ], |
133 | | - 'created_at' => [ |
134 | | - 'unix' => now()->subDays(3)->unix(), |
135 | | - ], |
136 | 170 | ]; |
137 | 171 | partialMock(NetifydLicenseRepository::class, function (MockInterface $mock) use ($license) { |
138 | 172 | $mock->expects('listLicenses') |
|
148 | 182 | ->assertOk(); |
149 | 183 | }); |
150 | 184 |
|
151 | | - it('cannot renew license', function () { |
| 185 | + it('cannot renew license, but it\'s not expired', function () { |
152 | 186 | $license = [ |
153 | 187 | 'issued_to' => NetifydLicenseType::ENTERPRISE->label(), |
154 | 188 | 'serial' => 'EXAMPLE-ENTERPRISE-SERIAL', |
155 | 189 | 'expire_at' => [ |
156 | 190 | 'unix' => now()->addDay()->unix(), |
157 | 191 | ], |
158 | | - 'created_at' => [ |
159 | | - 'unix' => now()->subDays(3)->unix(), |
| 192 | + ]; |
| 193 | + partialMock(NetifydLicenseRepository::class, function (MockInterface $mock) use ($license) { |
| 194 | + $mock->expects('listLicenses') |
| 195 | + ->andReturn([ |
| 196 | + $license, |
| 197 | + ]); |
| 198 | + $mock->expects('renewLicense') |
| 199 | + ->with(NetifydLicenseType::ENTERPRISE, 'EXAMPLE-ENTERPRISE-SERIAL') |
| 200 | + ->andThrow(new Exception('Cannot renew license')); |
| 201 | + }); |
| 202 | + withBasicAuth('', '') |
| 203 | + ->get('/api/netifyd/community/license') |
| 204 | + ->assertOk() |
| 205 | + ->assertJson($license); |
| 206 | + }); |
| 207 | + |
| 208 | + it('cannot renew license, but it\'s expired', function () { |
| 209 | + $license = [ |
| 210 | + 'issued_to' => NetifydLicenseType::ENTERPRISE->label(), |
| 211 | + 'serial' => 'EXAMPLE-ENTERPRISE-SERIAL', |
| 212 | + 'expire_at' => [ |
| 213 | + 'unix' => now()->subDay()->unix(), |
160 | 214 | ], |
161 | 215 | ]; |
162 | 216 | partialMock(NetifydLicenseRepository::class, function (MockInterface $mock) use ($license) { |
|
170 | 224 | }); |
171 | 225 | withBasicAuth('', '') |
172 | 226 | ->get('/api/netifyd/community/license') |
173 | | - ->assertInternalServerError() |
174 | | - ->assertJson(['message' => 'Cannot renew license']); |
| 227 | + ->assertInternalServerError(); |
175 | 228 | }); |
176 | 229 |
|
177 | 230 | }); |
0 commit comments