Skip to content

Commit

Permalink
test: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed May 23, 2024
1 parent cb434a6 commit 102a2ba
Show file tree
Hide file tree
Showing 14 changed files with 537 additions and 67 deletions.
3 changes: 3 additions & 0 deletions src/AtooloSecurityBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* @codeCoverageIgnore
*/
class AtooloSecurityBundle extends Bundle
{
public function build(ContainerBuilder $container): void
Expand Down
8 changes: 0 additions & 8 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,4 @@ public function getUserIdentifier(): string
{
return $this->username;
}

/**
* @deprecated since Symfony 5.3
*/
public function getUsername(): string
{
return $this->username;
}
}
3 changes: 3 additions & 0 deletions src/Exception/SecurityException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Atoolo\Security\Exception;

/**
* @codeCoverageIgnore
*/
class SecurityException extends \RuntimeException
{
}
55 changes: 29 additions & 26 deletions src/RealmPropertiesUserLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atoolo\Security\Entity\User;
use Atoolo\Security\SiteKit\RoleMapper;
use RuntimeException;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\User\UserInterface;

Expand Down Expand Up @@ -36,29 +37,33 @@ public function load(): array
{
$userList = [];
$realm = $this->loadRealm();
foreach ($realm as $name => $value) {
$user = $this->createUser($name, $value);
foreach ($realm as $name => $values) {
$user = $this->createUser($name, $values);
$userList[$user->getUserIdentifier()] = $user;
}

return $userList;
}

/**
* @return array<string,string>
* @return array<string,array<string>>
*/
private function loadRealm(): array
{
$realm = [];
if (!file_exists($this->realmPropertiesFile)) {
throw new RuntimeException(
'Realm properties file not found: ' . $this->realmPropertiesFile
);
}
$content = file_get_contents($this->realmPropertiesFile);
if (!is_string($content)) {
return $realm;
if ($content === false) {
throw new RuntimeException(
'Unable to load ' . $this->realmPropertiesFile
);
}

$lines = preg_split("/((\r?\n)|(\r\n?))/", $content);
if (!is_array($lines)) {
return $realm;
}
$lines = preg_split("/((\r?\n)|(\r\n?))/", $content) ?: [];

foreach ($lines as $line) {
if (str_starts_with($line, ';') || str_starts_with($line, '#')) {
Expand All @@ -69,22 +74,29 @@ private function loadRealm(): array
continue;
}
$user = $parts[0];
$realm[$user] = trim($parts[1]);
$values = trim($parts[1]);
if (empty($values)) {
$realm[$user] = [];
continue;
}
$realm[$user] = explode(',', $values);
}

return $realm;
}

private function createUser(string $name, string $value): User
/**
* @param array<string> $values
*/
private function createUser(string $name, array $values): User
{
$separator = strpos($value, ',');
if (is_int($separator)) {
$plaintextPassword = substr($value, 0, $separator);
} else {
if (empty($values)) {
$plaintextPassword = '';
} else {
$plaintextPassword = trim($values[0]);
}
$plaintextPassword = trim($plaintextPassword);
$roles = $this->parseRoles(substr($value, $separator + 1));
array_shift($values);
$roles = RoleMapper::map($values);
$user = new User($name, $roles);

/**
Expand All @@ -105,13 +117,4 @@ private function createUser(string $name, string $value): User

return $user;
}

/**
* @return array<string>
*/
private function parseRoles(string $value): array
{
$roles = explode(',', $value);
return RoleMapper::map($roles);
}
}
4 changes: 3 additions & 1 deletion src/SiteKit/Voter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ protected function voteOnAttribute(
return false;
}

$roles = $this->accessMap->getPatterns($subject)[0];
$patterns = $this->accessMap->getPatterns($subject);

$roles = $patterns[0];

if ($roles === null || count($roles) === 0) {
return true;
Expand Down
5 changes: 0 additions & 5 deletions src/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ public function loadUserByIdentifier(string $identifier): UserInterface
return $this->users[$identifier];
}

public function loadUserByUsername(string $username): UserInterface
{
return $this->loadUserByIdentifier($username);
}

/**
* Refreshes the user after being reloaded from the session.
*
Expand Down
124 changes: 124 additions & 0 deletions test/Entity/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace Atoolo\Security\Test\Entity;

use Atoolo\Security\Entity\User;
use Atoolo\Security\Exception\SecurityException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(User::class)]
class UserTest extends TestCase
{
public function testGetUserIdentifier(): void
{
$user = new User('test', []);
$this->assertEquals(
'test',
$user->getUserIdentifier(),
'User::getUserIdentifier should return the username'
);
}

public function testGetRoles(): void
{
$user = new User('', ['ROLE_TEST']);
$this->assertEquals(
['ROLE_TEST'],
$user->getRoles(),
'User::getRoles should return the roles'
);
}

public function testSetAndGetPassword(): void
{
$user = new User('', []);
$user->setPassword('develop');
$this->assertEquals(
'develop',
$user->getPassword(),
'User::setPassword should set the password'
);
}

public function testSetPasswordCallback(): void
{
$user = new User('', []);
$user->setPasswordCallback(function () {
return 'develop';
});
$this->assertEquals(
'develop',
$user->getPassword(),
'getPassword should use the callback to get the password'
);
}

public function testGetSalt(): void
{
$user = new User('', []);
$this->assertNull(
$user->getSalt(),
'User::getSalt should return null'
);
}

public function testEraseCredentials(): void
{
$user = new User('', []);
$user->setPassword('develop');
$user->eraseCredentials();
$this->assertEquals(
$user->getPassword(),
'develop',
'password should be the hashed password and must not be erased'
);
}

public function testOfArray(): void
{
$data = [
'username' => 'test',
'password' => 'test',
'roles' => ['test']
];
$user = User::ofArray($data);
$expected = new User('test', ['ROLE_TEST']);
$expected->setPassword('test');

$this->assertEquals(
$expected,
$user,
'User::ofArray should return a User object with the given data'
);
}

public function testOfArrayMissingUsername(): void
{
$this->expectException(SecurityException::class);
User::ofArray([
'password' => 'test',
'roles' => ['test']
]);
}

public function testOfArrayMissingPassword(): void
{
$this->expectException(SecurityException::class);
User::ofArray([
'username' => 'test',
'roles' => ['test']
]);
}

public function testOfArrayMissingRoles(): void
{
$this->expectException(SecurityException::class);
User::ofArray([
'username' => 'test',
'password' => 'test',
]);
}
}
Loading

0 comments on commit 102a2ba

Please sign in to comment.