FLOW3-Composer /Tests/Unit/Security/Authorization/Voter/PolicyTest.php

Language PHP Lines 228
MD5 Hash ecea537099be2496c273afea64cd0d67
Repository https://github.com/christianjul/FLOW3-Composer.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace TYPO3\FLOW3\Tests\Unit\Security\Authorization\Voter;

/*                                                                        *
 * This script belongs to the FLOW3 framework.                            *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License, either version 3   *
 * of the License, or (at your option) any later version.                 *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

/**
 * Testcase for the Policy voter
 *
 */
class PolicyTest extends \TYPO3\FLOW3\Tests\UnitTestCase {

	/**
	 * @test
	 */
	public function voteForJoinPointAbstainsIfNoPrivilegeWasConfigured() {
		$mockRoleAdministrator = $this->getMock('TYPO3\FLOW3\Security\Policy\Role', array(), array(), 'role1' . md5(uniqid(mt_rand(), TRUE)), FALSE);
		$mockRoleAdministrator->expects($this->any())->method('__toString')->will($this->returnValue('ADMINISTRATOR'));

		$mockRoleCustomer = $this->getMock('TYPO3\FLOW3\Security\Policy\Role', array(), array(), 'role2' . md5(uniqid(mt_rand(), TRUE)), FALSE);
		$mockRoleCustomer->expects($this->any())->method('__toString')->will($this->returnValue('CUSTOMER'));

		$mockSecurityContext = $this->getMock('TYPO3\FLOW3\Security\Context', array(), array(), '', FALSE);
		$mockSecurityContext->expects($this->once())->method('getRoles')->will($this->returnValue(array($mockRoleAdministrator, $mockRoleCustomer)));
		$mockJoinPoint = $this->getMock('TYPO3\FLOW3\Aop\JoinPointInterface');

		$mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService');
		$mockPolicyService->expects($this->any())->method('getPrivilegesForJoinPoint')->will($this->returnValue(array()));

		$Policy = new \TYPO3\FLOW3\Security\Authorization\Voter\Policy($mockPolicyService);
		$this->assertEquals($Policy->voteForJoinPoint($mockSecurityContext, $mockJoinPoint), \TYPO3\FLOW3\Security\Authorization\Voter\Policy::VOTE_ABSTAIN, 'The wrong vote was returned!');
	}

	/**
	 * @test
	 */
	public function voteForJoinPointAbstainsIfNoRolesAreAvailable() {
		$mockSecurityContext = $this->getMock('TYPO3\FLOW3\Security\Context', array(), array(), '', FALSE);
		$mockSecurityContext->expects($this->once())->method('getRoles')->will($this->returnValue(array()));

		$mockJoinPoint = $this->getMock('TYPO3\FLOW3\Aop\JoinPointInterface', array(), array(), '', FALSE);
		$mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);

