You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mail_handler_test.rb 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2014 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. require File.expand_path('../../test_helper', __FILE__)
  20. class MailHandlerTest < ActiveSupport::TestCase
  21. fixtures :users, :projects, :enabled_modules, :roles,
  22. :members, :member_roles, :users,
  23. :issues, :issue_statuses,
  24. :workflows, :trackers, :projects_trackers,
  25. :versions, :enumerations, :issue_categories,
  26. :custom_fields, :custom_fields_trackers, :custom_fields_projects,
  27. :boards, :messages
  28. FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
  29. def setup
  30. ActionMailer::Base.deliveries.clear
  31. Setting.notified_events = Redmine::Notifiable.all.collect(&:name)
  32. end
  33. def teardown
  34. Setting.clear_cache
  35. end
  36. def test_add_issue
  37. ActionMailer::Base.deliveries.clear
  38. # This email contains: 'Project: onlinestore'
  39. issue = submit_email('ticket_on_given_project.eml')
  40. assert issue.is_a?(Issue)
  41. assert !issue.new_record?
  42. issue.reload
  43. assert_equal Project.find(2), issue.project
  44. assert_equal issue.project.trackers.first, issue.tracker
  45. assert_equal 'New ticket on a given project', issue.subject
  46. assert_equal User.find_by_login('jsmith'), issue.author
  47. assert_equal IssueStatus.find_by_name('Resolved'), issue.status
  48. assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
  49. assert_equal '2010-01-01', issue.start_date.to_s
  50. assert_equal '2010-12-31', issue.due_date.to_s
  51. assert_equal User.find_by_login('jsmith'), issue.assigned_to
  52. assert_equal Version.find_by_name('Alpha'), issue.fixed_version
  53. assert_equal 2.5, issue.estimated_hours
  54. assert_equal 30, issue.done_ratio
  55. assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
  56. # keywords should be removed from the email body
  57. assert !issue.description.match(/^Project:/i)
  58. assert !issue.description.match(/^Status:/i)
  59. assert !issue.description.match(/^Start Date:/i)
  60. # Email notification should be sent
  61. mail = ActionMailer::Base.deliveries.last
  62. assert_not_nil mail
  63. assert mail.subject.include?('New ticket on a given project')
  64. end
  65. def test_add_issue_with_default_tracker
  66. # This email contains: 'Project: onlinestore'
  67. issue = submit_email(
  68. 'ticket_on_given_project.eml',
  69. :issue => {:tracker => 'Support request'}
  70. )
  71. assert issue.is_a?(Issue)
  72. assert !issue.new_record?
  73. issue.reload
  74. assert_equal 'Support request', issue.tracker.name
  75. end
  76. def test_add_issue_with_status
  77. # This email contains: 'Project: onlinestore' and 'Status: Resolved'
  78. issue = submit_email('ticket_on_given_project.eml')
  79. assert issue.is_a?(Issue)
  80. assert !issue.new_record?
  81. issue.reload
  82. assert_equal Project.find(2), issue.project
  83. assert_equal IssueStatus.find_by_name("Resolved"), issue.status
  84. end
  85. def test_add_issue_with_attributes_override
  86. issue = submit_email(
  87. 'ticket_with_attributes.eml',
  88. :allow_override => 'tracker,category,priority'
  89. )
  90. assert issue.is_a?(Issue)
  91. assert !issue.new_record?
  92. issue.reload
  93. assert_equal 'New ticket on a given project', issue.subject
  94. assert_equal User.find_by_login('jsmith'), issue.author
  95. assert_equal Project.find(2), issue.project
  96. assert_equal 'Feature request', issue.tracker.to_s
  97. assert_equal 'Stock management', issue.category.to_s
  98. assert_equal 'Urgent', issue.priority.to_s
  99. assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
  100. end
  101. def test_add_issue_with_group_assignment
  102. with_settings :issue_group_assignment => '1' do
  103. issue = submit_email('ticket_on_given_project.eml') do |email|
  104. email.gsub!('Assigned to: John Smith', 'Assigned to: B Team')
  105. end
  106. assert issue.is_a?(Issue)
  107. assert !issue.new_record?
  108. issue.reload
  109. assert_equal Group.find(11), issue.assigned_to
  110. end
  111. end
  112. def test_add_issue_with_partial_attributes_override
  113. issue = submit_email(
  114. 'ticket_with_attributes.eml',
  115. :issue => {:priority => 'High'},
  116. :allow_override => ['tracker']
  117. )
  118. assert issue.is_a?(Issue)
  119. assert !issue.new_record?
  120. issue.reload
  121. assert_equal 'New ticket on a given project', issue.subject
  122. assert_equal User.find_by_login('jsmith'), issue.author
  123. assert_equal Project.find(2), issue.project
  124. assert_equal 'Feature request', issue.tracker.to_s
  125. assert_nil issue.category
  126. assert_equal 'High', issue.priority.to_s
  127. assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
  128. end
  129. def test_add_issue_with_spaces_between_attribute_and_separator
  130. issue = submit_email(
  131. 'ticket_with_spaces_between_attribute_and_separator.eml',
  132. :allow_override => 'tracker,category,priority'
  133. )
  134. assert issue.is_a?(Issue)
  135. assert !issue.new_record?
  136. issue.reload
  137. assert_equal 'New ticket on a given project', issue.subject
  138. assert_equal User.find_by_login('jsmith'), issue.author
  139. assert_equal Project.find(2), issue.project
  140. assert_equal 'Feature request', issue.tracker.to_s
  141. assert_equal 'Stock management', issue.category.to_s
  142. assert_equal 'Urgent', issue.priority.to_s
  143. assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
  144. end
  145. def test_add_issue_with_attachment_to_specific_project
  146. issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
  147. assert issue.is_a?(Issue)
  148. assert !issue.new_record?
  149. issue.reload
  150. assert_equal 'Ticket created by email with attachment', issue.subject
  151. assert_equal User.find_by_login('jsmith'), issue.author
  152. assert_equal Project.find(2), issue.project
  153. assert_equal 'This is a new ticket with attachments', issue.description
  154. # Attachment properties
  155. assert_equal 1, issue.attachments.size
  156. assert_equal 'Paella.jpg', issue.attachments.first.filename
  157. assert_equal 'image/jpeg', issue.attachments.first.content_type
  158. assert_equal 10790, issue.attachments.first.filesize
  159. end
  160. def test_add_issue_with_custom_fields
  161. issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'})
  162. assert issue.is_a?(Issue)
  163. assert !issue.new_record?
  164. issue.reload
  165. assert_equal 'New ticket with custom field values', issue.subject
  166. assert_equal 'PostgreSQL', issue.custom_field_value(1)
  167. assert_equal 'Value for a custom field', issue.custom_field_value(2)
  168. assert !issue.description.match(/^searchable field:/i)
  169. end
  170. def test_add_issue_with_version_custom_fields
  171. field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true, :tracker_ids => [1,2,3])
  172. issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'ecookbook'}) do |email|
  173. email << "Affected version: 1.0\n"
  174. end
  175. assert issue.is_a?(Issue)
  176. assert !issue.new_record?
  177. issue.reload
  178. assert_equal '2', issue.custom_field_value(field)
  179. end
  180. def test_add_issue_should_match_assignee_on_display_name
  181. user = User.generate!(:firstname => 'Foo Bar', :lastname => 'Foo Baz')
  182. User.add_to_project(user, Project.find(2))
  183. issue = submit_email('ticket_on_given_project.eml') do |email|
  184. email.sub!(/^Assigned to.*$/, 'Assigned to: Foo Bar Foo baz')
  185. end
  186. assert issue.is_a?(Issue)
  187. assert_equal user, issue.assigned_to
  188. end
  189. def test_add_issue_with_cc
  190. issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
  191. assert issue.is_a?(Issue)
  192. assert !issue.new_record?
  193. issue.reload
  194. assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
  195. assert_equal 1, issue.watcher_user_ids.size
  196. end
  197. def test_add_issue_by_unknown_user
  198. assert_no_difference 'User.count' do
  199. assert_equal false,
  200. submit_email(
  201. 'ticket_by_unknown_user.eml',
  202. :issue => {:project => 'ecookbook'}
  203. )
  204. end
  205. end
  206. def test_add_issue_by_anonymous_user
  207. Role.anonymous.add_permission!(:add_issues)
  208. assert_no_difference 'User.count' do
  209. issue = submit_email(
  210. 'ticket_by_unknown_user.eml',
  211. :issue => {:project => 'ecookbook'},
  212. :unknown_user => 'accept'
  213. )
  214. assert issue.is_a?(Issue)
  215. assert issue.author.anonymous?
  216. end
  217. end
  218. def test_add_issue_by_anonymous_user_with_no_from_address
  219. Role.anonymous.add_permission!(:add_issues)
  220. assert_no_difference 'User.count' do
  221. issue = submit_email(
  222. 'ticket_by_empty_user.eml',
  223. :issue => {:project => 'ecookbook'},
  224. :unknown_user => 'accept'
  225. )
  226. assert issue.is_a?(Issue)
  227. assert issue.author.anonymous?
  228. end
  229. end
  230. def test_add_issue_by_anonymous_user_on_private_project
  231. Role.anonymous.add_permission!(:add_issues)
  232. assert_no_difference 'User.count' do
  233. assert_no_difference 'Issue.count' do
  234. assert_equal false,
  235. submit_email(
  236. 'ticket_by_unknown_user.eml',
  237. :issue => {:project => 'onlinestore'},
  238. :unknown_user => 'accept'
  239. )
  240. end
  241. end
  242. end
  243. def test_add_issue_by_anonymous_user_on_private_project_without_permission_check
  244. assert_no_difference 'User.count' do
  245. assert_difference 'Issue.count' do
  246. issue = submit_email(
  247. 'ticket_by_unknown_user.eml',
  248. :issue => {:project => 'onlinestore'},
  249. :no_permission_check => '1',
  250. :unknown_user => 'accept'
  251. )
  252. assert issue.is_a?(Issue)
  253. assert issue.author.anonymous?
  254. assert !issue.project.is_public?
  255. assert_equal [issue.id, 1, 2], [issue.root_id, issue.lft, issue.rgt]
  256. end
  257. end
  258. end
  259. def test_add_issue_by_created_user
  260. Setting.default_language = 'en'
  261. assert_difference 'User.count' do
  262. issue = submit_email(
  263. 'ticket_by_unknown_user.eml',
  264. :issue => {:project => 'ecookbook'},
  265. :unknown_user => 'create'
  266. )
  267. assert issue.is_a?(Issue)
  268. assert issue.author.active?
  269. assert_equal 'john.doe@somenet.foo', issue.author.mail
  270. assert_equal 'John', issue.author.firstname
  271. assert_equal 'Doe', issue.author.lastname
  272. # account information
  273. email = ActionMailer::Base.deliveries.first
  274. assert_not_nil email
  275. assert email.subject.include?('account activation')
  276. login = mail_body(email).match(/\* Login: (.*)$/)[1].strip
  277. password = mail_body(email).match(/\* Password: (.*)$/)[1].strip
  278. assert_equal issue.author, User.try_to_login(login, password)
  279. end
  280. end
  281. def test_created_user_should_be_added_to_groups
  282. group1 = Group.generate!
  283. group2 = Group.generate!
  284. assert_difference 'User.count' do
  285. submit_email(
  286. 'ticket_by_unknown_user.eml',
  287. :issue => {:project => 'ecookbook'},
  288. :unknown_user => 'create',
  289. :default_group => "#{group1.name},#{group2.name}"
  290. )
  291. end
  292. user = User.order('id DESC').first
  293. assert_same_elements [group1, group2], user.groups
  294. end
  295. def test_created_user_should_not_receive_account_information_with_no_account_info_option
  296. assert_difference 'User.count' do
  297. submit_email(
  298. 'ticket_by_unknown_user.eml',
  299. :issue => {:project => 'ecookbook'},
  300. :unknown_user => 'create',
  301. :no_account_notice => '1'
  302. )
  303. end
  304. # only 1 email for the new issue notification
  305. assert_equal 1, ActionMailer::Base.deliveries.size
  306. email = ActionMailer::Base.deliveries.first
  307. assert_include 'Ticket by unknown user', email.subject
  308. end
  309. def test_created_user_should_have_mail_notification_to_none_with_no_notification_option
  310. assert_difference 'User.count' do
  311. submit_email(
  312. 'ticket_by_unknown_user.eml',
  313. :issue => {:project => 'ecookbook'},
  314. :unknown_user => 'create',
  315. :no_notification => '1'
  316. )
  317. end
  318. user = User.order('id DESC').first
  319. assert_equal 'none', user.mail_notification
  320. end
  321. def test_add_issue_without_from_header
  322. Role.anonymous.add_permission!(:add_issues)
  323. assert_equal false, submit_email('ticket_without_from_header.eml')
  324. end
  325. def test_add_issue_with_invalid_attributes
  326. issue = submit_email(
  327. 'ticket_with_invalid_attributes.eml',
  328. :allow_override => 'tracker,category,priority'
  329. )
  330. assert issue.is_a?(Issue)
  331. assert !issue.new_record?
  332. issue.reload
  333. assert_nil issue.assigned_to
  334. assert_nil issue.start_date
  335. assert_nil issue.due_date
  336. assert_equal 0, issue.done_ratio
  337. assert_equal 'Normal', issue.priority.to_s
  338. assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
  339. end
  340. def test_add_issue_with_invalid_project_should_be_assigned_to_default_project
  341. issue = submit_email('ticket_on_given_project.eml', :issue => {:project => 'ecookbook'}, :allow_override => 'project') do |email|
  342. email.gsub!(/^Project:.+$/, 'Project: invalid')
  343. end
  344. assert issue.is_a?(Issue)
  345. assert !issue.new_record?
  346. assert_equal 'ecookbook', issue.project.identifier
  347. end
  348. def test_add_issue_with_localized_attributes
  349. User.find_by_mail('jsmith@somenet.foo').update_attribute 'language', 'fr'
  350. issue = submit_email(
  351. 'ticket_with_localized_attributes.eml',
  352. :allow_override => 'tracker,category,priority'
  353. )
  354. assert issue.is_a?(Issue)
  355. assert !issue.new_record?
  356. issue.reload
  357. assert_equal 'New ticket on a given project', issue.subject
  358. assert_equal User.find_by_login('jsmith'), issue.author
  359. assert_equal Project.find(2), issue.project
  360. assert_equal 'Feature request', issue.tracker.to_s
  361. assert_equal 'Stock management', issue.category.to_s
  362. assert_equal 'Urgent', issue.priority.to_s
  363. assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
  364. end
  365. def test_add_issue_with_japanese_keywords
  366. ja_dev = "\xe9\x96\x8b\xe7\x99\xba"
  367. ja_dev.force_encoding('UTF-8') if ja_dev.respond_to?(:force_encoding)
  368. tracker = Tracker.create!(:name => ja_dev)
  369. Project.find(1).trackers << tracker
  370. issue = submit_email(
  371. 'japanese_keywords_iso_2022_jp.eml',
  372. :issue => {:project => 'ecookbook'},
  373. :allow_override => 'tracker'
  374. )
  375. assert_kind_of Issue, issue
  376. assert_equal tracker, issue.tracker
  377. end
  378. def test_add_issue_from_apple_mail
  379. issue = submit_email(
  380. 'apple_mail_with_attachment.eml',
  381. :issue => {:project => 'ecookbook'}
  382. )
  383. assert_kind_of Issue, issue
  384. assert_equal 1, issue.attachments.size
  385. attachment = issue.attachments.first
  386. assert_equal 'paella.jpg', attachment.filename
  387. assert_equal 10790, attachment.filesize
  388. assert File.exist?(attachment.diskfile)
  389. assert_equal 10790, File.size(attachment.diskfile)
  390. assert_equal 'caaf384198bcbc9563ab5c058acd73cd', attachment.digest
  391. end
  392. def test_thunderbird_with_attachment_ja
  393. issue = submit_email(
  394. 'thunderbird_with_attachment_ja.eml',
  395. :issue => {:project => 'ecookbook'}
  396. )
  397. assert_kind_of Issue, issue
  398. assert_equal 1, issue.attachments.size
  399. ja = "\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88.txt"
  400. ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
  401. attachment = issue.attachments.first
  402. assert_equal ja, attachment.filename
  403. assert_equal 5, attachment.filesize
  404. assert File.exist?(attachment.diskfile)
  405. assert_equal 5, File.size(attachment.diskfile)
  406. assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
  407. end
  408. def test_gmail_with_attachment_ja
  409. issue = submit_email(
  410. 'gmail_with_attachment_ja.eml',
  411. :issue => {:project => 'ecookbook'}
  412. )
  413. assert_kind_of Issue, issue
  414. assert_equal 1, issue.attachments.size
  415. ja = "\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88.txt"
  416. ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
  417. attachment = issue.attachments.first
  418. assert_equal ja, attachment.filename
  419. assert_equal 5, attachment.filesize
  420. assert File.exist?(attachment.diskfile)
  421. assert_equal 5, File.size(attachment.diskfile)
  422. assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
  423. end
  424. def test_thunderbird_with_attachment_latin1
  425. issue = submit_email(
  426. 'thunderbird_with_attachment_iso-8859-1.eml',
  427. :issue => {:project => 'ecookbook'}
  428. )
  429. assert_kind_of Issue, issue
  430. assert_equal 1, issue.attachments.size
  431. u = ""
  432. u.force_encoding('UTF-8') if u.respond_to?(:force_encoding)
  433. u1 = "\xc3\x84\xc3\xa4\xc3\x96\xc3\xb6\xc3\x9c\xc3\xbc"
  434. u1.force_encoding('UTF-8') if u1.respond_to?(:force_encoding)
  435. 11.times { u << u1 }
  436. attachment = issue.attachments.first
  437. assert_equal "#{u}.png", attachment.filename
  438. assert_equal 130, attachment.filesize
  439. assert File.exist?(attachment.diskfile)
  440. assert_equal 130, File.size(attachment.diskfile)
  441. assert_equal '4d80e667ac37dddfe05502530f152abb', attachment.digest
  442. end
  443. def test_gmail_with_attachment_latin1
  444. issue = submit_email(
  445. 'gmail_with_attachment_iso-8859-1.eml',
  446. :issue => {:project => 'ecookbook'}
  447. )
  448. assert_kind_of Issue, issue
  449. assert_equal 1, issue.attachments.size
  450. u = ""
  451. u.force_encoding('UTF-8') if u.respond_to?(:force_encoding)
  452. u1 = "\xc3\x84\xc3\xa4\xc3\x96\xc3\xb6\xc3\x9c\xc3\xbc"
  453. u1.force_encoding('UTF-8') if u1.respond_to?(:force_encoding)
  454. 11.times { u << u1 }
  455. attachment = issue.attachments.first
  456. assert_equal "#{u}.txt", attachment.filename
  457. assert_equal 5, attachment.filesize
  458. assert File.exist?(attachment.diskfile)
  459. assert_equal 5, File.size(attachment.diskfile)
  460. assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
  461. end
  462. def test_multiple_inline_text_parts_should_be_appended_to_issue_description
  463. issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
  464. assert_include 'first', issue.description
  465. assert_include 'second', issue.description
  466. assert_include 'third', issue.description
  467. end
  468. def test_attachment_text_part_should_be_added_as_issue_attachment
  469. issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
  470. assert_not_include 'Plain text attachment', issue.description
  471. attachment = issue.attachments.detect {|a| a.filename == 'textfile.txt'}
  472. assert_not_nil attachment
  473. assert_include 'Plain text attachment', File.read(attachment.diskfile)
  474. end
  475. def test_add_issue_with_iso_8859_1_subject
  476. issue = submit_email(
  477. 'subject_as_iso-8859-1.eml',
  478. :issue => {:project => 'ecookbook'}
  479. )
  480. str = "Testmail from Webmail: \xc3\xa4 \xc3\xb6 \xc3\xbc..."
  481. str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
  482. assert_kind_of Issue, issue
  483. assert_equal str, issue.subject
  484. end
  485. def test_add_issue_with_japanese_subject
  486. issue = submit_email(
  487. 'subject_japanese_1.eml',
  488. :issue => {:project => 'ecookbook'}
  489. )
  490. assert_kind_of Issue, issue
  491. ja = "\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88"
  492. ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
  493. assert_equal ja, issue.subject
  494. end
  495. def test_add_issue_with_no_subject_header
  496. issue = submit_email(
  497. 'no_subject_header.eml',
  498. :issue => {:project => 'ecookbook'}
  499. )
  500. assert_kind_of Issue, issue
  501. assert_equal '(no subject)', issue.subject
  502. end
  503. def test_add_issue_with_mixed_japanese_subject
  504. issue = submit_email(
  505. 'subject_japanese_2.eml',
  506. :issue => {:project => 'ecookbook'}
  507. )
  508. assert_kind_of Issue, issue
  509. ja = "Re: \xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88"
  510. ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
  511. assert_equal ja, issue.subject
  512. end
  513. def test_should_ignore_emails_from_locked_users
  514. User.find(2).lock!
  515. MailHandler.any_instance.expects(:dispatch).never
  516. assert_no_difference 'Issue.count' do
  517. assert_equal false, submit_email('ticket_on_given_project.eml')
  518. end
  519. end
  520. def test_should_ignore_emails_from_emission_address
  521. Role.anonymous.add_permission!(:add_issues)
  522. assert_no_difference 'User.count' do
  523. assert_equal false,
  524. submit_email(
  525. 'ticket_from_emission_address.eml',
  526. :issue => {:project => 'ecookbook'},
  527. :unknown_user => 'create'
  528. )
  529. end
  530. end
  531. def test_should_ignore_auto_replied_emails
  532. MailHandler.any_instance.expects(:dispatch).never
  533. [
  534. "X-Auto-Response-Suppress: OOF",
  535. "Auto-Submitted: auto-replied",
  536. "Auto-Submitted: Auto-Replied",
  537. "Auto-Submitted: auto-generated"
  538. ].each do |header|
  539. raw = IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
  540. raw = header + "\n" + raw
  541. assert_no_difference 'Issue.count' do
  542. assert_equal false, MailHandler.receive(raw), "email with #{header} header was not ignored"
  543. end
  544. end
  545. end
  546. def test_add_issue_should_send_email_notification
  547. Setting.notified_events = ['issue_added']
  548. ActionMailer::Base.deliveries.clear
  549. # This email contains: 'Project: onlinestore'
  550. issue = submit_email('ticket_on_given_project.eml')
  551. assert issue.is_a?(Issue)
  552. assert_equal 1, ActionMailer::Base.deliveries.size
  553. end
  554. def test_update_issue
  555. journal = submit_email('ticket_reply.eml')
  556. assert journal.is_a?(Journal)
  557. assert_equal User.find_by_login('jsmith'), journal.user
  558. assert_equal Issue.find(2), journal.journalized
  559. assert_match /This is reply/, journal.notes
  560. assert_equal false, journal.private_notes
  561. assert_equal 'Feature request', journal.issue.tracker.name
  562. end
  563. def test_update_issue_with_attribute_changes
  564. # This email contains: 'Status: Resolved'
  565. journal = submit_email('ticket_reply_with_status.eml')
  566. assert journal.is_a?(Journal)
  567. issue = Issue.find(journal.issue.id)
  568. assert_equal User.find_by_login('jsmith'), journal.user
  569. assert_equal Issue.find(2), journal.journalized
  570. assert_match /This is reply/, journal.notes
  571. assert_equal 'Feature request', journal.issue.tracker.name
  572. assert_equal IssueStatus.find_by_name("Resolved"), issue.status
  573. assert_equal '2010-01-01', issue.start_date.to_s
  574. assert_equal '2010-12-31', issue.due_date.to_s
  575. assert_equal User.find_by_login('jsmith'), issue.assigned_to
  576. assert_equal "52.6", issue.custom_value_for(CustomField.find_by_name('Float field')).value
  577. # keywords should be removed from the email body
  578. assert !journal.notes.match(/^Status:/i)
  579. assert !journal.notes.match(/^Start Date:/i)
  580. end
  581. def test_update_issue_with_attachment
  582. assert_difference 'Journal.count' do
  583. assert_difference 'JournalDetail.count' do
  584. assert_difference 'Attachment.count' do
  585. assert_no_difference 'Issue.count' do
  586. journal = submit_email('ticket_with_attachment.eml') do |raw|
  587. raw.gsub! /^Subject: .*$/, 'Subject: Re: [Cookbook - Feature #2] (New) Add ingredients categories'
  588. end
  589. end
  590. end
  591. end
  592. end
  593. journal = Journal.first(:order => 'id DESC')
  594. assert_equal Issue.find(2), journal.journalized
  595. assert_equal 1, journal.details.size
  596. detail = journal.details.first
  597. assert_equal 'attachment', detail.property
  598. assert_equal 'Paella.jpg', detail.value
  599. end
  600. def test_update_issue_should_send_email_notification
  601. ActionMailer::Base.deliveries.clear
  602. journal = submit_email('ticket_reply.eml')
  603. assert journal.is_a?(Journal)
  604. assert_equal 1, ActionMailer::Base.deliveries.size
  605. end
  606. def test_update_issue_should_not_set_defaults
  607. journal = submit_email(
  608. 'ticket_reply.eml',
  609. :issue => {:tracker => 'Support request', :priority => 'High'}
  610. )
  611. assert journal.is_a?(Journal)
  612. assert_match /This is reply/, journal.notes
  613. assert_equal 'Feature request', journal.issue.tracker.name
  614. assert_equal 'Normal', journal.issue.priority.name
  615. end
  616. def test_replying_to_a_private_note_should_add_reply_as_private
  617. private_journal = Journal.create!(:notes => 'Private notes', :journalized => Issue.find(1), :private_notes => true, :user_id => 2)
  618. assert_difference 'Journal.count' do
  619. journal = submit_email('ticket_reply.eml') do |email|
  620. email.sub! %r{^In-Reply-To:.*$}, "In-Reply-To: <redmine.journal-#{private_journal.id}.20060719210421@osiris>"
  621. end
  622. assert_kind_of Journal, journal
  623. assert_match /This is reply/, journal.notes
  624. assert_equal true, journal.private_notes
  625. end
  626. end
  627. def test_reply_to_a_message
  628. m = submit_email('message_reply.eml')
  629. assert m.is_a?(Message)
  630. assert !m.new_record?
  631. m.reload
  632. assert_equal 'Reply via email', m.subject
  633. # The email replies to message #2 which is part of the thread of message #1
  634. assert_equal Message.find(1), m.parent
  635. end
  636. def test_reply_to_a_message_by_subject
  637. m = submit_email('message_reply_by_subject.eml')
  638. assert m.is_a?(Message)
  639. assert !m.new_record?
  640. m.reload
  641. assert_equal 'Reply to the first post', m.subject
  642. assert_equal Message.find(1), m.parent
  643. end
  644. def test_should_strip_tags_of_html_only_emails
  645. issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
  646. assert issue.is_a?(Issue)
  647. assert !issue.new_record?
  648. issue.reload
  649. assert_equal 'HTML email', issue.subject
  650. assert_equal 'This is a html-only email.', issue.description
  651. end
  652. test "truncate emails with no setting should add the entire email into the issue" do
  653. with_settings :mail_handler_body_delimiters => '' do
  654. issue = submit_email('ticket_on_given_project.eml')
  655. assert_issue_created(issue)
  656. assert issue.description.include?('---')
  657. assert issue.description.include?('This paragraph is after the delimiter')
  658. end
  659. end
  660. test "truncate emails with a single string should truncate the email at the delimiter for the issue" do
  661. with_settings :mail_handler_body_delimiters => '---' do
  662. issue = submit_email('ticket_on_given_project.eml')
  663. assert_issue_created(issue)
  664. assert issue.description.include?('This paragraph is before delimiters')
  665. assert issue.description.include?('--- This line starts with a delimiter')
  666. assert !issue.description.match(/^---$/)
  667. assert !issue.description.include?('This paragraph is after the delimiter')
  668. end
  669. end
  670. test "truncate emails with a single quoted reply should truncate the email at the delimiter with the quoted reply symbols (>)" do
  671. with_settings :mail_handler_body_delimiters => '--- Reply above. Do not remove this line. ---' do
  672. journal = submit_email('issue_update_with_quoted_reply_above.eml')
  673. assert journal.is_a?(Journal)
  674. assert journal.notes.include?('An update to the issue by the sender.')
  675. assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
  676. assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
  677. end
  678. end
  679. test "truncate emails with multiple quoted replies should truncate the email at the delimiter with the quoted reply symbols (>)" do
  680. with_settings :mail_handler_body_delimiters => '--- Reply above. Do not remove this line. ---' do
  681. journal = submit_email('issue_update_with_multiple_quoted_reply_above.eml')
  682. assert journal.is_a?(Journal)
  683. assert journal.notes.include?('An update to the issue by the sender.')
  684. assert !journal.notes.match(Regexp.escape("--- Reply above. Do not remove this line. ---"))
  685. assert !journal.notes.include?('Looks like the JSON api for projects was missed.')
  686. end
  687. end
  688. test "truncate emails with multiple strings should truncate the email at the first delimiter found (BREAK)" do
  689. with_settings :mail_handler_body_delimiters => "---\nBREAK" do
  690. issue = submit_email('ticket_on_given_project.eml')
  691. assert_issue_created(issue)
  692. assert issue.description.include?('This paragraph is before delimiters')
  693. assert !issue.description.include?('BREAK')
  694. assert !issue.description.include?('This paragraph is between delimiters')
  695. assert !issue.description.match(/^---$/)
  696. assert !issue.description.include?('This paragraph is after the delimiter')
  697. end
  698. end
  699. def test_attachments_that_match_mail_handler_excluded_filenames_should_be_ignored
  700. with_settings :mail_handler_excluded_filenames => '*.vcf, *.jpg' do
  701. issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
  702. assert issue.is_a?(Issue)
  703. assert !issue.new_record?
  704. assert_equal 0, issue.reload.attachments.size
  705. end
  706. end
  707. def test_attachments_that_do_not_match_mail_handler_excluded_filenames_should_be_attached
  708. with_settings :mail_handler_excluded_filenames => '*.vcf, *.gif' do
  709. issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
  710. assert issue.is_a?(Issue)
  711. assert !issue.new_record?
  712. assert_equal 1, issue.reload.attachments.size
  713. end
  714. end
  715. def test_email_with_long_subject_line
  716. issue = submit_email('ticket_with_long_subject.eml')
  717. assert issue.is_a?(Issue)
  718. assert_equal issue.subject, 'New ticket on a given project with a very long subject line which exceeds 255 chars and should not be ignored but chopped off. And if the subject line is still not long enough, we just add more text. And more text. Wow, this is really annoying. Especially, if you have nothing to say...'[0,255]
  719. end
  720. def test_new_user_from_attributes_should_return_valid_user
  721. to_test = {
  722. # [address, name] => [login, firstname, lastname]
  723. ['jsmith@example.net', nil] => ['jsmith@example.net', 'jsmith', '-'],
  724. ['jsmith@example.net', 'John'] => ['jsmith@example.net', 'John', '-'],
  725. ['jsmith@example.net', 'John Smith'] => ['jsmith@example.net', 'John', 'Smith'],
  726. ['jsmith@example.net', 'John Paul Smith'] => ['jsmith@example.net', 'John', 'Paul Smith'],
  727. ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsTheMaximumLength Smith'] => ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsT', 'Smith'],
  728. ['jsmith@example.net', 'John AVeryLongLastnameThatExceedsTheMaximumLength'] => ['jsmith@example.net', 'John', 'AVeryLongLastnameThatExceedsTh']
  729. }
  730. to_test.each do |attrs, expected|
  731. user = MailHandler.new_user_from_attributes(attrs.first, attrs.last)
  732. assert user.valid?, user.errors.full_messages.to_s
  733. assert_equal attrs.first, user.mail
  734. assert_equal expected[0], user.login
  735. assert_equal expected[1], user.firstname
  736. assert_equal expected[2], user.lastname
  737. assert_equal 'only_my_events', user.mail_notification
  738. end
  739. end
  740. def test_new_user_from_attributes_should_use_default_login_if_invalid
  741. user = MailHandler.new_user_from_attributes('foo+bar@example.net')
  742. assert user.valid?
  743. assert user.login =~ /^user[a-f0-9]+$/
  744. assert_equal 'foo+bar@example.net', user.mail
  745. end
  746. def test_new_user_with_utf8_encoded_fullname_should_be_decoded
  747. assert_difference 'User.count' do
  748. issue = submit_email(
  749. 'fullname_of_sender_as_utf8_encoded.eml',
  750. :issue => {:project => 'ecookbook'},
  751. :unknown_user => 'create'
  752. )
  753. end
  754. user = User.first(:order => 'id DESC')
  755. assert_equal "foo@example.org", user.mail
  756. str1 = "\xc3\x84\xc3\xa4"
  757. str2 = "\xc3\x96\xc3\xb6"
  758. str1.force_encoding('UTF-8') if str1.respond_to?(:force_encoding)
  759. str2.force_encoding('UTF-8') if str2.respond_to?(:force_encoding)
  760. assert_equal str1, user.firstname
  761. assert_equal str2, user.lastname
  762. end
  763. def test_extract_options_from_env_should_return_options
  764. options = MailHandler.extract_options_from_env({
  765. 'tracker' => 'defect',
  766. 'project' => 'foo',
  767. 'unknown_user' => 'create'
  768. })
  769. assert_equal({
  770. :issue => {:tracker => 'defect', :project => 'foo'},
  771. :unknown_user => 'create'
  772. }, options)
  773. end
  774. private
  775. def submit_email(filename, options={})
  776. raw = IO.read(File.join(FIXTURES_PATH, filename))
  777. yield raw if block_given?
  778. MailHandler.receive(raw, options)
  779. end
  780. def assert_issue_created(issue)
  781. assert issue.is_a?(Issue)
  782. assert !issue.new_record?
  783. issue.reload
  784. end
  785. end