update_service_spec.rb 2.3 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe Issues::UpdateService do
  let(:user) { create(:user) }
5
  let(:user2) { create(:user) }
D
Douwe Maan 已提交
6 7
  let(:user3) { create(:user) }
  let(:issue) { create(:issue, title: 'Old title', assignee_id: user3.id) }
N
Nikita Verkhovin 已提交
8
  let(:label) { create(:label) }
D
Douwe Maan 已提交
9
  let(:project) { issue.project }
10

11 12 13
  before do
    project.team << [user, :master]
    project.team << [user2, :developer]
D
Douwe Maan 已提交
14
    project.team << [user3, :developer]
15 16
  end

17
  describe 'execute' do
18 19 20 21
    context "valid params" do
      before do
        opts = {
          title: 'New title',
22 23
          description: 'Also please fix',
          assignee_id: user2.id,
N
Nikita Verkhovin 已提交
24 25
          state_event: 'close',
          label_ids: [label.id]
26 27 28
        }

        @issue = Issues::UpdateService.new(project, user, opts).execute(issue)
29
        @issue.reload
30 31
      end

32 33 34 35 36 37
      it { expect(@issue).to be_valid }
      it { expect(@issue.title).to eq('New title') }
      it { expect(@issue.assignee).to eq(user2) }
      it { expect(@issue).to be_closed }
      it { expect(@issue.labels.count).to eq(1) }
      it { expect(@issue.labels.first.title).to eq('Bug') }
38

D
Douwe Maan 已提交
39
      it 'should send email to user2 about assign of new issue and email to user3 about issue unassignment' do
D
Douwe Maan 已提交
40
        deliveries = ActionMailer::Base.deliveries
41
        email = deliveries.last
D
Douwe Maan 已提交
42 43
        recipients = deliveries.last(2).map(&:to).flatten
        expect(recipients).to include(user2.email, user3.email)
44
        expect(email.subject).to include(issue.title)
45 46
      end

47
      def find_note(starting_with)
48 49
        @issue.notes.find do |note|
          note && note.note.start_with?(starting_with)
50 51 52
        end
      end

53
      it 'should create system note about issue reassign' do
54 55 56
        note = find_note('Reassigned to')

        expect(note).not_to be_nil
57
        expect(note.note).to include "Reassigned to \@#{user2.username}"
58
      end
N
Nikita Verkhovin 已提交
59 60

      it 'should create system note about issue label edit' do
61 62 63
        note = find_note('Added ~')

        expect(note).not_to be_nil
64
        expect(note.note).to include "Added ~#{label.id} label"
N
Nikita Verkhovin 已提交
65
      end
66 67 68 69 70 71 72

      it 'creates system note about title change' do
        note = find_note('Title changed')

        expect(note).not_to be_nil
        expect(note.note).to eq 'Title changed from **Old title** to **New title**'
      end
73 74 75
    end
  end
end