Skip to content

Commit

Permalink
Added locking mechanism to Admin user login
Browse files Browse the repository at this point in the history
  • Loading branch information
mnocon committed Sep 26, 2023
1 parent 05448f6 commit 99e3b6b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"symfony/config": "^5.0",
"symfony/console": "^5.0",
"symfony/dependency-injection": "^5.0",
"symfony/lock": "^5.0",
"symfony/stopwatch": "^5.2",
"symfony/http-kernel": "^5.0",
"symfony/process": "^5.4",
Expand Down
2 changes: 1 addition & 1 deletion src/bundle/Resources/views/tests/login_data.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>

<h1> Current user is: {{ username }}</h1>
<h1 id="login-success"> Current user is: {{ username }}</h1>
<h1> Current siteaccess is: {{ siteaccess }}</h1>

</html>
22 changes: 13 additions & 9 deletions src/lib/Browser/Context/AuthenticationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@
use Ibexa\Behat\API\ContentData\FieldTypeData\PasswordProvider;
use Ibexa\Behat\Browser\Page\LoginPage;
use Ibexa\Behat\Browser\Page\RedirectLoginPage;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\FlockStore;

class AuthenticationContext extends RawMinkContext
{
/**
* @var \Ibexa\Behat\Browser\Page\LoginPage
*/
private $loginPage;
private LoginPage $loginPage;

/**
* @var \Ibexa\Behat\Browser\Page\RedirectLoginPage
*/
private $redirectLoginPage;
private RedirectLoginPage $redirectLoginPage;

public function __construct(LoginPage $loginPage, RedirectLoginPage $redirectLoginPage)
{
Expand All @@ -46,8 +42,16 @@ public function iLogInIn(string $username, string $password = null)
*/
public function loggedAsAdmin()
{
$store = new FlockStore(sys_get_temp_dir());
$factory = new LockFactory($store);

$lock = $factory->createLock('admin-login');
$this->redirectLoginPage->open('admin');
$this->redirectLoginPage->loginSuccessfully('admin', 'publish');

if ($lock->acquire(true)) {
$this->redirectLoginPage->loginSuccessfully('admin', 'publish');
$lock->release();
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/lib/Browser/Page/RedirectLoginPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Ibexa\Behat\Browser\Page;

use Ibexa\Behat\Browser\Locator\CSSLocator;
use PHPUnit\Framework\Assert;

class RedirectLoginPage extends LoginPage
Expand All @@ -22,6 +23,14 @@ public function verifyIsLoaded(): void
Assert::assertStringContainsString('/login', $this->getSession()->getCurrentUrl());
}

public function loginSuccessfully($username, $password): void
{
parent::loginSuccessfully($username, $password);
$this->getHTMLPage()
->findAll(new CSSLocator('loginSuccess', '#login-success'))
->assert()->hasElements();
}

protected function getRoute(): string
{
return '/unauthenticated_login_redirect';
Expand Down

0 comments on commit 99e3b6b

Please sign in to comment.