|
6 | 6 | use Illuminate\Support\Facades\Http;
|
7 | 7 | use Illuminate\Support\Facades\Mail;
|
8 | 8 | use Illuminate\Support\Str;
|
| 9 | +use InnoGE\LaravelMsGraphMail\Exceptions\ConfigurationInvalid; |
9 | 10 | use InnoGE\LaravelMsGraphMail\Exceptions\ConfigurationMissing;
|
| 11 | +use InnoGE\LaravelMsGraphMail\Exceptions\InvalidResponse; |
10 | 12 | use InnoGE\LaravelMsGraphMail\Tests\Stubs\TestMail;
|
11 | 13 | use InnoGE\LaravelMsGraphMail\Tests\Stubs\TestMailWithInlineImage;
|
12 | 14 |
|
|
219 | 221 | ->toBe('foo_access_token');
|
220 | 222 | });
|
221 | 223 |
|
222 |
| -it('throws exceptions when config is missing', function (array $config, string $exceptionMessage) { |
| 224 | +it('throws exceptions on invalid access token in response', function () { |
| 225 | + Config::set('mail.mailers.microsoft-graph', [ |
| 226 | + 'transport' => 'microsoft-graph', |
| 227 | + 'client_id' => 'foo_client_id', |
| 228 | + 'client_secret' => 'foo_client_secret', |
| 229 | + 'tenant_id' => 'foo_tenant_id', |
| 230 | + 'from' => [ |
| 231 | + |
| 232 | + 'name' => 'Taylor Otwell', |
| 233 | + ], |
| 234 | + ]); |
| 235 | + Config::set('mail.default', 'microsoft-graph'); |
| 236 | + |
| 237 | + Http::fake([ |
| 238 | + 'https://login.microsoftonline.com/foo_tenant_id/oauth2/v2.0/token' => Http::response(['access_token' => 123]), |
| 239 | + ]); |
| 240 | + |
| 241 | + expect( fn () => Mail:: to( '[email protected]')-> send( new TestMail( false))) |
| 242 | + ->toThrow(InvalidResponse::class, 'Expected response to contain key access_token of type string, got: 123.'); |
| 243 | +}); |
| 244 | + |
| 245 | +it('throws exceptions when config is invalid', function (array $config, Exception $exception) { |
223 | 246 | Config::set('mail.mailers.microsoft-graph', $config);
|
224 | 247 | Config::set('mail.default', 'microsoft-graph');
|
225 | 248 |
|
226 |
| - try { |
227 |
| - |
228 |
| - ->send(new TestMail(false)); |
229 |
| - } catch (Exception $e) { |
230 |
| - expect($e) |
231 |
| - ->toBeInstanceOf(ConfigurationMissing::class) |
232 |
| - ->getMessage()->toBe($exceptionMessage); |
233 |
| - } |
234 |
| -})->with( |
| 249 | + expect( fn () => Mail:: to( '[email protected]')-> send( new TestMail( false))) |
| 250 | + ->toThrow(get_class($exception), $exception->getMessage()); |
| 251 | +})->with([ |
235 | 252 | [
|
236 | 253 | [
|
237 |
| - [ |
238 |
| - 'transport' => 'microsoft-graph', |
239 |
| - 'client_id' => 'foo_client_id', |
240 |
| - 'client_secret' => 'foo_client_secret', |
241 |
| - 'tenant_id' => '', |
242 |
| - 'from' => [ |
243 |
| - |
244 |
| - 'name' => 'Taylor Otwell', |
245 |
| - ], |
| 254 | + 'transport' => 'microsoft-graph', |
| 255 | + 'client_id' => 'foo_client_id', |
| 256 | + 'client_secret' => 'foo_client_secret', |
| 257 | + 'from' => [ |
| 258 | + |
| 259 | + 'name' => 'Taylor Otwell', |
246 | 260 | ],
|
247 |
| - 'The tenant id is missing from the configuration file.', |
248 | 261 | ],
|
| 262 | + new ConfigurationMissing('tenant_id'), |
| 263 | + ], |
| 264 | + [ |
249 | 265 | [
|
250 |
| - [ |
251 |
| - 'transport' => 'microsoft-graph', |
252 |
| - 'client_id' => '', |
253 |
| - 'client_secret' => 'foo_client_secret', |
254 |
| - 'tenant_id' => 'foo_tenant_id', |
255 |
| - 'from' => [ |
256 |
| - |
257 |
| - 'name' => 'Taylor Otwell', |
258 |
| - ], |
| 266 | + 'transport' => 'microsoft-graph', |
| 267 | + 'tenant_id' => 123, |
| 268 | + 'client_id' => 'foo_client_id', |
| 269 | + 'client_secret' => 'foo_client_secret', |
| 270 | + 'from' => [ |
| 271 | + |
| 272 | + 'name' => 'Taylor Otwell', |
259 | 273 | ],
|
260 |
| - 'The client id is missing from the configuration file.', |
261 | 274 | ],
|
| 275 | + new ConfigurationInvalid('tenant_id', 123), |
| 276 | + ], |
| 277 | + [ |
262 | 278 | [
|
263 |
| - [ |
264 |
| - 'transport' => 'microsoft-graph', |
265 |
| - 'client_id' => 'foo_client_id', |
266 |
| - 'client_secret' => '', |
267 |
| - 'tenant_id' => 'foo_tenant_id', |
268 |
| - 'from' => [ |
269 |
| - |
270 |
| - 'name' => 'Taylor Otwell', |
271 |
| - ], |
| 279 | + 'transport' => 'microsoft-graph', |
| 280 | + 'tenant_id' => 'foo_tenant_id', |
| 281 | + 'client_secret' => 'foo_client_secret', |
| 282 | + 'from' => [ |
| 283 | + |
| 284 | + 'name' => 'Taylor Otwell', |
272 | 285 | ],
|
273 |
| - 'The client secret is missing from the configuration file.', |
274 | 286 | ],
|
| 287 | + new ConfigurationMissing('client_id'), |
| 288 | + ], |
| 289 | + [ |
275 | 290 | [
|
276 |
| - [ |
277 |
| - 'transport' => 'microsoft-graph', |
278 |
| - 'client_id' => 'foo_client_id', |
279 |
| - 'client_secret' => 'foo_client_secret', |
280 |
| - 'tenant_id' => 'foo_tenant_id', |
| 291 | + 'transport' => 'microsoft-graph', |
| 292 | + 'tenant_id' => 'foo_tenant_id', |
| 293 | + 'client_id' => '', |
| 294 | + 'client_secret' => 'foo_client_secret', |
| 295 | + 'from' => [ |
| 296 | + |
| 297 | + 'name' => 'Taylor Otwell', |
281 | 298 | ],
|
282 |
| - 'The mail from address is missing from the configuration file.', |
283 | 299 | ],
|
284 |
| - ]); |
| 300 | + new ConfigurationInvalid('client_id', ''), |
| 301 | + ], |
| 302 | + [ |
| 303 | + [ |
| 304 | + 'transport' => 'microsoft-graph', |
| 305 | + 'tenant_id' => 'foo_tenant_id', |
| 306 | + 'client_id' => 'foo_client_id', |
| 307 | + 'from' => [ |
| 308 | + |
| 309 | + 'name' => 'Taylor Otwell', |
| 310 | + ], |
| 311 | + ], |
| 312 | + new ConfigurationMissing('client_secret'), |
| 313 | + ], |
| 314 | + [ |
| 315 | + [ |
| 316 | + 'transport' => 'microsoft-graph', |
| 317 | + 'tenant_id' => 'foo_tenant_id', |
| 318 | + 'client_id' => 'foo_client_id', |
| 319 | + 'client_secret' => null, |
| 320 | + 'from' => [ |
| 321 | + |
| 322 | + 'name' => 'Taylor Otwell', |
| 323 | + ], |
| 324 | + ], |
| 325 | + new ConfigurationInvalid('client_secret', null), |
| 326 | + ], |
| 327 | + [ |
| 328 | + [ |
| 329 | + 'transport' => 'microsoft-graph', |
| 330 | + 'tenant_id' => 'foo_tenant_id', |
| 331 | + 'client_id' => 'foo_client_id', |
| 332 | + 'client_secret' => 'foo_client_secret', |
| 333 | + ], |
| 334 | + new ConfigurationMissing('from.address'), |
| 335 | + ], |
| 336 | + [ |
| 337 | + [ |
| 338 | + 'transport' => 'microsoft-graph', |
| 339 | + 'tenant_id' => 'foo_tenant_id', |
| 340 | + 'client_id' => 'foo_client_id', |
| 341 | + 'client_secret' => 'foo_client_secret', |
| 342 | + 'access_token_ttl' => false, |
| 343 | + 'from' => [ |
| 344 | + |
| 345 | + 'name' => 'Taylor Otwell', |
| 346 | + ], |
| 347 | + ], |
| 348 | + new ConfigurationInvalid('access_token_ttl', false), |
| 349 | + ], |
| 350 | +]); |
285 | 351 |
|
286 | 352 | it('sends html mails with inline images with microsoft graph', function () {
|
287 | 353 | Config::set('mail.mailers.microsoft-graph', [
|
|
0 commit comments