PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/MailRoute/API/Tests/TestAccess.php

https://github.com/MailRoute/mailroute_php
PHP | 490 lines | 408 code | 9 blank | 73 comment | 1 complexity | 1ef79fe5c24e18548c7724b24f920eaf MD5 | raw file
  1. <?php
  2. namespace MailRoute\API\Tests;
  3. use Jamm\Tester\ClassTest;
  4. use MailRoute\API\AccessDeniedException;
  5. use MailRoute\API\ActiveEntity;
  6. use MailRoute\API\Config;
  7. use MailRoute\API\IClient;
  8. use MailRoute\API\NotFoundException;
  9. class TestAccess extends ClassTest
  10. {
  11. /** @var Config */
  12. private $RootConfig;
  13. /** @var IClient */
  14. private $Client;
  15. /** @var Config */
  16. private $InitialConfig;
  17. public function __construct(Config $Config)
  18. {
  19. $this->InitialConfig = $Config;
  20. //$this->skipAllExcept('testResellerAdmin');
  21. }
  22. public function setUp()
  23. {
  24. $this->RootConfig = clone $this->InitialConfig;
  25. $this->Client = new ClientMock($this->RootConfig);
  26. }
  27. public function testResellerAdmin()
  28. {
  29. $reseller_name = 'reseller'.md5(microtime(1).mt_rand(1, 9999));
  30. $Reseller = $this->Client->API()->Reseller()->create(array('name' => $reseller_name));
  31. $ForeignReseller = $this->Client->API()->Reseller()->create(array('name' => 'f'.$reseller_name));
  32. $Admin = $Reseller->createAdmin('admin'.$reseller_name.'@example.com', 0, 'admin'.$reseller_name);
  33. $Customer = $Reseller->createCustomer('customer'.$reseller_name);
  34. $ForeignCustomer = $ForeignReseller->createCustomer('foreign_customer'.$reseller_name);
  35. $Domain = $Customer->createDomain('domain'.md5($reseller_name).'.example.com');
  36. $ForeignDomain = $ForeignCustomer->createDomain('domain'.md5($ForeignCustomer->getName()).'.example.com');
  37. $EmailAccount = $Domain->createEmailAccount('email'.md5($reseller_name));
  38. $ForeignEmailAccount = $ForeignDomain->createEmailAccount('email'.md5($ForeignReseller->getName()));
  39. $RootClient = $this->Client;
  40. $this->Client = $this->getClientForUser($Admin->getEmail(), $Admin->regenerateApiKey());
  41. $this->setClientToActiveEntities($this->Client, array($Reseller, $ForeignReseller, $Admin, $Customer, $ForeignCustomer, $Domain, $ForeignDomain, $EmailAccount, $ForeignEmailAccount));
  42. $this->assertEquals($this->Client, $Admin->getAPIClient());
  43. $this->assertEquals($this->Client, $Reseller->getAPIClient());
  44. // allowed
  45. try
  46. {
  47. $Reseller->setName($reseller_name.'change');
  48. $this->assertTrue($Reseller->save());
  49. // $Admin->setUsername($Admin->getUsername().'change');
  50. // $this->assertTrue($Admin->save());
  51. $Customer->setAllowBranding(!$Customer->getAllowBranding());
  52. $this->assertTrue($Customer->save());
  53. $Domain->setOutboundEnabled(!$Domain->getOutboundEnabled());
  54. $this->assertTrue($Domain->save());
  55. $EmailAccount->setPriority(15);
  56. $this->assertTrue($EmailAccount->save());
  57. }
  58. catch (AccessDeniedException $Exception)
  59. {
  60. $this->assertTrue(false)->addCommentary("Access denied: ".$Exception->getMessage());
  61. }
  62. // not allowed
  63. // reseller
  64. try
  65. {
  66. $ForeignReseller->setName($ForeignReseller->getName().'change');
  67. $result = $ForeignReseller->save();
  68. $this->assertTrue(!$result)->addCommentary("Can change foreign reseller name");
  69. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  70. }
  71. catch (AccessDeniedException $Exception)
  72. {
  73. $this->assertTrue(true);
  74. }
  75. catch (NotFoundException $E)
  76. {
  77. $this->assertTrue(true)->addCommentary('But not as true as should be');
  78. }
  79. // customer
  80. try
  81. {
  82. $ForeignCustomer->setAllowBranding(!$ForeignCustomer->getAllowBranding());
  83. $result = $ForeignCustomer->save();
  84. $this->assertTrue(!$result)->addCommentary("Can change foreign customer data");
  85. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  86. }
  87. catch (AccessDeniedException $Exception)
  88. {
  89. $this->assertTrue(true);
  90. }
  91. catch (NotFoundException $E)
  92. {
  93. $this->assertTrue(true)->addCommentary('But not as true as should be');
  94. }
  95. // Domain
  96. try
  97. {
  98. $ForeignDomain->setOutboundEnabled(!$ForeignDomain->getOutboundEnabled());
  99. $result = $ForeignDomain->save();
  100. $this->assertTrue(!$result)->addCommentary("Can change foreign domain data");
  101. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  102. }
  103. catch (AccessDeniedException $Exception)
  104. {
  105. $this->assertTrue(true);
  106. }
  107. catch (NotFoundException $E)
  108. {
  109. $this->assertTrue(true)->addCommentary('But not as true as should be');
  110. }
  111. // EmailAccount
  112. try
  113. {
  114. $ForeignEmailAccount->setPriority(20);
  115. $result = $ForeignEmailAccount->save();
  116. $this->assertTrue(!$result)->addCommentary("Can change foreign email account data");
  117. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  118. }
  119. catch (AccessDeniedException $Exception)
  120. {
  121. $this->assertTrue(true);
  122. }
  123. catch (NotFoundException $E)
  124. {
  125. $this->assertTrue(true)->addCommentary('But not as true as should be');
  126. }
  127. $this->setClientToActiveEntities($RootClient, array($Reseller, $ForeignReseller, $Admin, $Customer, $ForeignCustomer, $Domain, $ForeignDomain, $EmailAccount, $ForeignEmailAccount));
  128. $EmailAccount->delete();
  129. $Domain->delete();
  130. $Customer->delete();
  131. $Admin->delete();
  132. $Reseller->delete();
  133. $ForeignEmailAccount->delete();
  134. $ForeignDomain->delete();
  135. $ForeignCustomer->delete();
  136. $ForeignReseller->delete();
  137. }
  138. public function testCustomerAdmin()
  139. {
  140. $reseller_name = 'reseller'.md5(microtime(1).mt_rand(1, 9999));
  141. $Reseller = $this->Client->API()->Reseller()->create(array('name' => $reseller_name));
  142. $ForeignReseller = $this->Client->API()->Reseller()->create(array('name' => 'f'.$reseller_name));
  143. $Customer = $Reseller->createCustomer('customer'.md5($reseller_name));
  144. $ForeignCustomer = $ForeignReseller->createCustomer('customer'.md5($ForeignReseller->getName()));
  145. $Admin = $Customer->createAdmin('admin'.$Customer->getName().'@example.com', 0, 'admin'.$Customer->getName());
  146. $ForeignAdmin = $ForeignCustomer->createAdmin('admin'.$ForeignCustomer->getName().'@example.com', 0, 'admin'.$ForeignCustomer->getName());
  147. $Domain = $Customer->createDomain('domain'.md5($Customer->getName()).'.example.com');
  148. $ForeignDomain = $ForeignCustomer->createDomain('domain'.md5($ForeignCustomer->getName()).'.example.com');
  149. $EmailAccount = $Domain->createEmailAccount('email'.md5($Customer->getName()));
  150. $ForeignEmailAccount = $ForeignDomain->createEmailAccount('email'.md5($ForeignCustomer->getName()));
  151. $RootClient = $this->Client;
  152. $this->Client = $this->getClientForUser($Admin->getEmail(), $Admin->regenerateApiKey());
  153. $this->setClientToActiveEntities($this->Client, array($Reseller, $ForeignReseller, $Admin, $Customer, $ForeignCustomer, $Domain, $ForeignDomain, $EmailAccount, $ForeignEmailAccount));
  154. // allowed actions
  155. try
  156. {
  157. $EmailAccount->setPriority(10);
  158. $this->assertTrue($EmailAccount->save());
  159. $Domain->setOutboundEnabled(!$Domain->getOutboundEnabled());
  160. $this->assertTrue($Domain->save());
  161. // $Admin->setIsActive(!$Admin->getIsActive());
  162. // $this->assertTrue($Admin->save());
  163. $Customer->setAllowBranding(!$Customer->getAllowBranding());
  164. $this->assertTrue($Customer->save());
  165. }
  166. catch (AccessDeniedException $E)
  167. {
  168. $this->assertTrue(false)->addCommentary('Access error: '.$E->getMessage());
  169. }
  170. catch (NotFoundException $E)
  171. {
  172. $this->assertTrue(false)->addCommentary($E->getMessage());
  173. }
  174. // not allowed actions
  175. // foreign reseller
  176. try
  177. {
  178. $ForeignReseller->setAllowCustomerBranding(!$ForeignReseller->getAllowCustomerBranding());
  179. $result = $ForeignReseller->save();
  180. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change foreign reseller data");
  181. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  182. }
  183. catch (AccessDeniedException $E)
  184. {
  185. $this->assertTrue(true);
  186. }
  187. catch (NotFoundException $E)
  188. {
  189. $this->assertTrue(true)->addCommentary('But not as true as should be');
  190. }
  191. // reseller
  192. try
  193. {
  194. $Reseller->setAllowCustomerBranding(!$Reseller->getAllowCustomerBranding());
  195. $result = $Reseller->save();
  196. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change reseller data");
  197. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  198. }
  199. catch (AccessDeniedException $E)
  200. {
  201. $this->assertTrue(true);
  202. }
  203. catch (NotFoundException $E)
  204. {
  205. $this->assertTrue(true)->addCommentary('But not as true as should be');
  206. }
  207. // foreign customer
  208. try
  209. {
  210. $ForeignCustomer->setAllowBranding(!$ForeignCustomer->getAllowBranding());
  211. $result = $ForeignCustomer->save();
  212. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change foreign customer data");
  213. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  214. }
  215. catch (AccessDeniedException $E)
  216. {
  217. $this->assertTrue(true);
  218. }
  219. catch (NotFoundException $E)
  220. {
  221. $this->assertTrue(true)->addCommentary('But not as true as should be');
  222. }
  223. // // foreign admin
  224. // try
  225. // {
  226. // $ForeignAdmin->setSendWelcome(!$ForeignAdmin->getSendWelcome());
  227. // $result = $ForeignAdmin->save();
  228. // $this->assertTrue(!$result)->addCommentary("shouldn't be able to change foreign admin data");
  229. // $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  230. // }
  231. // catch (AccessDeniedException $E)
  232. // {
  233. // $this->assertTrue(true);
  234. // }
  235. // foreign domain
  236. try
  237. {
  238. $ForeignDomain->setOutboundEnabled(!$ForeignDomain->getOutboundEnabled());
  239. $result = $ForeignDomain->save();
  240. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change foreign domain data");
  241. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  242. }
  243. catch (AccessDeniedException $E)
  244. {
  245. $this->assertTrue(true);
  246. }
  247. catch (NotFoundException $E)
  248. {
  249. $this->assertTrue(true)->addCommentary('But not as true as should be');
  250. }
  251. // foreign email account
  252. try
  253. {
  254. $ForeignEmailAccount->setPriority(50);
  255. $result = $ForeignEmailAccount->save();
  256. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change foreign email account data");
  257. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  258. }
  259. catch (AccessDeniedException $E)
  260. {
  261. $this->assertTrue(true);
  262. }
  263. catch (NotFoundException $E)
  264. {
  265. $this->assertTrue(true)->addCommentary('But not as true as should be');
  266. }
  267. $this->setClientToActiveEntities($RootClient, array($Reseller, $ForeignReseller, $Admin, $Customer, $ForeignCustomer, $Domain, $ForeignDomain, $EmailAccount, $ForeignEmailAccount));
  268. $EmailAccount->delete();
  269. $Domain->delete();
  270. $Admin->delete();
  271. $Customer->delete();
  272. $Reseller->delete();
  273. $ForeignEmailAccount->delete();
  274. $ForeignDomain->delete();
  275. $ForeignAdmin->delete();
  276. $ForeignCustomer->delete();
  277. $ForeignReseller->delete();
  278. }
  279. public function testEmailAccount()
  280. {
  281. $reseller_name = 'reseller'.md5(microtime(1).mt_rand(1, 9999));
  282. $Reseller = $this->Client->API()->Reseller()->create(array('name' => $reseller_name));
  283. $ForeignReseller = $this->Client->API()->Reseller()->create(array('name' => 'f'.$reseller_name));
  284. $Customer = $Reseller->createCustomer('customer'.md5($reseller_name));
  285. $ForeignCustomer = $ForeignReseller->createCustomer('customer'.md5($ForeignReseller->getName()));
  286. $Admin = $Customer->createAdmin('admin'.$Customer->getName().'@example.com', 0, 'admin'.$Customer->getName());
  287. $ForeignAdmin = $ForeignCustomer->createAdmin('admin'.$ForeignCustomer->getName().'@example.com', 0, 'admin'.$ForeignCustomer->getName());
  288. $Domain = $Customer->createDomain('domain'.md5($Customer->getName()).'.example.com');
  289. $ForeignDomain = $ForeignCustomer->createDomain('domain'.md5($ForeignCustomer->getName()).'.example.com');
  290. $EmailAccount = $Domain->createEmailAccount('email'.md5($Customer->getName()));
  291. $ForeignEmailAccount = $ForeignDomain->createEmailAccount('email'.md5($ForeignCustomer->getName()));
  292. $RootClient = $this->Client;
  293. $this->Client = $this->getClientForUser($EmailAccount->getLocalpart().'@'.$Domain->getName(), $EmailAccount->regenerateApiKey());
  294. $this->setClientToActiveEntities($this->Client, array($Reseller, $ForeignReseller, $Admin, $Customer, $ForeignCustomer, $Domain, $ForeignDomain, $EmailAccount, $ForeignEmailAccount));
  295. // allowed actions
  296. try
  297. {
  298. $EmailAccount->setPriority(10);
  299. $this->assertTrue($EmailAccount->save());
  300. }
  301. catch (AccessDeniedException $E)
  302. {
  303. $this->assertTrue(false)->addCommentary('Access error: '.$E->getMessage());
  304. }
  305. // not allowed actions
  306. // Customer
  307. try
  308. {
  309. $Customer->setAllowBranding(!$Customer->getAllowBranding());
  310. $result = $Customer->save();
  311. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change customer data");
  312. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  313. }
  314. catch (AccessDeniedException $E)
  315. {
  316. $this->assertTrue(true);
  317. }
  318. catch (NotFoundException $E)
  319. {
  320. $this->assertTrue(true)->addCommentary('But not as true as should be');
  321. }
  322. // // Admin
  323. // try
  324. // {
  325. // $Admin->setIsActive(!$Admin->getIsActive());
  326. // $result = $Admin->save();
  327. // $this->assertTrue(!$result)->addCommentary("shouldn't be able to change admin data");
  328. // $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  329. // }
  330. // catch (AccessDeniedException $E)
  331. // {
  332. // $this->assertTrue(true);
  333. // }
  334. // domain
  335. try
  336. {
  337. $Domain->setOutboundEnabled(!$Domain->getOutboundEnabled());
  338. $result = $Domain->save();
  339. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change domain data");
  340. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  341. }
  342. catch (AccessDeniedException $E)
  343. {
  344. $this->assertTrue(true);
  345. }
  346. catch (NotFoundException $E)
  347. {
  348. $this->assertTrue(true)->addCommentary('But not as true as should be');
  349. }
  350. // foreign reseller
  351. try
  352. {
  353. $ForeignReseller->setAllowCustomerBranding(!$ForeignReseller->getAllowCustomerBranding());
  354. $result = $ForeignReseller->save();
  355. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change foreign reseller data");
  356. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  357. }
  358. catch (AccessDeniedException $E)
  359. {
  360. $this->assertTrue(true);
  361. }
  362. catch (NotFoundException $E)
  363. {
  364. $this->assertTrue(true)->addCommentary('But not as true as should be');
  365. }
  366. // reseller
  367. try
  368. {
  369. $Reseller->setAllowCustomerBranding(!$Reseller->getAllowCustomerBranding());
  370. $result = $Reseller->save();
  371. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change reseller data");
  372. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  373. }
  374. catch (AccessDeniedException $E)
  375. {
  376. $this->assertTrue(true);
  377. }
  378. catch (NotFoundException $E)
  379. {
  380. $this->assertTrue(true)->addCommentary('But not as true as should be');
  381. }
  382. // foreign customer
  383. try
  384. {
  385. $ForeignCustomer->setAllowBranding(!$ForeignCustomer->getAllowBranding());
  386. $result = $ForeignCustomer->save();
  387. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change foreign customer data");
  388. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  389. }
  390. catch (AccessDeniedException $E)
  391. {
  392. $this->assertTrue(true);
  393. }
  394. catch (NotFoundException $E)
  395. {
  396. $this->assertTrue(true)->addCommentary('But not as true as should be');
  397. }
  398. // // foreign admin
  399. // try
  400. // {
  401. // $ForeignAdmin->setSendWelcome(!$ForeignAdmin->getSendWelcome());
  402. // $result = $ForeignAdmin->save();
  403. // $this->assertTrue(!$result)->addCommentary("shouldn't be able to change foreign admin data");
  404. // $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  405. // }
  406. // catch (AccessDeniedException $E)
  407. // {
  408. // $this->assertTrue(true);
  409. // }
  410. // foreign domain
  411. try
  412. {
  413. $ForeignDomain->setOutboundEnabled(!$ForeignDomain->getOutboundEnabled());
  414. $result = $ForeignDomain->save();
  415. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change foreign domain data");
  416. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  417. }
  418. catch (AccessDeniedException $E)
  419. {
  420. $this->assertTrue(true);
  421. }
  422. catch (NotFoundException $E)
  423. {
  424. $this->assertTrue(true)->addCommentary('But not as true as should be');
  425. }
  426. // foreign email account
  427. try
  428. {
  429. $ForeignEmailAccount->setPriority(50);
  430. $result = $ForeignEmailAccount->save();
  431. $this->assertTrue(!$result)->addCommentary("shouldn't be able to change foreign email account data");
  432. $this->assertTrue(false)->addCommentary("403 exception wasn't thrown!");
  433. }
  434. catch (AccessDeniedException $E)
  435. {
  436. $this->assertTrue(true);
  437. }
  438. catch (NotFoundException $E)
  439. {
  440. $this->assertTrue(true)->addCommentary('But not as true as should be');
  441. }
  442. $this->setClientToActiveEntities($RootClient, array($Reseller, $ForeignReseller, $Admin, $Customer, $ForeignCustomer, $Domain, $ForeignDomain, $EmailAccount, $ForeignEmailAccount));
  443. $EmailAccount->delete();
  444. $Domain->delete();
  445. $Admin->delete();
  446. $Customer->delete();
  447. $Reseller->delete();
  448. $ForeignEmailAccount->delete();
  449. $ForeignDomain->delete();
  450. $ForeignAdmin->delete();
  451. $ForeignCustomer->delete();
  452. $ForeignReseller->delete();
  453. }
  454. /**
  455. * @param $user
  456. * @param $password
  457. * @return ClientMock
  458. */
  459. protected function getClientForUser($user, $password)
  460. {
  461. $Config = clone $this->RootConfig;
  462. $Config->login = $user;
  463. $Config->password = $password;
  464. $Client = new ClientMock($Config);
  465. return $Client;
  466. }
  467. /**
  468. * @param IClient $Client
  469. * @param ActiveEntity[] $ActiveEntities
  470. */
  471. protected function setClientToActiveEntities(IClient $Client, $ActiveEntities)
  472. {
  473. if (!empty($ActiveEntities))
  474. {
  475. foreach ($ActiveEntities as $ActiveEntity)
  476. {
  477. $ActiveEntity->setAPIClient($Client);
  478. }
  479. }
  480. }
  481. }