test_pagure_lib_task_services.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import os
  10. import shutil
  11. import sys
  12. import tempfile
  13. import time
  14. import unittest
  15. import pygit2
  16. import six
  17. from mock import ANY, patch, MagicMock, call
  18. sys.path.insert(
  19. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  20. )
  21. import pagure.lib.tasks_services
  22. import pagure.lib.query
  23. import tests
  24. import pagure.lib.tasks_services
  25. class PagureLibTaskServicestests(tests.Modeltests):
  26. """ Tests for pagure.lib.task_services """
  27. maxDiff = None
  28. def setUp(self):
  29. """ Set up the environnment, ran before every tests. """
  30. super(PagureLibTaskServicestests, self).setUp()
  31. tests.create_projects(self.session)
  32. # Create a fork of test for foo
  33. item = pagure.lib.model.Project(
  34. user_id=2, # foo
  35. name="test",
  36. is_fork=True,
  37. parent_id=1,
  38. description="test project #1",
  39. hook_token="aaabbbccc_foo",
  40. )
  41. item.close_status = [
  42. "Invalid",
  43. "Insufficient data",
  44. "Fixed",
  45. "Duplicate",
  46. ]
  47. self.session.add(item)
  48. self.session.commit()
  49. def test_webhook_notification_invalid_project(self):
  50. """ Test the webhook_notification method. """
  51. self.assertRaises(
  52. RuntimeError,
  53. pagure.lib.tasks_services.webhook_notification,
  54. topic="topic",
  55. msg={"payload": ["a", "b", "c"]},
  56. namespace=None,
  57. name="invalid",
  58. user=None,
  59. )
  60. @patch("pagure.lib.tasks_services.call_web_hooks")
  61. def test_webhook_notification_no_webhook(self, call_wh):
  62. """ Test the webhook_notification method. """
  63. output = pagure.lib.tasks_services.webhook_notification(
  64. topic="topic",
  65. msg={"payload": ["a", "b", "c"]},
  66. namespace=None,
  67. name="test",
  68. user=None,
  69. )
  70. self.assertIsNone(output)
  71. call_wh.assert_not_called()
  72. @patch("pagure.lib.git.log_commits_to_db")
  73. def test_log_commit_send_notifications_invalid_project(self, log):
  74. """ Test the log_commit_send_notifications method. """
  75. output = pagure.lib.tasks_services.log_commit_send_notifications(
  76. name="invalid",
  77. commits=[],
  78. abspath=None,
  79. branch=None,
  80. default_branch=None,
  81. namespace=None,
  82. username=None,
  83. )
  84. self.assertIsNone(output)
  85. log.assert_not_called()
  86. @patch("pagure.lib.notify.notify_new_commits")
  87. @patch("pagure.lib.git.log_commits_to_db")
  88. def test_log_commit_send_notifications_valid_project(self, log, notif):
  89. """ Test the log_commit_send_notifications method. """
  90. output = pagure.lib.tasks_services.log_commit_send_notifications(
  91. name="test",
  92. commits=["hash1", "hash2"],
  93. abspath="/path/to/git",
  94. branch="master",
  95. default_branch="master",
  96. namespace=None,
  97. username=None,
  98. )
  99. self.assertIsNone(output)
  100. log.assert_called_once_with(
  101. ANY, ANY, ["hash1", "hash2"], "/path/to/git"
  102. )
  103. notif.assert_called_once_with(
  104. "/path/to/git", ANY, "master", ["hash1", "hash2"]
  105. )
  106. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  107. def test_trigger_ci_build_invalid_project(self, trigger_jenk):
  108. """ Test the trigger_ci_build method. """
  109. output = pagure.lib.tasks_services.trigger_ci_build(
  110. project_name="invalid",
  111. cause="PR#ID",
  112. branch="feature",
  113. branch_to="master",
  114. ci_type="jenkins",
  115. )
  116. self.assertIsNone(output)
  117. trigger_jenk.assert_not_called()
  118. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  119. def test_trigger_ci_build_not_configured_project(self, trigger_jenk):
  120. """ Test the trigger_ci_build method. """
  121. self.assertRaises(
  122. pagure.exceptions.PagureException,
  123. pagure.lib.tasks_services.trigger_ci_build,
  124. project_name="test",
  125. cause="PR#ID",
  126. branch="feature",
  127. branch_to="master",
  128. ci_type="jenkins",
  129. )
  130. trigger_jenk.assert_not_called()
  131. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  132. def test_trigger_ci_build_not_configured_project_fork(self, trigger_jenk):
  133. """ Test the trigger_ci_build method. """
  134. self.assertRaises(
  135. pagure.exceptions.PagureException,
  136. pagure.lib.tasks_services.trigger_ci_build,
  137. project_name="forks/foo/test",
  138. cause="PR#ID",
  139. branch="feature",
  140. branch_to="master",
  141. ci_type="jenkins",
  142. )
  143. trigger_jenk.assert_not_called()
  144. @patch("pagure.lib.query._get_project")
  145. def test_load_json_commits_to_db_invalid_data_type(self, get_project):
  146. """ Test the load_json_commits_to_db method. """
  147. output = pagure.lib.tasks_services.load_json_commits_to_db(
  148. name="test",
  149. commits=["hash1", "hash2"],
  150. abspath="/path/to/git",
  151. data_type="invalid",
  152. agent="pingou",
  153. namespace=None,
  154. username=None,
  155. )
  156. self.assertIsNone(output)
  157. get_project.assert_not_called()
  158. @patch("pagure.lib.tasks_services.get_files_to_load")
  159. def test_load_json_commits_to_db_invalid_project(self, get_files):
  160. """ Test the load_json_commits_to_db method. """
  161. output = pagure.lib.tasks_services.load_json_commits_to_db(
  162. name="invalid",
  163. commits=["hash1", "hash2"],
  164. abspath="/path/to/git",
  165. data_type="ticket",
  166. agent="pingou",
  167. namespace=None,
  168. username=None,
  169. )
  170. self.assertIsNone(output)
  171. get_files.assert_not_called()
  172. @patch("pagure.lib.git.update_request_from_git")
  173. @patch("pagure.lib.git.update_ticket_from_git")
  174. def test_load_json_commits_to_db_invalid_path(self, up_issue, up_pr):
  175. """ Test the load_json_commits_to_db method. """
  176. output = pagure.lib.tasks_services.load_json_commits_to_db(
  177. name="test",
  178. commits=["hash1", "hash2"],
  179. abspath=self.path,
  180. data_type="ticket",
  181. agent="pingou",
  182. namespace=None,
  183. username=None,
  184. )
  185. self.assertIsNone(output)
  186. up_issue.assert_not_called()
  187. up_pr.assert_not_called()
  188. @patch("pagure.lib.git.update_request_from_git")
  189. @patch("pagure.lib.git.update_ticket_from_git")
  190. def test_load_json_commits_to_db_invalid_path_one_commit(
  191. self, up_issue, up_pr
  192. ):
  193. """ Test the load_json_commits_to_db method. """
  194. output = pagure.lib.tasks_services.load_json_commits_to_db(
  195. name="test",
  196. commits=["hash1"],
  197. abspath=self.path,
  198. data_type="ticket",
  199. agent="pingou",
  200. namespace=None,
  201. username=None,
  202. )
  203. self.assertIsNone(output)
  204. up_issue.assert_not_called()
  205. up_pr.assert_not_called()
  206. @patch("pagure.lib.notify.send_email")
  207. @patch("pagure.lib.git.update_request_from_git")
  208. @patch("pagure.lib.git.update_ticket_from_git")
  209. def test_load_json_commits_to_db_no_agent(self, up_issue, up_pr, send):
  210. """ Test the load_json_commits_to_db method. """
  211. output = pagure.lib.tasks_services.load_json_commits_to_db(
  212. name="test",
  213. commits=[],
  214. abspath=None,
  215. data_type="ticket",
  216. agent=None,
  217. namespace=None,
  218. username=None,
  219. )
  220. self.assertIsNone(output)
  221. up_issue.assert_not_called()
  222. up_pr.assert_not_called()
  223. send.assert_not_called()
  224. @patch("pagure.lib.notify.send_email")
  225. @patch("pagure.lib.git.update_request_from_git")
  226. @patch("pagure.lib.git.update_ticket_from_git")
  227. @patch("pagure.lib.git.read_git_lines")
  228. def test_load_json_commits_to_db_no_agent(
  229. self, git, up_issue, up_pr, send
  230. ):
  231. """ Test the load_json_commits_to_db method. """
  232. git.side_effect = [["file1"], ["file2"], ["files/image"], ["file1"]]
  233. output = pagure.lib.tasks_services.load_json_commits_to_db(
  234. name="test",
  235. commits=["hash1", "hash2"],
  236. abspath=self.path,
  237. data_type="ticket",
  238. agent=None,
  239. namespace=None,
  240. username=None,
  241. )
  242. self.assertIsNone(output)
  243. up_issue.assert_not_called()
  244. up_pr.assert_not_called()
  245. send.assert_not_called()
  246. @patch("json.loads")
  247. @patch("pagure.lib.notify.send_email")
  248. @patch("pagure.lib.git.update_request_from_git")
  249. @patch("pagure.lib.git.update_ticket_from_git")
  250. @patch("pagure.lib.git.read_git_lines")
  251. def test_load_json_commits_to_db_tickets(
  252. self, git, up_issue, up_pr, send, json_loads
  253. ):
  254. """ Test the load_json_commits_to_db method. """
  255. git.side_effect = [["file1"], ["file2"], ["files/image"], ["file1"]]
  256. json_loads.return_value = "foobar"
  257. output = pagure.lib.tasks_services.load_json_commits_to_db(
  258. name="test",
  259. commits=["hash1", "hash2"],
  260. abspath=self.path,
  261. data_type="ticket",
  262. agent=None,
  263. namespace=None,
  264. username=None,
  265. )
  266. self.assertIsNone(output)
  267. calls = [
  268. call(
  269. ANY,
  270. agent=None,
  271. issue_uid="file1",
  272. json_data="foobar",
  273. namespace=None,
  274. reponame="test",
  275. username=None,
  276. ),
  277. call(
  278. ANY,
  279. agent=None,
  280. issue_uid="file2",
  281. json_data="foobar",
  282. namespace=None,
  283. reponame="test",
  284. username=None,
  285. ),
  286. ]
  287. self.assertEqual(calls, up_issue.mock_calls)
  288. up_pr.assert_not_called()
  289. send.assert_not_called()
  290. @patch("json.loads")
  291. @patch("pagure.lib.notify.send_email")
  292. @patch("pagure.lib.git.update_request_from_git")
  293. @patch("pagure.lib.git.update_ticket_from_git")
  294. @patch("pagure.lib.git.read_git_lines")
  295. def test_load_json_commits_to_db_prs(
  296. self, git, up_issue, up_pr, send, json_loads
  297. ):
  298. """ Test the load_json_commits_to_db method. """
  299. git.side_effect = [["file1"], ["file2"], ["files/image"], ["file1"]]
  300. json_loads.return_value = "foobar"
  301. output = pagure.lib.tasks_services.load_json_commits_to_db(
  302. name="test",
  303. commits=["hash1", "hash2"],
  304. abspath=self.path,
  305. data_type="pull-request",
  306. agent="pingou",
  307. namespace=None,
  308. username=None,
  309. )
  310. self.assertIsNone(output)
  311. calls = [
  312. call(
  313. ANY,
  314. json_data="foobar",
  315. namespace=None,
  316. reponame="test",
  317. request_uid="file1",
  318. username=None,
  319. ),
  320. call(
  321. ANY,
  322. json_data="foobar",
  323. namespace=None,
  324. reponame="test",
  325. request_uid="file2",
  326. username=None,
  327. ),
  328. ]
  329. up_issue.assert_not_called()
  330. self.assertEqual(calls, up_pr.mock_calls)
  331. calls = [
  332. call(
  333. "Good Morning\n\n"
  334. "This is the log of loading all the files pushed in the git "
  335. "repo into\n"
  336. "the database. It should ignore files that are not JSON files,"
  337. " this\nis fine.\n\n"
  338. "Loading: file1 -- 1/2 ... ... Done\n"
  339. "Loading: file2 -- 2/2 ... ... Done",
  340. "Issue import report",
  341. "bar@pingou.com",
  342. )
  343. ]
  344. self.assertEqual(calls, send.mock_calls)
  345. @patch("json.loads")
  346. @patch("pagure.lib.notify.send_email")
  347. @patch("pagure.lib.git.update_request_from_git")
  348. @patch("pagure.lib.git.update_ticket_from_git")
  349. @patch("pagure.lib.git.read_git_lines")
  350. def test_load_json_commits_to_db_prs_raises_error(
  351. self, git, up_issue, up_pr, send, json_loads
  352. ):
  353. """ Test the load_json_commits_to_db method. """
  354. git.side_effect = [["file1"], ["file2"], ["files/image"], ["file1"]]
  355. json_loads.return_value = "foobar"
  356. up_pr.side_effect = Exception("foo error")
  357. output = pagure.lib.tasks_services.load_json_commits_to_db(
  358. name="test",
  359. commits=["hash1", "hash2"],
  360. abspath=self.path,
  361. data_type="pull-request",
  362. agent="pingou",
  363. namespace=None,
  364. username=None,
  365. )
  366. self.assertIsNone(output)
  367. calls = [
  368. call(
  369. ANY,
  370. json_data="foobar",
  371. namespace=None,
  372. reponame="test",
  373. request_uid="file1",
  374. username=None,
  375. )
  376. ]
  377. up_issue.assert_not_called()
  378. self.assertEqual(calls, up_pr.mock_calls)
  379. calls = [
  380. call(
  381. "Good Morning\n\n"
  382. "This is the log of loading all the files pushed in the git "
  383. "repo into\n"
  384. "the database. It should ignore files that are not JSON files,"
  385. " this\nis fine.\n\n"
  386. "Loading: file1 -- 1/2 ... ... FAILED\n",
  387. "Issue import report",
  388. "bar@pingou.com",
  389. )
  390. ]
  391. self.assertEqual(calls, send.mock_calls)
  392. class PagureLibTaskServicesWithWebHooktests(tests.Modeltests):
  393. """ Tests for pagure.lib.task_services """
  394. maxDiff = None
  395. def setUp(self):
  396. """ Set up the environnment, ran before every tests. """
  397. super(PagureLibTaskServicesWithWebHooktests, self).setUp()
  398. pagure.config.config["REQUESTS_FOLDER"] = None
  399. self.sshkeydir = os.path.join(self.path, "sshkeys")
  400. pagure.config.config["MIRROR_SSHKEYS_FOLDER"] = self.sshkeydir
  401. tests.create_projects(self.session)
  402. project = pagure.lib.query._get_project(self.session, "test")
  403. settings = project.settings
  404. settings["Web-hooks"] = "http://foo.com/api/flag\nhttp://bar.org/bar"
  405. project.settings = settings
  406. self.session.add(project)
  407. self.session.commit()
  408. @patch("pagure.lib.tasks_services.call_web_hooks")
  409. def test_webhook_notification_no_webhook(self, call_wh):
  410. """ Test the webhook_notification method. """
  411. output = pagure.lib.tasks_services.webhook_notification(
  412. topic="topic",
  413. msg={"payload": ["a", "b", "c"]},
  414. namespace=None,
  415. name="test",
  416. user=None,
  417. )
  418. self.assertIsNone(output)
  419. project = pagure.lib.query._get_project(self.session, "test")
  420. call_wh.assert_called_once_with(
  421. ANY,
  422. "topic",
  423. {"payload": ["a", "b", "c"]},
  424. ["http://foo.com/api/flag", "http://bar.org/bar"],
  425. )
  426. @patch("time.time", MagicMock(return_value=2))
  427. @patch("uuid.uuid4", MagicMock(return_value="not_so_random"))
  428. @patch("datetime.datetime")
  429. @patch("requests.post")
  430. def test_webhook_notification_no_webhook(self, post, dt):
  431. """ Test the webhook_notification method. """
  432. post.return_value = False
  433. utcnow = MagicMock()
  434. utcnow.year = 2018
  435. dt.utcnow.return_value = utcnow
  436. output = pagure.lib.tasks_services.webhook_notification(
  437. topic="topic",
  438. msg={"payload": ["a", "b", "c"]},
  439. namespace=None,
  440. name="test",
  441. user=None,
  442. )
  443. self.assertIsNone(output)
  444. project = pagure.lib.query._get_project(self.session, "test")
  445. self.assertEqual(post.call_count, 2)
  446. calls = [
  447. call(
  448. "http://bar.org/bar",
  449. data="{"
  450. '"i": 1, '
  451. '"msg": {'
  452. '"pagure_instance": "http://localhost.localdomain/", '
  453. '"payload": ["a", "b", "c"], '
  454. '"project_fullname": "test"}, '
  455. '"msg_id": "2018-not_so_random", '
  456. '"timestamp": 2, '
  457. '"topic": "topic"}',
  458. headers={
  459. "X-Pagure": "http://localhost.localdomain/",
  460. "X-Pagure-project": "test",
  461. "X-Pagure-Signature": "74b12f0b25bf7767014a0c0de9f3c10"
  462. "191e943d8",
  463. "X-Pagure-Signature-256": "f3d757796554466eac49a5282b2"
  464. "4ee32a1ecfb65dedd6c6231fb207240a9fe58",
  465. "X-Pagure-Topic": b"topic",
  466. "Content-Type": "application/json",
  467. },
  468. timeout=60,
  469. ),
  470. call(
  471. "http://foo.com/api/flag",
  472. data="{"
  473. '"i": 1, '
  474. '"msg": {'
  475. '"pagure_instance": "http://localhost.localdomain/", '
  476. '"payload": ["a", "b", "c"], '
  477. '"project_fullname": "test"}, '
  478. '"msg_id": "2018-not_so_random", '
  479. '"timestamp": 2, '
  480. '"topic": "topic"}',
  481. headers={
  482. "X-Pagure": "http://localhost.localdomain/",
  483. "X-Pagure-project": "test",
  484. "X-Pagure-Signature": "74b12f0b25bf7767014a0c0de9f3c10"
  485. "191e943d8",
  486. "X-Pagure-Signature-256": "f3d757796554466eac49a5282b2"
  487. "4ee32a1ecfb65dedd6c6231fb207240a9fe58",
  488. "X-Pagure-Topic": b"topic",
  489. "Content-Type": "application/json",
  490. },
  491. timeout=60,
  492. ),
  493. ]
  494. print(post.mock_calls)
  495. self.assertEqual(calls, post.mock_calls)
  496. class PagureLibTaskServicesJenkinsCItests(tests.Modeltests):
  497. """ Tests for pagure.lib.task_services """
  498. maxDiff = None
  499. def setUp(self):
  500. """ Set up the environnment, ran before every tests. """
  501. super(PagureLibTaskServicesJenkinsCItests, self).setUp()
  502. pagure.config.config["REQUESTS_FOLDER"] = None
  503. self.sshkeydir = os.path.join(self.path, "sshkeys")
  504. pagure.config.config["MIRROR_SSHKEYS_FOLDER"] = self.sshkeydir
  505. tests.create_projects(self.session)
  506. project = pagure.lib.query.get_authorized_project(self.session, "test")
  507. # Install the plugin at the DB level
  508. plugin = pagure.lib.plugins.get_plugin("Pagure CI")
  509. dbobj = plugin.db_object()
  510. dbobj.ci_url = "https://ci.server.org/"
  511. dbobj.ci_job = "pagure"
  512. dbobj.pagure_ci_token = "random_token"
  513. dbobj.project_id = project.id
  514. self.session.add(dbobj)
  515. self.session.commit()
  516. # Create a fork of test for foo
  517. item = pagure.lib.model.Project(
  518. user_id=2, # foo
  519. name="test",
  520. is_fork=True,
  521. parent_id=1,
  522. description="test project #1",
  523. hook_token="aaabbbccc_foo",
  524. )
  525. item.close_status = [
  526. "Invalid",
  527. "Insufficient data",
  528. "Fixed",
  529. "Duplicate",
  530. ]
  531. self.session.add(item)
  532. self.session.commit()
  533. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  534. def test_trigger_ci_build_invalid_ci(self, trigger_jenk):
  535. """ Test the trigger_ci_build method. """
  536. output = pagure.lib.tasks_services.trigger_ci_build(
  537. project_name="test",
  538. cause="PR#ID",
  539. branch="feature",
  540. branch_to="master",
  541. ci_type="travis",
  542. )
  543. self.assertIsNone(output)
  544. trigger_jenk.assert_not_called()
  545. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  546. def test_trigger_ci_build_invalid_ci_fork(self, trigger_jenk):
  547. """ Test the trigger_ci_build method. """
  548. output = pagure.lib.tasks_services.trigger_ci_build(
  549. project_name="forks/foo/test",
  550. cause="PR#ID",
  551. branch="feature",
  552. branch_to="master",
  553. ci_type="travis",
  554. )
  555. self.assertIsNone(output)
  556. trigger_jenk.assert_not_called()
  557. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  558. def test_trigger_ci_build_valid_project(self, trigger_jenk):
  559. """ Test the trigger_ci_build method. """
  560. output = pagure.lib.tasks_services.trigger_ci_build(
  561. project_name="test",
  562. cause="PR#ID",
  563. branch="feature",
  564. branch_to="master",
  565. ci_type="jenkins",
  566. )
  567. self.assertIsNone(output)
  568. trigger_jenk.assert_called_once_with(
  569. branch="feature",
  570. cause="PR#ID",
  571. ci_password=None,
  572. ci_username=None,
  573. job="pagure",
  574. project_path="test.git",
  575. token="random_token",
  576. url="https://ci.server.org/",
  577. branch_to="master",
  578. )
  579. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  580. def test_trigger_ci_build_valid_project_fork(self, trigger_jenk):
  581. """ Test the trigger_ci_build method. """
  582. output = pagure.lib.tasks_services.trigger_ci_build(
  583. project_name="forks/foo/test",
  584. cause="PR#ID",
  585. branch="feature",
  586. branch_to="master",
  587. ci_type="jenkins",
  588. )
  589. self.assertIsNone(output)
  590. trigger_jenk.assert_called_once_with(
  591. branch="feature",
  592. cause="PR#ID",
  593. ci_password=None,
  594. ci_username=None,
  595. job="pagure",
  596. project_path="forks/foo/test.git",
  597. token="random_token",
  598. url="https://ci.server.org/",
  599. branch_to="master",
  600. )
  601. class PagureLibTaskServicesJenkinsCIAuthtests(tests.Modeltests):
  602. """ Tests for pagure.lib.task_services """
  603. maxDiff = None
  604. def setUp(self):
  605. """ Set up the environnment, ran before every tests. """
  606. super(PagureLibTaskServicesJenkinsCIAuthtests, self).setUp()
  607. pagure.config.config["REQUESTS_FOLDER"] = None
  608. self.sshkeydir = os.path.join(self.path, "sshkeys")
  609. pagure.config.config["MIRROR_SSHKEYS_FOLDER"] = self.sshkeydir
  610. tests.create_projects(self.session)
  611. project = pagure.lib.query.get_authorized_project(self.session, "test")
  612. # Install the plugin at the DB level
  613. plugin = pagure.lib.plugins.get_plugin("Pagure CI")
  614. dbobj = plugin.db_object()
  615. dbobj.ci_url = "https://ci.server.org/"
  616. dbobj.ci_job = "pagure"
  617. dbobj.ci_username = "jenkins_username"
  618. dbobj.ci_password = "jenkins_password"
  619. dbobj.pagure_ci_token = "random_token"
  620. dbobj.project_id = project.id
  621. self.session.add(dbobj)
  622. self.session.commit()
  623. # Create a fork of test for foo
  624. item = pagure.lib.model.Project(
  625. user_id=2, # foo
  626. name="test",
  627. is_fork=True,
  628. parent_id=1,
  629. description="test project #1",
  630. hook_token="aaabbbccc_foo",
  631. )
  632. item.close_status = [
  633. "Invalid",
  634. "Insufficient data",
  635. "Fixed",
  636. "Duplicate",
  637. ]
  638. self.session.add(item)
  639. self.session.commit()
  640. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  641. def test_trigger_ci_build_invalid_ci(self, trigger_jenk):
  642. """ Test the trigger_ci_build method. """
  643. output = pagure.lib.tasks_services.trigger_ci_build(
  644. project_name="test",
  645. cause="PR#ID",
  646. branch="feature",
  647. branch_to="master",
  648. ci_type="travis",
  649. )
  650. self.assertIsNone(output)
  651. trigger_jenk.assert_not_called()
  652. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  653. def test_trigger_ci_build_invalid_ci_fork(self, trigger_jenk):
  654. """ Test the trigger_ci_build method. """
  655. output = pagure.lib.tasks_services.trigger_ci_build(
  656. project_name="forks/foo/test",
  657. cause="PR#ID",
  658. branch="feature",
  659. branch_to="master",
  660. ci_type="travis",
  661. )
  662. self.assertIsNone(output)
  663. trigger_jenk.assert_not_called()
  664. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  665. def test_trigger_ci_build_valid_project(self, trigger_jenk):
  666. """ Test the trigger_ci_build method. """
  667. output = pagure.lib.tasks_services.trigger_ci_build(
  668. project_name="test",
  669. cause="PR#ID",
  670. branch="feature",
  671. branch_to="master",
  672. ci_type="jenkins",
  673. )
  674. self.assertIsNone(output)
  675. trigger_jenk.assert_called_once_with(
  676. branch="feature",
  677. cause="PR#ID",
  678. ci_password="jenkins_password",
  679. ci_username="jenkins_username",
  680. job="pagure",
  681. project_path="test.git",
  682. token="random_token",
  683. url="https://ci.server.org/",
  684. branch_to="master",
  685. )
  686. @patch("pagure.lib.tasks_services.trigger_jenkins_build")
  687. def test_trigger_ci_build_valid_project_fork(self, trigger_jenk):
  688. """ Test the trigger_ci_build method. """
  689. output = pagure.lib.tasks_services.trigger_ci_build(
  690. project_name="forks/foo/test",
  691. cause="PR#ID",
  692. branch="feature",
  693. branch_to="master",
  694. ci_type="jenkins",
  695. )
  696. self.assertIsNone(output)
  697. trigger_jenk.assert_called_once_with(
  698. branch="feature",
  699. cause="PR#ID",
  700. ci_password="jenkins_password",
  701. ci_username="jenkins_username",
  702. job="pagure",
  703. project_path="forks/foo/test.git",
  704. token="random_token",
  705. url="https://ci.server.org/",
  706. branch_to="master",
  707. )
  708. class PagureLibTaskServicesLoadJsonTickettests(tests.Modeltests):
  709. """ Tests for pagure.lib.task_services """
  710. maxDiff = None
  711. def setUp(self):
  712. """ Set up the environnment, ran before every tests. """
  713. super(PagureLibTaskServicesLoadJsonTickettests, self).setUp()
  714. tests.create_projects(self.session)
  715. self.gitrepo = os.path.join(self.path, "repos", "tickets", "test.git")
  716. repopath = os.path.join(self.path, "repos", "tickets")
  717. os.makedirs(self.gitrepo)
  718. self.repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  719. project = pagure.lib.query.get_authorized_project(self.session, "test")
  720. # Create an issue to play with
  721. msg = pagure.lib.query.new_issue(
  722. session=self.session,
  723. repo=project,
  724. title="Test issue",
  725. content="We should work on this",
  726. user="pingou",
  727. )
  728. self.assertEqual(msg.title, "Test issue")
  729. issue = pagure.lib.query.search_issues(
  730. self.session, project, issueid=1
  731. )
  732. # Add a couple of comment on the ticket
  733. msg = pagure.lib.query.add_issue_comment(
  734. session=self.session,
  735. issue=issue,
  736. comment="Hey look a comment!",
  737. user="foo",
  738. )
  739. self.session.commit()
  740. self.assertEqual(msg, "Comment added")
  741. commits = [
  742. commit
  743. for commit in self.repo_obj.walk(
  744. self.repo_obj.head.target, pygit2.GIT_SORT_NONE
  745. )
  746. ]
  747. # 2 commits: creation - new comment
  748. self.assertEqual(len(commits), 2)
  749. issue = pagure.lib.query.search_issues(
  750. self.session, project, issueid=1
  751. )
  752. self.assertEqual(len(issue.comments), 1)
  753. @patch("pagure.lib.notify.send_email")
  754. @patch("pagure.lib.git.update_request_from_git")
  755. def test_loading_issue_json(self, up_pr, send):
  756. """ Test loading the JSON file of a ticket. """
  757. project = pagure.lib.query.get_authorized_project(self.session, "test")
  758. issue = pagure.lib.query.search_issues(
  759. self.session, project, issueid=1
  760. )
  761. commits = [
  762. commit.oid.hex
  763. for commit in self.repo_obj.walk(
  764. self.repo_obj.head.target, pygit2.GIT_SORT_NONE
  765. )
  766. ]
  767. output = pagure.lib.tasks_services.load_json_commits_to_db(
  768. name="test",
  769. commits=commits,
  770. abspath=self.gitrepo,
  771. data_type="ticket",
  772. agent="pingou",
  773. namespace=None,
  774. username=None,
  775. )
  776. self.assertIsNone(output)
  777. up_pr.assert_not_called()
  778. calls = [
  779. call(
  780. "Good Morning\n\n"
  781. "This is the log of loading all the files pushed in the git "
  782. "repo into\n"
  783. "the database. It should ignore files that are not JSON files,"
  784. " this\nis fine.\n\n"
  785. "Loading: %s -- 1/1 ... ... Done" % issue.uid,
  786. "Issue import report",
  787. "bar@pingou.com",
  788. )
  789. ]
  790. self.assertEqual(calls, send.mock_calls)
  791. project = pagure.lib.query.get_authorized_project(self.session, "test")
  792. issue = pagure.lib.query.search_issues(
  793. self.session, project, issueid=1
  794. )
  795. self.assertEqual(len(issue.comments), 1)
  796. if __name__ == "__main__":
  797. unittest.main(verbosity=2)