2020-02-07 16:59:39 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace October\Tests\Concerns;
|
2019-10-10 00:41:53 +02:00
|
|
|
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
|
|
|
|
|
|
|
|
trait InteractsWithAuthentication
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Set the currently logged in user for the application.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
|
* @param string|null $driver
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function actingAs(UserContract $user, $driver = null)
|
|
|
|
{
|
|
|
|
$this->be($user, $driver);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the currently logged in user for the application.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
|
* @param string|null $driver
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function be(UserContract $user, $driver = null)
|
|
|
|
{
|
2020-07-01 10:52:55 +08:00
|
|
|
$this->app['backend.auth']->setUser($user);
|
2019-10-10 00:41:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the user is authenticated.
|
|
|
|
*
|
|
|
|
* @param string|null $guard
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function assertAuthenticated($guard = null)
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->isAuthenticated($guard), 'The user is not authenticated');
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the user is not authenticated.
|
|
|
|
*
|
|
|
|
* @param string|null $guard
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function assertGuest($guard = null)
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->isAuthenticated($guard), 'The user is authenticated');
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the user is authenticated, false otherwise.
|
|
|
|
*
|
|
|
|
* @param string|null $guard
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isAuthenticated($guard = null)
|
|
|
|
{
|
2020-07-01 10:52:55 +08:00
|
|
|
return $this->app->make('backend.auth')->guard($guard)->check();
|
2019-10-10 00:41:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the user is authenticated as the given user.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
|
* @param string|null $guard
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function assertAuthenticatedAs($user, $guard = null)
|
|
|
|
{
|
2020-07-01 10:52:55 +08:00
|
|
|
$expected = $this->app->make('backend.auth')->guard($guard)->user();
|
2019-10-10 00:41:53 +02:00
|
|
|
|
|
|
|
$this->assertNotNull($expected, 'The current user is not authenticated.');
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
get_class($expected),
|
|
|
|
$user,
|
|
|
|
'The currently authenticated user is not who was expected'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expected->getAuthIdentifier(),
|
|
|
|
$user->getAuthIdentifier(),
|
|
|
|
'The currently authenticated user is not who was expected'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the given credentials are valid.
|
|
|
|
*
|
|
|
|
* @param array $credentials
|
|
|
|
* @param string|null $guard
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function assertCredentials(array $credentials, $guard = null)
|
|
|
|
{
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->hasCredentials($credentials, $guard),
|
|
|
|
'The given credentials are invalid.'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the given credentials are invalid.
|
|
|
|
*
|
|
|
|
* @param array $credentials
|
|
|
|
* @param string|null $guard
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function assertInvalidCredentials(array $credentials, $guard = null)
|
|
|
|
{
|
|
|
|
$this->assertFalse(
|
|
|
|
$this->hasCredentials($credentials, $guard),
|
|
|
|
'The given credentials are valid.'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the credentials are valid, false otherwise.
|
|
|
|
*
|
|
|
|
* @param array $credentials
|
|
|
|
* @param string|null $guard
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function hasCredentials(array $credentials, $guard = null)
|
|
|
|
{
|
2020-07-01 10:52:55 +08:00
|
|
|
$provider = $this->app->make('backend.auth')->guard($guard)->getProvider();
|
2019-10-10 00:41:53 +02:00
|
|
|
|
|
|
|
$user = $provider->retrieveByCredentials($credentials);
|
|
|
|
|
|
|
|
return $user && $provider->validateCredentials($user, $credentials);
|
|
|
|
}
|
|
|
|
}
|