test_pagure_lib_notify.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import shutil
  10. import sys
  11. import os
  12. from mock import patch, MagicMock
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure.lib.model
  16. import pagure.lib.notify
  17. import pagure.lib.query
  18. import tests
  19. class PagureLibNotifytests(tests.Modeltests):
  20. """ Tests for pagure.lib.notify """
  21. maxDiff = None
  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.query.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. )
  44. self.session.commit()
  45. self.assertEqual(iss.id, 4)
  46. self.assertEqual(iss.title, 'test issue')
  47. exp = set(['bar@pingou.com'])
  48. out = pagure.lib.notify._get_emails_for_obj(iss)
  49. self.assertEqual(out, exp)
  50. # Comment on the ticket
  51. out = pagure.lib.query.add_issue_comment(
  52. self.session,
  53. issue=iss,
  54. comment='This is a comment',
  55. user='foo',
  56. notify=False)
  57. self.assertEqual(out, 'Comment added')
  58. exp = set(['bar@pingou.com', 'foo@bar.com'])
  59. out = pagure.lib.notify._get_emails_for_obj(iss)
  60. self.assertEqual(out, exp)
  61. # Create user `bar`
  62. item = pagure.lib.model.User(
  63. user='bar',
  64. fullname='bar name',
  65. password='bar',
  66. default_email='bar@bar.com',
  67. )
  68. self.session.add(item)
  69. item = pagure.lib.model.UserEmail(
  70. user_id=3,
  71. email='bar@bar.com')
  72. self.session.add(item)
  73. self.session.commit()
  74. # Watch the ticket
  75. out = pagure.lib.query.set_watch_obj(self.session, 'bar', iss, True)
  76. self.assertEqual(out, 'You are now watching this issue')
  77. exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
  78. out = pagure.lib.notify._get_emails_for_obj(iss)
  79. self.assertEqual(out, exp)
  80. def test_get_emails_for_obj_issue_watching_project(self):
  81. """ Test the _get_emails_for_obj method from pagure.lib.notify. """
  82. # Create the project ns/test
  83. item = pagure.lib.model.Project(
  84. user_id=1, # pingou
  85. name='test3',
  86. namespace='ns',
  87. description='test project #1',
  88. hook_token='aaabbbcccdd',
  89. )
  90. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  91. self.session.add(item)
  92. self.session.commit()
  93. # Create the ticket
  94. iss = pagure.lib.query.new_issue(
  95. issue_id=4,
  96. session=self.session,
  97. repo=item,
  98. title='test issue',
  99. content='content test issue',
  100. user='pingou',
  101. )
  102. self.session.commit()
  103. self.assertEqual(iss.id, 4)
  104. self.assertEqual(iss.title, 'test issue')
  105. exp = set(['bar@pingou.com'])
  106. out = pagure.lib.notify._get_emails_for_obj(iss)
  107. self.assertEqual(out, exp)
  108. # Comment on the ticket
  109. out = pagure.lib.query.add_issue_comment(
  110. self.session,
  111. issue=iss,
  112. comment='This is a comment',
  113. user='foo',
  114. notify=False)
  115. self.assertEqual(out, 'Comment added')
  116. exp = set(['bar@pingou.com', 'foo@bar.com'])
  117. out = pagure.lib.notify._get_emails_for_obj(iss)
  118. self.assertEqual(out, exp)
  119. # Create user `bar`
  120. item = pagure.lib.model.User(
  121. user='bar',
  122. fullname='bar name',
  123. password='bar',
  124. default_email='bar@bar.com',
  125. )
  126. self.session.add(item)
  127. item = pagure.lib.model.UserEmail(
  128. user_id=3,
  129. email='bar@bar.com')
  130. self.session.add(item)
  131. self.session.commit()
  132. # Watch the project
  133. repo = pagure.lib.query.get_authorized_project(self.session, 'test3', namespace='ns')
  134. out = pagure.lib.query.update_watch_status(self.session, repo, 'bar', '1')
  135. self.assertEqual(
  136. out, 'You are now watching issues and PRs on this project')
  137. exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
  138. out = pagure.lib.notify._get_emails_for_obj(iss)
  139. self.assertEqual(out, exp)
  140. @patch('pagure.lib.notify.smtplib.SMTP')
  141. def test_get_emails_for_obj_pr(self, mock_smtp):
  142. """ Test the _get_emails_for_obj method from pagure.lib.notify. """
  143. mock_smtp.return_value = MagicMock()
  144. tests.create_projects(self.session)
  145. # Create the project ns/test
  146. item = pagure.lib.model.Project(
  147. user_id=1, # pingou
  148. name='test3',
  149. namespace='ns',
  150. description='test project #1',
  151. hook_token='aaabbbcccdd',
  152. )
  153. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  154. self.session.add(item)
  155. self.session.commit()
  156. # Create the PR
  157. repo = pagure.lib.query._get_project(self.session, 'test')
  158. req = pagure.lib.query.new_pull_request(
  159. session=self.session,
  160. repo_from=repo,
  161. branch_from='master',
  162. repo_to=repo,
  163. branch_to='master',
  164. title='test pull-request',
  165. user='pingou',
  166. )
  167. self.session.commit()
  168. self.assertEqual(req.id, 1)
  169. self.assertEqual(req.title, 'test pull-request')
  170. self.assertEqual(repo.open_requests, 1)
  171. exp = set(['bar@pingou.com'])
  172. out = pagure.lib.notify._get_emails_for_obj(req)
  173. self.assertEqual(out, exp)
  174. # Comment on the ticket
  175. out = pagure.lib.query.add_pull_request_comment(
  176. self.session,
  177. request=req,
  178. commit=None,
  179. tree_id=None,
  180. filename=None,
  181. row=None,
  182. comment='This is a comment',
  183. user='foo',
  184. notify=False)
  185. self.assertEqual(out, 'Comment added')
  186. exp = set(['bar@pingou.com', 'foo@bar.com'])
  187. out = pagure.lib.notify._get_emails_for_obj(req)
  188. self.assertEqual(out, exp)
  189. # Create user `bar`
  190. item = pagure.lib.model.User(
  191. user='bar',
  192. fullname='bar name',
  193. password='bar',
  194. default_email='bar@bar.com',
  195. )
  196. self.session.add(item)
  197. item = pagure.lib.model.UserEmail(
  198. user_id=3,
  199. email='bar@bar.com')
  200. self.session.add(item)
  201. self.session.commit()
  202. # Watch the pull-request
  203. out = pagure.lib.query.set_watch_obj(self.session, 'bar', req, True)
  204. self.assertEqual(out, 'You are now watching this pull-request')
  205. exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
  206. out = pagure.lib.notify._get_emails_for_obj(req)
  207. self.assertEqual(out, exp)
  208. @patch('pagure.lib.notify.smtplib.SMTP')
  209. def test_get_emails_for_obj_pr_watching_project(self, mock_smtp):
  210. """ Test the _get_emails_for_obj method from pagure.lib.notify. """
  211. mock_smtp.return_value = MagicMock()
  212. tests.create_projects(self.session)
  213. # Create the project ns/test
  214. item = pagure.lib.model.Project(
  215. user_id=1, # pingou
  216. name='test3',
  217. namespace='ns',
  218. description='test project #1',
  219. hook_token='aaabbbcccdd',
  220. )
  221. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  222. self.session.add(item)
  223. self.session.commit()
  224. # Create the PR
  225. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  226. req = pagure.lib.query.new_pull_request(
  227. session=self.session,
  228. repo_from=repo,
  229. branch_from='master',
  230. repo_to=repo,
  231. branch_to='master',
  232. title='test pull-request',
  233. user='pingou',
  234. )
  235. self.session.commit()
  236. self.assertEqual(req.id, 1)
  237. self.assertEqual(req.title, 'test pull-request')
  238. self.assertEqual(repo.open_requests, 1)
  239. exp = set(['bar@pingou.com'])
  240. out = pagure.lib.notify._get_emails_for_obj(req)
  241. self.assertEqual(out, exp)
  242. # Comment on the ticket
  243. out = pagure.lib.query.add_pull_request_comment(
  244. self.session,
  245. request=req,
  246. commit=None,
  247. tree_id=None,
  248. filename=None,
  249. row=None,
  250. comment='This is a comment',
  251. user='foo',
  252. notify=False)
  253. self.assertEqual(out, 'Comment added')
  254. exp = set(['bar@pingou.com', 'foo@bar.com'])
  255. out = pagure.lib.notify._get_emails_for_obj(req)
  256. self.assertEqual(out, exp)
  257. # Create user `bar`
  258. item = pagure.lib.model.User(
  259. user='bar',
  260. fullname='bar name',
  261. password='bar',
  262. default_email='bar@bar.com',
  263. )
  264. self.session.add(item)
  265. item = pagure.lib.model.UserEmail(
  266. user_id=3,
  267. email='bar@bar.com')
  268. self.session.add(item)
  269. self.session.commit()
  270. # Watch the project
  271. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  272. out = pagure.lib.query.update_watch_status(self.session, repo, 'bar', '1')
  273. self.assertEqual(
  274. out, 'You are now watching issues and PRs on this project')
  275. exp = set(['bar@pingou.com', 'foo@bar.com', 'bar@bar.com'])
  276. out = pagure.lib.notify._get_emails_for_obj(req)
  277. self.assertEqual(out, exp)
  278. def test_get_emails_for_obj_private_issue(self):
  279. """ Test the _get_emails_for_obj method from pagure.lib.notify. """
  280. # Create the project ns/test
  281. item = pagure.lib.model.Project(
  282. user_id=1, # pingou
  283. name='test3',
  284. namespace='ns',
  285. description='test project #1',
  286. hook_token='aaabbbcccdd',
  287. )
  288. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  289. self.session.add(item)
  290. self.session.commit()
  291. # Create the private ticket
  292. iss = pagure.lib.query.new_issue(
  293. issue_id=4,
  294. session=self.session,
  295. repo=item,
  296. title='test issue',
  297. content='content test issue',
  298. user='pingou',
  299. private=True,
  300. )
  301. self.session.commit()
  302. self.assertEqual(iss.id, 4)
  303. self.assertEqual(iss.title, 'test issue')
  304. exp = set(['bar@pingou.com'])
  305. out = pagure.lib.notify._get_emails_for_obj(iss)
  306. self.assertEqual(out, exp)
  307. # Comment on the ticket
  308. out = pagure.lib.query.add_issue_comment(
  309. self.session,
  310. issue=iss,
  311. comment='This is a comment',
  312. user='foo',
  313. notify=False)
  314. self.assertEqual(out, 'Comment added')
  315. exp = set(['bar@pingou.com', 'foo@bar.com'])
  316. out = pagure.lib.notify._get_emails_for_obj(iss)
  317. self.assertEqual(out, exp)
  318. # Create user `bar`
  319. item = pagure.lib.model.User(
  320. user='bar',
  321. fullname='bar name',
  322. password='bar',
  323. default_email='bar@bar.com',
  324. )
  325. self.session.add(item)
  326. item = pagure.lib.model.UserEmail(
  327. user_id=3,
  328. email='bar@bar.com')
  329. self.session.add(item)
  330. self.session.commit()
  331. # Add bar on the project with ticket acl
  332. project = pagure.lib.query._get_project(self.session, 'test3', namespace='ns')
  333. msg = pagure.lib.query.add_user_to_project(
  334. session=self.session,
  335. project=project,
  336. new_user='bar',
  337. user='pingou',
  338. access='ticket',
  339. )
  340. self.session.commit()
  341. self.assertEqual(msg, 'User added')
  342. exp = set(['bar@pingou.com', 'foo@bar.com'])
  343. out = pagure.lib.notify._get_emails_for_obj(iss)
  344. self.assertEqual(out, exp)
  345. @patch.dict(
  346. 'pagure.config.config',
  347. {'EVENTSOURCE_SOURCE': 'localhost.localdomain'})
  348. @patch('pagure.lib.notify.smtplib.SMTP')
  349. def test_send_email(self, mock_smtp):
  350. """ Test the send_email method from pagure.lib.notify. """
  351. mock_smtp.return_value = MagicMock()
  352. email = pagure.lib.notify.send_email(
  353. 'Email content',
  354. 'Email “Subject“',
  355. 'foo@bar.com,zöé@foo.net',
  356. mail_id='test-pull-request-2edbf96ebe644f4bb31b94605e-1',
  357. in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e',
  358. project_name='namespace/project',
  359. user_from='Zöé',
  360. )
  361. # Due to differences in the way Python2 and Python3 encode non-ascii
  362. # email headers, we compare the From and To headers separately from the
  363. # rest of the message.
  364. self.assertEqual(
  365. email["From"],
  366. "=?utf-8?b?WsO2w6k=?= <pagure@localhost.localdomain>")
  367. self.assertEqual(email["To"], "zöé@foo.net")
  368. del email["From"]
  369. del email["To"]
  370. exp = '''Content-Type: text/plain; charset="utf-8"
  371. MIME-Version: 1.0
  372. Content-Transfer-Encoding: base64
  373. Subject: =?utf-8?b?W25hbWVzcGFjZS9wcm9qZWN0XSBFbWFpbCDigJxTdWJqZWN04oCc?=
  374. mail-id: test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain
  375. Message-Id: <test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain>
  376. In-Reply-To: <test-pull-request-2edbf96ebe644f4bb31b94605e@localhost.localdomain>
  377. X-Auto-Response-Suppress: All
  378. X-pagure: http://localhost.localdomain/
  379. X-pagure-project: namespace/project
  380. List-ID: namespace/project
  381. List-Archive: http://localhost.localdomain/namespace/project
  382. Reply-To: reply+ddd73d6bcace71598118ece5808b9c1b8e68c73e1acc4302538257a0951bb920ea0765d3f262ddb3725e9369519b086a7873cb65fbfceb1a2a25897f8e2a54fa@localhost.localdomain
  383. Mail-Followup-To: reply+ddd73d6bcace71598118ece5808b9c1b8e68c73e1acc4302538257a0951bb920ea0765d3f262ddb3725e9369519b086a7873cb65fbfceb1a2a25897f8e2a54fa@localhost.localdomain
  384. RW1haWwgY29udGVudA==
  385. '''
  386. self.assertEqual(email.as_string(), exp)
  387. email = pagure.lib.notify.send_email(
  388. 'Email content',
  389. 'Email “Subject“',
  390. '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(
  397. email["From"],
  398. "=?utf-8?b?WsO2w6k=?= <pagure@localhost.localdomain>")
  399. self.assertEqual(email["To"], "zöé@foo.net")
  400. del email["From"]
  401. del email["To"]
  402. self.assertEqual(email.as_string(), exp)
  403. exp = '''Content-Type: text/plain; charset="utf-8"
  404. MIME-Version: 1.0
  405. Content-Transfer-Encoding: base64
  406. Subject: =?utf-8?b?W25hbWVzcGFjZS9wcm9qZWN0XSBFbWFpbCDigJxTdWJqZWN04oCc?=
  407. In-Reply-To: <test-pull-request-2edbf96ebe644f4bb31b94605e@localhost.localdomain>
  408. X-Auto-Response-Suppress: All
  409. X-pagure: http://localhost.localdomain/
  410. X-pagure-project: namespace/project
  411. List-ID: namespace/project
  412. List-Archive: http://localhost.localdomain/namespace/project
  413. RW1haWwgY29udGVudA==
  414. '''
  415. email = pagure.lib.notify.send_email(
  416. 'Email content',
  417. 'Email “Subject“',
  418. 'foo@bar.com,zöé@foo.net',
  419. mail_id=None,
  420. in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e',
  421. project_name='namespace/project',
  422. user_from='Zöé',
  423. )
  424. del email["From"]
  425. del email["To"]
  426. self.assertEqual(email.as_string(), exp)
  427. @patch.dict('pagure.config.config', {'EVENTSOURCE_SOURCE': None})
  428. @patch('pagure.lib.notify.smtplib.SMTP')
  429. def test_send_email_no_reply_to(self, mock_smtp):
  430. """ Test the send_email method from pagure.lib.notify when there
  431. should not be a Reply-To header even if mail_id is defined. """
  432. mock_smtp.return_value = MagicMock()
  433. email = pagure.lib.notify.send_email(
  434. 'Email content',
  435. 'Email “Subject“',
  436. 'foo@bar.com,zöé@foo.net',
  437. mail_id='test-pull-request-2edbf96ebe644f4bb31b94605e-1',
  438. in_reply_to='test-pull-request-2edbf96ebe644f4bb31b94605e',
  439. project_name='namespace/project',
  440. reporter='reporter',
  441. assignee='assignee',
  442. user_from='Zöé',
  443. )
  444. # Due to differences in the way Python2 and Python3 encode non-ascii
  445. # email headers, we compare the From and To headers separately from the
  446. # rest of the message.
  447. self.assertEqual(
  448. email["From"],
  449. "=?utf-8?b?WsO2w6k=?= <pagure@localhost.localdomain>")
  450. self.assertEqual(email["To"], "zöé@foo.net")
  451. del email["From"]
  452. del email["To"]
  453. exp = '''Content-Type: text/plain; charset="utf-8"
  454. MIME-Version: 1.0
  455. Content-Transfer-Encoding: base64
  456. Subject: =?utf-8?b?W25hbWVzcGFjZS9wcm9qZWN0XSBFbWFpbCDigJxTdWJqZWN04oCc?=
  457. mail-id: test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain
  458. Message-Id: <test-pull-request-2edbf96ebe644f4bb31b94605e-1@localhost.localdomain>
  459. In-Reply-To: <test-pull-request-2edbf96ebe644f4bb31b94605e@localhost.localdomain>
  460. X-Auto-Response-Suppress: All
  461. X-pagure: http://localhost.localdomain/
  462. X-pagure-project: namespace/project
  463. List-ID: namespace/project
  464. List-Archive: http://localhost.localdomain/namespace/project
  465. X-pagure-reporter: reporter
  466. X-pagure-assignee: assignee
  467. RW1haWwgY29udGVudA==
  468. '''
  469. self.assertEqual(email.as_string(), exp)
  470. if __name__ == '__main__':
  471. unittest.main(verbosity=2)