AbstractEntityVoterTest.php 14 KB

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