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

https://gitlab.com/Munken/gitlab-ce · Ruby · 179 lines · 174 code · 5 blank · 0 comment · 0 complexity · 3307f07b871e9e333010cfbcab8d00e5 MD5 · raw file

  1. require 'spec_helper'
  2. require_relative '../email_shared_blocks'
  3. describe Gitlab::Email::Handler::CreateNoteHandler, lib: true do
  4. include_context :email_shared_context
  5. it_behaves_like :email_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) }
  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 email was auto generated" do
  31. let!(:mail_key) { '636ca428858779856c226bb145ef4fad' }
  32. let!(:email_raw) { fixture_file("emails/auto_reply.eml") }
  33. it "raises an AutoGeneratedEmailError" do
  34. expect { receiver.execute }.to raise_error(Gitlab::Email::AutoGeneratedEmailError)
  35. end
  36. end
  37. context "when the noteable could not be found" do
  38. before do
  39. noteable.destroy
  40. end
  41. it "raises a NoteableNotFoundError" do
  42. expect { receiver.execute }.to raise_error(Gitlab::Email::NoteableNotFoundError)
  43. end
  44. end
  45. context "when the note could not be saved" do
  46. before do
  47. allow_any_instance_of(Note).to receive(:persisted?).and_return(false)
  48. end
  49. it "raises an InvalidNoteError" do
  50. expect { receiver.execute }.to raise_error(Gitlab::Email::InvalidNoteError)
  51. end
  52. context 'because the note was commands only' do
  53. let!(:email_raw) { fixture_file("emails/commands_only_reply.eml") }
  54. context 'and current user cannot update noteable' do
  55. it 'raises a CommandsOnlyNoteError' do
  56. expect { receiver.execute }.to raise_error(Gitlab::Email::InvalidNoteError)
  57. end
  58. end
  59. context 'and current user can update noteable' do
  60. before do
  61. project.team << [user, :developer]
  62. end
  63. it 'does not raise an error' do
  64. expect(TodoService.new.todo_exist?(noteable, user)).to be_falsy
  65. # One system note is created for the 'close' event
  66. expect { receiver.execute }.to change { noteable.notes.count }.by(1)
  67. expect(noteable.reload).to be_closed
  68. expect(TodoService.new.todo_exist?(noteable, user)).to be_truthy
  69. end
  70. end
  71. end
  72. end
  73. context 'when the note contains slash commands' do
  74. let!(:email_raw) { fixture_file("emails/commands_in_reply.eml") }
  75. context 'and current user cannot update noteable' do
  76. it 'post a note and does not update the noteable' do
  77. expect(TodoService.new.todo_exist?(noteable, user)).to be_falsy
  78. # One system note is created for the new note
  79. expect { receiver.execute }.to change { noteable.notes.count }.by(1)
  80. expect(noteable.reload).to be_open
  81. expect(TodoService.new.todo_exist?(noteable, user)).to be_falsy
  82. end
  83. end
  84. context 'and current user can update noteable' do
  85. before do
  86. project.team << [user, :developer]
  87. end
  88. it 'post a note and updates the noteable' do
  89. expect(TodoService.new.todo_exist?(noteable, user)).to be_falsy
  90. # One system note is created for the new note, one for the 'close' event
  91. expect { receiver.execute }.to change { noteable.notes.count }.by(2)
  92. expect(noteable.reload).to be_closed
  93. expect(TodoService.new.todo_exist?(noteable, user)).to be_truthy
  94. end
  95. end
  96. end
  97. context "when the reply is blank" do
  98. let!(:email_raw) { fixture_file("emails/no_content_reply.eml") }
  99. it "raises an EmptyEmailError" do
  100. expect { receiver.execute }.to raise_error(Gitlab::Email::EmptyEmailError)
  101. end
  102. end
  103. context "when everything is fine" do
  104. before do
  105. setup_attachment
  106. end
  107. it "creates a comment" do
  108. expect { receiver.execute }.to change { noteable.notes.count }.by(1)
  109. new_note = noteable.notes.last
  110. expect(new_note.author).to eq(sent_notification.recipient)
  111. expect(new_note.position).to eq(note.position)
  112. expect(new_note.note).to include("I could not disagree more.")
  113. end
  114. it "adds all attachments" do
  115. receiver.execute
  116. note = noteable.notes.last
  117. expect(note.note).to include(markdown)
  118. end
  119. context 'when sub-addressing is not supported' do
  120. before do
  121. stub_incoming_email_setting(enabled: true, address: nil)
  122. end
  123. shared_examples 'an email that contains a mail key' do |header|
  124. it "fetches the mail key from the #{header} header and creates a comment" do
  125. expect { receiver.execute }.to change { noteable.notes.count }.by(1)
  126. new_note = noteable.notes.last
  127. expect(new_note.author).to eq(sent_notification.recipient)
  128. expect(new_note.position).to eq(note.position)
  129. expect(new_note.note).to include('I could not disagree more.')
  130. end
  131. end
  132. context 'mail key is in the References header' do
  133. let(:email_raw) { fixture_file('emails/reply_without_subaddressing_and_key_inside_references.eml') }
  134. it_behaves_like 'an email that contains a mail key', 'References'
  135. end
  136. end
  137. end
  138. end