-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix username not being a string resulting in exception (#887)
* Add tests * Cast username to string
- Loading branch information
1 parent
fefacb9
commit 0378a40
Showing
2 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Sentry\EventHandler; | ||
|
||
use Illuminate\Auth\Events\Authenticated; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Sentry\Laravel\Tests\TestCase; | ||
|
||
class AuthEventsTest extends TestCase | ||
{ | ||
protected $setupConfig = [ | ||
'sentry.send_default_pii' => true, | ||
]; | ||
|
||
public function testAuthenticatedEventFillsUserOnScope(): void | ||
{ | ||
$user = new AuthEventsTestUserModel(); | ||
|
||
$user->id = 123; | ||
$user->username = 'username'; | ||
$user->email = '[email protected]'; | ||
|
||
$scope = $this->getCurrentSentryScope(); | ||
|
||
$this->assertNull($scope->getUser()); | ||
|
||
$this->dispatchLaravelEvent(new Authenticated('test', $user)); | ||
|
||
$this->assertNotNull($scope->getUser()); | ||
|
||
$this->assertEquals($scope->getUser()->getId(), 123); | ||
$this->assertEquals($scope->getUser()->getUsername(), 'username'); | ||
$this->assertEquals($scope->getUser()->getEmail(), '[email protected]'); | ||
} | ||
|
||
public function testAuthenticatedEventFillsUserOnScopeWhenUsernameIsNotAString(): void | ||
{ | ||
$user = new AuthEventsTestUserModel(); | ||
|
||
$user->id = 123; | ||
$user->username = 456; | ||
|
||
$scope = $this->getCurrentSentryScope(); | ||
|
||
$this->assertNull($scope->getUser()); | ||
|
||
$this->dispatchLaravelEvent(new Authenticated('test', $user)); | ||
|
||
$this->assertNotNull($scope->getUser()); | ||
|
||
$this->assertEquals($scope->getUser()->getId(), 123); | ||
$this->assertEquals($scope->getUser()->getUsername(), '456'); | ||
} | ||
|
||
public function testAuthenticatedEventDoesNotFillUserOnScopeWhenPIIShouldNotBeSent(): void | ||
{ | ||
$this->resetApplicationWithConfig([ | ||
'sentry.send_default_pii' => false, | ||
]); | ||
|
||
$user = new AuthEventsTestUserModel(); | ||
|
||
$user->id = 123; | ||
|
||
$scope = $this->getCurrentSentryScope(); | ||
|
||
$this->assertNull($scope->getUser()); | ||
|
||
$this->dispatchLaravelEvent(new Authenticated('test', $user)); | ||
|
||
$this->assertNull($scope->getUser()); | ||
} | ||
} | ||
|
||
class AuthEventsTestUserModel extends Model implements Authenticatable | ||
{ | ||
use \Illuminate\Auth\Authenticatable; | ||
} |