AbstractEntityVoterTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace App\Tests\Unit\Security\Voter;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Person\Person;
  5. use App\Security\Voter\EntityVoter\AbstractEntityVoter;
  6. use App\Service\Access\Utils;
  7. use App\Service\Security\InternalRequestsService;
  8. use App\Service\Security\SwitchUser;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use PHPUnit\Framework\MockObject\MockObject;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\SecurityBundle\Security;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. class TestableAbstractEntityVoter extends AbstractEntityVoter {
  15. public const READ = 'READ';
  16. public const EDIT = 'EDIT';
  17. public const CREATE = 'CREATE';
  18. public const DELETE = 'DELETE';
  19. public static function setEntityClass(?string $entityClass): void {
  20. static::$entityClass = $entityClass;
  21. }
  22. public static function getEntityClass(): ?string {
  23. return static::$entityClass;
  24. }
  25. /**
  26. * @param array<string> $allowedOperations
  27. * @return void
  28. */
  29. public static function setAllowedOperations(array $allowedOperations): void {
  30. static::$allowedOperations = $allowedOperations;
  31. }
  32. public static function getAllowedOperations(): array {
  33. return static::$allowedOperations;
  34. }
  35. public function supports(string $attribute, mixed $subject): bool {
  36. return parent::supports($attribute, $subject);
  37. }
  38. public function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool {
  39. return parent::voteOnAttribute($attribute, $subject, $token);
  40. }
  41. public function canView(object $subject): bool { return true; }
  42. public function canEdit(object $subject): bool { return true; }
  43. public function canCreate(object $subject): bool { return true; }
  44. public function canDelete(object $subject): bool { return true; }
  45. public function isUserLoggedIn(): bool { return parent::isUserLoggedIn(); }
  46. public function getUser(): ?Access { return parent::getUser(); }
  47. public function isValidInternalRequest(): bool { return parent::isValidInternalRequest(); }
  48. }
  49. class AbstractVoterTest extends TestCase
  50. {
  51. protected Security | MockObject $security;
  52. protected Utils | MockObject $accessUtils;
  53. protected EntityManagerInterface | MockObject $em;
  54. protected InternalRequestsService | MockObject $internalRequestsService;
  55. protected SwitchUser | MockObject $switchUserService;
  56. private bool $initialConditionSaved = false;
  57. private ?string $initialEntityClass = null;
  58. private ?array $initialAllowedOperations = null;
  59. private ?int $initialSwitchHeader = null;
  60. private ?string $initialRemoteAddr = null;
  61. private ?string $initialInternalRequestToken = null;
  62. private function saveInitialCondition() {
  63. $this->initialEntityClass = TestableAbstractEntityVoter::getEntityClass();
  64. $this->initialAllowedOperations = TestableAbstractEntityVoter::getAllowedOperations();
  65. $this->initialSwitchHeader = $_SERVER['HTTP_X_SWITCH_USER'] ?? null;
  66. $this->initialRemoteAddr = $_SERVER['REMOTE_ADDR'] ?? null;
  67. $this->initialInternalRequestToken = $_SERVER['HTTP_INTERNAL_REQUESTS_TOKEN'] ?? null;
  68. $this->initialConditionSaved = true;
  69. }
  70. private function reinitialize() {
  71. // Reinitialize the TestableAbstractVoter static properties
  72. TestableAbstractEntityVoter::setEntityClass($this->initialEntityClass);
  73. TestableAbstractEntityVoter::setAllowedOperations($this->initialAllowedOperations);
  74. // Reinitialize the global variables
  75. if ($this->initialSwitchHeader !== null) {
  76. $_SERVER['HTTP_X_SWITCH_USER'] = $this->initialSwitchHeader;
  77. } else if (array_key_exists('HTTP_X_SWITCH_USER', $_SERVER)) {
  78. unset($_SERVER['HTTP_X_SWITCH_USER']);
  79. }
  80. $_SERVER['REMOTE_ADDR'] = $this->initialRemoteAddr;
  81. if ($this->initialSwitchHeader !== null) {
  82. $_SERVER['HTTP_INTERNAL_REQUESTS_TOKEN'] = $this->initialSwitchHeader;
  83. } else if (array_key_exists('HTTP_INTERNAL_REQUESTS_TOKEN', $_SERVER)) {
  84. unset($_SERVER['HTTP_INTERNAL_REQUESTS_TOKEN']);
  85. }
  86. }
  87. public function setUp(): void {
  88. if (!$this->initialConditionSaved) {
  89. $this->saveInitialCondition();
  90. }
  91. $this->security = $this->getMockBuilder(Security::class)->disableOriginalConstructor()->getMock();
  92. $this->accessUtils = $this->getMockBuilder(Utils::class)->disableOriginalConstructor()->getMock();
  93. $this->internalRequestsService = $this->getMockBuilder(InternalRequestsService::class)->disableOriginalConstructor()->getMock();
  94. $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  95. $this->switchUserService = $this->getMockBuilder(SwitchUser::class)->disableOriginalConstructor()->getMock();
  96. }
  97. public function tearDown(): void
  98. {
  99. $this->reinitialize();
  100. }
  101. private function makeAbstractVoterMockFor(string $methodName): MockObject | TestableAbstractEntityVoter {
  102. return $this->getMockBuilder(TestableAbstractEntityVoter::class)
  103. ->setConstructorArgs([
  104. $this->security,
  105. $this->accessUtils,
  106. $this->internalRequestsService,
  107. $this->em,
  108. $this->switchUserService
  109. ])
  110. ->setMethodsExcept([$methodName])
  111. ->getMock();
  112. }
  113. public function testSupports(): void {
  114. TestableAbstractEntityVoter::setEntityClass(Access::class);
  115. $abstractVoter = $this->makeAbstractVoterMockFor('supports');
  116. $access = new Access();
  117. $this->assertTrue(
  118. $abstractVoter->supports(TestableAbstractEntityVoter::READ, $access)
  119. );
  120. $this->assertTrue(
  121. $abstractVoter->supports(TestableAbstractEntityVoter::EDIT, $access)
  122. );
  123. $this->assertTrue(
  124. $abstractVoter->supports(TestableAbstractEntityVoter::CREATE, $access)
  125. );
  126. $this->assertTrue(
  127. $abstractVoter->supports(TestableAbstractEntityVoter::DELETE, $access)
  128. );
  129. }
  130. public function testSupportsNotSupportedClass(): void {
  131. TestableAbstractEntityVoter::setEntityClass(Access::class);
  132. $abstractVoter = $this->makeAbstractVoterMockFor('supports');
  133. $person = new Person();
  134. $this->assertFalse(
  135. $abstractVoter->supports(TestableAbstractEntityVoter::READ, $person)
  136. );
  137. }
  138. public function testSupportsNotAllowedOperation(): void {
  139. TestableAbstractEntityVoter::setEntityClass(Access::class);
  140. TestableAbstractEntityVoter::setAllowedOperations([TestableAbstractEntityVoter::READ]);
  141. $abstractVoter = $this->makeAbstractVoterMockFor('supports');
  142. $access = new Access();
  143. $this->assertTrue(
  144. $abstractVoter->supports(TestableAbstractEntityVoter::READ, $access)
  145. );
  146. $this->assertFalse(
  147. $abstractVoter->supports(TestableAbstractEntityVoter::EDIT, $access)
  148. );
  149. $this->assertFalse(
  150. $abstractVoter->supports(TestableAbstractEntityVoter::CREATE, $access)
  151. );
  152. $this->assertFalse(
  153. $abstractVoter->supports(TestableAbstractEntityVoter::DELETE, $access)
  154. );
  155. }
  156. public function testSupportsMissingEntityClass(): void {
  157. $abstractVoter = $this->makeAbstractVoterMockFor('supports');
  158. $this->expectException(\RuntimeException::class);
  159. $this->expectExceptionMessage('Setup the self::$entityClass property, or override the supports() method');
  160. $access = new Access();
  161. $abstractVoter->supports(TestableAbstractEntityVoter::READ, $access);
  162. }
  163. public function testVoteOnAttributeRead(): void {
  164. $abstractVoter = $this->makeAbstractVoterMockFor('voteOnAttribute');
  165. $subject = $this->getMockBuilder(Access::class)->getMock();
  166. $token = $this->getMockBuilder(TokenInterface::class)->getMock();
  167. $abstractVoter->expects(self::once())->method('canView')->with($subject)->willReturn(true);
  168. $abstractVoter->expects(self::never())->method('canEdit');
  169. $abstractVoter->expects(self::never())->method('canCreate');
  170. $abstractVoter->expects(self::never())->method('canDelete');
  171. $this->assertTrue(
  172. $abstractVoter->voteOnAttribute(TestableAbstractEntityVoter::READ, $subject, $token)
  173. );
  174. }
  175. public function testVoteOnAttributeEdit(): void {
  176. $abstractVoter = $this->makeAbstractVoterMockFor('voteOnAttribute');
  177. $subject = $this->getMockBuilder(Access::class)->getMock();
  178. $token = $this->getMockBuilder(TokenInterface::class)->getMock();
  179. $abstractVoter->expects(self::never())->method('canView');
  180. $abstractVoter->expects(self::once())->method('canEdit')->with($subject)->willReturn(true);
  181. $abstractVoter->expects(self::never())->method('canCreate');
  182. $abstractVoter->expects(self::never())->method('canDelete');
  183. $this->assertTrue(
  184. $abstractVoter->voteOnAttribute(TestableAbstractEntityVoter::EDIT, $subject, $token)
  185. );
  186. }
  187. public function testVoteOnAttributeCreate(): void {
  188. $abstractVoter = $this->makeAbstractVoterMockFor('voteOnAttribute');
  189. $subject = $this->getMockBuilder(Access::class)->getMock();
  190. $token = $this->getMockBuilder(TokenInterface::class)->getMock();
  191. $abstractVoter->expects(self::never())->method('canView');
  192. $abstractVoter->expects(self::never())->method('canEdit');
  193. $abstractVoter->expects(self::once())->method('canCreate')->with($subject)->willReturn(true);
  194. $abstractVoter->expects(self::never())->method('canDelete');
  195. $this->assertTrue(
  196. $abstractVoter->voteOnAttribute(TestableAbstractEntityVoter::CREATE, $subject, $token)
  197. );
  198. }
  199. public function testVoteOnAttributeDelete(): void {
  200. $abstractVoter = $this->makeAbstractVoterMockFor('voteOnAttribute');
  201. $subject = $this->getMockBuilder(Access::class)->getMock();
  202. $token = $this->getMockBuilder(TokenInterface::class)->getMock();
  203. $abstractVoter->expects(self::never())->method('canView');
  204. $abstractVoter->expects(self::never())->method('canEdit');
  205. $abstractVoter->expects(self::never())->method('canCreate');
  206. $abstractVoter->expects(self::once())->method('canDelete')->with($subject)->willReturn(true);
  207. $this->assertTrue(
  208. $abstractVoter->voteOnAttribute(TestableAbstractEntityVoter::DELETE, $subject, $token)
  209. );
  210. }
  211. public function testVoteOnAttributeInvalidAttribute(): void {
  212. $abstractVoter = $this->makeAbstractVoterMockFor('voteOnAttribute');
  213. $subject = $this->getMockBuilder(Access::class)->getMock();
  214. $token = $this->getMockBuilder(TokenInterface::class)->getMock();
  215. $abstractVoter->expects(self::never())->method('canView');
  216. $abstractVoter->expects(self::never())->method('canEdit');
  217. $abstractVoter->expects(self::never())->method('canCreate');
  218. $abstractVoter->expects(self::never())->method('canDelete');
  219. $this->assertFalse(
  220. $abstractVoter->voteOnAttribute('other_attr', $subject, $token)
  221. );
  222. }
  223. public function testGetUser() {
  224. $user = $this->getMockBuilder(Access::class)->getMock();
  225. $this->security->expects(self::once())->method('getUser')->willReturn($user);
  226. $abstractVoter = $this->makeAbstractVoterMockFor('getUser');
  227. $this->assertEquals(
  228. $user,
  229. $abstractVoter->getUser()
  230. );
  231. }
  232. public function testGetUserNoUser() {
  233. $this->security->expects(self::once())->method('getUser')->willReturn(null);
  234. $abstractVoter = $this->makeAbstractVoterMockFor('getUser');
  235. $this->assertNull(
  236. $abstractVoter->getUser()
  237. );
  238. }
  239. public function testGetUserSwitchUser() {
  240. $originalUser = $this->getMockBuilder(Access::class)->getMock();
  241. $switchUser = $this->getMockBuilder(Access::class)->getMock();
  242. $_SERVER['HTTP_X_SWITCH_USER'] = 1;
  243. $this->security->expects(self::once())->method('getUser')->willReturn($originalUser);
  244. $this->em->expects(self::once())->method('find')->with(Access::class, 1)->willReturn($switchUser);
  245. $abstractVoter = $this->makeAbstractVoterMockFor('getUser');
  246. $this->assertEquals(
  247. $switchUser,
  248. $abstractVoter->getUser()
  249. );
  250. }
  251. public function testIsUserLoggedIn(): void {
  252. $user = $this->getMockBuilder(Access::class)->getMock();
  253. $abstractVoter = $this->makeAbstractVoterMockFor('isUserLoggedIn');
  254. $abstractVoter->expects(self::once())->method('getUser')->willReturn($user);
  255. $this->assertTrue($abstractVoter->isUserLoggedIn());
  256. }
  257. public function testIsUserLoggedInNoUser(): void {
  258. $abstractVoter = $this->makeAbstractVoterMockFor('isUserLoggedIn');
  259. $abstractVoter->expects(self::once())->method('getUser')->willReturn(null);
  260. $this->assertFalse($abstractVoter->isUserLoggedIn());
  261. }
  262. public function testIsValidInternalRequest(): void {
  263. $clientIp = '0.0.0.0';
  264. $token = 'valid_token';
  265. $_SERVER['REMOTE_ADDR'] = $clientIp;
  266. $_SERVER['HTTP_INTERNAL_REQUESTS_TOKEN'] = $token;
  267. $this->internalRequestsService
  268. ->expects(self::once())
  269. ->method('isAllowed')
  270. ->with($clientIp, $token)
  271. ->willReturn(true);
  272. $abstractVoter = $this->makeAbstractVoterMockFor('isValidInternalRequest');
  273. $this->assertTrue($abstractVoter->isValidInternalRequest());
  274. }
  275. }