BigW Consortium Gitlab

create_issue_handler_spec.rb 2.44 KB
Newer Older
1 2 3
require 'spec_helper'
require_relative '../email_shared_blocks'

4
describe Gitlab::Email::Handler::CreateIssueHandler, lib: true do
5
  include_context :email_shared_context
6
  it_behaves_like :reply_processing_shared_examples
7 8 9 10 11 12 13 14 15

  before do
    stub_incoming_email_setting(enabled: true, address: "incoming+%{key}@appmail.adventuretime.ooo")
    stub_config_setting(host: 'localhost')
  end

  let(:email_raw) { fixture_file('emails/valid_new_issue.eml') }
  let(:namespace) { create(:namespace, path: 'gitlabhq') }

16
  let!(:project)  { create(:project, :public, :repository, namespace: namespace) }
17 18 19 20
  let!(:user) do
    create(
      :user,
      email: 'jake@adventuretime.ooo',
21
      incoming_email_token: 'auth_token'
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    )
  end

  context "when everything is fine" do
    it "creates a new issue" do
      setup_attachment

      expect { receiver.execute }.to change { project.issues.count }.by(1)
      issue = project.issues.last

      expect(issue.author).to eq(user)
      expect(issue.title).to eq('New Issue by email')
      expect(issue.description).to include('reply by email')
      expect(issue.description).to include(markdown)
    end

    context "when the reply is blank" do
      let(:email_raw) { fixture_file("emails/valid_new_issue_empty.eml") }

      it "creates a new issue" do
        expect { receiver.execute }.to change { project.issues.count }.by(1)
        issue = project.issues.last

        expect(issue.author).to eq(user)
        expect(issue.title).to eq('New Issue by email')
        expect(issue.description).to eq('')
      end
    end
  end

  context "something is wrong" do
    context "when the issue could not be saved" do
      before do
        allow_any_instance_of(Issue).to receive(:persisted?).and_return(false)
      end

      it "raises an InvalidIssueError" do
        expect { receiver.execute }.to raise_error(Gitlab::Email::InvalidIssueError)
      end
    end

63 64
    context "when we can't find the incoming_email_token" do
      let(:email_raw) { fixture_file("emails/wrong_incoming_email_token.eml") }
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

      it "raises an UserNotFoundError" do
        expect { receiver.execute }.to raise_error(Gitlab::Email::UserNotFoundError)
      end
    end

    context "when project is private" do
      let(:project) { create(:project, :private, namespace: namespace) }

      it "raises a ProjectNotFound if the user is not a member" do
        expect { receiver.execute }.to raise_error(Gitlab::Email::ProjectNotFound)
      end
    end
  end
end