1
0

test_pagure_lib_notify.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.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.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.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. @patch('pagure.lib.notify.smtplib.SMTP')
  287. def test_send_email(self, mock_smtp):
  288. """ Test the notify_new_comment method from pagure.lib.notify. """
  289. mock_smtp.return_value = MagicMock()
  290. email = pagure.lib.notify.send_email(
  291. 'Email content',
  292. 'Email “Subject“',
  293. 'foo@bar.com,zöé@foo.net',
  294. mail_id='test-pull-request-2edbf96ebe644f4bb31b94605e-1@pagure',
  295. in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e@pagure',
  296. project_name='namespace/project',
  297. user_from='Zöé',
  298. )
  299. exp = '''Content-Type: text/plain; charset="utf-8"
  300. MIME-Version: 1.0
  301. Content-Transfer-Encoding: base64
  302. Subject: =?utf-8?b?W25hbWVzcGFjZS9wcm9qZWN0XSBFbWFpbCDigJxTdWJqZWN04oCc?=
  303. From: =?utf-8?b?WsO2w6k=?= <pagure@pagure.org>
  304. mail-id: test-pull-request-2edbf96ebe644f4bb31b94605e-1@pagure
  305. Message-Id: <test-pull-request-2edbf96ebe644f4bb31b94605e-1@pagure>
  306. In-Reply-To: <test-pull-request-2edbf96ebe644f4bb31b94605e@pagure>
  307. X-pagure: https://pagure.org/
  308. X-pagure-project: namespace/project
  309. To: zöé@foo.net
  310. Reply-To: reply+42f5809bca16d73f59180bdcc76c981e939b5eab5c02930d7d7dd38f45118b89e9ceb877e94e7f22376fbf35aab1d0e8e83dfb074ee82640cc82da12ea8019ca@pagure.org
  311. Mail-Followup-To: reply+42f5809bca16d73f59180bdcc76c981e939b5eab5c02930d7d7dd38f45118b89e9ceb877e94e7f22376fbf35aab1d0e8e83dfb074ee82640cc82da12ea8019ca@pagure.org
  312. RW1haWwgY29udGVudA==
  313. '''
  314. self.assertEqual(email.as_string(), exp)
  315. email = pagure.lib.notify.send_email(
  316. 'Email content',
  317. 'Email “Subject“',
  318. u'foo@bar.com,zöé@foo.net',
  319. mail_id='test-pull-request-2edbf96ebe644f4bb31b94605e-1@pagure',
  320. in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e@pagure',
  321. project_name='namespace/project',
  322. user_from='Zöé',
  323. )
  324. self.assertEqual(email.as_string(), exp)
  325. if __name__ == '__main__':
  326. unittest.main(verbosity=2)