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

/tests/integration/bb-modules/mod_servicelicense/ServerTest.php

https://bitbucket.org/RedSerenityDev/serenitybilling
PHP | 298 lines | 216 code | 32 blank | 50 comment | 1 complexity | 5b9fe93ecb35735e63c6f32bc8038eb4 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @group Core
  4. */
  5. class Box_Mod_Servicelicense_ServerTest extends BBDbApiTestCase
  6. {
  7. protected $_initialSeedFile = 'servicelicense.xml';
  8. public static function variations()
  9. {
  10. return array(
  11. // array(array(
  12. // 'fail' => '',
  13. // ), false),
  14. array(array(
  15. 'license' => 'BOX-NOT-EXISTS',
  16. 'host' => 'tests.com',
  17. 'path' => dirname(__FILE__),
  18. 'version' => '0.0.2',
  19. ), false, false),
  20. array(array(
  21. 'license' => 'no_validation',
  22. 'host' => 'tests.com',
  23. 'path' => dirname(__FILE__),
  24. 'version' => '0.0.2',
  25. ), false, false),
  26. array(array(
  27. 'license' => 'valid',
  28. 'host' => 'www.tests.com',
  29. 'path' => dirname(__FILE__),
  30. 'version' => '0.0.2',
  31. ), true, true),
  32. );
  33. }
  34. /**
  35. * @dataProvider variations
  36. */
  37. public function testLicenseServer($data, $valid, $validation)
  38. {
  39. $service = $this->getMockBuilder('Box\Mod\Servicelicense\Service')->getMock();
  40. $service->expects($this->any())
  41. ->method('isLicenseActive')
  42. ->will($this->returnValue(true));
  43. if ($validation) {
  44. $service->expects($this->atLeastOnce())
  45. ->method('isValidIp')
  46. ->will($this->returnValue(true));
  47. $service->expects($this->atLeastOnce())
  48. ->method('isValidHost')
  49. ->will($this->returnValue(true));
  50. $service->expects($this->atLeastOnce())
  51. ->method('isValidVersion')
  52. ->will($this->returnValue(true));
  53. $service->expects($this->atLeastOnce())
  54. ->method('isValidPath')
  55. ->will($this->returnValue(true));
  56. }
  57. $di = new Box_Di();
  58. $di['db'] = $this->di['db'];
  59. $di['logger'] = new Box_Log();
  60. $di['mod'] = $di->protect(function () use ($service) {
  61. return new Box_Mod('servicelicense');
  62. });
  63. $di['mod_service'] = $di->protect(function () use ($service) {
  64. return $service;
  65. });
  66. $server = new \Box\Mod\Servicelicense\Server($di['logger']);
  67. $server->setDi($di);
  68. $result = $server->handle_deprecated(json_encode($data));
  69. $this->assertEquals($valid, $result['valid'], print_r($result, 1));
  70. }
  71. public static function testLicenseServerProcessProvicer()
  72. {
  73. return array(
  74. array(array(
  75. 'license' => 'validation_fail',
  76. 'host' => 'tests.com',
  77. 'path' => dirname(__FILE__),
  78. 'version' => '0.0.2',
  79. ), false),
  80. array(array(
  81. 'license' => 'valid',
  82. 'host' => 'www.tests.com',
  83. 'path' => dirname(__FILE__),
  84. 'version' => '0.0.2',
  85. ), true),
  86. );
  87. }
  88. public function testLicenseServerProcess()
  89. {
  90. $data = array(
  91. 'license' => 'valid',
  92. 'host' => 'tests.com',
  93. 'path' => dirname(__FILE__),
  94. 'version' => '0.0.2',
  95. );
  96. $valid = true;
  97. $service = $this->getMockBuilder('Box\Mod\Servicelicense\Service')->getMock();
  98. $service->expects($this->any())
  99. ->method('isLicenseActive')
  100. ->will($this->returnValue(true));
  101. $service->expects($this->atLeastOnce())
  102. ->method('isValidIp')
  103. ->will($this->returnValue(true));
  104. $service->expects($this->atLeastOnce())
  105. ->method('isValidHost')
  106. ->will($this->returnValue(true));
  107. $service->expects($this->atLeastOnce())
  108. ->method('isValidVersion')
  109. ->will($this->returnValue(true));
  110. $service->expects($this->atLeastOnce())
  111. ->method('isValidPath')
  112. ->will($this->returnValue(true));
  113. $di = new Box_Di();
  114. $di['db'] = $this->di['db'];
  115. $di['logger'] = $this->di['logger'];
  116. $di['mod'] = $di->protect(function () use ($service) {
  117. return new Box_Mod('servicelicense');
  118. });
  119. $di['mod_service'] = $di->protect(function () use ($service) {
  120. return $service;
  121. });
  122. $server = new \Box\Mod\Servicelicense\Server($this->di['logger']);
  123. $server->setDi($di);
  124. $result = $server->process(json_encode($data));
  125. $this->assertEquals($valid, $result['valid'], print_r($result, 1));
  126. }
  127. /**
  128. * @expectedException LogicException
  129. */
  130. public function testLicenseServerProcessNotFound()
  131. {
  132. $data = array(
  133. 'license' => 'non_existing',
  134. 'host' => 'tests.com',
  135. 'path' => dirname(__FILE__),
  136. 'version' => '0.0.2',
  137. );
  138. $valid = true;
  139. $service = $this->getMockBuilder('Box\Mod\Servicelicense\Service')->getMock();
  140. $service->expects($this->never())
  141. ->method('isLicenseActive')
  142. ->will($this->returnValue(true));
  143. $service->expects($this->never())
  144. ->method('isValidIp')
  145. ->will($this->returnValue(true));
  146. $service->expects($this->never())
  147. ->method('isValidHost')
  148. ->will($this->returnValue(true));
  149. $service->expects($this->never())
  150. ->method('isValidVersion')
  151. ->will($this->returnValue(true));
  152. $service->expects($this->never())
  153. ->method('isValidPath')
  154. ->will($this->returnValue(true));
  155. $di = new Box_Di();
  156. $di['db'] = $this->di['db'];
  157. $di['logger'] = $this->di['logger'];
  158. $di['mod'] = $di->protect(function () use ($service) {
  159. return new Box_Mod('servicelicense');
  160. });
  161. $di['mod_service'] = $di->protect(function () use ($service) {
  162. return $service;
  163. });
  164. $server = new \Box\Mod\Servicelicense\Server($this->di['logger']);
  165. $server->setDi($di);
  166. $result = $server->process(json_encode($data));
  167. $this->assertEquals($valid, $result['valid'], print_r($result, 1));
  168. }
  169. public function testLicenseServerProcessValidationFailProvider()
  170. {
  171. return array(
  172. array(false, false, false, false, false, array(
  173. $this->atLeastOnce(), $this->never(), $this->never(), $this->never(), $this->never()
  174. )),
  175. array(true, false, false, false, false, array(
  176. $this->atLeastOnce(), $this->atLeastOnce(), $this->never(), $this->never(), $this->never()
  177. )),
  178. array(true, true, false, false, false, array(
  179. $this->atLeastOnce(), $this->atLeastOnce(), $this->atLeastOnce(), $this->never(), $this->never()
  180. )),
  181. array(true, true, true, false, false, array(
  182. $this->atLeastOnce(), $this->atLeastOnce(), $this->atLeastOnce(), $this->atLeastOnce(), $this->never()
  183. )),
  184. array(true, true, true, true, false, array(
  185. $this->atLeastOnce(), $this->atLeastOnce(), $this->atLeastOnce(), $this->atLeastOnce(), $this->atLeastOnce()
  186. )),
  187. );
  188. }
  189. /**
  190. * @expectedException LogicException
  191. * @dataProvider testLicenseServerProcessValidationFailProvider
  192. */
  193. public function testLicenseServerProcessValidationFail($isActive, $validIp, $validHost, $validVersion, $validPath, $called)
  194. {
  195. $data = array(
  196. 'license' => 'valid',
  197. 'host' => 'tests.com',
  198. 'path' => dirname(__FILE__),
  199. 'version' => '0.0.2',
  200. );
  201. $valid = true;
  202. $service = $this->getMockBuilder('Box\Mod\Servicelicense\Service')->getMock();
  203. $service->expects($called[0])
  204. ->method('isLicenseActive')
  205. ->will($this->returnValue($isActive));
  206. $service->expects($called[1])
  207. ->method('isValidIp')
  208. ->will($this->returnValue($validIp));
  209. $service->expects($called[2])
  210. ->method('isValidHost')
  211. ->will($this->returnValue($validHost));
  212. $service->expects($called[3])
  213. ->method('isValidVersion')
  214. ->will($this->returnValue($validVersion));
  215. $service->expects($called[4])
  216. ->method('isValidPath')
  217. ->will($this->returnValue($validPath));
  218. $di = new Box_Di();
  219. $di['db'] = $this->di['db'];
  220. $di['logger'] = $this->di['logger'];
  221. $di['mod'] = $di->protect(function () use ($service) {
  222. return new Box_Mod('servicelicense');
  223. });
  224. $di['mod_service'] = $di->protect(function () use ($service) {
  225. return $service;
  226. });
  227. $server = new \Box\Mod\Servicelicense\Server($this->di['logger']);
  228. $server->setDi($di);
  229. $result = $server->process(json_encode($data));
  230. $this->assertEquals($valid, $result['valid'], print_r($result, 1));
  231. }
  232. /*
  233. public function testLicense()
  234. {
  235. return ;
  236. $data = array();
  237. $data['license'] = 'BOX-NOT-EXISTS';
  238. $data['host'] = 'tests.com';
  239. $data['path'] = dirname(__FILE__);
  240. $data['version'] = '0.0.2';
  241. $k = base64_encode(json_encode($data));
  242. $url = 'http://www.box.local/licenses';
  243. $params = 'key='.$k;
  244. $process = curl_init($url);
  245. $params = trim( preg_replace( '/\s+/', '', $params ) );
  246. curl_setopt($process, CURLOPT_HEADER, 0);
  247. curl_setopt($process, CURLOPT_TIMEOUT, 30);
  248. curl_setopt($process, CURLOPT_POSTFIELDS, $params);
  249. curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
  250. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off'))
  251. curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
  252. curl_setopt($process, CURLOPT_POST, 1);
  253. $response = curl_exec($process);
  254. curl_close($process);
  255. $json= json_decode($response);
  256. $this->assertTrue(is_object($json));
  257. $this->assertEquals('License key not found', $json->error);
  258. }
  259. */
  260. }