test_pagure_flask_ui_plugins_pagure_ci.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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() >= (3, 0):
  205. self.assertIn(
  206. '<div class="col-sm-10">\n '
  207. '<input class="form-control pl-0" id="ci_url" maxlength="255" 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" maxlength="255" 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. elif self.get_wtforms_version() >= (2, 2):
  220. self.assertIn(
  221. '<div class="col-sm-10">\n '
  222. '<input class="form-control pl-0" id="ci_url" name="ci_url" '
  223. 'required 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. 'required type="text" value="">\n </div>\n </div>\n '
  231. '<div class="alert alert-danger">This field is required.</div>',
  232. output_text,
  233. )
  234. else:
  235. self.assertIn(
  236. '<div class="col-sm-10">\n '
  237. '<input class="form-control pl-0" id="ci_url" name="ci_url" '
  238. 'type="text" value="">\n </div>\n </div>\n '
  239. '<div class="alert alert-danger">This field is required.</div>',
  240. output_text,
  241. )
  242. self.assertIn(
  243. '<div class="col-sm-10">\n '
  244. '<input class="form-control pl-0" id="ci_job" name="ci_job" '
  245. 'type="text" value="">\n </div>\n </div>\n '
  246. '<div class="alert alert-danger">This field is required.</div>',
  247. output_text,
  248. )
  249. self.assertIn(
  250. '<input checked class="form-check-input mt-2" id="active_commit" '
  251. 'name="active_commit" type="checkbox" value="y">',
  252. output_text,
  253. )
  254. def test_plugin_pagure_ci_namespaced(self):
  255. """Test the pagure ci plugin on/off endpoint."""
  256. tests.create_projects(self.session)
  257. tests.create_projects_git(os.path.join(self.path, "repos"))
  258. user = tests.FakeUser(username="pingou")
  259. with tests.user_set(self.app.application, user):
  260. output = self.app.get("/somenamespace/test3/settings/Pagure CI")
  261. self.assertEqual(output.status_code, 200)
  262. output_text = output.get_data(as_text=True)
  263. self.assertIn(
  264. "<title>Settings Pagure CI - somenamespace/test3 - "
  265. "Pagure</title>",
  266. output_text,
  267. )
  268. self.assertIn(
  269. '<label for="ci_url">URL to the project on the CI '
  270. "service</label>",
  271. output_text,
  272. )
  273. self.assertIn(
  274. '<label for="ci_job">Name of the job to trigger' "</label>",
  275. output_text,
  276. )
  277. self.assertIn(
  278. '<input class="form-check-input mt-2" id="active_pr" name="active_pr" '
  279. 'type="checkbox" value="y">',
  280. output_text,
  281. )
  282. csrf_token = output_text.split(
  283. 'name="csrf_token" type="hidden" value="'
  284. )[1].split('">')[0]
  285. # Activate hook
  286. data = {
  287. "active_pr": "y",
  288. "ci_url": "https://jenkins.fedoraproject.org",
  289. "ci_job": "test/job",
  290. "ci_type": "jenkins",
  291. "csrf_token": csrf_token,
  292. }
  293. # Activate hook
  294. output = self.app.post(
  295. "/somenamespace/test3/settings/Pagure CI",
  296. data=data,
  297. follow_redirects=True,
  298. )
  299. self.assertEqual(output.status_code, 200)
  300. output_text = output.get_data(as_text=True)
  301. self.assertIn(
  302. '<h5 class="pl-2 font-weight-bold text-muted">'
  303. "Project Settings</h5>\n",
  304. output_text,
  305. )
  306. self.assertIn(
  307. "<title>Settings - somenamespace/test3 - Pagure</title>",
  308. output_text,
  309. )
  310. self.assertIn(
  311. '<h5 class="pl-2 font-weight-bold text-muted">'
  312. "Project Settings</h5>\n",
  313. output_text,
  314. )
  315. self.assertIn("Hook Pagure CI activated", output_text)
  316. output = self.app.get("/somenamespace/test3/settings/Pagure CI")
  317. self.assertEqual(output.status_code, 200)
  318. output_text = output.get_data(as_text=True)
  319. self.assertIn(
  320. "<title>Settings Pagure CI - somenamespace/test3 - "
  321. "Pagure</title>",
  322. output_text,
  323. )
  324. self.assertIn(
  325. '<label for="ci_url">URL to the project on the CI '
  326. "service</label>",
  327. output_text,
  328. )
  329. self.assertIn(
  330. '<label for="ci_job">Name of the job to trigger' "</label>",
  331. output_text,
  332. )
  333. self.assertIn(
  334. '<input checked class="form-check-input mt-2" id="active_pr" name="active_pr" '
  335. 'type="checkbox" value="y">',
  336. output_text,
  337. )
  338. self.assertIn(
  339. "<pre>\nhttp://localhost.localdomain/api/0/ci/jenkins/somenamespace/test3/",
  340. output_text,
  341. )
  342. @mock.patch("pagure.api.ci.jenkins.trigger_build")
  343. def test_plugin_pagure_ci_namespaced_auth(self, trigger_jenk):
  344. """Test the pagure ci plugin on/off endpoint."""
  345. tests.create_projects(self.session)
  346. tests.create_projects_git(os.path.join(self.path, "repos"))
  347. user = tests.FakeUser(username="pingou")
  348. with tests.user_set(self.app.application, user):
  349. output = self.app.get("/somenamespace/test3/settings/Pagure CI")
  350. self.assertEqual(output.status_code, 200)
  351. output_text = output.get_data(as_text=True)
  352. self.assertIn(
  353. "<title>Settings Pagure CI - somenamespace/test3 - "
  354. "Pagure</title>",
  355. output_text,
  356. )
  357. self.assertIn(
  358. '<label for="ci_url">URL to the project on the CI '
  359. "service</label>",
  360. output_text,
  361. )
  362. self.assertIn(
  363. '<label for="ci_job">Name of the job to trigger' "</label>",
  364. output_text,
  365. )
  366. self.assertIn(
  367. '<input class="form-check-input mt-2" id="active_pr" name="active_pr" '
  368. 'type="checkbox" value="y">',
  369. output_text,
  370. )
  371. csrf_token = output_text.split(
  372. 'name="csrf_token" type="hidden" value="'
  373. )[1].split('">')[0]
  374. # Activate hook
  375. data = {
  376. "active_pr": "y",
  377. "ci_url": "https://jenkins.fedoraproject.org",
  378. "ci_job": "test/job",
  379. "ci_type": "jenkins",
  380. "ci_username": "jenkins_username",
  381. "ci_password": "jenkins_password",
  382. "csrf_token": csrf_token,
  383. }
  384. # Activate hook
  385. output = self.app.post(
  386. "/somenamespace/test3/settings/Pagure CI",
  387. data=data,
  388. follow_redirects=True,
  389. )
  390. self.assertEqual(output.status_code, 200)
  391. output_text = output.get_data(as_text=True)
  392. self.assertIn(
  393. '<h5 class="pl-2 font-weight-bold text-muted">'
  394. "Project Settings</h5>\n",
  395. output_text,
  396. )
  397. self.assertIn(
  398. "<title>Settings - somenamespace/test3 - Pagure</title>",
  399. output_text,
  400. )
  401. self.assertIn(
  402. '<h5 class="pl-2 font-weight-bold text-muted">'
  403. "Project Settings</h5>\n",
  404. output_text,
  405. )
  406. self.assertIn("Hook Pagure CI activated", output_text)
  407. output = self.app.get("/somenamespace/test3/settings/Pagure CI")
  408. self.assertEqual(output.status_code, 200)
  409. output_text = output.get_data(as_text=True)
  410. self.assertIn(
  411. "<title>Settings Pagure CI - somenamespace/test3 - "
  412. "Pagure</title>",
  413. output_text,
  414. )
  415. self.assertIn(
  416. '<label for="ci_url">URL to the project on the CI '
  417. "service</label>",
  418. output_text,
  419. )
  420. self.assertIn(
  421. '<label for="ci_job">Name of the job to trigger' "</label>",
  422. output_text,
  423. )
  424. self.assertIn(
  425. '<input checked class="form-check-input mt-2" id="active_pr" name="active_pr" '
  426. 'type="checkbox" value="y">',
  427. output_text,
  428. )
  429. self.assertIn(
  430. "<pre>\nhttp://localhost.localdomain/api/0/ci/jenkins/somenamespace/test3/",
  431. output_text,
  432. )
  433. output = pagure.lib.tasks_services.trigger_ci_build(
  434. project_name="somenamespace/test3",
  435. cause="PR#ID",
  436. branch="feature",
  437. branch_to="master",
  438. ci_type="jenkins",
  439. )
  440. self.assertIsNone(output)
  441. trigger_jenk.assert_called_once_with(
  442. branch="feature",
  443. cause="PR#ID",
  444. ci_password="jenkins_password",
  445. ci_username="jenkins_username",
  446. job="test/job",
  447. project_path="somenamespace/test3.git",
  448. token=mock.ANY,
  449. url="https://jenkins.fedoraproject.org",
  450. branch_to="master",
  451. )
  452. if __name__ == "__main__":
  453. unittest.main(verbosity=2)