diff test/unit/mail_handler_test.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents 0c939c159af4
children 433d4f72a19b
line wrap: on
line diff
--- a/test/unit/mail_handler_test.rb	Fri Feb 24 18:36:29 2012 +0000
+++ b/test/unit/mail_handler_test.rb	Fri Feb 24 19:09:32 2012 +0000
@@ -110,6 +110,18 @@
     assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
   end
 
+  def test_add_issue_with_group_assignment
+    with_settings :issue_group_assignment => '1' do
+      issue = submit_email('ticket_on_given_project.eml') do |email|
+        email.gsub!('Assigned to: John Smith', 'Assigned to: B Team')
+      end
+      assert issue.is_a?(Issue)
+      assert !issue.new_record?
+      issue.reload
+      assert_equal Group.find(11), issue.assigned_to
+    end
+  end
+
   def test_add_issue_with_partial_attributes_override
     issue = submit_email('ticket_with_attributes.eml', :issue => {:priority => 'High'}, :allow_override => ['tracker'])
     assert issue.is_a?(Issue)
@@ -279,6 +291,16 @@
     assert_equal tracker, issue.tracker
   end
 
+  def test_add_issue_from_apple_mail
+    issue = submit_email('apple_mail_with_attachment.eml', :issue => {:project => 'ecookbook'})
+    assert_kind_of Issue, issue
+    assert_equal 1, issue.attachments.size
+
+    attachment = issue.attachments.first
+    assert_equal 'paella.jpg', attachment.filename
+    assert_equal 10790, attachment.filesize
+  end
+
   def test_should_ignore_emails_from_emission_address
     Role.anonymous.add_permission!(:add_issues)
     assert_no_difference 'User.count' do
@@ -295,7 +317,7 @@
     assert_equal 1, ActionMailer::Base.deliveries.size
   end
 
-  def test_add_issue_note
+  def test_update_issue
     journal = submit_email('ticket_reply.eml')
     assert journal.is_a?(Journal)
     assert_equal User.find_by_login('jsmith'), journal.user
@@ -304,7 +326,7 @@
     assert_equal 'Feature request', journal.issue.tracker.name
   end
 
-  def test_add_issue_note_with_attribute_changes
+  def test_update_issue_with_attribute_changes
     # This email contains: 'Status: Resolved'
     journal = submit_email('ticket_reply_with_status.eml')
     assert journal.is_a?(Journal)
@@ -323,14 +345,35 @@
     assert !journal.notes.match(/^Start Date:/i)
   end
 
-  def test_add_issue_note_should_send_email_notification
+  def test_update_issue_with_attachment
+    assert_difference 'Journal.count' do
+      assert_difference 'JournalDetail.count' do
+        assert_difference 'Attachment.count' do
+          assert_no_difference 'Issue.count' do
+            journal = submit_email('ticket_with_attachment.eml') do |raw|
+              raw.gsub! /^Subject: .*$/, 'Subject: Re: [Cookbook - Feature #2] (New) Add ingredients categories'
+            end
+          end
+        end
+      end
+    end
+    journal = Journal.first(:order => 'id DESC')
+    assert_equal Issue.find(2), journal.journalized
+    assert_equal 1, journal.details.size
+
+    detail = journal.details.first
+    assert_equal 'attachment', detail.property
+    assert_equal 'Paella.jpg', detail.value
+  end
+
+  def test_update_issue_should_send_email_notification
     ActionMailer::Base.deliveries.clear
     journal = submit_email('ticket_reply.eml')
     assert journal.is_a?(Journal)
     assert_equal 1, ActionMailer::Base.deliveries.size
   end
 
-  def test_add_issue_note_should_not_set_defaults
+  def test_update_issue_should_not_set_defaults
     journal = submit_email('ticket_reply.eml', :issue => {:tracker => 'Support request', :priority => 'High'})
     assert journal.is_a?(Journal)
     assert_match /This is reply/, journal.notes
@@ -442,10 +485,51 @@
     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]
   end
 
+  def test_new_user_from_attributes_should_return_valid_user
+    to_test = {
+      # [address, name] => [login, firstname, lastname]
+      ['jsmith@example.net', nil] => ['jsmith@example.net', 'jsmith', '-'],
+      ['jsmith@example.net', 'John'] => ['jsmith@example.net', 'John', '-'],
+      ['jsmith@example.net', 'John Smith'] => ['jsmith@example.net', 'John', 'Smith'],
+      ['jsmith@example.net', 'John Paul Smith'] => ['jsmith@example.net', 'John', 'Paul Smith'],
+      ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsTheMaximumLength Smith'] => ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsT', 'Smith'],
+      ['jsmith@example.net', 'John AVeryLongLastnameThatExceedsTheMaximumLength'] => ['jsmith@example.net', 'John', 'AVeryLongLastnameThatExceedsTh'],
+      ['alongemailaddressthatexceedsloginlength@example.net', 'John Smith'] => ['alongemailaddressthatexceedslo', 'John', 'Smith']
+    }
+
+    to_test.each do |attrs, expected|
+      user = MailHandler.new_user_from_attributes(attrs.first, attrs.last)
+
+      assert user.valid?
+      assert_equal attrs.first, user.mail
+      assert_equal expected[0], user.login
+      assert_equal expected[1], user.firstname
+      assert_equal expected[2], user.lastname
+    end
+  end
+
+  def test_new_user_from_attributes_should_respect_minimum_password_length
+    with_settings :password_min_length => 15 do
+      user = MailHandler.new_user_from_attributes('jsmith@example.net')
+      assert user.valid?
+      assert user.password.length >= 15
+    end
+  end
+ 
+  def test_new_user_from_attributes_should_use_default_login_if_invalid
+    MailHandler.new_user_from_attributes('alongemailaddressthatexceedsloginlength-1@example.net').save!
+
+    # another long address that would result in duplicate login
+    user = MailHandler.new_user_from_attributes('alongemailaddressthatexceedsloginlength-2@example.net')
+    assert user.valid?
+    assert user.login =~ /^user[a-f0-9]+$/
+  end
+
   private
 
   def submit_email(filename, options={})
     raw = IO.read(File.join(FIXTURES_PATH, filename))
+    yield raw if block_given?
     MailHandler.receive(raw, options)
   end