PageRenderTime 35ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Cacic/WSBundle/Tests/Controller/NeoInstallControllerTest.php

https://gitlab.com/adrianovieira/cacic
PHP | 421 lines | 306 code | 67 blank | 48 comment | 0 complexity | 2bb0ed3850c2fce3673f3eee80d297cb MD5 | raw file
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: eduardo
  5. * Date: 20/04/15
  6. * Time: 12:47
  7. */
  8. namespace Cacic\WSBundle\Tests\Controller;
  9. use Cacic\WSBundle\Tests\BaseTestCase;
  10. use Cacic\CommonBundle\Entity\RedeVersaoModulo;
  11. use Cacic\CommonBundle\Entity\Rede;
  12. class NeoInstallControllerTest extends BaseTestCase {
  13. public function setUp() {
  14. // Carrega dados da classe de cima
  15. parent::setUp();
  16. $this->em =$this->container->get('doctrine')->getManager();
  17. $this->hash = file_get_contents($this->data_dir."instala/hash.json");
  18. $this->erro = file_get_contents($this->data_dir."instala/erro.json");
  19. $rede = new Rede();
  20. $rede->setTeIpRede('10.209.8.0');
  21. $rede->setTeMascaraRede('255.255.255.255');
  22. $rede->setTeServCacic('http://localhost');
  23. $rede->setTeServUpdates('http://localhost');
  24. $rede->setTePathServUpdates('/msi');
  25. $rede->setNuLimiteFtp(100);
  26. $rede->setCsPermitirDesativarSrcacic('S');
  27. $rede->setDownloadMethod('http');
  28. //$this->em->persist($rede);
  29. $elemento = array(
  30. 'nmModulo' => 'cacic-service.exe',
  31. 'teVersaoModulo' => '3.0a1',
  32. 'csTipoSo' => 'windows',
  33. 'teHash' => '79df3561f83ac86eb19e2996b17d5e31',
  34. 'tipo' => 'cacic',
  35. 'tipoSo' => 'windows',
  36. 'filepath' => 'cacic/current/windows/cacic-service.exe'
  37. );
  38. $tipo_so = $this->em->getRepository("CacicCommonBundle:TipoSo")->findOneBy(array(
  39. 'tipo' => 'windows'
  40. ));
  41. $classe = new RedeVersaoModulo(null, null, null, null, null, $rede);
  42. $classe->setNmModulo($elemento['nmModulo']);
  43. $classe->setTeVersaoModulo($elemento['teVersaoModulo']);
  44. $classe->setDtAtualizacao(new \DateTime());
  45. $classe->setCsTipoSo($elemento['csTipoSo']);
  46. $classe->setTeHash($elemento['teHash']);
  47. $classe->setTipo('cacic');
  48. $classe->setTipoSo($tipo_so);
  49. $classe->setFilepath($elemento['filepath']);
  50. //$this->em->persist($classe);
  51. //$this->em->flush();
  52. // Carrega parâmetros globais
  53. $this->rede = $rede;
  54. $this->modulo = $classe;
  55. $this->tipo_so = $tipo_so;
  56. // Encontra hash do último serviço
  57. $rede = $this->em->getRepository("CacicCommonBundle:Rede")->findOneBy(array(
  58. 'teIpRede' => '0.0.0.0'
  59. ));
  60. $cacic_service = $this->em->getRepository("CacicCommonBundle:RedeVersaoModulo")->getModulo(
  61. $rede->getIdRede(),
  62. 'cacic-service.exe',
  63. 'windows'
  64. );
  65. $this->hash_service = $cacic_service[0]['teHash'];
  66. }
  67. /**
  68. * Testa verificação de hash do Serviço no Gerente
  69. */
  70. public function testInstallHash() {
  71. $logger = $this->container->get('logger');
  72. $client = $this->client;
  73. $client->request(
  74. 'POST',
  75. '/ws/instala/hash',
  76. array(),
  77. array(),
  78. array(
  79. 'CONTENT_TYPE' => 'application/json',
  80. 'HTTPS' => true
  81. ),
  82. $this->hash
  83. );
  84. $logger->debug("Dados JSON do computador enviados \n".$this->client->getRequest()->getcontent());
  85. $response = $this->client->getResponse();
  86. $status = $response->getStatusCode();
  87. $logger->debug("Response status: $status");
  88. $this->assertEquals(200, $status);
  89. // Testa se o erro foi inserido com sucesso para o computador
  90. //$em =$this->container->get('doctrine')->getManager();
  91. // Encontra hash do último serviço
  92. $rede = $this->em->getRepository("CacicCommonBundle:Rede")->findOneBy(array(
  93. 'teIpRede' => '0.0.0.0'
  94. ));
  95. $cacic_service = $this->em->getRepository("CacicCommonBundle:RedeVersaoModulo")->getModulo(
  96. $rede->getIdRede(),
  97. 'cacic-service.exe',
  98. 'windows'
  99. );
  100. // Verifica se o sistema informou JSON inválido
  101. $dados = json_decode($response->getContent(), true);
  102. $this->assertEquals($cacic_service[0]['teHash'], $dados['valor']);
  103. // Agora testa o módulo com outro hash e verifica se identificou a rede
  104. $this->em->persist($this->rede);
  105. $this->em->persist($this->modulo);
  106. $this->em->flush();
  107. $computador = json_decode($this->hash, true);
  108. $rede = $this->em->getRepository("CacicCommonBundle:Rede")->getDadosRedePreColeta(
  109. $computador['ip_address'],
  110. $computador['netmask']
  111. );
  112. $this->assertEquals($this->rede->getTeIpRede(), $rede->getTeIpRede());
  113. // Agora verifica se o módulo retornado foi o correto
  114. $cacic_service = $this->em->getRepository("CacicCommonBundle:RedeVersaoModulo")->getModulo(
  115. $this->rede->getIdRede(),
  116. 'cacic-service.exe',
  117. 'windows'
  118. );
  119. // Envia requisição novamente e testa resposta novamente
  120. $client->request(
  121. 'POST',
  122. '/ws/instala/hash',
  123. array(),
  124. array(),
  125. array(
  126. 'CONTENT_TYPE' => 'application/json',
  127. 'HTTPS' => true
  128. ),
  129. $this->hash
  130. );
  131. $logger->debug("Dados JSON do computador enviados \n".$this->client->getRequest()->getcontent());
  132. $response = $this->client->getResponse();
  133. $status = $response->getStatusCode();
  134. $logger->debug("Response status: $status");
  135. $this->assertEquals(200, $status);
  136. $dados = json_decode($response->getContent(), true);
  137. $this->assertEquals($cacic_service[0]['teHash'], $dados['valor']);
  138. // Remove dados do teste
  139. $this->em->remove($this->rede);
  140. $this->em->remove($this->modulo);
  141. $this->em->flush();
  142. }
  143. /**
  144. * Testa armazenamento de erro de instalação
  145. */
  146. public function testInstallErro() {
  147. $logger = $this->container->get('logger');
  148. $client = $this->client;
  149. $client->request(
  150. 'POST',
  151. '/ws/instala/erro',
  152. array(),
  153. array(),
  154. array(
  155. 'CONTENT_TYPE' => 'application/json',
  156. 'HTTPS' => true
  157. ),
  158. $this->erro
  159. );
  160. $logger->debug("Dados JSON do computador enviados \n".$this->client->getRequest()->getcontent());
  161. $response = $this->client->getResponse();
  162. $status = $response->getStatusCode();
  163. $logger->debug("Response status: $status");
  164. $this->assertEquals(200, $status);
  165. // Verifica se o dado foi inserido com sucesso
  166. $request = $client->getRequest();
  167. $ip_computador = $request->getClientIp();
  168. $computador = $this->em->getRepository("CacicCommonBundle:InsucessoInstalacao")->findOneBy(array(
  169. 'teIpComputador' => $ip_computador
  170. ));
  171. $this->assertNotEmpty($computador);
  172. }
  173. /**
  174. * Testa download da última versão do serviço para a subrede
  175. */
  176. public function testDownloadService() {
  177. $logger = $this->container->get('logger');
  178. $client = $this->client;
  179. $client->request(
  180. 'POST',
  181. '/ws/instala/download/service/'.$this->hash_service,
  182. array(),
  183. array(),
  184. array(
  185. 'CONTENT_TYPE' => 'application/json',
  186. 'HTTPS' => true
  187. ),
  188. '{}'
  189. );
  190. $logger->debug("Dados JSON do computador enviados \n".$this->client->getRequest()->getcontent());
  191. $response = $this->client->getResponse();
  192. $status = $response->getStatusCode();
  193. $logger->debug("Response status: $status");
  194. $this->assertEquals(200, $status);
  195. // Verifica que foi fornecida uma URL para o serviço
  196. $dados = json_decode($response->getContent(), true);
  197. // Pega servidor de updates para a rede
  198. $rede = $this->em->getRepository("CacicCommonBundle:Rede")->findOneBy(array(
  199. 'teIpRede' => '0.0.0.0'
  200. ));
  201. $modulo = $this->em->getRepository("CacicCommonBundle:RedeVersaoModulo")->findOneBy(array(
  202. 'nmModulo' => 'cacic-service.exe',
  203. 'teHash' => $this->hash_service,
  204. 'idRede' => $rede->getIdRede(),
  205. 'tipo' => 'cacic'
  206. ));
  207. $url = $rede->getTeServUpdates()
  208. . "/downloads/"
  209. . $modulo->getFilepath();
  210. $this->assertEquals($url, $dados['valor']);
  211. // Testa envio do IP e máscara
  212. $client = $this->client;
  213. $client->request(
  214. 'POST',
  215. '/ws/instala/download/service/'.$this->hash_service,
  216. array(),
  217. array(),
  218. array(
  219. 'CONTENT_TYPE' => 'application/json',
  220. 'HTTPS' => true
  221. ),
  222. $this->hash
  223. );
  224. $response = $this->client->getResponse();
  225. $status = $response->getStatusCode();
  226. $logger->debug("Response status: $status");
  227. $this->assertEquals(200, $status);
  228. // Verifica que foi fornecida uma URL para o serviço
  229. $dados = json_decode($response->getContent(), true);
  230. // Pega servidor de updates para a rede
  231. $rede = $this->em->getRepository("CacicCommonBundle:Rede")->findOneBy(array(
  232. 'teIpRede' => '0.0.0.0'
  233. ));
  234. $modulo = $this->em->getRepository("CacicCommonBundle:RedeVersaoModulo")->findOneBy(array(
  235. 'nmModulo' => 'cacic-service.exe',
  236. 'teHash' => $this->hash_service,
  237. 'idRede' => $rede->getIdRede(),
  238. 'tipo' => 'cacic'
  239. ));
  240. $url = $rede->getTeServUpdates()
  241. . "/downloads/"
  242. . $modulo->getFilepath();
  243. $this->assertEquals($url, $dados['valor']);
  244. // Agora testa o módulo com outro hash e verifica se identificou a rede
  245. $this->em->persist($this->rede);
  246. $this->em->persist($this->modulo);
  247. $this->em->flush();
  248. // Testa envio do IP e máscara
  249. $client = $this->client;
  250. $client->request(
  251. 'POST',
  252. '/ws/instala/download/service/'.$this->modulo->getTeHash(),
  253. array(),
  254. array(),
  255. array(
  256. 'CONTENT_TYPE' => 'application/json',
  257. 'HTTPS' => true
  258. ),
  259. $this->hash
  260. );
  261. $response = $this->client->getResponse();
  262. $status = $response->getStatusCode();
  263. $logger->debug("Response status: $status");
  264. $this->assertEquals(200, $status);
  265. $dados = json_decode($response->getContent(), true);
  266. $computador = json_decode($this->hash, true);
  267. $rede = $this->em->getRepository("CacicCommonBundle:Rede")->getDadosRedePreColeta(
  268. $computador['ip_address'],
  269. $computador['netmask']
  270. );
  271. $modulo = $this->em->getRepository("CacicCommonBundle:RedeVersaoModulo")->findOneBy(array(
  272. 'nmModulo' => 'cacic-service.exe',
  273. 'teHash' => $this->modulo->getTeHash(),
  274. 'idRede' => $rede->getIdRede(),
  275. 'tipo' => 'cacic'
  276. ));
  277. $url = $rede->getTeServUpdates()
  278. . "/downloads/"
  279. . $modulo->getFilepath();
  280. $this->assertEquals($url, $dados['valor']);
  281. // Remove dados do teste
  282. $this->em->remove($this->rede);
  283. $this->em->remove($this->modulo);
  284. $this->em->flush();
  285. }
  286. /**
  287. * Teste que deve retornar sempre a URL para a última versão do MSI disponível
  288. */
  289. public function testDownloadMsi() {
  290. $logger = $this->container->get('logger');
  291. $client = $this->client;
  292. $client->request(
  293. 'POST',
  294. '/ws/instala/download/msi',
  295. array(),
  296. array(),
  297. array(
  298. 'CONTENT_TYPE' => 'application/json',
  299. 'HTTPS' => true
  300. ),
  301. '{}'
  302. );
  303. $logger->debug("Dados JSON do computador enviados \n".$this->client->getRequest()->getcontent());
  304. $response = $this->client->getResponse();
  305. $status = $response->getStatusCode();
  306. $logger->debug("Response status: $status");
  307. $this->assertEquals(200, $status);
  308. // Testa retorno da URL
  309. $dados = json_decode($response->getContent(), true);
  310. // Pega servidor de updates para a rede
  311. $rede = $this->em->getRepository("CacicCommonBundle:Rede")->findOneBy(array(
  312. 'teIpRede' => '0.0.0.0'
  313. ));
  314. $url = $rede->getTeServUpdates()
  315. . "/Cacic.msi";
  316. $logger->debug("URL download: $url");
  317. $this->assertEquals($url, $dados['valor']);
  318. // Agora testa o módulo com outro hash e verifica se identificou a rede
  319. $this->em->persist($this->rede);
  320. $this->em->persist($this->modulo);
  321. $this->em->flush();
  322. $client->request(
  323. 'POST',
  324. '/ws/instala/download/msi',
  325. array(),
  326. array(),
  327. array(
  328. 'CONTENT_TYPE' => 'application/json',
  329. 'HTTPS' => true
  330. ),
  331. $this->hash
  332. );
  333. $response = $this->client->getResponse();
  334. $status = $response->getStatusCode();
  335. $this->assertEquals(200, $status);
  336. // Testa retorno da URL
  337. $dados = json_decode($response->getContent(), true);
  338. // Pega servidor de updates para a rede
  339. $url = $this->rede->getTeServUpdates()
  340. . "/"
  341. . "/msi"
  342. . "/"
  343. . "Cacic.msi";
  344. $logger->debug("URL download: $url");
  345. $this->assertEquals($url, $dados['valor']);
  346. // Remove dados do teste
  347. $this->em->remove($this->rede);
  348. $this->em->flush();
  349. }
  350. public function tearDown() {
  351. // Apaga dados da classe de cima
  352. parent::tearDown();
  353. }
  354. }