mirror of
https://github.com/wallabag/wallabag.git
synced 2025-09-15 18:57:05 +00:00
Use IsGranted in UserController
This commit is contained in:
parent
39c24ab6e2
commit
beaca32493
9 changed files with 277 additions and 21 deletions
67
tests/Security/Voter/AdminVoterTest.php
Normal file
67
tests/Security/Voter/AdminVoterTest.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Wallabag\Security\Voter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Wallabag\Entity\User;
|
||||
use Wallabag\Security\Voter\AdminVoter;
|
||||
|
||||
class AdminVoterTest extends TestCase
|
||||
{
|
||||
private $security;
|
||||
private $token;
|
||||
private $adminVoter;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->security = $this->createMock(Security::class);
|
||||
|
||||
$this->token = $this->createMock(TokenInterface::class);
|
||||
$this->token->method('getUser')->willReturn(new User());
|
||||
|
||||
$this->adminVoter = new AdminVoter($this->security);
|
||||
}
|
||||
|
||||
public function testVoteReturnsAbstainForInvalidAttribute(): void
|
||||
{
|
||||
$this->assertSame(VoterInterface::ACCESS_ABSTAIN, $this->adminVoter->vote($this->token, null, ['INVALID']));
|
||||
}
|
||||
|
||||
public function testVoteReturnsDeniedForInvalidUser(): void
|
||||
{
|
||||
$this->token->method('getUser')->willReturn(new \stdClass());
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_DENIED, $this->adminVoter->vote($this->token, null, [AdminVoter::LIST_USERS]));
|
||||
}
|
||||
|
||||
public function testVoteReturnsDeniedForNonSuperAdminListUsers(): void
|
||||
{
|
||||
$this->security->method('isGranted')->with('ROLE_SUPER_ADMIN')->willReturn(false);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_DENIED, $this->adminVoter->vote($this->token, null, [AdminVoter::LIST_USERS]));
|
||||
}
|
||||
|
||||
public function testVoteReturnsGrantedForSuperAdminListUsers(): void
|
||||
{
|
||||
$this->security->method('isGranted')->with('ROLE_SUPER_ADMIN')->willReturn(true);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_GRANTED, $this->adminVoter->vote($this->token, null, [AdminVoter::LIST_USERS]));
|
||||
}
|
||||
|
||||
public function testVoteReturnsDeniedForNonSuperAdminCreateUsers(): void
|
||||
{
|
||||
$this->security->method('isGranted')->with('ROLE_SUPER_ADMIN')->willReturn(false);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_DENIED, $this->adminVoter->vote($this->token, null, [AdminVoter::CREATE_USERS]));
|
||||
}
|
||||
|
||||
public function testVoteReturnsGrantedForSuperAdminCreateUsers(): void
|
||||
{
|
||||
$this->security->method('isGranted')->with('ROLE_SUPER_ADMIN')->willReturn(true);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_GRANTED, $this->adminVoter->vote($this->token, null, [AdminVoter::CREATE_USERS]));
|
||||
}
|
||||
}
|
73
tests/Security/Voter/UserVoterTest.php
Normal file
73
tests/Security/Voter/UserVoterTest.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Wallabag\Security\Voter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Wallabag\Entity\User;
|
||||
use Wallabag\Security\Voter\UserVoter;
|
||||
|
||||
class UserVoterTest extends TestCase
|
||||
{
|
||||
private $security;
|
||||
private $token;
|
||||
private $userVoter;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->security = $this->createMock(Security::class);
|
||||
|
||||
$this->token = $this->createMock(TokenInterface::class);
|
||||
$this->token->method('getUser')->willReturn(new User());
|
||||
|
||||
$this->userVoter = new UserVoter($this->security);
|
||||
}
|
||||
|
||||
public function testVoteReturnsAbstainForInvalidSubject(): void
|
||||
{
|
||||
$this->assertSame(VoterInterface::ACCESS_ABSTAIN, $this->userVoter->vote($this->token, new \stdClass(), [UserVoter::EDIT]));
|
||||
}
|
||||
|
||||
public function testVoteReturnsAbstainForInvalidAttribute(): void
|
||||
{
|
||||
$this->assertSame(VoterInterface::ACCESS_ABSTAIN, $this->userVoter->vote($this->token, new User(), ['INVALID']));
|
||||
}
|
||||
|
||||
public function testVoteReturnsDeniedForNonSuperAdminEdit(): void
|
||||
{
|
||||
$this->security->method('isGranted')->with('ROLE_SUPER_ADMIN')->willReturn(false);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_DENIED, $this->userVoter->vote($this->token, new User(), [UserVoter::EDIT]));
|
||||
}
|
||||
|
||||
public function testVoteReturnsGrantedForSuperAdminEdit(): void
|
||||
{
|
||||
$this->security->method('isGranted')->with('ROLE_SUPER_ADMIN')->willReturn(true);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_GRANTED, $this->userVoter->vote($this->token, new User(), [UserVoter::EDIT]));
|
||||
}
|
||||
|
||||
public function testVoteReturnsDeniedForSelfDelete(): void
|
||||
{
|
||||
$user = new User();
|
||||
$this->token->method('getUser')->willReturn($user);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_DENIED, $this->userVoter->vote($this->token, $user, [UserVoter::DELETE]));
|
||||
}
|
||||
|
||||
public function testVoteReturnsDeniedForNonSuperAdminDelete(): void
|
||||
{
|
||||
$this->security->method('isGranted')->with('ROLE_SUPER_ADMIN')->willReturn(false);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_DENIED, $this->userVoter->vote($this->token, new User(), [UserVoter::DELETE]));
|
||||
}
|
||||
|
||||
public function testVoteReturnsGrantedForSuperAdminDelete(): void
|
||||
{
|
||||
$this->security->method('isGranted')->with('ROLE_SUPER_ADMIN')->willReturn(true);
|
||||
|
||||
$this->assertSame(VoterInterface::ACCESS_GRANTED, $this->userVoter->vote($this->token, new User(), [UserVoter::DELETE]));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue