|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\EventHandler; |
| 4 | + |
| 5 | +use Illuminate\Auth\Events\Authenticated; |
| 6 | +use Illuminate\Contracts\Auth\Authenticatable; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | +use Sentry\Laravel\Tests\TestCase; |
| 9 | + |
| 10 | +class AuthEventsTest extends TestCase |
| 11 | +{ |
| 12 | + protected $setupConfig = [ |
| 13 | + 'sentry.send_default_pii' => true, |
| 14 | + ]; |
| 15 | + |
| 16 | + public function testAuthenticatedEventFillsUserOnScope(): void |
| 17 | + { |
| 18 | + $user = new AuthEventsTestUserModel(); |
| 19 | + |
| 20 | + $user->id = 123; |
| 21 | + $user->username = 'username'; |
| 22 | + $user-> email = '[email protected]'; |
| 23 | + |
| 24 | + $scope = $this->getCurrentSentryScope(); |
| 25 | + |
| 26 | + $this->assertNull($scope->getUser()); |
| 27 | + |
| 28 | + $this->dispatchLaravelEvent(new Authenticated('test', $user)); |
| 29 | + |
| 30 | + $this->assertNotNull($scope->getUser()); |
| 31 | + |
| 32 | + $this->assertEquals($scope->getUser()->getId(), 123); |
| 33 | + $this->assertEquals($scope->getUser()->getUsername(), 'username'); |
| 34 | + $this-> assertEquals( $scope-> getUser()-> getEmail(), '[email protected]'); |
| 35 | + } |
| 36 | + |
| 37 | + public function testAuthenticatedEventFillsUserOnScopeWhenUsernameIsNotAString(): void |
| 38 | + { |
| 39 | + $user = new AuthEventsTestUserModel(); |
| 40 | + |
| 41 | + $user->id = 123; |
| 42 | + $user->username = 456; |
| 43 | + |
| 44 | + $scope = $this->getCurrentSentryScope(); |
| 45 | + |
| 46 | + $this->assertNull($scope->getUser()); |
| 47 | + |
| 48 | + $this->dispatchLaravelEvent(new Authenticated('test', $user)); |
| 49 | + |
| 50 | + $this->assertNotNull($scope->getUser()); |
| 51 | + |
| 52 | + $this->assertEquals($scope->getUser()->getId(), 123); |
| 53 | + $this->assertEquals($scope->getUser()->getUsername(), '456'); |
| 54 | + } |
| 55 | + |
| 56 | + public function testAuthenticatedEventDoesNotFillUserOnScopeWhenPIIShouldNotBeSent(): void |
| 57 | + { |
| 58 | + $this->resetApplicationWithConfig([ |
| 59 | + 'sentry.send_default_pii' => false, |
| 60 | + ]); |
| 61 | + |
| 62 | + $user = new AuthEventsTestUserModel(); |
| 63 | + |
| 64 | + $user->id = 123; |
| 65 | + |
| 66 | + $scope = $this->getCurrentSentryScope(); |
| 67 | + |
| 68 | + $this->assertNull($scope->getUser()); |
| 69 | + |
| 70 | + $this->dispatchLaravelEvent(new Authenticated('test', $user)); |
| 71 | + |
| 72 | + $this->assertNull($scope->getUser()); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class AuthEventsTestUserModel extends Model implements Authenticatable |
| 77 | +{ |
| 78 | + use \Illuminate\Auth\Authenticatable; |
| 79 | +} |
0 commit comments