Project

General

Profile

Feature #38274 » 1-receive_news_comment_reply_with_tests.patch

Felix Schäfer, 2023-02-14 16:13

View differences:

app/models/mail_handler.rb
305 305
    end
306 306
  end
307 307

  
308
  # Receives a reply to a news entry
309
  def receive_news_reply(news_id)
310
    news = News.find_by_id(news_id)
311
    if news.nil?
312
      raise MissingContainer, "reply to nonexistant news [#{news_id}]"
313
    end
314

  
315
    # Never receive emails to projects where adding news comments is not possible
316
    project = news.project
317
    raise NotAllowedInProject, "not possible to add news comments to project [#{project.name}]" unless project.allows_to?(:comment_news)
318

  
319
    unless handler_options[:no_permission_check]
320
      unless news.commentable?(user)
321
        raise InsufficientPermissions, "not allowed to comment on news item [#{news.id} #{news.title}]"
322
      end
323
    end
324

  
325
    comment = news.comments.new
326
    comment.author = user
327
    comment.comments = cleaned_up_text_body
328
    comment.save!
329
    comment
330
  end
331

  
332
  # Receives a reply to a comment to a news entry
333
  def receive_comment_reply(comment_id)
334
    comment = Comment.find_by_id(comment_id)
335

  
336
    if comment && comment.commented_type == 'News'
337
      receive_news_reply(comment.commented.id)
338
    else
339
      raise MissingContainer, "reply to nonexistant comment [#{comment_id}]"
340
    end
341
  end
342

  
308 343
  def add_attachments(obj)
309 344
    if email.attachments && email.attachments.any?
310 345
      email.attachments.each do |attachment|
test/fixtures/mail_handler/news_comment_reply.eml
1
Message-ID: <4974C93E.3071105@somenet.foo>
2
Date: Mon, 19 Jan 2023 19:41:02 +0100
3
From: "John Smith" <jsmith@somenet.foo>
4
User-Agent: Thunderbird 2.0.0.19 (Windows/20081209)
5
MIME-Version: 1.0
6
To: redmine@somenet.foo
7
Subject: News comment reply via email
8
References: <redmine.comment-1.20230214171800@somenet.foo>
9
In-Reply-To: <redmine.comment-1.20230214171800@somenet.foo>
10
Content-Type: text/plain; charset=UTF-8; format=flowed
11
Content-Transfer-Encoding: 7bit
12

  
13
This is a reply to a comment.
14

  
15

  
test/fixtures/mail_handler/news_reply.eml
1
Message-ID: <4974C93E.3071005@somenet.foo>
2
Date: Mon, 19 Jan 2023 19:41:02 +0100
3
From: "John Smith" <jsmith@somenet.foo>
4
User-Agent: Thunderbird 2.0.0.19 (Windows/20081209)
5
MIME-Version: 1.0
6
To: redmine@somenet.foo
7
Subject: News comment via email
8
References: <redmine.news-1.20230214171800@somenet.foo>
9
In-Reply-To: <redmine.news-1.20230214171800@somenet.foo>
10
Content-Type: text/plain; charset=UTF-8; format=flowed
11
Content-Transfer-Encoding: 7bit
12

  
13
This is a reply to a news.
14

  
15

  
test/unit/mail_handler_test.rb
28 28
           :workflows, :trackers, :projects_trackers,
29 29
           :versions, :enumerations, :issue_categories,
30 30
           :custom_fields, :custom_fields_trackers, :custom_fields_projects, :custom_values,
31
           :boards, :messages, :watchers
31
           :boards, :messages, :watchers, :news, :comments
32 32

  
33 33
  FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
34 34

  
......
1159 1159
    end
1160 1160
  end
1161 1161

  
1162
  def test_reply_to_a_news
1163
    m = submit_email('news_reply.eml')
1164
    assert m.is_a?(Comment)
1165
    assert !m.new_record?
1166
    m.reload
1167
    assert_equal News.find(1), m.commented
1168
    assert_equal "This is a reply to a news.", m.content
1169
  end
1170

  
1171
  def test_reply_to_a_news_comment
1172
    m = submit_email('news_comment_reply.eml')
1173
    assert m.is_a?(Comment)
1174
    assert !m.new_record?
1175
    m.reload
1176
    assert_equal News.find(1), m.commented
1177
    assert_equal "This is a reply to a comment.", m.content
1178
  end
1179

  
1180
  def test_reply_to_a_nonexistant_news
1181
    News.find(1).destroy
1182
    assert_no_difference('Comment.count') do
1183
      assert_not submit_email('news_reply.eml')
1184
      assert_not submit_email('news_comment_reply.eml')
1185
    end
1186
  end
1187

  
1188
  def test_reply_to_a_news_without_permission
1189
    Role.all.each {|r| r.remove_permission! :comment_news}
1190
    assert_no_difference('Comment.count') do
1191
      assert_not submit_email('news_reply.eml')
1192
      assert_not submit_email('news_comment_reply.eml')
1193
    end
1194
  end
1195

  
1162 1196
  def test_should_convert_tags_of_html_only_emails
1163 1197
    with_settings :text_formatting => 'textile' do
1164 1198
      issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
(2-2/2)