test_pagure_lib_notify.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import unittest
  10. import shutil
  11. import sys
  12. import os
  13. from mock import patch, MagicMock
  14. sys.path.insert(0, os.path.join(os.path.dirname(
  15. os.path.abspath(__file__)), '..'))
  16. import pagure.lib
  17. import pagure.lib.model
  18. import pagure.lib.notify
  19. import tests
  20. class PagureLibNotifytests(tests.Modeltests):
  21. """ Tests for pagure.lib.notify """
  22. def test_get_emails_for_obj_issue(self):
  23. """ Test the _get_emails_for_obj method from pagure.lib.notify. """
  24. # Create the project ns/test
  25. item = pagure.lib.model.Project(
  26. user_id=1, # pingou
  27. name='test3',
  28. namespace='ns',
  29. description='test project #1',
  30. hook_token='aaabbbcccdd',
  31. )
  32. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  33. self.session.add(item)
  34. self.session.commit()
  35. # Create the ticket
  36. iss = pagure.lib.new_issue(
  37. issue_id=4,
  38. session=self.session,
  39. repo=item,
  40. title='test issue',
  41. content='content test issue',
  42. user='pingou',
  43. ticketfolder=None,
  44. )
  45. self.session.commit()
  46. self.assertEqual(iss.id, 4)
  47. self.assertEqual(iss.title, 'test issue')
  48. exp = set(['bar@pingou.com'])
  49. out = pagure.lib.notify._get_emails_for_obj(iss)
  50. self.assertEqual(out, exp)
  51. # Comment on the ticket
  52. out = pagure.lib.add_issue_comment(
  53. self.session,
  54. issue=iss,
  55. comment='This is a comment',
  56. user='foo',
  57. ticketfolder=None,
  58. notify=False)
  59. self.assertEqual(out, 'Comment added')
  60. exp = set(['bar@pingou.com', 'foo@bar.com'])
  61. out = pagure.lib.notify._get_emails_for_obj(iss)
  62. self.assertEqual(out, exp)
  63. # Create user `bar`
  64. item = pagure.lib.model.User(
  65. user='bar',
  66. fullname='bar name',
  67. password='bar',
  68. default_email='bar@bar.com',
  69. )
  70. self.session.add(item)
  71. item = pagure.lib.model.UserEmail(
  72. user_id=3,
  73. email='bar@bar.com')
  74. self.session.add(item)
  75. self.session.commit()
  76. # Watch the ticket
  77. out = pagure.lib.set_watch_obj(self.session, 'bar', iss, True)
  78. self.assertEqual(out, 'You are now watching this issue')
  79. exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
  80. out = pagure.lib.notify._get_emails_for_obj(iss)
  81. self.assertEqual(out, exp)
  82. def test_get_emails_for_obj_issue_watching_project(self):
  83. """ Test the _get_emails_for_obj method from pagure.lib.notify. """
  84. # Create the project ns/test
  85. item = pagure.lib.model.Project(
  86. user_id=1, # pingou
  87. name='test3',
  88. namespace='ns',
  89. description='test project #1',
  90. hook_token='aaabbbcccdd',
  91. )
  92. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  93. self.session.add(item)
  94. self.session.commit()
  95. # Create the ticket
  96. iss = pagure.lib.new_issue(
  97. issue_id=4,
  98. session=self.session,
  99. repo=item,
  100. title='test issue',
  101. content='content test issue',
  102. user='pingou',
  103. ticketfolder=None,
  104. )
  105. self.session.commit()
  106. self.assertEqual(iss.id, 4)
  107. self.assertEqual(iss.title, 'test issue')
  108. exp = set(['bar@pingou.com'])
  109. out = pagure.lib.notify._get_emails_for_obj(iss)
  110. self.assertEqual(out, exp)
  111. # Comment on the ticket
  112. out = pagure.lib.add_issue_comment(
  113. self.session,
  114. issue=iss,
  115. comment='This is a comment',
  116. user='foo',
  117. ticketfolder=None,
  118. notify=False)
  119. self.assertEqual(out, 'Comment added')
  120. exp = set(['bar@pingou.com', 'foo@bar.com'])
  121. out = pagure.lib.notify._get_emails_for_obj(iss)
  122. self.assertEqual(out, exp)
  123. # Create user `bar`
  124. item = pagure.lib.model.User(
  125. user='bar',
  126. fullname='bar name',
  127. password='bar',
  128. default_email='bar@bar.com',
  129. )
  130. self.session.add(item)
  131. item = pagure.lib.model.UserEmail(
  132. user_id=3,
  133. email='bar@bar.com')
  134. self.session.add(item)
  135. self.session.commit()
  136. # Watch the project
  137. repo = pagure.lib.get_authorized_project(self.session, 'test3', namespace='ns')
  138. out = pagure.lib.update_watch_status(self.session, repo, 'bar', '1')
  139. self.assertEqual(
  140. out, 'You are now watching issues and PRs on this project')
  141. exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
  142. out = pagure.lib.notify._get_emails_for_obj(iss)
  143. self.assertEqual(out, exp)
  144. @patch('pagure.lib.notify.smtplib.SMTP')
  145. def test_get_emails_for_obj_pr(self, mock_smtp):
  146. """ Test the _get_emails_for_obj method from pagure.lib.notify. """
  147. mock_smtp.return_value = MagicMock()
  148. tests.create_projects(self.session)
  149. # Create the project ns/test
  150. item = pagure.lib.model.Project(
  151. user_id=1, # pingou
  152. name='test3',
  153. namespace='ns',
  154. description='test project #1',
  155. hook_token='aaabbbcccdd',
  156. )
  157. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  158. self.session.add(item)
  159. self.session.commit()
  160. # Create the PR
  161. repo = pagure.lib._get_project(self.session, 'test')
  162. req = pagure.lib.new_pull_request(
  163. session=self.session,
  164. repo_from=repo,
  165. branch_from='master',
  166. repo_to=repo,
  167. branch_to='master',
  168. title='test pull-request',
  169. user='pingou',
  170. requestfolder=None,
  171. )
  172. self.session.commit()
  173. self.assertEqual(req.id, 1)
  174. self.assertEqual(req.title, 'test pull-request')
  175. self.assertEqual(repo.open_requests, 1)
  176. exp = set(['bar@pingou.com'])
  177. out = pagure.lib.notify._get_emails_for_obj(req)
  178. self.assertEqual(out, exp)
  179. # Comment on the ticket
  180. out = pagure.lib.add_pull_request_comment(
  181. self.session,
  182. request=req,
  183. commit=None,
  184. tree_id=None,
  185. filename=None,
  186. row=None,
  187. comment='This is a comment',
  188. user='foo',
  189. requestfolder=None,
  190. notify=False)
  191. self.assertEqual(out, 'Comment added')
  192. exp = set(['bar@pingou.com', 'foo@bar.com'])
  193. out = pagure.lib.notify._get_emails_for_obj(req)
  194. self.assertEqual(out, exp)
  195. # Create user `bar`
  196. item = pagure.lib.model.User(
  197. user='bar',
  198. fullname='bar name',
  199. password='bar',
  200. default_email='bar@bar.com',
  201. )
  202. self.session.add(item)
  203. item = pagure.lib.model.UserEmail(
  204. user_id=3,
  205. email='bar@bar.com')
  206. self.session.add(item)
  207. self.session.commit()
  208. # Watch the pull-request
  209. out = pagure.lib.set_watch_obj(self.session, 'bar', req, True)
  210. self.assertEqual(out, 'You are now watching this pull-request')
  211. exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
  212. out = pagure.lib.notify._get_emails_for_obj(req)
  213. self.assertEqual(out, exp)
  214. @patch('pagure.lib.notify.smtplib.SMTP')
  215. def test_get_emails_for_obj_pr_watching_project(self, mock_smtp):
  216. """ Test the _get_emails_for_obj method from pagure.lib.notify. """
  217. mock_smtp.return_value = MagicMock()
  218. tests.create_projects(self.session)
  219. # Create the project ns/test
  220. item = pagure.lib.model.Project(
  221. user_id=1, # pingou
  222. name='test3',
  223. namespace='ns',
  224. description='test project #1',
  225. hook_token='aaabbbcccdd',
  226. )
  227. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  228. self.session.add(item)
  229. self.session.commit()
  230. # Create the PR
  231. repo = pagure.lib.get_authorized_project(self.session, 'test')
  232. req = pagure.lib.new_pull_request(
  233. session=self.session,
  234. repo_from=repo,
  235. branch_from='master',
  236. repo_to=repo,
  237. branch_to='master',
  238. title='test pull-request',
  239. user='pingou',
  240. requestfolder=None,
  241. )
  242. self.session.commit()
  243. self.assertEqual(req.id, 1)
  244. self.assertEqual(req.title, 'test pull-request')
  245. self.assertEqual(repo.open_requests, 1)
  246. exp = set(['bar@pingou.com'])
  247. out = pagure.lib.notify._get_emails_for_obj(req)
  248. self.assertEqual(out, exp)
  249. # Comment on the ticket
  250. out = pagure.lib.add_pull_request_comment(
  251. self.session,
  252. request=req,
  253. commit=None,
  254. tree_id=None,
  255. filename=None,
  256. row=None,
  257. comment='This is a comment',
  258. user='foo',
  259. requestfolder=None,
  260. notify=False)
  261. self.assertEqual(out, 'Comment added')
  262. exp = set(['bar@pingou.com', 'foo@bar.com'])
  263. out = pagure.lib.notify._get_emails_for_obj(req)
  264. self.assertEqual(out, exp)
  265. # Create user `bar`
  266. item = pagure.lib.model.User(
  267. user='bar',
  268. fullname='bar name',
  269. password='bar',
  270. default_email='bar@bar.com',
  271. )
  272. self.session.add(item)
  273. item = pagure.lib.model.UserEmail(
  274. user_id=3,
  275. email='bar@bar.com')
  276. self.session.add(item)
  277. self.session.commit()
  278. # Watch the project
  279. repo = pagure.lib.get_authorized_project(self.session, 'test')
  280. out = pagure.lib.update_watch_status(self.session, repo, 'bar', '1')
  281. self.assertEqual(
  282. out, 'You are now watching issues and PRs on this project')
  283. exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
  284. out = pagure.lib.notify._get_emails_for_obj(req)
  285. self.assertEqual(out, exp)
  286. def test_get_emails_for_obj_private_issue(self):
  287. """ Test the _get_emails_for_obj method from pagure.lib.notify. """
  288. # Create the project ns/test
  289. item = pagure.lib.model.Project(
  290. user_id=1, # pingou
  291. name='test3',
  292. namespace='ns',
  293. description='test project #1',
  294. hook_token='aaabbbcccdd',
  295. )
  296. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  297. self.session.add(item)
  298. self.session.commit()
  299. # Create the private ticket
  300. iss = pagure.lib.new_issue(
  301. issue_id=4,
  302. session=self.session,
  303. repo=item,
  304. title='test issue',
  305. content='content test issue',
  306. user='pingou',
  307. private=True,
  308. ticketfolder=None,
  309. )
  310. self.session.commit()
  311. self.assertEqual(iss.id, 4)
  312. self.assertEqual(iss.title, 'test issue')
  313. exp = set(['bar@pingou.com'])
  314. out = pagure.lib.notify._get_emails_for_obj(iss)
  315. self.assertEqual(out, exp)
  316. # Comment on the ticket
  317. out = pagure.lib.add_issue_comment(
  318. self.session,
  319. issue=iss,
  320. comment='This is a comment',
  321. user='foo',
  322. ticketfolder=None,
  323. notify=False)
  324. self.assertEqual(out, 'Comment added')
  325. exp = set(['bar@pingou.com', 'foo@bar.com'])
  326. out = pagure.lib.notify._get_emails_for_obj(iss)
  327. self.assertEqual(out, exp)
  328. # Create user `bar`
  329. item = pagure.lib.model.User(
  330. user='bar',
  331. fullname='bar name',
  332. password='bar',
  333. default_email='bar@bar.com',
  334. )
  335. self.session.add(item)
  336. item = pagure.lib.model.UserEmail(
  337. user_id=3,
  338. email='bar@bar.com')
  339. self.session.add(item)
  340. self.session.commit()
  341. # Add bar on the project with ticket acl
  342. project = pagure.lib._get_project(self.session, 'test3', namespace='ns')
  343. msg = pagure.lib.add_user_to_project(
  344. session=self.session,
  345. project=project,
  346. new_user='bar',
  347. user='pingou',
  348. access='ticket',
  349. )
  350. self.session.commit()
  351. self.assertEqual(msg, 'User added')
  352. exp = set(['bar@pingou.com', 'foo@bar.com'])
  353. out = pagure.lib.notify._get_emails_for_obj(iss)
  354. self.assertEqual(out, exp)
  355. @patch('pagure.lib.notify.smtplib.SMTP')
  356. def test_send_email(self, mock_smtp):
  357. """ Test the notify_new_comment method from pagure.lib.notify. """
  358. mock_smtp.return_value = MagicMock()
  359. email = pagure.lib.notify.send_email(
  360. 'Email content',
  361. 'Email “Subject“',
  362. 'foo@bar.com,zöé@foo.net',
  363. mail_id='test-pull-request-2edbf96ebe644f4bb31b94605e-1',
  364. in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e',
  365. project_name='namespace/project',
  366. user_from='Zöé',
  367. )
  368. exp = '''Content-Type: text/plain; charset="utf-8"
  369. MIME-Version: 1.0
  370. Content-Transfer-Encoding: base64
  371. Subject: =?utf-8?b?W25hbWVzcGFjZS9wcm9qZWN0XSBFbWFpbCDigJxTdWJqZWN04oCc?=
  372. From: =?utf-8?b?WsO2w6k=?= <pagure@pagure.org>
  373. mail-id: test-pull-request-2edbf96ebe644f4bb31b94605e-1@pagure.org
  374. Message-Id: <test-pull-request-2edbf96ebe644f4bb31b94605e-1@pagure.org>
  375. In-Reply-To: <test-pull-request-2edbf96ebe644f4bb31b94605e@pagure.org>
  376. X-Auto-Response-Suppress: All
  377. X-pagure: https://pagure.org/
  378. X-pagure-project: namespace/project
  379. List-ID: namespace/project
  380. List-Archive: https://pagure.org/namespace/project
  381. To: zöé@foo.net
  382. Reply-To: reply+819debcaa294a19ddedd9cfe0cb8faebb50d126adda0f02f203fce83824add55f2e640b75691d4a57cb95b25cc856b0815d1ad2a35ab358cda9de42e2b021957@pagure.org
  383. Mail-Followup-To: reply+819debcaa294a19ddedd9cfe0cb8faebb50d126adda0f02f203fce83824add55f2e640b75691d4a57cb95b25cc856b0815d1ad2a35ab358cda9de42e2b021957@pagure.org
  384. RW1haWwgY29udGVudA==
  385. '''
  386. self.assertEqual(email.as_string(), exp)
  387. email = pagure.lib.notify.send_email(
  388. 'Email content',
  389. 'Email “Subject“',
  390. u'foo@bar.com,zöé@foo.net',
  391. mail_id='test-pull-request-2edbf96ebe644f4bb31b94605e-1',
  392. in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e',
  393. project_name='namespace/project',
  394. user_from='Zöé',
  395. )
  396. self.assertEqual(email.as_string(), exp)
  397. if __name__ == '__main__':
  398. unittest.main(verbosity=2)