/spec/lib/gitlab/email/handler/create_note_handler_spec.rb

https://gitlab.com/nulldoxyz/gitlab-ce · Ruby · 177 lines · 172 code · 5 blank · 0 comment · 0 complexity · aae8d665dcc7f68e483775daa6d71982 MD5 · raw file

  1. require 'spec_helper'
  2. require_relative '../email_shared_blocks'
  3. describe Gitlab::Email::Handler::CreateNoteHandler do
  4. include_context :email_shared_context
  5. it_behaves_like :reply_processing_shared_examples
  6. before do
  7. stub_incoming_email_setting(enabled: true, address: "reply+%{key}@appmail.adventuretime.ooo")
  8. stub_config_setting(host: 'localhost')
  9. end
  10. let(:email_raw) { fixture_file('emails/valid_reply.eml') }
  11. let(:project) { create(:project, :public, :repository) }
  12. let(:user) { create(:user) }
  13. let(:note) { create(:diff_note_on_merge_request, project: project) }
  14. let(:noteable) { note.noteable }
  15. let!(:sent_notification) do
  16. SentNotification.record_note(note, user.id, mail_key)
  17. end
  18. context "when the recipient address doesn't include a mail key" do
  19. let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, "") }
  20. it "raises a UnknownIncomingEmail" do
  21. expect { receiver.execute }.to raise_error(Gitlab::Email::UnknownIncomingEmail)
  22. end
  23. end
  24. context "when no sent notification for the mail key could be found" do
  25. let(:email_raw) { fixture_file('emails/wrong_mail_key.eml') }
  26. it "raises a SentNotificationNotFoundError" do
  27. expect { receiver.execute }.to raise_error(Gitlab::Email::SentNotificationNotFoundError)
  28. end
  29. end
  30. context "when the noteable could not be found" do
  31. before do
  32. noteable.destroy
  33. end
  34. it "raises a NoteableNotFoundError" do
  35. expect { receiver.execute }.to raise_error(Gitlab::Email::NoteableNotFoundError)
  36. end
  37. end
  38. context "when the note could not be saved" do
  39. before do
  40. allow_any_instance_of(Note).to receive(:persisted?).and_return(false)
  41. end
  42. it "raises an InvalidNoteError" do
  43. expect { receiver.execute }.to raise_error(Gitlab::Email::InvalidNoteError)
  44. end
  45. context 'because the note was commands only' do
  46. let!(:email_raw) { fixture_file("emails/commands_only_reply.eml") }
  47. context 'and current user cannot update noteable' do
  48. it 'raises a CommandsOnlyNoteError' do
  49. expect { receiver.execute }.to raise_error(Gitlab::Email::InvalidNoteError)
  50. end
  51. end
  52. context 'and current user can update noteable' do
  53. before do
  54. project.add_developer(user)
  55. end
  56. it 'does not raise an error' do
  57. expect(TodoService.new.todo_exist?(noteable, user)).to be_falsy
  58. # One system note is created for the 'close' event
  59. expect { receiver.execute }.to change { noteable.notes.count }.by(1)
  60. expect(noteable.reload).to be_closed
  61. expect(TodoService.new.todo_exist?(noteable, user)).to be_truthy
  62. end
  63. end
  64. end
  65. end
  66. context 'when the note contains quick actions' do
  67. let!(:email_raw) { fixture_file("emails/commands_in_reply.eml") }
  68. context 'and current user cannot update noteable' do
  69. it 'post a note and does not update the noteable' do
  70. expect(TodoService.new.todo_exist?(noteable, user)).to be_falsy
  71. # One system note is created for the new note
  72. expect { receiver.execute }.to change { noteable.notes.count }.by(1)
  73. expect(noteable.reload).to be_open
  74. expect(TodoService.new.todo_exist?(noteable, user)).to be_falsy
  75. end
  76. end
  77. context 'and current user can update noteable' do
  78. before do
  79. project.add_developer(user)
  80. end
  81. it 'post a note and updates the noteable' do
  82. expect(TodoService.new.todo_exist?(noteable, user)).to be_falsy
  83. # One system note is created for the new note, one for the 'close' event
  84. expect { receiver.execute }.to change { noteable.notes.count }.by(2)
  85. expect(noteable.reload).to be_closed
  86. expect(TodoService.new.todo_exist?(noteable, user)).to be_truthy
  87. end
  88. end
  89. end
  90. context "when the reply is blank" do
  91. let!(:email_raw) { fixture_file("emails/no_content_reply.eml") }
  92. it "raises an EmptyEmailError" do
  93. expect { receiver.execute }.to raise_error(Gitlab::Email::EmptyEmailError)
  94. end
  95. end
  96. context "when everything is fine" do
  97. before do
  98. setup_attachment
  99. end
  100. it "creates a comment" do
  101. expect { receiver.execute }.to change { noteable.notes.count }.by(1)
  102. new_note = noteable.notes.last
  103. expect(new_note.author).to eq(sent_notification.recipient)
  104. expect(new_note.position).to eq(note.position)
  105. expect(new_note.note).to include("I could not disagree more.")
  106. expect(new_note.in_reply_to?(note)).to be_truthy
  107. end
  108. it "adds all attachments" do
  109. receiver.execute
  110. note = noteable.notes.last
  111. expect(note.note).to include(markdown)
  112. end
  113. context 'when sub-addressing is not supported' do
  114. before do
  115. stub_incoming_email_setting(enabled: true, address: nil)
  116. end
  117. shared_examples 'an email that contains a mail key' do |header|
  118. it "fetches the mail key from the #{header} header and creates a comment" do
  119. expect { receiver.execute }.to change { noteable.notes.count }.by(1)
  120. new_note = noteable.notes.last
  121. expect(new_note.author).to eq(sent_notification.recipient)
  122. expect(new_note.position).to eq(note.position)
  123. expect(new_note.note).to include('I could not disagree more.')
  124. end
  125. end
  126. context 'mail key is in the References header' do
  127. let(:email_raw) { fixture_file('emails/reply_without_subaddressing_and_key_inside_references.eml') }
  128. it_behaves_like 'an email that contains a mail key', 'References'
  129. end
  130. context 'mail key is in the References header with a comma' do
  131. let(:email_raw) { fixture_file('emails/reply_without_subaddressing_and_key_inside_references_with_a_comma.eml') }
  132. it_behaves_like 'an email that contains a mail key', 'References'
  133. end
  134. end
  135. end
  136. end