TinyAuthorizeTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * TinyAuthorizeTest file
  4. *
  5. * 2012-11-05 ms
  6. */
  7. App::uses('TinyAuthorize', 'Tools.Controller/Component/Auth');
  8. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  9. App::uses('Controller', 'Controller');
  10. App::uses('ComponentCollection', 'Controller');
  11. App::uses('CakeRequest', 'Network');
  12. /**
  13. * Test case for DirectAuthentication
  14. *
  15. * @package Test.Case.Controller.Component.Auth
  16. */
  17. class TinyAuthorizeTest extends MyCakeTestCase {
  18. public $fixtures = array('core.user', 'core.auth_user');
  19. public $Collection;
  20. public $request;
  21. /**
  22. * setup
  23. *
  24. * @return void
  25. */
  26. public function setUp() {
  27. parent::setUp();
  28. $this->Collection = new ComponentCollection();
  29. $this->request = new CakeRequest(null, false);
  30. $aclData = <<<INI
  31. [Users]
  32. ; add = public
  33. edit = user
  34. admin_index = admin
  35. [Comments]
  36. ; index is public
  37. add,edit,delete = user
  38. * = admin
  39. [Tags]
  40. add = *
  41. public_action = public
  42. INI;
  43. file_put_contents(TMP . 'acl.ini', $aclData);
  44. $this->assertTrue(file_exists(TMP . 'acl.ini'));
  45. Configure::write('Role', array('user' => 1, 'moderator' => 2, 'admin' => 3, 'public' => -1));
  46. }
  47. public function tearDown() {
  48. unlink(TMP . 'acl.ini');
  49. parent::tearDown();
  50. }
  51. /**
  52. * test applying settings in the constructor
  53. *
  54. * @return void
  55. */
  56. public function testConstructor() {
  57. $object = new TestTinyAuthorize($this->Collection, array(
  58. 'aclModel' => 'AuthRole',
  59. 'aclKey' => 'auth_role_id',
  60. 'autoClearCache' => true,
  61. ));
  62. $this->assertEquals('AuthRole', $object->settings['aclModel']);
  63. $this->assertEquals('auth_role_id', $object->settings['aclKey']);
  64. }
  65. /**
  66. * @return void
  67. */
  68. public function testGetAcl() {
  69. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  70. $res = $object->getAcl();
  71. $expected = array(
  72. 'users' => array(
  73. 'edit' => array(1),
  74. 'admin_index' => array(3)
  75. ),
  76. 'comments' => array(
  77. 'add' => array(1),
  78. 'edit' => array(1),
  79. 'delete' => array(1),
  80. '*' => array(3),
  81. ),
  82. 'tags' => array(
  83. 'add' => array(1, 2, 3, -1),
  84. 'public_action' => array(-1)
  85. ),
  86. );
  87. $this->debug($res);
  88. $this->assertEquals($expected, $res);
  89. }
  90. /**
  91. * @return void
  92. */
  93. public function testBasicUserMethodDisallowed() {
  94. $this->request->params['controller'] = 'users';
  95. $this->request->params['action'] = 'edit';
  96. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  97. $this->assertEquals('Role', $object->settings['aclModel']);
  98. $this->assertEquals('role_id', $object->settings['aclKey']);
  99. $user = array(
  100. 'role_id' => 4,
  101. );
  102. $res = $object->authorize($user, $this->request);
  103. $this->assertFalse($res);
  104. $user = array(
  105. 'role_id' => 3,
  106. );
  107. $res = $object->authorize($user, $this->request);
  108. $this->assertFalse($res);
  109. }
  110. /**
  111. * @return void
  112. */
  113. public function testBasicUserMethodAllowed() {
  114. $this->request->params['controller'] = 'users';
  115. $this->request->params['action'] = 'edit';
  116. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  117. // single role_id field in users table
  118. $user = array(
  119. 'role_id' => 1,
  120. );
  121. $res = $object->authorize($user, $this->request);
  122. $this->assertTrue($res);
  123. $this->request->params['action'] = 'admin_index';
  124. $user = array(
  125. 'role_id' => 3,
  126. );
  127. $res = $object->authorize($user, $this->request);
  128. $this->assertTrue($res);
  129. }
  130. /**
  131. * @return void
  132. */
  133. public function testBasicUserMethodAllowedMultiRole() {
  134. $this->request->params['controller'] = 'users';
  135. $this->request->params['action'] = 'admin_index';
  136. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  137. // flat list of roles
  138. $user = array(
  139. 'Role' => array(1, 3),
  140. );
  141. $res = $object->authorize($user, $this->request);
  142. $this->assertTrue($res);
  143. // verbose role defition using the new 2.x contain param for Auth
  144. $user = array(
  145. 'Role' => array(array('id' => 1, 'RoleUser' => array()), array('id' => 3, 'RoleUser' => array())),
  146. );
  147. $res = $object->authorize($user, $this->request);
  148. $this->assertTrue($res);
  149. }
  150. /**
  151. * @return void
  152. */
  153. public function testBasicUserMethodAllowedWildcard() {
  154. $this->request->params['controller'] = 'tags';
  155. $this->request->params['action'] = 'public_action';
  156. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  157. $user = array(
  158. 'role_id' => 6,
  159. );
  160. $res = $object->authorize($user, $this->request);
  161. $this->assertTrue($res);
  162. }
  163. /**
  164. * @return void
  165. */
  166. public function testUserMethodsAllowed() {
  167. $this->request->params['controller'] = 'users';
  168. $this->request->params['action'] = 'some_action';
  169. $object = new TestTinyAuthorize($this->Collection, array('allowUser' => true, 'autoClearCache' => true));
  170. $user = array(
  171. 'role_id' => 1,
  172. );
  173. $res = $object->authorize($user, $this->request);
  174. $this->assertTrue($res);
  175. $this->request->params['controller'] = 'users';
  176. $this->request->params['action'] = 'admin_index';
  177. $object = new TestTinyAuthorize($this->Collection, array('allowUser' => true, 'autoClearCache' => true));
  178. $user = array(
  179. 'role_id' => 1,
  180. );
  181. $res = $object->authorize($user, $this->request);
  182. $this->assertFalse($res);
  183. $user = array(
  184. 'role_id' => 3,
  185. );
  186. $res = $object->authorize($user, $this->request);
  187. $this->assertTrue($res);
  188. }
  189. /**
  190. * @return void
  191. */
  192. public function testAdminMethodsAllowed() {
  193. $this->request->params['controller'] = 'users';
  194. $this->request->params['action'] = 'some_action';
  195. $config = array('allowAdmin' => true, 'adminRole' => 3, 'autoClearCache' => true);
  196. $object = new TestTinyAuthorize($this->Collection, $config);
  197. $user = array(
  198. 'role_id' => 1,
  199. );
  200. $res = $object->authorize($user, $this->request);
  201. $this->assertFalse($res);
  202. $this->request->params['controller'] = 'users';
  203. $this->request->params['action'] = 'admin_index';
  204. $object = new TestTinyAuthorize($this->Collection, $config);
  205. $user = array(
  206. 'role_id' => 1,
  207. );
  208. $res = $object->authorize($user, $this->request);
  209. $this->assertFalse($res);
  210. $user = array(
  211. 'role_id' => 3,
  212. );
  213. $res = $object->authorize($user, $this->request);
  214. $this->assertTrue($res);
  215. }
  216. /**
  217. * Should only be used in combination with Auth->allow() to mark those as public in the acl.ini, as well.
  218. * Not necessary and certainly not recommended as acl.ini only.
  219. *
  220. * @return void
  221. */
  222. public function testBasicUserMethodAllowedPublically() {
  223. $this->request->params['controller'] = 'tags';
  224. $this->request->params['action'] = 'add';
  225. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  226. $user = array(
  227. 'role_id' => 2,
  228. );
  229. $res = $object->authorize($user, $this->request);
  230. $this->assertTrue($res);
  231. $this->request->params['controller'] = 'comments';
  232. $this->request->params['action'] = 'foo';
  233. $user = array(
  234. 'role_id' => 3,
  235. );
  236. $res = $object->authorize($user, $this->request);
  237. $this->assertTrue($res);
  238. }
  239. }
  240. class TestTinyAuthorize extends TinyAuthorize {
  241. public function matchArray() {
  242. return $this->_matchArray;
  243. }
  244. public function getAcl() {
  245. return $this->_getAcl();
  246. }
  247. protected function _getAcl($path = TMP) {
  248. return parent::_getAcl($path);
  249. }
  250. }