Explorar o código

[Security] added some unit tests (WIP)

Fabien Potencier %!s(int64=14) %!d(string=hai) anos
pai
achega
82f8ab839f

+ 3 - 12
src/Symfony/Component/Security/Authorization/Voter/AuthenticatedVoter.php

@@ -53,24 +53,15 @@ class AuthenticatedVoter implements VoterInterface
 
             $result = VoterInterface::ACCESS_DENIED;
 
-            if (self::IS_AUTHENTICATED_FULLY === $attribute) {
-                if ($this->isFullyAuthenticated($token)) {
-                    return VoterInterface::ACCESS_GRANTED;
-                }
+            if (self::IS_AUTHENTICATED_FULLY === $attribute && !$token instanceof AnonymousToken) {
+                return VoterInterface::ACCESS_GRANTED;
             }
 
             if (self::IS_AUTHENTICATED_ANONYMOUSLY === $attribute) {
-                if (null === $token || $token instanceof AnonymousToken || $this->isFullyAuthenticated($token)) {
-                    return VoterInterface::ACCESS_GRANTED;
-                }
+                return VoterInterface::ACCESS_GRANTED;
             }
         }
 
         return $result;
     }
-
-    protected function isFullyAuthenticated(TokenInterface $token)
-    {
-        return null !== $token && !$token instanceof AnonymousToken;
-    }
 }

+ 96 - 0
tests/Symfony/Tests/Component/Security/Authorization/AccessDecisionManagerTest.php

@@ -0,0 +1,96 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Security\Authorization;
+
+use Symfony\Component\Security\Authorization\AccessDecisionManager;
+use Symfony\Component\Security\Authorization\Voter\VoterInterface;
+
+class AccessDecisionManagerTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @dataProvider getStrategyTests
+     */
+    public function testStrategies($strategy, $voters, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions, $expected)
+    {
+        $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
+        $manager = new AccessDecisionManager($voters, $strategy, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions);
+
+        $this->assertSame($expected, $manager->decide($token, array('ROLE_FOO')));
+    }
+
+    public function getStrategyTests()
+    {
+        return array(
+            // affirmative
+            array('affirmative', $this->getVoters(1, 0, 0), false, true, true),
+            array('affirmative', $this->getVoters(1, 2, 0), false, true, true),
+            array('affirmative', $this->getVoters(0, 1, 0), false, true, false),
+            array('affirmative', $this->getVoters(0, 0, 0), false, true, false),
+            array('affirmative', $this->getVoters(0, 0, 1), false, true, false),
+            array('affirmative', $this->getVoters(0, 0, 1), true, true, true),
+
+            // consensus
+            array('consensus', $this->getVoters(1, 0, 0), false, true, true),
+            array('consensus', $this->getVoters(1, 2, 0), false, true, false),
+            array('consensus', $this->getVoters(2, 1, 0), false, true, true),
+
+            array('consensus', $this->getVoters(0, 0, 0), false, true, false),
+            array('consensus', $this->getVoters(0, 0, 1), false, true, false),
+
+            array('consensus', $this->getVoters(0, 0, 0), true, true, true),
+            array('consensus', $this->getVoters(0, 0, 1), true, true, true),
+
+            array('consensus', $this->getVoters(2, 2, 0), false, true, true),
+            array('consensus', $this->getVoters(2, 2, 1), false, true, true),
+
+            array('consensus', $this->getVoters(2, 2, 0), false, false, false),
+            array('consensus', $this->getVoters(2, 2, 1), false, false, false),
+
+            // unanimous
+            array('unanimous', $this->getVoters(1, 0, 0), false, true, true),
+            array('unanimous', $this->getVoters(1, 0, 1), false, true, true),
+            array('unanimous', $this->getVoters(1, 1, 0), false, true, false),
+
+            array('unanimous', $this->getVoters(0, 0, 0), false, true, false),
+            array('unanimous', $this->getVoters(0, 0, 0), true, true, true),
+
+            array('unanimous', $this->getVoters(0, 0, 2), false, true, false),
+            array('unanimous', $this->getVoters(0, 0, 2), true, true, true),
+        );
+    }
+
+    protected function getVoters($grants, $denies, $abstains)
+    {
+        $voters = array();
+        for ($i = 0; $i < $grants; $i++) {
+            $voters[] = $this->getVoter(VoterInterface::ACCESS_GRANTED);
+        }
+        for ($i = 0; $i < $denies; $i++) {
+            $voters[] = $this->getVoter(VoterInterface::ACCESS_DENIED);
+        }
+        for ($i = 0; $i < $abstains; $i++) {
+            $voters[] = $this->getVoter(VoterInterface::ACCESS_ABSTAIN);
+        }
+
+        return $voters;
+    }
+
+    protected function getVoter($vote)
+    {
+        $voter = $this->getMock('Symfony\Component\Security\Authorization\Voter\VoterInterface');
+        $voter->expects($this->any())
+              ->method('vote')
+              ->will($this->returnValue($vote));
+        ;
+
+        return $voter;
+    }
+}

+ 53 - 0
tests/Symfony/Tests/Component/Security/Authorization/Voter/AuthenticatedVoterTest.php