		$Policy = new \TYPO3\FLOW3\Security\Authorization\Voter\Policy($mockPolicyService);
		$this->assertEquals($Policy->voteForJoinPoint($mockSecurityContext, $mockJoinPoint), \TYPO3\FLOW3\Security\Authorization\Voter\Policy::VOTE_ABSTAIN, 'The wrong vote was returned!');
	}

	/**
	 * @test
	 */
	public function voteForJoinPointAbstainsIfNoPolicyEntryCouldBeFound() {
		$mockSecurityContext = $this->getMock('TYPO3\FLOW3\Security\Context', array(), array(), '', FALSE);
		$mockSecurityContext->expects($this->once())->method('getRoles')->will($this->returnValue(array(new \TYPO3\FLOW3\Security\Policy\Role('role1'))));

		$mockJoinPoint = $this->getMock('TYPO3\FLOW3\Aop\JoinPointInterface', array(), array(), '', FALSE);
		$mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);
		$mockPolicyService->expects($this->once())->method('getPrivilegesForJoinPoint')->will($this->throwException(new \TYPO3\FLOW3\Security\Exception\NoEntryInPolicyException()));

		$voter = new \TYPO3\FLOW3\Security\Authorization\Voter\Policy($mockPolicyService);
		$this->assertEquals($voter->voteForJoinPoint($mockSecurityContext, $mockJoinPoint), \TYPO3\FLOW3\Security\Authorization\Voter\Policy::VOTE_ABSTAIN, 'The wrong vote was returned!');
	}

	/**
	 * @test
	 */
	public function voteForJoinPointDeniesAccessIfADenyPrivilegeWasConfiguredForOneOfTheRoles() {
		$role1ClassName = 'role1' . md5(uniqid(mt_rand(), TRUE));
		$role2ClassName = 'role2' . md5(uniqid(mt_rand(), TRUE));

		$mockRoleAdministrator = $this->getMock('TYPO3\FLOW3\Security\Policy\Role', array(), array(), $role1ClassName, FALSE);
		$mockRoleAdministrator->expects($this->any())->method('__toString')->will($this->returnValue('Administrator'));

		$mockRoleCustomer = $this->getMock('TYPO3\FLOW3\Security\Policy\Role', array(), array(), $role2ClassName, FALSE);
		$mockRoleCustomer->expects($this->any())->method('__toString')->will($this->returnValue('Customer'));

		$mockSecurityContext = $this->getMock('TYPO3\FLOW3\Security\Context', array(), array(), '', FALSE);
		$mockSecurityContext->expects($this->once())->method('getRoles')->will($this->returnValue(array($mockRoleAdministrator, $mockRoleCustomer)));
		$mockJoinPoint = $this->getMock('TYPO3\FLOW3\Aop\JoinPointInterface');

		$getPrivilegesCallback = function() use (&$role1ClassName) {
			$args = func_get_args();
			if ($args[0] instanceof $role1ClassName) {
				return array(\TYPO3\FLOW3\Security\Policy\PolicyService::PRIVILEGE_DENY);
			} else {
				return array(\TYPO3\FLOW3\Security\Policy\PolicyService::PRIVILEGE_GRANT);
			}
		};

		$mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService');
		$mockPolicyService->expects($this->any())->method('getPrivilegesForJoinPoint')->will($this->returnCallback($getPrivilegesCallback));

		$Policy = new \TYPO3\FLOW3\Security\Authorization\Voter\Policy($mockPolicyService);
		$this->assertEquals($Policy->voteForJoinPoint($mockSecurityContext, $mockJoinPoint), \TYPO3\FLOW3\Security\Authorization\Voter\Policy::VOTE_DENY , 'The wrong vote was returned!');
	}

	/**
	 * @test
	 */
	public function voteForJoinPointGrantsAccessIfAGrantPrivilegeAndNoDenyPrivilegeWasConfigured() {
		$role1ClassName = 'role1' . md5(uniqid(mt_rand(), TRUE));
		$role2ClassName = 'role2' . md5(uniqid(mt_rand(), TRUE));

		$mockRoleAdministrator = $this->getMock('TYPO3\FLOW3\Security\Policy\Role', array(), array(), $role1ClassName, FALSE);
		$mockRoleAdministrator->expects($this->any())->method('__toString')->will($this->returnValue('Administrator'));

		$mockRoleCustomer = $this->getMock('TYPO3\FLOW3\Security\Policy\Role', array(), array(), $role2ClassName, FALSE);
		$mockRoleCustomer->expects($this->any())->method('__toString')->will($this->returnValue('Customer'));

		$mockSecurityContext = $this->getMock('TYPO3\FLOW3\Security\Context', array(), array(), '', FALSE);
		$mockSecurityContext->expects($this->once())->method('getRoles')->will($this->returnValue(array($mockRoleAdministrator, $mockRoleCustomer)));
		$mockJoinPoint = $this->getMock('TYPO3\FLOW3\Aop\JoinPointInterface');

		$getPrivilegesCallback = function() use (&$role1ClassName) {
			$args = func_get_args();
			if ($args[0] instanceof $role1ClassName) {
				return array(\TYPO3\FLOW3\Security\Policy\PolicyService::PRIVILEGE_GRANT);
			} else {
				return array();
			}
		};

		$mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService');
		$mockPolicyService->expects($this->any())->method('getPrivilegesForJoinPoint')->will($this->returnCallback($getPrivilegesCallback));

		$Policy = new \TYPO3\FLOW3\Security\Authorization\Voter\Policy($mockPolicyService);
		$this->assertEquals($Policy->voteForJoinPoint($mockSecurityContext, $mockJoinPoint), \TYPO3\FLOW3\Security\Authorization\Voter\Policy::VOTE_GRANT , 'The wrong vote was returned!');
	}

	/**
	 * @test
	 */
	public function voteForResourceAbstainsIfNoRolesAreAvailable() {
		$mockSecurityContext = $this->getMock('TYPO3\FLOW3\Security\Context', array(), array(), '', FALSE);
		$mockSecurityContext->expects($this->once())->method('getRoles')->will($this->returnValue(array()));

		$mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);

		$voter = new \TYPO3\FLOW3\Security\Authorization\Voter\Policy($mockPolicyService);
		$this->assertEquals($voter->voteForResource($mockSecurityContext, 'myResource'), \TYPO3\FLOW3\Security\Authorization\Voter\Policy::VOTE_ABSTAIN, 'The wrong vote was returned!');
	}

	/**
	 * @test
	 */
	public function voteForResourceAbstainsIfNoPolicyEntryCouldBeFound() {
		$mockSecurityContext = $this->getMock('TYPO3\FLOW3\Security\Context', array(), array(), '', FALSE);
		$mockSecurityContext->expects($this->once())->method('getRoles')->will($this->returnValue(array(new \TYPO3\FLOW3\Security\Policy\Role('role1'))));

		$mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService', array(), array(), '', FALSE);
		$mockPolicyService->expects($this->once())->method('getPrivilegeForResource')->will($this->throwException(new \TYPO3\FLOW3\Security\Exception\NoEntryInPolicyException()));

		$voter = new \TYPO3\FLOW3\Security\Authorization\Voter\Policy($mockPolicyService);
		$this->assertEquals($voter->voteForResource($mockSecurityContext, 'myResource'), \TYPO3\FLOW3\Security\Authorization\Voter\Policy::VOTE_ABSTAIN, 'The wrong vote was returned!');
	}

	/**
	 * @test
	 */
	public function voteForResourceDeniesAccessIfADenyPrivilegeWasConfiguredForOneOfTheRoles() {
		$role1ClassName = 'role1' . md5(uniqid(mt_rand(), TRUE));
		$role2ClassName = 'role2' . md5(uniqid(mt_rand(), TRUE));

		$mockRoleAdministrator = $this->getMock('TYPO3\FLOW3\Security\Policy\Role', array(), array(), $role1ClassName, FALSE);
		$mockRoleAdministrator->expects($this->any())->method('__toString')->will($this->returnValue('ADMINISTRATOR'));

		$mockRoleCustomer = $this->getMock('TYPO3\FLOW3\Security\Policy\Role', array(), array(), $role2ClassName, FALSE);
		$mockRoleCustomer->expects($this->any())->method('__toString')->will($this->returnValue('CUSTOMER'));

		$mockSecurityContext = $this->getMock('TYPO3\FLOW3\Security\Context', array(), array(), '', FALSE);
		$mockSecurityContext->expects($this->once())->method('getRoles')->will($this->returnValue(array($mockRoleAdministrator, $mockRoleCustomer)));

		$getPrivilegeCallback = function() use (&$role1ClassName) {
			$args = func_get_args();
			if ($args[0] instanceof $role1ClassName) {
				return \TYPO3\FLOW3\Security\Policy\PolicyService::PRIVILEGE_DENY;
			} else {
				return NULL;
			}
		};

		$mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService');
		$mockPolicyService->expects($this->any())->method('getPrivilegeForResource')->will($this->returnCallback($getPrivilegeCallback));

		$Policy = new \TYPO3\FLOW3\Security\Authorization\Voter\Policy($mockPolicyService);
		$this->assertEquals($Policy->voteForResource($mockSecurityContext, 'myResource'), \TYPO3\FLOW3\Security\Authorization\Voter\Policy::VOTE_DENY , 'The wrong vote was returned!');
	}

	/**
	 * @test
	 */
	public function voteForResourceGrantsAccessIfAGrantPrivilegeAndNoDenyPrivilegeWasConfigured() {
		$role1ClassName = 'role1' . md5(uniqid(mt_rand(), TRUE));
		$role2ClassName = 'role2' . md5(uniqid(mt_rand(), TRUE));

		$mockRoleAdministrator = $this->getMock('TYPO3\FLOW3\Security\Policy\Role', array(), array(), $role1ClassName, FALSE);
		$mockRoleAdministrator->expects($this->any())->method('__toString')->will($this->returnValue('Administrator'));

		$mockRoleCustomer = $this->getMock('TYPO3\FLOW3\Security\Policy\Role', array(), array(), $role2ClassName, FALSE);
		$mockRoleCustomer->expects($this->any())->method('__toString')->will($this->returnValue('Customer'));

		$mockSecurityContext = $this->getMock('TYPO3\FLOW3\Security\Context', array(), array(), '', FALSE);
		$mockSecurityContext->expects($this->once())->method('getRoles')->will($this->returnValue(array($mockRoleAdministrator, $mockRoleCustomer)));

		$getPrivilegesCallback = function() use (&$role1ClassName) {
			$args = func_get_args();
			if ($args[0] instanceof $role1ClassName) {
				return \TYPO3\FLOW3\Security\Policy\PolicyService::PRIVILEGE_GRANT;
			} else {
				return NULL;
			}
		};

		$mockPolicyService = $this->getMock('TYPO3\FLOW3\Security\Policy\PolicyService');
		$mockPolicyService->expects($this->any())->method('getPrivilegeForResource')->will($this->returnCallback($getPrivilegesCallback));

		$Policy = new \TYPO3\FLOW3\Security\Authorization\Voter\Policy($mockPolicyService);
		$this->assertEquals($Policy->voteForResource($mockSecurityContext, 'myResource'), \TYPO3\FLOW3\Security\Authorization\Voter\Policy::VOTE_GRANT , 'The wrong vote was returned!');
	}
}

?>
Back to Top