/spec/lib/gitlab/email/receiver_spec.rb

https://gitlab.com/imrampukar/gitlab-ce · Ruby · 164 lines · 159 code · 5 blank · 0 comment · 0 complexity · d4469a949a885dd617d171c6a9af96b0 MD5 · raw file

  1. require "spec_helper"
  2. describe Gitlab::Email::Receiver, lib: true do
  3. before do
  4. stub_incoming_email_setting(enabled: true, address: "reply+%{key}@appmail.adventuretime.ooo")
  5. stub_config_setting(host: 'localhost')
  6. end
  7. let(:reply_key) { "59d8df8370b7e95c5a49fbf86aeb2c93" }
  8. let(:email_raw) { fixture_file('emails/valid_reply.eml') }
  9. let(:project) { create(:project, :public) }
  10. let(:noteable) { create(:issue, project: project) }
  11. let(:user) { create(:user) }
  12. let!(:sent_notification) { SentNotification.record(noteable, user.id, reply_key) }
  13. let(:receiver) { described_class.new(email_raw) }
  14. context "when the recipient address doesn't include a reply key" do
  15. let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(reply_key, "") }
  16. it "raises a SentNotificationNotFoundError" do
  17. expect { receiver.execute }.to raise_error(Gitlab::Email::Receiver::SentNotificationNotFoundError)
  18. end
  19. end
  20. context "when no sent notificiation for the reply key could be found" do
  21. let(:email_raw) { fixture_file('emails/wrong_reply_key.eml') }
  22. it "raises a SentNotificationNotFoundError" do
  23. expect { receiver.execute }.to raise_error(Gitlab::Email::Receiver::SentNotificationNotFoundError)
  24. end
  25. end
  26. context "when the email is blank" do
  27. let(:email_raw) { "" }
  28. it "raises an EmptyEmailError" do
  29. expect { receiver.execute }.to raise_error(Gitlab::Email::Receiver::EmptyEmailError)
  30. end
  31. end
  32. context "when the email was auto generated" do
  33. let!(:reply_key) { '636ca428858779856c226bb145ef4fad' }
  34. let!(:email_raw) { fixture_file("emails/auto_reply.eml") }
  35. it "raises an AutoGeneratedEmailError" do
  36. expect { receiver.execute }.to raise_error(Gitlab::Email::Receiver::AutoGeneratedEmailError)
  37. end
  38. end
  39. context "when the user could not be found" do
  40. before do
  41. user.destroy
  42. end
  43. it "raises a UserNotFoundError" do
  44. expect { receiver.execute }.to raise_error(Gitlab::Email::Receiver::UserNotFoundError)
  45. end
  46. end
  47. context "when the user has been blocked" do
  48. before do
  49. user.block
  50. end
  51. it "raises a UserBlockedError" do
  52. expect { receiver.execute }.to raise_error(Gitlab::Email::Receiver::UserBlockedError)
  53. end
  54. end
  55. context "when the user is not authorized to create a note" do
  56. before do
  57. project.update_attribute(:visibility_level, Project::PRIVATE)
  58. end
  59. it "raises a UserNotAuthorizedError" do
  60. expect { receiver.execute }.to raise_error(Gitlab::Email::Receiver::UserNotAuthorizedError)
  61. end
  62. end
  63. context "when the noteable could not be found" do
  64. before do
  65. noteable.destroy
  66. end
  67. it "raises a NoteableNotFoundError" do
  68. expect { receiver.execute }.to raise_error(Gitlab::Email::Receiver::NoteableNotFoundError)
  69. end
  70. end
  71. context "when the reply is blank" do
  72. let!(:email_raw) { fixture_file("emails/no_content_reply.eml") }
  73. it "raises an EmptyEmailError" do
  74. expect { receiver.execute }.to raise_error(Gitlab::Email::Receiver::EmptyEmailError)
  75. end
  76. end
  77. context "when the note could not be saved" do
  78. before do
  79. allow_any_instance_of(Note).to receive(:persisted?).and_return(false)
  80. end
  81. it "raises an InvalidNoteError" do
  82. expect { receiver.execute }.to raise_error(Gitlab::Email::Receiver::InvalidNoteError)
  83. end
  84. end
  85. context "when everything is fine" do
  86. let(:markdown) { "![image](uploads/image.png)" }
  87. before do
  88. allow_any_instance_of(Gitlab::Email::AttachmentUploader).to receive(:execute).and_return(
  89. [
  90. {
  91. url: "uploads/image.png",
  92. is_image: true,
  93. alt: "image",
  94. markdown: markdown
  95. }
  96. ]
  97. )
  98. end
  99. it "creates a comment" do
  100. expect { receiver.execute }.to change { noteable.notes.count }.by(1)
  101. note = noteable.notes.last
  102. expect(note.author).to eq(sent_notification.recipient)
  103. expect(note.note).to include("I could not disagree more.")
  104. end
  105. it "adds all attachments" do
  106. receiver.execute
  107. note = noteable.notes.last
  108. expect(note.note).to include(markdown)
  109. end
  110. context 'when sub-addressing is not supported' do
  111. before do
  112. stub_incoming_email_setting(enabled: true, address: nil)
  113. end
  114. shared_examples 'an email that contains a reply key' do |header|
  115. it "fetches the reply key from the #{header} header and creates a comment" do
  116. expect { receiver.execute }.to change { noteable.notes.count }.by(1)
  117. note = noteable.notes.last
  118. expect(note.author).to eq(sent_notification.recipient)
  119. expect(note.note).to include('I could not disagree more.')
  120. end
  121. end
  122. context 'reply key is in the References header' do
  123. let(:email_raw) { fixture_file('emails/reply_without_subaddressing_and_key_inside_references.eml') }
  124. it_behaves_like 'an email that contains a reply key', 'References'
  125. end
  126. end
  127. end
  128. end