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