Skip to content

Commit

Permalink
Testing & refactoring in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
rotimi committed Dec 8, 2023
1 parent 7273509 commit 8cb3d99
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
130 changes: 130 additions & 0 deletions tests/BaseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,8 @@ public function testThat_actionLogin_WithRequestPostMethod_WorksAsExpected4() {
$expectedRedirectPath2 = $controller->makeLink(
"/{$controller->getLoginSuccessRedirectController()}/{$action}{$controller->getLoginSuccessRedirectAction()}"
);

$controller->actionLogout(); // logout, clean up

// Response Object with a redirect uri should not be returned in this scenario
self::assertInstanceOf(\Psr\Http\Message\ResponseInterface::class, $actionLoginResult2);
Expand Down Expand Up @@ -1102,8 +1104,136 @@ public function testThat_actionLogin_WithRequestPostMethod_WorksAsExpected4() {
self::assertInstanceOf(\Psr\Http\Message\ResponseInterface::class, $actionLoginResult3);
self::assertEquals(302, $actionLoginResult3->getStatusCode());
self::assertEquals($expectedRedirectPath3, $actionLoginResult3->getHeaderLine('Location'));

$controller2->actionLogout(); // logout, clean up
}


/**
* @runInSeparateProcess
*/
public function testThat_doLogin_WorksAsExpected() {

if( !defined('SMVC_APP_AUTO_PREPEND_ACTION_TO_ACTION_METHOD_NAMES') ) {

define('SMVC_APP_AUTO_PREPEND_ACTION_TO_ACTION_METHOD_NAMES', true );
}

$req = $this->newRequest('http://google.com/');
$resp = $this->newResponse();
$psr11Container = $this->getContainer();

$controller = new \SMVCTools\Tests\TestObjects\ControllerWithPublicDoLogin(
$psr11Container, 'da-controller', 'da-action', $req, $resp
);

$success_redirect_path = '/random-path';
$expected_redirect_path =
$_SESSION[\SMVCTools\Tests\TestObjects\ControllerWithPublicDoLogin::SESSN_PARAM_LOGIN_REDIRECT];
$credentials = [ 'username'=> 'admin', 'password'=> 'admin', ];
$result = $controller->doLoginPublic(
$controller->getVespulaAuthObject(), $credentials, $success_redirect_path
);

////////////////////////////////////////////////////////////////////////
// $success_redirect_path should have the value stored in
// $_SESSION[\SMVCTools\Tests\TestObjects\ControllerWithPublicDoLogin::SESSN_PARAM_LOGIN_REDIRECT]
////////////////////////////////////////////////////////////////////////
self::assertEquals(
$expected_redirect_path,
$success_redirect_path
);

////////////////////////////////////////////////////
// Check that successful login message was returned
// and that the controller is in a logged in state
////////////////////////////////////////////////////
self::assertEquals(
$controller->getAppSetting('base_controller_do_login_auth_is_valid_msg'),
$result
);
self::assertTrue($controller->isLoggedIn());

//////////
// logout
//////////
$controller->getVespulaAuthObject()->logout();

//////////////////////////////////////////////
// Controller should be in a logged out state
//////////////////////////////////////////////
self::assertFalse($controller->isLoggedIn());

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
// Try logging in again, this time $success_redirect_path should not
// be changed because $_SESSION[\SMVCTools\Tests\TestObjects\ControllerWithPublicDoLogin::SESSN_PARAM_LOGIN_REDIRECT]
// was unset in the previous login above
////////////////////////////////////////////////////////////////////////
$success_redirect_path2 = '/random-path';
$original_success_redirect_path = $success_redirect_path2;
$result2 = $controller->doLoginPublic(
$controller->getVespulaAuthObject(), $credentials, $success_redirect_path
);

////////////////////////////////////////////////////////////////////////
// $success_redirect_path should not have been changed by doLogin
////////////////////////////////////////////////////////////////////////
self::assertEquals(
$original_success_redirect_path,
$success_redirect_path2
);

////////////////////////////////////////////////////
// Check that successful login message was returned
// and that the controller is in a logged in state
////////////////////////////////////////////////////
self::assertEquals(
$controller->getAppSetting('base_controller_do_login_auth_is_valid_msg'),
$result2
);
self::assertTrue($controller->isLoggedIn());

//////////
// logout
//////////
$controller->getVespulaAuthObject()->logout();

//////////////////////////////////////////////
// Controller should be in a logged out state
//////////////////////////////////////////////
self::assertFalse($controller->isLoggedIn());

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

// Try to login with a username and password not in the database
$bad_credentials = [ 'username'=> 'non-existent-user', 'password'=> 'non-existent-password', ];
$result3 = $controller->doLoginPublic(
$controller->getVespulaAuthObject(), $bad_credentials, $success_redirect_path
);

///////////////////////////////////////////////////////
// Check that unsuccessful login message was returned
// and that the controller is in a logged out state
//////////////////////////////////////////////////////
self::assertStringContainsString(
$controller->getAppSetting('base_controller_do_login_auth_not_is_valid_msg'),
$result3
);
self::assertStringContainsString(
'<br>',
$result3
);
self::assertStringContainsString(
\Vespula\Auth\Adapter\Sql::ERROR_NO_ROWS,
$result3
);
self::assertFalse($controller->isLoggedIn());
}

/**
* @runInSeparateProcess
*/
Expand Down
34 changes: 34 additions & 0 deletions tests/test-objects/InMemoryLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

namespace SMVCTools\Tests\TestObjects;

/**
* Description of InMemoryLogger
*
* @author rotimi
*/
class InMemoryLogger extends \Psr\Log\AbstractLogger {

/**
* @var string[]
*/
protected array $log_entries = [];


/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param mixed[] $context
*
* @return void
*
* @throws \Psr\Log\InvalidArgumentException
*/
public function log($level, $message, array $context=[]) {

$this->log_entries[] = "[LEVEL: {$level}] [MESSAGE: {$message}]";
}
}

0 comments on commit 8cb3d99

Please sign in to comment.