@@ -0,0 +1,53 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Security\Authorization\Voter;
+
+use Symfony\Component\Security\Authorization\Voter\AuthenticatedVoter;
+use Symfony\Component\Security\Authorization\Voter\VoterInterface;
+use Symfony\Component\Security\Role\Role;
+
+class AuthenticatedVoterTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @dataProvider getVoteTests
+     */
+    public function testVote($authenticated, $attributes, $expected)
+    {
+        $voter = new AuthenticatedVoter();
+
+        $this->assertSame($expected, $voter->vote($this->getToken($authenticated), null, $attributes));
+    }
+
+    public function getVoteTests()
+    {
+        return array(
+            array(true, array(), VoterInterface::ACCESS_ABSTAIN),
+            array(true, array('FOO'), VoterInterface::ACCESS_ABSTAIN),
+            array(false, array(), VoterInterface::ACCESS_ABSTAIN),
+            array(false, array('FOO'), VoterInterface::ACCESS_ABSTAIN),
+
+            array(true, array('IS_AUTHENTICATED_ANONYMOUSLY'), VoterInterface::ACCESS_GRANTED),
+            array(false, array('IS_AUTHENTICATED_ANONYMOUSLY'), VoterInterface::ACCESS_GRANTED),
+
+            array(true, array('IS_AUTHENTICATED_FULLY'), VoterInterface::ACCESS_GRANTED),
+            array(false, array('IS_AUTHENTICATED_FULLY'), VoterInterface::ACCESS_DENIED),
+        );
+    }
+
+    protected function getToken($authenticated)
+    {
+        if ($authenticated) {
+            return $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
+        } else {
+            return $this->getMock('Symfony\Component\Security\Authentication\Token\AnonymousToken', null, array('', ''));
+        }
+    }
+}

+ 37 - 0
tests/Symfony/Tests/Component/Security/Authorization/Voter/RoleHierarchyVoterTest.php

@@ -0,0 +1,37 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Security\Authorization\Voter;
+
+use Symfony\Component\Security\Authorization\Voter\RoleHierarchyVoter;
+use Symfony\Component\Security\Authorization\Voter\VoterInterface;
+use Symfony\Component\Security\Role\RoleHierarchy;
+
+require_once __DIR__.'/RoleVoterTest.php';
+
+class RoleHierarchyVoterTest extends RoleVoterTest
+{
+    /**
+     * @dataProvider getVoteTests
+     */
+    public function testVote($roles, $attributes, $expected)
+    {
+        $voter = new RoleHierarchyVoter(new RoleHierarchy(array('ROLE_FOO' => array('ROLE_FOOBAR'))));
+
+        $this->assertSame($expected, $voter->vote($this->getToken($roles), null, $attributes));
+    }
+
+    public function getVoteTests()
+    {
+        return array_merge(parent::getVoteTests(), array(
+            array(array('ROLE_FOO'), array('ROLE_FOOBAR'), VoterInterface::ACCESS_GRANTED),
+        ));
+    }
+}

+ 54 - 0
tests/Symfony/Tests/Component/Security/Authorization/Voter/RoleVoterTest.php

@@ -0,0 +1,54 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Security\Authorization\Voter;
+
+use Symfony\Component\Security\Authorization\Voter\RoleVoter;
+use Symfony\Component\Security\Authorization\Voter\VoterInterface;
+use Symfony\Component\Security\Role\Role;
+
+class RoleVoterTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @dataProvider getVoteTests
+     */
+    public function testVote($roles, $attributes, $expected)
+    {
+        $voter = new RoleVoter();
+
+        $this->assertSame($expected, $voter->vote($this->getToken($roles), null, $attributes));
+    }
+
+    public function getVoteTests()
+    {
+        return array(
+            array(array(), array(), VoterInterface::ACCESS_ABSTAIN),
+            array(array(), array('FOO'), VoterInterface::ACCESS_ABSTAIN),
+            array(array(), array('ROLE_FOO'), VoterInterface::ACCESS_DENIED),
+            array(array('ROLE_FOO'), array('ROLE_FOO'), VoterInterface::ACCESS_GRANTED),
+            array(array('ROLE_FOO'), array('FOO', 'ROLE_FOO'), VoterInterface::ACCESS_GRANTED),
+            array(array('ROLE_BAR', 'ROLE_FOO'), array('ROLE_FOO'), VoterInterface::ACCESS_GRANTED),
+        );
+    }
+
+    protected function getToken(array $roles)
+    {
+        foreach ($roles as $i => $role) {
+            $roles[$i] = new Role($role);
+        }
+        $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
+        $token->expects($this->once())
+              ->method('getRoles')
+              ->will($this->returnValue($roles));
+        ;
+
+        return $token;
+    }
+}

+ 38 - 0
tests/Symfony/Tests/Component/Security/Encoder/PlaintextPasswordEncoderTest.php

@@ -0,0 +1,38 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Security\Encoder;
+
+use Symfony\Component\Security\Encoder\PlaintextPasswordEncoder;
+
+class PlaintextPasswordEncoderTest extends \PHPUnit_Framework_TestCase
+{
+    public function testIsPasswordValid()
+    {
+        $encoder = new PlaintextPasswordEncoder();
+
+        $this->assertSame(true, $encoder->isPasswordValid('foo', 'foo', ''));
+        $this->assertSame(false, $encoder->isPasswordValid('bar', 'foo', ''));
+        $this->assertSame(false, $encoder->isPasswordValid('FOO', 'foo', ''));
+
+        $encoder = new PlaintextPasswordEncoder(true);
+
+        $this->assertSame(true, $encoder->isPasswordValid('foo', 'foo', ''));
+        $this->assertSame(false, $encoder->isPasswordValid('bar', 'foo', ''));
+        $this->assertSame(true, $encoder->isPasswordValid('FOO', 'foo', ''));
+    }
+
+    public function testEncodePassword()
+    {
+        $encoder = new PlaintextPasswordEncoder();
+
+        $this->assertSame('foo', $encoder->encodePassword('foo', ''));
+    }
+}