test_pagure_lib_task_services.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib.tasks_services
  21. import pagure.lib.query
  22. import tests
  23. import pagure.lib.tasks_services
  24. class PagureLibTaskServicestests(tests.Modeltests):
  25. """ Tests for pagure.lib.task_services """
  26. maxDiff = None
  27. def setUp(self):
  28. """ Set up the environnment, ran before every tests. """
  29. super(PagureLibTaskServicestests, self).setUp()
  30. tests.create_projects(self.session)
  31. # Create a fork of test for foo
  32. item = pagure.lib.model.Project(
  33. user_id=2, # foo
  34. name='test',
  35. is_fork=True,
  36. parent_id=1,
  37. description='test project #1',
  38. hook_token='aaabbbccc_foo',
  39. )
  40. item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  41. self.session.add(item)
  42. self.session.commit()
  43. def test_webhook_notification_invalid_project(self):
  44. """ Test the webhook_notification method. """
  45. self.assertRaises(
  46. RuntimeError,
  47. pagure.lib.tasks_services.webhook_notification,
  48. topic='topic',
  49. msg={'payload': ['a', 'b', 'c']},
  50. namespace=None,
  51. name='invalid',
  52. user=None)
  53. @patch('pagure.lib.tasks_services.call_web_hooks')
  54. def test_webhook_notification_no_webhook(self, call_wh):
  55. """ Test the webhook_notification method. """
  56. output = pagure.lib.tasks_services.webhook_notification(
  57. topic='topic',
  58. msg={'payload': ['a', 'b', 'c']},
  59. namespace=None,
  60. name='test',
  61. user=None)
  62. self.assertIsNone(output)
  63. call_wh.assert_not_called()
  64. @patch('pagure.lib.git.log_commits_to_db')
  65. def test_log_commit_send_notifications_invalid_project(self, log):
  66. """ Test the log_commit_send_notifications method. """
  67. output = pagure.lib.tasks_services.log_commit_send_notifications(
  68. name='invalid',
  69. commits=[],
  70. abspath=None,
  71. branch=None,
  72. default_branch=None,
  73. namespace=None,
  74. username=None)
  75. self.assertIsNone(output)
  76. log.assert_not_called()
  77. @patch('pagure.lib.notify.notify_new_commits')
  78. @patch('pagure.lib.git.log_commits_to_db')
  79. def test_log_commit_send_notifications_valid_project(self, log, notif):
  80. """ Test the log_commit_send_notifications method. """
  81. output = pagure.lib.tasks_services.log_commit_send_notifications(
  82. name='test',
  83. commits=['hash1', 'hash2'],
  84. abspath='/path/to/git',
  85. branch='master',
  86. default_branch='master',
  87. namespace=None,
  88. username=None)
  89. self.assertIsNone(output)
  90. log.assert_called_once_with(
  91. ANY, ANY, ['hash1', 'hash2'], '/path/to/git'
  92. )
  93. notif.assert_called_once_with(
  94. '/path/to/git', ANY, 'master', ['hash1', 'hash2']
  95. )
  96. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  97. def test_trigger_ci_build_invalid_project(self, trigger_jenk):
  98. """ Test the trigger_ci_build method. """
  99. output = pagure.lib.tasks_services.trigger_ci_build(
  100. project_name='invalid',
  101. cause='PR#ID',
  102. branch='feature',
  103. ci_type='jenkins')
  104. self.assertIsNone(output)
  105. trigger_jenk.assert_not_called()
  106. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  107. def test_trigger_ci_build_not_configured_project(self, trigger_jenk):
  108. """ Test the trigger_ci_build method. """
  109. self.assertRaises(
  110. pagure.exceptions.PagureException,
  111. pagure.lib.tasks_services.trigger_ci_build,
  112. project_name='test',
  113. cause='PR#ID',
  114. branch='feature',
  115. ci_type='jenkins')
  116. trigger_jenk.assert_not_called()
  117. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  118. def test_trigger_ci_build_not_configured_project_fork(self, trigger_jenk):
  119. """ Test the trigger_ci_build method. """
  120. self.assertRaises(
  121. pagure.exceptions.PagureException,
  122. pagure.lib.tasks_services.trigger_ci_build,
  123. project_name='forks/foo/test',
  124. cause='PR#ID',
  125. branch='feature',
  126. ci_type='jenkins')
  127. trigger_jenk.assert_not_called()
  128. @patch('pagure.lib.query._get_project')
  129. def test_load_json_commits_to_db_invalid_data_type(self, get_project):
  130. """ Test the load_json_commits_to_db method. """
  131. output = pagure.lib.tasks_services.load_json_commits_to_db(
  132. name='test',
  133. commits=['hash1', 'hash2'],
  134. abspath='/path/to/git',
  135. data_type='invalid',
  136. agent='pingou',
  137. namespace=None,
  138. username=None)
  139. self.assertIsNone(output)
  140. get_project.assert_not_called()
  141. @patch('pagure.lib.tasks_services.get_files_to_load')
  142. def test_load_json_commits_to_db_invalid_project(self, get_files):
  143. """ Test the load_json_commits_to_db method. """
  144. output = pagure.lib.tasks_services.load_json_commits_to_db(
  145. name='invalid',
  146. commits=['hash1', 'hash2'],
  147. abspath='/path/to/git',
  148. data_type='ticket',
  149. agent='pingou',
  150. namespace=None,
  151. username=None)
  152. self.assertIsNone(output)
  153. get_files.assert_not_called()
  154. @patch('pagure.lib.git.update_request_from_git')
  155. @patch('pagure.lib.git.update_ticket_from_git')
  156. def test_load_json_commits_to_db_invalid_path(self, up_issue, up_pr):
  157. """ Test the load_json_commits_to_db method. """
  158. output = pagure.lib.tasks_services.load_json_commits_to_db(
  159. name='test',
  160. commits=['hash1', 'hash2'],
  161. abspath=self.path,
  162. data_type='ticket',
  163. agent='pingou',
  164. namespace=None,
  165. username=None)
  166. self.assertIsNone(output)
  167. up_issue.assert_not_called()
  168. up_pr.assert_not_called()
  169. @patch('pagure.lib.git.update_request_from_git')
  170. @patch('pagure.lib.git.update_ticket_from_git')
  171. def test_load_json_commits_to_db_invalid_path_one_commit(self, up_issue, up_pr):
  172. """ Test the load_json_commits_to_db method. """
  173. output = pagure.lib.tasks_services.load_json_commits_to_db(
  174. name='test',
  175. commits=['hash1'],
  176. abspath=self.path,
  177. data_type='ticket',
  178. agent='pingou',
  179. namespace=None,
  180. username=None)
  181. self.assertIsNone(output)
  182. up_issue.assert_not_called()
  183. up_pr.assert_not_called()
  184. @patch('pagure.lib.notify.send_email')
  185. @patch('pagure.lib.git.update_request_from_git')
  186. @patch('pagure.lib.git.update_ticket_from_git')
  187. def test_load_json_commits_to_db_no_agent(self, up_issue, up_pr, send):
  188. """ Test the load_json_commits_to_db method. """
  189. output = pagure.lib.tasks_services.load_json_commits_to_db(
  190. name='test',
  191. commits=[],
  192. abspath=None,
  193. data_type='ticket',
  194. agent=None,
  195. namespace=None,
  196. username=None)
  197. self.assertIsNone(output)
  198. up_issue.assert_not_called()
  199. up_pr.assert_not_called()
  200. send.assert_not_called()
  201. @patch('pagure.lib.notify.send_email')
  202. @patch('pagure.lib.git.update_request_from_git')
  203. @patch('pagure.lib.git.update_ticket_from_git')
  204. @patch('pagure.lib.git.read_git_lines')
  205. def test_load_json_commits_to_db_no_agent(
  206. self, git, up_issue, up_pr, send):
  207. """ Test the load_json_commits_to_db method. """
  208. git.side_effect = [
  209. ['file1'], ['file2'], ['files/image'], ['file1']]
  210. output = pagure.lib.tasks_services.load_json_commits_to_db(
  211. name='test',
  212. commits=['hash1', 'hash2'],
  213. abspath=self.path,
  214. data_type='ticket',
  215. agent=None,
  216. namespace=None,
  217. username=None)
  218. self.assertIsNone(output)
  219. up_issue.assert_not_called()
  220. up_pr.assert_not_called()
  221. send.assert_not_called()
  222. @patch('json.loads')
  223. @patch('pagure.lib.notify.send_email')
  224. @patch('pagure.lib.git.update_request_from_git')
  225. @patch('pagure.lib.git.update_ticket_from_git')
  226. @patch('pagure.lib.git.read_git_lines')
  227. def test_load_json_commits_to_db_tickets(
  228. self, git, up_issue, up_pr, send, json_loads):
  229. """ Test the load_json_commits_to_db method. """
  230. git.side_effect = [
  231. ['file1'], ['file2'], ['files/image'], ['file1']]
  232. json_loads.return_value = 'foobar'
  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. self.assertIsNone(output)
  242. calls = [
  243. call(
  244. ANY, agent=None, issue_uid=u'file1', json_data=u'foobar',
  245. namespace=None, reponame=u'test', username=None
  246. ),
  247. call(
  248. ANY, agent=None, issue_uid=u'file2', json_data=u'foobar',
  249. namespace=None, reponame=u'test', username=None
  250. ),
  251. ]
  252. self.assertEqual(
  253. calls,
  254. up_issue.mock_calls
  255. )
  256. up_pr.assert_not_called()
  257. send.assert_not_called()
  258. @patch('json.loads')
  259. @patch('pagure.lib.notify.send_email')
  260. @patch('pagure.lib.git.update_request_from_git')
  261. @patch('pagure.lib.git.update_ticket_from_git')
  262. @patch('pagure.lib.git.read_git_lines')
  263. def test_load_json_commits_to_db_prs(
  264. self, git, up_issue, up_pr, send, json_loads):
  265. """ Test the load_json_commits_to_db method. """
  266. git.side_effect = [
  267. ['file1'], ['file2'], ['files/image'], ['file1']]
  268. json_loads.return_value = 'foobar'
  269. output = pagure.lib.tasks_services.load_json_commits_to_db(
  270. name='test',
  271. commits=['hash1', 'hash2'],
  272. abspath=self.path,
  273. data_type='pull-request',
  274. agent='pingou',
  275. namespace=None,
  276. username=None)
  277. self.assertIsNone(output)
  278. calls = [
  279. call(
  280. ANY, json_data=u'foobar', namespace=None, reponame=u'test',
  281. request_uid=u'file1', username=None
  282. ),
  283. call(
  284. ANY, json_data=u'foobar', namespace=None, reponame=u'test',
  285. request_uid=u'file2', username=None
  286. ),
  287. ]
  288. up_issue.assert_not_called()
  289. self.assertEqual(
  290. calls,
  291. up_pr.mock_calls
  292. )
  293. calls = [
  294. call(
  295. u'Loading: file1 -- 1/2 ... ... Done\n'
  296. u'Loading: file2 -- 2/2 ... ... Done',
  297. u'Issue import report',
  298. u'bar@pingou.com'
  299. )
  300. ]
  301. self.assertEqual(
  302. calls,
  303. send.mock_calls
  304. )
  305. @patch('json.loads')
  306. @patch('pagure.lib.notify.send_email')
  307. @patch('pagure.lib.git.update_request_from_git')
  308. @patch('pagure.lib.git.update_ticket_from_git')
  309. @patch('pagure.lib.git.read_git_lines')
  310. def test_load_json_commits_to_db_prs_raises_error(
  311. self, git, up_issue, up_pr, send, json_loads):
  312. """ Test the load_json_commits_to_db method. """
  313. git.side_effect = [
  314. ['file1'], ['file2'], ['files/image'], ['file1']]
  315. json_loads.return_value = 'foobar'
  316. up_pr.side_effect = Exception('foo error')
  317. output = pagure.lib.tasks_services.load_json_commits_to_db(
  318. name='test',
  319. commits=['hash1', 'hash2'],
  320. abspath=self.path,
  321. data_type='pull-request',
  322. agent='pingou',
  323. namespace=None,
  324. username=None)
  325. self.assertIsNone(output)
  326. calls = [
  327. call(
  328. ANY, json_data=u'foobar', namespace=None, reponame=u'test',
  329. request_uid=u'file1', username=None
  330. )
  331. ]
  332. up_issue.assert_not_called()
  333. self.assertEqual(
  334. calls,
  335. up_pr.mock_calls
  336. )
  337. calls = [
  338. call(
  339. u'Loading: file1 -- 1/2 ... ... FAILED\n',
  340. u'Issue import report',
  341. u'bar@pingou.com'
  342. )
  343. ]
  344. self.assertEqual(
  345. calls,
  346. send.mock_calls
  347. )
  348. class PagureLibTaskServicesWithWebHooktests(tests.Modeltests):
  349. """ Tests for pagure.lib.task_services """
  350. maxDiff = None
  351. def setUp(self):
  352. """ Set up the environnment, ran before every tests. """
  353. super(PagureLibTaskServicesWithWebHooktests, self).setUp()
  354. pagure.config.config['REQUESTS_FOLDER'] = None
  355. self.sshkeydir = os.path.join(self.path, 'sshkeys')
  356. pagure.config.config['MIRROR_SSHKEYS_FOLDER'] = self.sshkeydir
  357. tests.create_projects(self.session)
  358. project = pagure.lib.query._get_project(self.session, 'test')
  359. settings = project.settings
  360. settings['Web-hooks'] = 'http://foo.com/api/flag\nhttp://bar.org/bar'
  361. project.settings = settings
  362. self.session.add(project)
  363. self.session.commit()
  364. @patch('pagure.lib.tasks_services.call_web_hooks')
  365. def test_webhook_notification_no_webhook(self, call_wh):
  366. """ Test the webhook_notification method. """
  367. output = pagure.lib.tasks_services.webhook_notification(
  368. topic='topic',
  369. msg={'payload': ['a', 'b', 'c']},
  370. namespace=None,
  371. name='test',
  372. user=None)
  373. self.assertIsNone(output)
  374. project = pagure.lib.query._get_project(self.session, 'test')
  375. call_wh.assert_called_once_with(
  376. ANY, u'topic', {u'payload': [u'a', u'b', u'c']},
  377. [u'http://foo.com/api/flag', u'http://bar.org/bar']
  378. )
  379. @patch('time.time', MagicMock(return_value=2))
  380. @patch('uuid.uuid4', MagicMock(return_value='not_so_random'))
  381. @patch('datetime.datetime')
  382. @patch('requests.post')
  383. def test_webhook_notification_no_webhook(self, post, dt):
  384. """ Test the webhook_notification method. """
  385. post.return_value = False
  386. utcnow = MagicMock()
  387. utcnow.year = 2018
  388. dt.utcnow.return_value = utcnow
  389. output = pagure.lib.tasks_services.webhook_notification(
  390. topic='topic',
  391. msg={'payload': ['a', 'b', 'c']},
  392. namespace=None,
  393. name='test',
  394. user=None)
  395. self.assertIsNone(output)
  396. project = pagure.lib.query._get_project(self.session, 'test')
  397. self.assertEqual(post.call_count, 2)
  398. calls = [
  399. call(
  400. 'http://bar.org/bar',
  401. data='{'
  402. '"i": 1, '
  403. '"msg": {'
  404. '"pagure_instance": "http://localhost.localdomain/", '
  405. '"payload": ["a", "b", "c"], '
  406. '"project_fullname": "test"}, '
  407. '"msg_id": "2018-not_so_random", '
  408. '"timestamp": 2, '
  409. '"topic": "topic"}'
  410. ,
  411. headers={
  412. 'X-Pagure': 'http://localhost.localdomain/',
  413. 'X-Pagure-project': 'test',
  414. 'X-Pagure-Signature': '74b12f0b25bf7767014a0c0de9f3c10'
  415. '191e943d8',
  416. 'X-Pagure-Signature-256': 'f3d757796554466eac49a5282b2'
  417. '4ee32a1ecfb65dedd6c6231fb207240a9fe58',
  418. 'X-Pagure-Topic': b'topic',
  419. 'Content-Type': 'application/json'
  420. },
  421. timeout=60
  422. ),
  423. call(
  424. 'http://foo.com/api/flag',
  425. data='{'
  426. '"i": 1, '
  427. '"msg": {'
  428. '"pagure_instance": "http://localhost.localdomain/", '
  429. '"payload": ["a", "b", "c"], '
  430. '"project_fullname": "test"}, '
  431. '"msg_id": "2018-not_so_random", '
  432. '"timestamp": 2, '
  433. '"topic": "topic"}'
  434. ,
  435. headers={
  436. 'X-Pagure': 'http://localhost.localdomain/',
  437. 'X-Pagure-project': 'test',
  438. 'X-Pagure-Signature': '74b12f0b25bf7767014a0c0de9f3c10'
  439. '191e943d8',
  440. 'X-Pagure-Signature-256': 'f3d757796554466eac49a5282b2'
  441. '4ee32a1ecfb65dedd6c6231fb207240a9fe58',
  442. 'X-Pagure-Topic': b'topic',
  443. 'Content-Type': 'application/json'
  444. },
  445. timeout=60
  446. )
  447. ]
  448. print(post.mock_calls)
  449. self.assertEqual(
  450. calls,
  451. post.mock_calls
  452. )
  453. class PagureLibTaskServicesJenkinsCItests(tests.Modeltests):
  454. """ Tests for pagure.lib.task_services """
  455. maxDiff = None
  456. def setUp(self):
  457. """ Set up the environnment, ran before every tests. """
  458. super(PagureLibTaskServicesJenkinsCItests, self).setUp()
  459. pagure.config.config['REQUESTS_FOLDER'] = None
  460. self.sshkeydir = os.path.join(self.path, 'sshkeys')
  461. pagure.config.config['MIRROR_SSHKEYS_FOLDER'] = self.sshkeydir
  462. tests.create_projects(self.session)
  463. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  464. # Install the plugin at the DB level
  465. plugin = pagure.lib.plugins.get_plugin('Pagure CI')
  466. dbobj = plugin.db_object()
  467. dbobj.ci_url = 'https://ci.server.org/'
  468. dbobj.ci_job = 'pagure'
  469. dbobj.pagure_ci_token = 'random_token'
  470. dbobj.project_id = project.id
  471. self.session.add(dbobj)
  472. self.session.commit()
  473. # Create a fork of test for foo
  474. item = pagure.lib.model.Project(
  475. user_id=2, # foo
  476. name='test',
  477. is_fork=True,
  478. parent_id=1,
  479. description='test project #1',
  480. hook_token='aaabbbccc_foo',
  481. )
  482. item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  483. self.session.add(item)
  484. self.session.commit()
  485. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  486. def test_trigger_ci_build_invalid_ci(self, trigger_jenk):
  487. """ Test the trigger_ci_build method. """
  488. output = pagure.lib.tasks_services.trigger_ci_build(
  489. project_name='test',
  490. cause='PR#ID',
  491. branch='feature',
  492. ci_type='travis')
  493. self.assertIsNone(output)
  494. trigger_jenk.assert_not_called()
  495. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  496. def test_trigger_ci_build_invalid_ci_fork(self, trigger_jenk):
  497. """ Test the trigger_ci_build method. """
  498. output = pagure.lib.tasks_services.trigger_ci_build(
  499. project_name='forks/foo/test',
  500. cause='PR#ID',
  501. branch='feature',
  502. ci_type='travis')
  503. self.assertIsNone(output)
  504. trigger_jenk.assert_not_called()
  505. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  506. def test_trigger_ci_build_valid_project(self, trigger_jenk):
  507. """ Test the trigger_ci_build method. """
  508. output = pagure.lib.tasks_services.trigger_ci_build(
  509. project_name='test',
  510. cause='PR#ID',
  511. branch='feature',
  512. ci_type='jenkins')
  513. self.assertIsNone(output)
  514. trigger_jenk.assert_called_once_with(
  515. branch=u'feature',
  516. cause=u'PR#ID',
  517. job=u'pagure',
  518. project_path=u'test.git',
  519. token=u'random_token',
  520. url=u'https://ci.server.org/'
  521. )
  522. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  523. def test_trigger_ci_build_valid_project_fork(self, trigger_jenk):
  524. """ Test the trigger_ci_build method. """
  525. output = pagure.lib.tasks_services.trigger_ci_build(
  526. project_name='forks/foo/test',
  527. cause='PR#ID',
  528. branch='feature',
  529. ci_type='jenkins')
  530. self.assertIsNone(output)
  531. trigger_jenk.assert_called_once_with(
  532. branch=u'feature',
  533. cause=u'PR#ID',
  534. job=u'pagure',
  535. project_path=u'forks/foo/test.git',
  536. token=u'random_token',
  537. url=u'https://ci.server.org/'
  538. )
  539. class PagureLibTaskServicesLoadJsonTickettests(tests.Modeltests):
  540. """ Tests for pagure.lib.task_services """
  541. maxDiff = None
  542. def setUp(self):
  543. """ Set up the environnment, ran before every tests. """
  544. super(PagureLibTaskServicesLoadJsonTickettests, self).setUp()
  545. tests.create_projects(self.session)
  546. self.gitrepo = os.path.join(self.path, 'repos', 'tickets', 'test.git')
  547. repopath = os.path.join(self.path, 'repos', 'tickets')
  548. os.makedirs(self.gitrepo)
  549. self.repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  550. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  551. # Create an issue to play with
  552. msg = pagure.lib.query.new_issue(
  553. session=self.session,
  554. repo=project,
  555. title='Test issue',
  556. content='We should work on this',
  557. user='pingou',
  558. )
  559. self.assertEqual(msg.title, 'Test issue')
  560. issue = pagure.lib.query.search_issues(self.session, project, issueid=1)
  561. # Add a couple of comment on the ticket
  562. msg = pagure.lib.query.add_issue_comment(
  563. session=self.session,
  564. issue=issue,
  565. comment='Hey look a comment!',
  566. user='foo',
  567. )
  568. self.session.commit()
  569. self.assertEqual(msg, 'Comment added')
  570. commits = [
  571. commit
  572. for commit in self.repo_obj.walk(
  573. self.repo_obj.head.target, pygit2.GIT_SORT_NONE)
  574. ]
  575. # 2 commits: creation - new comment
  576. self.assertEqual(len(commits), 2)
  577. issue = pagure.lib.query.search_issues(self.session, project, issueid=1)
  578. self.assertEqual(len(issue.comments), 1)
  579. @patch('pagure.lib.notify.send_email')
  580. @patch('pagure.lib.git.update_request_from_git')
  581. def test_loading_issue_json(self, up_pr, send):
  582. """ Test loading the JSON file of a ticket. """
  583. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  584. issue = pagure.lib.query.search_issues(self.session, project, issueid=1)
  585. commits = [
  586. commit.oid.hex
  587. for commit in self.repo_obj.walk(
  588. self.repo_obj.head.target, pygit2.GIT_SORT_NONE)
  589. ]
  590. output = pagure.lib.tasks_services.load_json_commits_to_db(
  591. name='test',
  592. commits=commits,
  593. abspath=self.gitrepo,
  594. data_type='ticket',
  595. agent='pingou',
  596. namespace=None,
  597. username=None)
  598. self.assertIsNone(output)
  599. up_pr.assert_not_called()
  600. calls = [
  601. call(
  602. u'Loading: %s -- 1/1 ... ... Done' % issue.uid,
  603. u'Issue import report',
  604. u'bar@pingou.com'
  605. )
  606. ]
  607. self.assertEqual(
  608. calls,
  609. send.mock_calls
  610. )
  611. project = pagure.lib.query.get_authorized_project(self.session, 'test')
  612. issue = pagure.lib.query.search_issues(self.session, project, issueid=1)
  613. self.assertEqual(len(issue.comments), 1)
  614. if __name__ == '__main__':
  615. unittest.main(verbosity=2)