Skip to content

Commit

Permalink
test: add folders and move test files
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jun 1, 2022
1 parent 8c507cf commit 3a7894c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Authentication;
namespace Tests\Authentication\Authenticators;

use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authentication\Authentication;
Expand Down Expand Up @@ -28,7 +28,7 @@ protected function setUp(): void

$config = new Auth();
$auth = new Authentication($config);
$auth->setProvider(model(UserModel::class)); // @phpstan-ignore-line
$auth->setProvider(\model(UserModel::class)); // @phpstan-ignore-line

/** @var AccessTokens $authenticator */
$authenticator = $auth->factory('tokens');
Expand All @@ -39,7 +39,7 @@ protected function setUp(): void

public function testLogin()
{
$user = fake(UserModel::class);
$user = \fake(UserModel::class);

$this->auth->login($user);

Expand All @@ -51,7 +51,7 @@ public function testLogin()
public function testLogout()
{
// this one's a little odd since it's stateless, but roll with it...
$user = fake(UserModel::class);
$user = \fake(UserModel::class);

$this->auth->login($user);
$this->assertNotNull($this->auth->getUser());
Expand All @@ -62,7 +62,7 @@ public function testLogout()

public function testLoginByIdNoToken()
{
$user = fake(UserModel::class);
$user = \fake(UserModel::class);

$this->assertFalse($this->auth->loggedIn());

Expand All @@ -75,7 +75,7 @@ public function testLoginByIdNoToken()
public function testLoginByIdWithToken()
{
/** @var User $user */
$user = fake(UserModel::class);
$user = \fake(UserModel::class);
$token = $user->generateAccessToken('foo');

$this->setRequestHeader($token->raw_token);
Expand All @@ -90,7 +90,7 @@ public function testLoginByIdWithToken()
public function testLoginByIdWithMultipleTokens()
{
/** @var User $user */
$user = fake(UserModel::class);
$user = \fake(UserModel::class);
$token1 = $user->generateAccessToken('foo');
$user->generateAccessToken('bar');

Expand All @@ -108,37 +108,37 @@ public function testCheckNoToken()
$result = $this->auth->check([]);

$this->assertFalse($result->isOK());
$this->assertSame(lang('Auth.noToken'), $result->reason());
$this->assertSame(\lang('Auth.noToken'), $result->reason());
}

public function testCheckBadToken()
{
$result = $this->auth->check(['token' => 'abc123']);

$this->assertFalse($result->isOK());
$this->assertSame(lang('Auth.badToken'), $result->reason());
$this->assertSame(\lang('Auth.badToken'), $result->reason());
}

public function testCheckOldToken()
{
/** @var User $user */
$user = fake(UserModel::class);
$user = \fake(UserModel::class);
/** @var UserIdentityModel $identities */
$identities = model(UserIdentityModel::class);
$identities = \model(UserIdentityModel::class);
$token = $user->generateAccessToken('foo');
$token->last_used_at = Time::now()->subYears(1)->subMinutes(1);
$identities->save($token);

$result = $this->auth->check(['token' => $token->raw_token]);

$this->assertFalse($result->isOK());
$this->assertSame(lang('Auth.oldToken'), $result->reason());
$this->assertSame(\lang('Auth.oldToken'), $result->reason());
}

public function testCheckSuccess()
{
/** @var User $user */
$user = fake(UserModel::class);
$user = \fake(UserModel::class);
$token = $user->generateAccessToken('foo');

$this->seeInDatabase('auth_identities', [
Expand All @@ -165,7 +165,7 @@ public function testAttemptCannotFindUser()

$this->assertInstanceOf(Result::class, $result);
$this->assertFalse($result->isOK());
$this->assertSame(lang('Auth.badToken'), $result->reason());
$this->assertSame(\lang('Auth.badToken'), $result->reason());

// A login attempt should have always been recorded
$this->seeInDatabase('auth_token_logins', [
Expand All @@ -178,7 +178,7 @@ public function testAttemptCannotFindUser()
public function testAttemptSuccess()
{
/** @var User $user */
$user = fake(UserModel::class);
$user = \fake(UserModel::class);
$token = $user->generateAccessToken('foo');
$this->setRequestHeader($token->raw_token);

Expand All @@ -205,7 +205,7 @@ public function testAttemptSuccess()

protected function setRequestHeader(string $token)
{
$request = service('request');
$request = \service('request');
$request->setHeader('Authorization', 'Bearer ' . $token);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Authentication;
namespace Tests\Authentication\Authenticators;

use CodeIgniter\Shield\Authentication\Authentication;
use CodeIgniter\Shield\Authentication\AuthenticationException;
Expand Down Expand Up @@ -34,7 +34,7 @@ protected function setUp(): void

$config = new Auth();
$auth = new Authentication($config);
$auth->setProvider(model(UserModel::class)); // @phpstan-ignore-line
$auth->setProvider(\model(UserModel::class)); // @phpstan-ignore-line

/** @var Session $authenticator */
$authenticator = $auth->factory('session');
Expand Down Expand Up @@ -70,7 +70,7 @@ public function testLoggedInWithRememberCookie()

// Insert remember-me token.
/** @var RememberModel $rememberModel */
$rememberModel = model(RememberModel::class);
$rememberModel = \model(RememberModel::class);
$selector = 'selector';
$validator = 'validator';
$expires = date('Y-m-d H:i:s', time() + setting('Auth.sessionConfig')['rememberLength']);
Expand Down Expand Up @@ -114,7 +114,7 @@ public function testLoginWithRemember()
]);

// Cookie should have been set
$response = service('response');
$response = \service('response');
$this->assertNotNull($response->getCookie('remember'));
}

Expand All @@ -134,7 +134,7 @@ public function testLogout()
public function testLoginByIdBadUser()
{
$this->expectException(AuthenticationException::class);
$this->expectExceptionMessage(lang('Auth.invalidUser'));
$this->expectExceptionMessage(\lang('Auth.invalidUser'));

$this->auth->loginById(123);
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public function testForgetCurrentUser()

public function testForgetAnotherUser()
{
fake(RememberModel::class, ['user_id' => $this->user->id]);
\fake(RememberModel::class, ['user_id' => $this->user->id]);

$this->seeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]);

Expand All @@ -193,7 +193,7 @@ public function testCheckNoPassword()

$this->assertInstanceOf(Result::class, $result);
$this->assertFalse($result->isOK());
$this->assertSame(lang('Auth.badAttempt'), $result->reason());
$this->assertSame(\lang('Auth.badAttempt'), $result->reason());
}

public function testCheckCannotFindUser()
Expand All @@ -205,7 +205,7 @@ public function testCheckCannotFindUser()

$this->assertInstanceOf(Result::class, $result);
$this->assertFalse($result->isOK());
$this->assertSame(lang('Auth.badAttempt'), $result->reason());
$this->assertSame(\lang('Auth.badAttempt'), $result->reason());
}

public function testCheckBadPassword()
Expand All @@ -222,7 +222,7 @@ public function testCheckBadPassword()

$this->assertInstanceOf(Result::class, $result);
$this->assertFalse($result->isOK());
$this->assertSame(lang('Auth.invalidPassword'), $result->reason());
$this->assertSame(\lang('Auth.invalidPassword'), $result->reason());
}

public function testCheckSuccess()
Expand Down Expand Up @@ -253,7 +253,7 @@ public function testAttemptCannotFindUser()

$this->assertInstanceOf(Result::class, $result);
$this->assertFalse($result->isOK());
$this->assertSame(lang('Auth.badAttempt'), $result->reason());
$this->assertSame(\lang('Auth.badAttempt'), $result->reason());

// A login attempt should have always been recorded
$this->seeInDatabase('auth_logins', [
Expand Down Expand Up @@ -325,7 +325,7 @@ public function testAttemptCaseInsensitive()
public function testAttemptUsernameOnly()
{
/** @var User $user */
$user = fake(UserModel::class, ['username' => 'foorog']);
$user = \fake(UserModel::class, ['username' => 'foorog']);
$user->createEmailIdentity([
'email' => 'FOO@example.com',
'password' => 'secret123',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Authentication;
namespace Tests\Authentication\Filters;

use CodeIgniter\Config\Factories;
use CodeIgniter\Shield\Entities\AccessToken;
Expand Down Expand Up @@ -32,12 +32,12 @@ protected function setUp(): void
$_SESSION = [];

// Register our filter
$filterConfig = config('Filters');
$filterConfig = \config('Filters');
$filterConfig->aliases['chain'] = ChainAuth::class;
Factories::injectMock('filters', 'filters', $filterConfig);

// Add a test route that we can visit to trigger.
$routes = service('routes');
$routes = \service('routes');
$routes->group('/', ['filter' => 'chain'], static function ($routes) {
$routes->get('protected-route', static function () {
echo 'Protected';
Expand Down Expand Up @@ -71,8 +71,8 @@ public function testFilterSuccessSeession()
$result->assertStatus(200);
$result->assertSee('Protected');

$this->assertSame($this->user->id, auth()->id());
$this->assertSame($this->user->id, auth()->user()->id);
$this->assertSame($this->user->id, \auth()->id());
$this->assertSame($this->user->id, \auth()->user()->id);
}

public function testFilterSuccessTokens()
Expand All @@ -85,11 +85,11 @@ public function testFilterSuccessTokens()
$result->assertStatus(200);
$result->assertSee('Protected');

$this->assertSame($this->user->id, auth()->id());
$this->assertSame($this->user->id, auth()->user()->id);
$this->assertSame($this->user->id, \auth()->id());
$this->assertSame($this->user->id, \auth()->user()->id);

// User should have the current token set.
$this->assertInstanceOf(AccessToken::class, auth('tokens')->user()->currentAccessToken());
$this->assertSame($token->id, auth('tokens')->user()->currentAccessToken()->id);
$this->assertInstanceOf(AccessToken::class, \auth('tokens')->user()->currentAccessToken());
$this->assertSame($token->id, \auth('tokens')->user()->currentAccessToken()->id);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Authentication;
namespace Tests\Authentication\Filters;

use CodeIgniter\Config\Factories;
use CodeIgniter\Shield\Filters\SessionAuth;
Expand All @@ -27,12 +27,12 @@ protected function setUp(): void
$_SESSION = [];

// Register our filter
$filterConfig = config('Filters');
$filterConfig = \config('Filters');
$filterConfig->aliases['sessionAuth'] = SessionAuth::class;
Factories::injectMock('filters', 'filters', $filterConfig);

// Add a test route that we can visit to trigger.
$routes = service('routes');
$routes = \service('routes');
$routes->group('/', ['filter' => 'sessionAuth'], static function ($routes) {
$routes->get('protected-route', static function () {
echo 'Protected';
Expand All @@ -58,7 +58,7 @@ public function testFilterNotAuthorized()

public function testFilterSuccess()
{
$user = fake(UserModel::class);
$user = \fake(UserModel::class);
$_SESSION['user']['id'] = $user->id;

$result = $this->withSession(['user' => ['id' => $user->id]])
Expand All @@ -67,9 +67,9 @@ public function testFilterSuccess()
$result->assertStatus(200);
$result->assertSee('Protected');

$this->assertSame($user->id, auth('session')->id());
$this->assertSame($user->id, auth('session')->user()->id);
$this->assertSame($user->id, \auth('session')->id());
$this->assertSame($user->id, \auth('session')->user()->id);
// Last Active should have been updated
$this->assertNotEmpty(auth('session')->user()->last_active);
$this->assertNotEmpty(\auth('session')->user()->last_active);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Authentication;
namespace Tests\Authentication\Filters;

use CodeIgniter\Config\Factories;
use CodeIgniter\Shield\Entities\AccessToken;
Expand Down Expand Up @@ -29,12 +29,12 @@ protected function setUp(): void
$_SESSION = [];

// Register our filter
$filterConfig = config('Filters');
$filterConfig = \config('Filters');
$filterConfig->aliases['tokenAuth'] = TokenAuth::class;
Factories::injectMock('filters', 'filters', $filterConfig);

// Add a test route that we can visit to trigger.
$routes = service('routes');
$routes = \service('routes');
$routes->group('/', ['filter' => 'tokenAuth'], static function ($routes) {
$routes->get('protected-route', static function () {
echo 'Protected';
Expand All @@ -61,7 +61,7 @@ public function testFilterNotAuthorized()
public function testFilterSuccess()
{
/** @var User $user */
$user = fake(UserModel::class);
$user = \fake(UserModel::class);
$token = $user->generateAccessToken('foo');

$result = $this->withHeaders(['Authorization' => 'Bearer ' . $token->raw_token])
Expand All @@ -70,11 +70,11 @@ public function testFilterSuccess()
$result->assertStatus(200);
$result->assertSee('Protected');

$this->assertSame($user->id, auth('tokens')->id());
$this->assertSame($user->id, auth('tokens')->user()->id);
$this->assertSame($user->id, \auth('tokens')->id());
$this->assertSame($user->id, \auth('tokens')->user()->id);

// User should have the current token set.
$this->assertInstanceOf(AccessToken::class, auth('tokens')->user()->currentAccessToken());
$this->assertSame($token->id, auth('tokens')->user()->currentAccessToken()->id);
$this->assertInstanceOf(AccessToken::class, \auth('tokens')->user()->currentAccessToken());
$this->assertSame($token->id, \auth('tokens')->user()->currentAccessToken()->id);
}
}

0 comments on commit 3a7894c

Please sign in to comment.