test_pagure_lib_task_services.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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
  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
  21. import pagure.lib.git
  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._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._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._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('requests.post')
  382. def test_webhook_notification_no_webhook(self, post):
  383. """ Test the webhook_notification method. """
  384. post.return_value = False
  385. output = pagure.lib.tasks_services.webhook_notification(
  386. topic='topic',
  387. msg={'payload': ['a', 'b', 'c']},
  388. namespace=None,
  389. name='test',
  390. user=None)
  391. self.assertIsNone(output)
  392. project = pagure.lib._get_project(self.session, 'test')
  393. self.assertEqual(post.call_count, 2)
  394. calls = [
  395. call(
  396. 'http://bar.org/bar',
  397. data='{'
  398. '"i": 1, '
  399. '"msg": {'
  400. '"pagure_instance": "http://localhost.localdomain/", '
  401. '"payload": ["a", "b", "c"], '
  402. '"project_fullname": "test"}, '
  403. '"msg_id": "2018-not_so_random", '
  404. '"timestamp": 2, '
  405. '"topic": "topic"}'
  406. ,
  407. headers={
  408. 'X-Pagure': 'http://localhost.localdomain/',
  409. 'X-Pagure-project': 'test',
  410. 'X-Pagure-Signature': '74b12f0b25bf7767014a0c0de9f3c10'
  411. '191e943d8',
  412. 'X-Pagure-Signature-256': 'f3d757796554466eac49a5282b2'
  413. '4ee32a1ecfb65dedd6c6231fb207240a9fe58',
  414. 'X-Pagure-Topic': b'topic',
  415. 'Content-Type': 'application/json'
  416. },
  417. timeout=60
  418. ),
  419. call(
  420. 'http://foo.com/api/flag',
  421. data='{'
  422. '"i": 1, '
  423. '"msg": {'
  424. '"pagure_instance": "http://localhost.localdomain/", '
  425. '"payload": ["a", "b", "c"], '
  426. '"project_fullname": "test"}, '
  427. '"msg_id": "2018-not_so_random", '
  428. '"timestamp": 2, '
  429. '"topic": "topic"}'
  430. ,
  431. headers={
  432. 'X-Pagure': 'http://localhost.localdomain/',
  433. 'X-Pagure-project': 'test',
  434. 'X-Pagure-Signature': '74b12f0b25bf7767014a0c0de9f3c10'
  435. '191e943d8',
  436. 'X-Pagure-Signature-256': 'f3d757796554466eac49a5282b2'
  437. '4ee32a1ecfb65dedd6c6231fb207240a9fe58',
  438. 'X-Pagure-Topic': b'topic',
  439. 'Content-Type': 'application/json'
  440. },
  441. timeout=60
  442. )
  443. ]
  444. print(post.mock_calls)
  445. self.assertEqual(
  446. calls,
  447. post.mock_calls
  448. )
  449. class PagureLibTaskServicesJenkinsCItests(tests.Modeltests):
  450. """ Tests for pagure.lib.task_services """
  451. maxDiff = None
  452. def setUp(self):
  453. """ Set up the environnment, ran before every tests. """
  454. super(PagureLibTaskServicesJenkinsCItests, self).setUp()
  455. pagure.config.config['REQUESTS_FOLDER'] = None
  456. self.sshkeydir = os.path.join(self.path, 'sshkeys')
  457. pagure.config.config['MIRROR_SSHKEYS_FOLDER'] = self.sshkeydir
  458. tests.create_projects(self.session)
  459. project = pagure.lib.get_authorized_project(self.session, 'test')
  460. # Install the plugin at the DB level
  461. plugin = pagure.lib.plugins.get_plugin('Pagure CI')
  462. dbobj = plugin.db_object()
  463. dbobj.ci_url = 'https://ci.server.org/'
  464. dbobj.ci_job = 'pagure'
  465. dbobj.pagure_ci_token = 'random_token'
  466. dbobj.project_id = project.id
  467. self.session.add(dbobj)
  468. self.session.commit()
  469. # Create a fork of test for foo
  470. item = pagure.lib.model.Project(
  471. user_id=2, # foo
  472. name='test',
  473. is_fork=True,
  474. parent_id=1,
  475. description='test project #1',
  476. hook_token='aaabbbccc_foo',
  477. )
  478. item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  479. self.session.add(item)
  480. self.session.commit()
  481. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  482. def test_trigger_ci_build_invalid_ci(self, trigger_jenk):
  483. """ Test the trigger_ci_build method. """
  484. output = pagure.lib.tasks_services.trigger_ci_build(
  485. project_name='test',
  486. cause='PR#ID',
  487. branch='feature',
  488. ci_type='travis')
  489. self.assertIsNone(output)
  490. trigger_jenk.assert_not_called()
  491. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  492. def test_trigger_ci_build_invalid_ci_fork(self, trigger_jenk):
  493. """ Test the trigger_ci_build method. """
  494. output = pagure.lib.tasks_services.trigger_ci_build(
  495. project_name='forks/foo/test',
  496. cause='PR#ID',
  497. branch='feature',
  498. ci_type='travis')
  499. self.assertIsNone(output)
  500. trigger_jenk.assert_not_called()
  501. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  502. def test_trigger_ci_build_valid_project(self, trigger_jenk):
  503. """ Test the trigger_ci_build method. """
  504. output = pagure.lib.tasks_services.trigger_ci_build(
  505. project_name='test',
  506. cause='PR#ID',
  507. branch='feature',
  508. ci_type='jenkins')
  509. self.assertIsNone(output)
  510. trigger_jenk.assert_called_once_with(
  511. branch=u'feature',
  512. cause=u'PR#ID',
  513. job=u'pagure',
  514. project_path=u'test.git',
  515. token=u'random_token',
  516. url=u'https://ci.server.org/'
  517. )
  518. @patch('pagure.lib.tasks_services.trigger_jenkins_build')
  519. def test_trigger_ci_build_valid_project_fork(self, trigger_jenk):
  520. """ Test the trigger_ci_build method. """
  521. output = pagure.lib.tasks_services.trigger_ci_build(
  522. project_name='forks/foo/test',
  523. cause='PR#ID',
  524. branch='feature',
  525. ci_type='jenkins')
  526. self.assertIsNone(output)
  527. trigger_jenk.assert_called_once_with(
  528. branch=u'feature',
  529. cause=u'PR#ID',
  530. job=u'pagure',
  531. project_path=u'forks/foo/test.git',
  532. token=u'random_token',
  533. url=u'https://ci.server.org/'
  534. )
  535. class PagureLibTaskServicesLoadJsonTickettests(tests.Modeltests):
  536. """ Tests for pagure.lib.task_services """
  537. maxDiff = None
  538. def setUp(self):
  539. """ Set up the environnment, ran before every tests. """
  540. super(PagureLibTaskServicesLoadJsonTickettests, self).setUp()
  541. tests.create_projects(self.session)
  542. self.gitrepo = os.path.join(self.path, 'repos', 'tickets', 'test.git')
  543. repopath = os.path.join(self.path, 'repos', 'tickets')
  544. os.makedirs(self.gitrepo)
  545. self.repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  546. project = pagure.lib.get_authorized_project(self.session, 'test')
  547. # Create an issue to play with
  548. msg = pagure.lib.new_issue(
  549. session=self.session,
  550. repo=project,
  551. title='Test issue',
  552. content='We should work on this',
  553. user='pingou',
  554. )
  555. self.assertEqual(msg.title, 'Test issue')
  556. issue = pagure.lib.search_issues(self.session, project, issueid=1)
  557. # Add a couple of comment on the ticket
  558. msg = pagure.lib.add_issue_comment(
  559. session=self.session,
  560. issue=issue,
  561. comment='Hey look a comment!',
  562. user='foo',
  563. )
  564. self.session.commit()
  565. self.assertEqual(msg, 'Comment added')
  566. commits = [
  567. commit
  568. for commit in self.repo_obj.walk(
  569. self.repo_obj.head.target, pygit2.GIT_SORT_NONE)
  570. ]
  571. # 2 commits: creation - new comment
  572. self.assertEqual(len(commits), 2)
  573. issue = pagure.lib.search_issues(self.session, project, issueid=1)
  574. self.assertEqual(len(issue.comments), 1)
  575. @patch('pagure.lib.notify.send_email')
  576. @patch('pagure.lib.git.update_request_from_git')
  577. def test_loading_issue_json(self, up_pr, send):
  578. """ Test loading the JSON file of a ticket. """
  579. project = pagure.lib.get_authorized_project(self.session, 'test')
  580. issue = pagure.lib.search_issues(self.session, project, issueid=1)
  581. commits = [
  582. commit.oid.hex
  583. for commit in self.repo_obj.walk(
  584. self.repo_obj.head.target, pygit2.GIT_SORT_NONE)
  585. ]
  586. output = pagure.lib.tasks_services.load_json_commits_to_db(
  587. name='test',
  588. commits=commits,
  589. abspath=self.gitrepo,
  590. data_type='ticket',
  591. agent='pingou',
  592. namespace=None,
  593. username=None)
  594. self.assertIsNone(output)
  595. up_pr.assert_not_called()
  596. calls = [
  597. call(
  598. u'Loading: %s -- 1/1 ... ... Done' % issue.uid,
  599. u'Issue import report',
  600. u'bar@pingou.com'
  601. )
  602. ]
  603. self.assertEqual(
  604. calls,
  605. send.mock_calls
  606. )
  607. project = pagure.lib.get_authorized_project(self.session, 'test')
  608. issue = pagure.lib.search_issues(self.session, project, issueid=1)
  609. self.assertEqual(len(issue.comments), 1)
  610. if __name__ == '__main__':
  611. unittest.main(verbosity=2)