Skip to content

Commit

Permalink
Fixing some tests. Refactoring AuthController to use model() helper per
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Apr 14, 2020
1 parent dd6c43a commit 4c3f359
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
34 changes: 16 additions & 18 deletions src/Controllers/AuthController.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php namespace Myth\Auth\Controllers;

use CodeIgniter\Controller;
use Config\Email;
use Config\Services;
use CodeIgniter\Controller;
use Myth\Auth\Entities\User;
use Myth\Auth\Models\UserModel;

class AuthController extends Controller
{
Expand All @@ -23,10 +21,10 @@ public function __construct()
{
// Most services in this controller require
// the session to be started - so fire it up!
$this->session = Services::session();
$this->session = service('session');

$this->config = config('Auth');
$this->auth = Services::authentication();
$this->auth = service('authentication');
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -149,7 +147,7 @@ public function attemptRegister()
return redirect()->back()->withInput()->with('error', lang('Auth.registerDisabled'));
}

$users = new UserModel();
$users = model('UserModel');

// Validate here first, since some things,
// like the password, can only be validated properly here.
Expand Down Expand Up @@ -182,7 +180,7 @@ public function attemptRegister()

if ($this->config->requireActivation !== false)
{
$activator = Services::activator();
$activator = service('activator');
$sent = $activator->send($user);

if (! $sent)
Expand Down Expand Up @@ -216,7 +214,7 @@ public function forgotPassword()
*/
public function attemptForgot()
{
$users = new UserModel();
$users = model('UserModel');

$user = $users->where('email', $this->request->getPost('email'))->first();

Expand All @@ -229,8 +227,8 @@ public function attemptForgot()
$user->generateResetHash();
$users->save($user);

$email = Services::email();
$config = new Email();
$email = service('email');
$config = config(Email::class);

$sent = $email->setFrom($config->fromEmail, $config->fromEmail)
->setTo($user->email)
Expand Down Expand Up @@ -269,7 +267,7 @@ public function resetPassword()
*/
public function attemptReset()
{
$users = new UserModel();
$users = model('UserModel');

// First things first - log the reset attempt.
$users->logResetAttempt(
Expand Down Expand Up @@ -323,7 +321,7 @@ public function attemptReset()
*/
public function activateAccount()
{
$users = new UserModel();
$users = model('UserModel');

// First things first - log the activation attempt.
$users->logActivationAttempt(
Expand All @@ -332,11 +330,11 @@ public function activateAccount()
(string) $this->request->getUserAgent()
);

$throttler = Services::throttler();
$throttler = service('throttler');

if ($throttler->check($this->request->getIPAddress(), 2, MINUTE) === false)
{
return Services::response()->setStatusCode(429)->setBody(lang('Auth.tooManyRequests', [$throttler->getTokentime()]));
return service('response')->setStatusCode(429)->setBody(lang('Auth.tooManyRequests', [$throttler->getTokentime()]));
}

$user = $users->where('activate_hash', $this->request->getGet('token'))
Expand Down Expand Up @@ -367,17 +365,17 @@ public function resendActivateAccount()
return redirect()->route('login');
}

$throttler = Services::throttler();
$throttler = service('throttler');

if ($throttler->check($this->request->getIPAddress(), 2, MINUTE) === false)
{
return Services::response()->setStatusCode(429)->setBody(lang('Auth.tooManyRequests', [$throttler->getTokentime()]));
return service('response')->setStatusCode(429)->setBody(lang('Auth.tooManyRequests', [$throttler->getTokentime()]));
}

$login = urldecode($this->request->getGet('login'));
$type = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

$users = new UserModel();
$users = model('UserModel');

$user = $users->where($type, $login)
->where('active', 0)
Expand All @@ -388,7 +386,7 @@ public function resendActivateAccount()
return redirect()->route('login')->with('error', lang('Auth.activationNoUser'));
}

$activator = Services::activator();
$activator = service('activator');
$sent = $activator->send($user);

if (! $sent)
Expand Down
2 changes: 2 additions & 0 deletions tests/authentication/LocalAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,7 @@ public function testCheckWithForcedPasswordReset()
$this->expectExceptionMessage(route_to('reset-password') .'?token='. $user->reset_hash);

$this->auth->check();

unset($_SESSION['logged_in']);
}
}
7 changes: 5 additions & 2 deletions tests/controllers/RegisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class RegisterTest extends AuthTestCase

public function setUp(): void
{
\Config\Services::reset();

parent::setUp();

// Make sure our valiation rules include strong_password
Expand All @@ -23,14 +25,15 @@ public function setUp(): void
// Make sure our routes are mapped
$routes = service('routes');
$routes->add('login', 'AuthController::login', ['as' => 'login']);
$routes->add('register', 'AuthController::register', ['as' => 'register']);
\Config\Services::injectMock('routes', $routes);

$_SESSION = [];
}

public function tearDown(): void
{
parent::tearDown();

\Config\Services::reset();
}

public function testRegisterDisplaysForm()
Expand Down
14 changes: 10 additions & 4 deletions tests/unit/UserEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ class UserEntityTest extends AuthTestCase

public function setUp(): void
{
\Config\Services::reset();

parent::setUp();

$this->user = $this->createUser();
// Don't worry about default groups for this...
$config = new \Myth\Auth\Config\Auth();
$config->defaultGroup = 'Administrators';
\CodeIgniter\Config\Config::injectMock('Auth', $config);
}

/**
Expand All @@ -31,13 +36,14 @@ public function testGetPermissionsNotSaved()

public function testGetPermissionSuccess()
{
$user = $this->createUser();
$perm = $this->createPermission();
$model = new PermissionModel();

$this->assertEmpty($this->user->getPermissions());
$this->assertEmpty($user->getPermissions());

$model->addPermissionToUser($perm->id, $this->user->id);
$model->addPermissionToUser($perm->id, $user->id);

$this->assertTrue(in_array($perm->name, $this->user->getPermissions()));
$this->assertTrue(in_array($perm->name, $user->getPermissions()));
}
}

0 comments on commit 4c3f359

Please sign in to comment.