|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Integration; |
| 4 | + |
| 5 | +use BoxedCode\Laravel\Auth\Device\Notifications\AuthorizationRequest; |
| 6 | +use Illuminate\Encryption\Encrypter; |
| 7 | + |
| 8 | +class EnforcingMiddlewareTestCase extends TestCase |
| 9 | +{ |
| 10 | + protected $da; |
| 11 | + |
| 12 | + /** |
| 13 | + * Setup the test environment. |
| 14 | + */ |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + |
| 19 | + $this->da = app('auth.device.broker'); |
| 20 | + } |
| 21 | + |
| 22 | + protected function getEnvironmentSetUp($app) |
| 23 | + { |
| 24 | + parent::getEnvironmentSetUp($app); |
| 25 | + |
| 26 | + // Set the application encryption key. |
| 27 | + $app['config']->set('app.key', 'base64:'.base64_encode( |
| 28 | + Encrypter::generateKey($app['config']['app.cipher']) |
| 29 | + )); |
| 30 | + } |
| 31 | + |
| 32 | + public function testDefaultFlow() |
| 33 | + { |
| 34 | + \Notification::fake(); |
| 35 | + |
| 36 | + // Trigger auth request as an unverified user. |
| 37 | + $response = $this->actingAs($this->testUser)->get('/'); |
| 38 | + $response->assertRedirect('http://localhost/auth/device/challenge'); |
| 39 | + |
| 40 | + // Visit the challenge page which will send us the email. |
| 41 | + $response = $this->actingAs($this->testUser)->get('/auth/device/challenge'); |
| 42 | + $response->assertRedirect('http://localhost/auth/device/challenged'); |
| 43 | + |
| 44 | + // Visit the 'challenged' page which instructs the user to check their mail. |
| 45 | + $response = $this->actingAs($this->testUser)->get('/auth/device/challenged'); |
| 46 | + $response->assertSee('We haven\'t seen you using this device before'); |
| 47 | + |
| 48 | + $latestAuthorization = $this->testUser->deviceAuthorizations()->latest()->first(); |
| 49 | + |
| 50 | + // Verify the notification was sent. |
| 51 | + \Notification::assertSentTo($this->testUser, AuthorizationRequest::class, function ($mail) use ($latestAuthorization) { |
| 52 | + return ($mail->verifyToken === $latestAuthorization->verify_token && |
| 53 | + $mail->browser === $latestAuthorization->browser && |
| 54 | + $mail->ip === $latestAuthorization->ip); |
| 55 | + }); |
| 56 | + |
| 57 | + // 'Click' the link in the notification e-mail. |
| 58 | + $response = $this->actingAs($this->testUser) |
| 59 | + ->withoutMiddleware([\Illuminate\Cookie\Middleware\EncryptCookies::class]) |
| 60 | + ->get('/auth/device/verify/' . $latestAuthorization->verify_token); |
| 61 | + $response->assertRedirect('/'); |
| 62 | + |
| 63 | + $authCookie = collect($response->headers->getCookies()) |
| 64 | + ->filter(function ($cookie) { |
| 65 | + return $cookie->getName() === '_la_dat'; |
| 66 | + })->first(); |
| 67 | + |
| 68 | + // Check we can see the homepage. |
| 69 | + $response = $this->withUnencryptedCookie($authCookie->getName(), $authCookie->getValue()) |
| 70 | + ->actingAs($this->testUser)->get('/'); |
| 71 | + |
| 72 | + $response->assertStatus(200); |
| 73 | + $response->assertSeeText('Hello Test User!'); |
| 74 | + } |
| 75 | +} |
0 commit comments