test_pagure_lib_notify.py 18 KB

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