test_pagure_flask_ui_plugins_pagure_ci.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals, absolute_import
  3. import unittest
  4. import sys
  5. import os
  6. import mock
  7. sys.path.insert(
  8. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  9. )
  10. import pagure.lib.tasks_services
  11. import tests
  12. class PagureFlaskPluginPagureCItests(tests.SimplePagureTest):
  13. """ Tests for flask plugins controller of pagure """
  14. def test_plugin_pagure_ci(self):
  15. """ Test the pagure ci plugin on/off endpoint. """
  16. tests.create_projects(self.session)
  17. tests.create_projects_git(os.path.join(self.path, "repos"))
  18. user = tests.FakeUser(username="pingou")
  19. with tests.user_set(self.app.application, user):
  20. output = self.app.get("/test/settings/Pagure CI")
  21. self.assertEqual(output.status_code, 200)
  22. output_text = output.get_data(as_text=True)
  23. self.assertIn(
  24. "<title>Settings Pagure CI - test - Pagure</title>",
  25. output_text,
  26. )
  27. self.assertIn(
  28. '<label for="ci_url">URL to the project on the CI '
  29. "service</label>",
  30. output_text,
  31. )
  32. self.assertIn(
  33. '<label for="ci_job">Name of the job to trigger' "</label>",
  34. output_text,
  35. )
  36. self.assertIn(
  37. '<input class="form-check-input mt-2" id="active_commit" '
  38. 'name="active_commit" type="checkbox" value="y">',
  39. output_text,
  40. )
  41. self.assertIn(
  42. '<input class="form-check-input mt-2" id="active_pr" '
  43. 'name="active_pr" type="checkbox" value="y">',
  44. output_text,
  45. )
  46. csrf_token = output_text.split(
  47. 'name="csrf_token" type="hidden" value="'
  48. )[1].split('">')[0]
  49. data = {}
  50. output = self.app.post("/test/settings/Pagure CI", data=data)
  51. self.assertEqual(output.status_code, 200)
  52. output_text = output.get_data(as_text=True)
  53. self.assertIn(
  54. "<title>Settings Pagure CI - test - Pagure</title>",
  55. output_text,
  56. )
  57. self.assertIn(
  58. '<label for="ci_url">URL to the project on the CI '
  59. "service</label>",
  60. output_text,
  61. )
  62. self.assertIn(
  63. '<label for="ci_job">Name of the job to trigger' "</label>",
  64. output_text,
  65. )
  66. self.assertIn(
  67. '<input class="form-check-input mt-2" id="active_commit" '
  68. 'name="active_commit" type="checkbox" value="y">',
  69. output_text,
  70. )
  71. self.assertIn(
  72. '<input class="form-check-input mt-2" id="active_pr" '
  73. 'name="active_pr" type="checkbox" value="y">',
  74. output_text,
  75. )
  76. # Activate hook
  77. data = {
  78. "active_commit": "y",
  79. "ci_url": "https://jenkins.fedoraproject.org",
  80. "ci_type": "jenkins",
  81. "ci_job": "test/job",
  82. }
  83. # CSRF Token missing
  84. output = self.app.post(
  85. "/test/settings/Pagure CI", data=data, follow_redirects=True
  86. )
  87. self.assertEqual(output.status_code, 200)
  88. output_text = output.get_data(as_text=True)
  89. self.assertIn(
  90. "<title>Settings Pagure CI - test - Pagure</title>",
  91. output_text,
  92. )
  93. self.assertIn(
  94. '<label for="ci_url">URL to the project on the CI '
  95. "service</label>",
  96. output_text,
  97. )
  98. self.assertIn(
  99. '<label for="ci_job">Name of the job to trigger' "</label>",
  100. output_text,
  101. )
  102. self.assertIn(
  103. '<input checked class="form-check-input mt-2" id="active_commit" '
  104. 'name="active_commit" type="checkbox" value="y">',
  105. output_text,
  106. )
  107. self.assertIn(
  108. '<input class="form-check-input mt-2" id="active_pr" '
  109. 'name="active_pr" type="checkbox" value="y">',
  110. output_text,
  111. )
  112. data["csrf_token"] = csrf_token
  113. # Activate hook
  114. output = self.app.post(
  115. "/test/settings/Pagure CI", data=data, follow_redirects=True
  116. )
  117. self.assertEqual(output.status_code, 200)
  118. output_text = output.get_data(as_text=True)
  119. self.assertIn(
  120. '<h5 class="pl-2 font-weight-bold text-muted">'
  121. "Project Settings</h5>\n",
  122. output_text,
  123. )
  124. self.assertIn(
  125. "<title>Settings - test - Pagure</title>", output_text
  126. )
  127. self.assertIn(
  128. '<h5 class="pl-2 font-weight-bold text-muted">'
  129. "Project Settings</h5>\n",
  130. output_text,
  131. )
  132. self.assertIn("Hook Pagure CI activated", output_text)
  133. output = self.app.get("/test/settings/Pagure CI")
  134. self.assertEqual(output.status_code, 200)
  135. output_text = output.get_data(as_text=True)
  136. self.assertIn(
  137. "<title>Settings Pagure CI - test - Pagure</title>",
  138. output_text,
  139. )
  140. self.assertIn(
  141. '<label for="ci_url">URL to the project on the CI '
  142. "service</label>",
  143. output_text,
  144. )
  145. self.assertIn(
  146. '<label for="ci_job">Name of the job to trigger' "</label>",
  147. output_text,
  148. )
  149. self.assertIn(
  150. '<input checked class="form-check-input mt-2" id="active_commit" '
  151. 'name="active_commit" type="checkbox" value="y">',
  152. output_text,
  153. )
  154. self.assertIn(
  155. "<pre>\nhttp://localhost.localdomain/api/0/ci/jenkins/test/",
  156. output_text,
  157. )
  158. # De-activate the hook
  159. data = {"csrf_token": csrf_token}
  160. output = self.app.post(
  161. "/test/settings/Pagure CI", data=data, follow_redirects=True
  162. )
  163. self.assertEqual(output.status_code, 200)
  164. output_text = output.get_data(as_text=True)
  165. self.assertIn(
  166. '<h5 class="pl-2 font-weight-bold text-muted">'
  167. "Project Settings</h5>\n",
  168. output_text,
  169. )
  170. self.assertIn("Hook Pagure CI deactivated", output_text)
  171. output = self.app.get("/test/settings/Pagure CI")
  172. self.assertEqual(output.status_code, 200)
  173. output_text = output.get_data(as_text=True)
  174. self.assertIn(
  175. "<title>Settings Pagure CI - test - Pagure</title>",
  176. output_text,
  177. )
  178. self.assertIn(
  179. '<label for="ci_url">URL to the project on the CI '
  180. "service</label>",
  181. output_text,
  182. )
  183. self.assertIn(
  184. '<label for="ci_job">Name of the job to trigger' "</label>",
  185. output_text,
  186. )
  187. self.assertIn(
  188. '<input class="form-check-input mt-2" id="active_commit" '
  189. 'name="active_commit" type="checkbox" value="y">',
  190. output_text,
  191. )
  192. # Missing the required ci_url
  193. data = {"csrf_token": csrf_token, "active_commit": "y"}
  194. output = self.app.post(
  195. "/test/settings/Pagure CI", data=data, follow_redirects=True
  196. )
  197. self.assertEqual(output.status_code, 200)
  198. output_text = output.get_data(as_text=True)
  199. self.assertIn(
  200. "<title>Settings Pagure CI - test - Pagure</title>",
  201. output_text,
  202. )
  203. self.assertNotIn("Hook Pagure CI activated", output_text)
  204. if self.get_wtforms_version() >= (2, 2):
  205. self.assertIn(
  206. '<div class="col-sm-10">\n '
  207. '<input class="form-control pl-0" id="ci_url" name="ci_url" '
  208. 'required type="text" value="">\n </div>\n </div>\n '
  209. '<div class="alert alert-danger">This field is required.</div>',
  210. output_text,
  211. )
  212. self.assertIn(
  213. '<div class="col-sm-10">\n '
  214. '<input class="form-control pl-0" id="ci_job" name="ci_job" '
  215. 'required type="text" value="">\n </div>\n </div>\n '
  216. '<div class="alert alert-danger">This field is required.</div>',
  217. output_text,
  218. )
  219. else:
  220. self.assertIn(
  221. '<div class="col-sm-10">\n '
  222. '<input class="form-control pl-0" id="ci_url" name="ci_url" '
  223. 'type="text" value="">\n </div>\n </div>\n '
  224. '<div class="alert alert-danger">This field is required.</div>',
  225. output_text,
  226. )
  227. self.assertIn(
  228. '<div class="col-sm-10">\n '
  229. '<input class="form-control pl-0" id="ci_job" name="ci_job" '
  230. 'type="text" value="">\n </div>\n </div>\n '
  231. '<div class="alert alert-danger">This field is required.</div>',
  232. output_text,
  233. )
  234. self.assertIn(
  235. '<input checked class="form-check-input mt-2" id="active_commit" '
  236. 'name="active_commit" type="checkbox" value="y">',
  237. output_text,
  238. )
  239. def test_plugin_pagure_ci_namespaced(self):
  240. """ Test the pagure ci plugin on/off endpoint. """
  241. tests.create_projects(self.session)
  242. tests.create_projects_git(os.path.join(self.path, "repos"))
  243. user = tests.FakeUser(username="pingou")
  244. with tests.user_set(self.app.application, user):
  245. output = self.app.get("/somenamespace/test3/settings/Pagure CI")
  246. self.assertEqual(output.status_code, 200)
  247. output_text = output.get_data(as_text=True)
  248. self.assertIn(
  249. "<title>Settings Pagure CI - somenamespace/test3 - "
  250. "Pagure</title>",
  251. output_text,
  252. )
  253. self.assertIn(
  254. '<label for="ci_url">URL to the project on the CI '
  255. "service</label>",
  256. output_text,
  257. )
  258. self.assertIn(
  259. '<label for="ci_job">Name of the job to trigger' "</label>",
  260. output_text,
  261. )
  262. self.assertIn(
  263. '<input class="form-check-input mt-2" id="active_pr" name="active_pr" '
  264. 'type="checkbox" value="y">',
  265. output_text,
  266. )
  267. csrf_token = output_text.split(
  268. 'name="csrf_token" type="hidden" value="'
  269. )[1].split('">')[0]
  270. # Activate hook
  271. data = {
  272. "active_pr": "y",
  273. "ci_url": "https://jenkins.fedoraproject.org",
  274. "ci_job": "test/job",
  275. "ci_type": "jenkins",
  276. "csrf_token": csrf_token,
  277. }
  278. # Activate hook
  279. output = self.app.post(
  280. "/somenamespace/test3/settings/Pagure CI",
  281. data=data,
  282. follow_redirects=True,
  283. )
  284. self.assertEqual(output.status_code, 200)
  285. output_text = output.get_data(as_text=True)
  286. self.assertIn(
  287. '<h5 class="pl-2 font-weight-bold text-muted">'
  288. "Project Settings</h5>\n",
  289. output_text,
  290. )
  291. self.assertIn(
  292. "<title>Settings - somenamespace/test3 - Pagure</title>",
  293. output_text,
  294. )
  295. self.assertIn(
  296. '<h5 class="pl-2 font-weight-bold text-muted">'
  297. "Project Settings</h5>\n",
  298. output_text,
  299. )
  300. self.assertIn("Hook Pagure CI activated", output_text)
  301. output = self.app.get("/somenamespace/test3/settings/Pagure CI")
  302. self.assertEqual(output.status_code, 200)
  303. output_text = output.get_data(as_text=True)
  304. self.assertIn(
  305. "<title>Settings Pagure CI - somenamespace/test3 - "
  306. "Pagure</title>",
  307. output_text,
  308. )
  309. self.assertIn(
  310. '<label for="ci_url">URL to the project on the CI '
  311. "service</label>",
  312. output_text,
  313. )
  314. self.assertIn(
  315. '<label for="ci_job">Name of the job to trigger' "</label>",
  316. output_text,
  317. )
  318. self.assertIn(
  319. '<input checked class="form-check-input mt-2" id="active_pr" name="active_pr" '
  320. 'type="checkbox" value="y">',
  321. output_text,
  322. )
  323. self.assertIn(
  324. "<pre>\nhttp://localhost.localdomain/api/0/ci/jenkins/somenamespace/test3/",
  325. output_text,
  326. )
  327. @mock.patch("pagure.lib.tasks_services.trigger_jenkins_build")
  328. def test_plugin_pagure_ci_namespaced_auth(self, trigger_jenk):
  329. """ Test the pagure ci plugin on/off endpoint. """
  330. tests.create_projects(self.session)
  331. tests.create_projects_git(os.path.join(self.path, "repos"))
  332. user = tests.FakeUser(username="pingou")
  333. with tests.user_set(self.app.application, user):
  334. output = self.app.get("/somenamespace/test3/settings/Pagure CI")
  335. self.assertEqual(output.status_code, 200)
  336. output_text = output.get_data(as_text=True)
  337. self.assertIn(
  338. "<title>Settings Pagure CI - somenamespace/test3 - "
  339. "Pagure</title>",
  340. output_text,
  341. )
  342. self.assertIn(
  343. '<label for="ci_url">URL to the project on the CI '
  344. "service</label>",
  345. output_text,
  346. )
  347. self.assertIn(
  348. '<label for="ci_job">Name of the job to trigger' "</label>",
  349. output_text,
  350. )
  351. self.assertIn(
  352. '<input class="form-check-input mt-2" id="active_pr" name="active_pr" '
  353. 'type="checkbox" value="y">',
  354. output_text,
  355. )
  356. csrf_token = output_text.split(
  357. 'name="csrf_token" type="hidden" value="'
  358. )[1].split('">')[0]
  359. # Activate hook
  360. data = {
  361. "active_pr": "y",
  362. "ci_url": "https://jenkins.fedoraproject.org",
  363. "ci_job": "test/job",
  364. "ci_type": "jenkins",
  365. "ci_username": "jenkins_username",
  366. "ci_password": "jenkins_password",
  367. "csrf_token": csrf_token,
  368. }
  369. # Activate hook
  370. output = self.app.post(
  371. "/somenamespace/test3/settings/Pagure CI",
  372. data=data,
  373. follow_redirects=True,
  374. )
  375. self.assertEqual(output.status_code, 200)
  376. output_text = output.get_data(as_text=True)
  377. self.assertIn(
  378. '<h5 class="pl-2 font-weight-bold text-muted">'
  379. "Project Settings</h5>\n",
  380. output_text,
  381. )
  382. self.assertIn(
  383. "<title>Settings - somenamespace/test3 - Pagure</title>",
  384. output_text,
  385. )
  386. self.assertIn(
  387. '<h5 class="pl-2 font-weight-bold text-muted">'
  388. "Project Settings</h5>\n",
  389. output_text,
  390. )
  391. self.assertIn("Hook Pagure CI activated", output_text)
  392. output = self.app.get("/somenamespace/test3/settings/Pagure CI")
  393. self.assertEqual(output.status_code, 200)
  394. output_text = output.get_data(as_text=True)
  395. self.assertIn(
  396. "<title>Settings Pagure CI - somenamespace/test3 - "
  397. "Pagure</title>",
  398. output_text,
  399. )
  400. self.assertIn(
  401. '<label for="ci_url">URL to the project on the CI '
  402. "service</label>",
  403. output_text,
  404. )
  405. self.assertIn(
  406. '<label for="ci_job">Name of the job to trigger' "</label>",
  407. output_text,
  408. )
  409. self.assertIn(
  410. '<input checked class="form-check-input mt-2" id="active_pr" name="active_pr" '
  411. 'type="checkbox" value="y">',
  412. output_text,
  413. )
  414. self.assertIn(
  415. "<pre>\nhttp://localhost.localdomain/api/0/ci/jenkins/somenamespace/test3/",
  416. output_text,
  417. )
  418. output = pagure.lib.tasks_services.trigger_ci_build(
  419. project_name="somenamespace/test3",
  420. cause="PR#ID",
  421. branch="feature",
  422. branch_to="master",
  423. ci_type="jenkins",
  424. )
  425. self.assertIsNone(output)
  426. trigger_jenk.assert_called_once_with(
  427. branch="feature",
  428. cause="PR#ID",
  429. ci_password="jenkins_password",
  430. ci_username="jenkins_username",
  431. job="test/job",
  432. project_path="somenamespace/test3.git",
  433. token=mock.ANY,
  434. url="https://jenkins.fedoraproject.org",
  435. branch_to="master",
  436. )
  437. if __name__ == "__main__":
  438. unittest.main(verbosity=2)