test_pagure_lib_notify.py 18 KB

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