move_service_spec.rb 5.2 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe Issues::MoveService, services: true do
  let(:user) { create(:user) }
5
  let(:author) { create(:user) }
6 7
  let(:title) { 'Some issue' }
  let(:description) { 'Some issue description' }
8
  let(:old_project) { create(:project) }
9
  let(:new_project) { create(:project) }
10 11
  let(:issue_params) { old_issue.serializable_hash }

12 13 14 15 16
  let(:old_issue) do
    create(:issue, title: title, description: description,
           project: old_project, author: author)
  end

17 18 19
  let(:move_service) do
    described_class.new(old_project, user, issue_params, old_issue, new_project_id)
  end
20

21
  shared_context 'issue move requested' do
22
    let(:new_project_id) { new_project.id }
23
  end
24

25 26 27 28 29 30 31
  shared_context 'user can move issue' do
    before do
      old_project.team << [user, :master]
      new_project.team << [user, :master]
    end
  end
    
32
  context 'issue movable' do
33 34 35
    include_context 'issue move requested'
    include_context 'user can move issue'

36 37 38
    describe '#move?' do
      subject { move_service.move? }
      it { is_expected.to be_truthy }
39 40
    end

41
    describe '#execute' do
42 43
      shared_context 'issue move executed' do
        let!(:new_issue) { move_service.execute }
44 45
      end

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
      context 'generic issue' do
        include_context 'issue move executed'

        it 'creates a new issue in a new project' do
          expect(new_issue.project).to eq new_project
        end

        it 'rewrites issue title' do
          expect(new_issue.title).to eq title
        end

        it 'rewrites issue description' do
          expect(new_issue.description).to include description
        end

        it 'adds system note to old issue at the end' do
          expect(old_issue.notes.last.note).to match /^Moved to/
        end

        it 'adds system note to new issue at the end' do
          expect(new_issue.notes.last.note).to match /^Moved from/
        end
68 69 70 71 72

        it 'closes old issue' do
          expect(old_issue.closed?).to be true
        end

73 74
        it 'persists new issue' do
          expect(new_issue.persisted?).to be true
75
        end
76 77 78 79 80

        it 'persist all changes' do
          expect(old_issue.changed?).to be false
          expect(new_issue.changed?).to be false
        end
81 82 83 84 85 86 87 88 89 90 91 92 93

        it 'changes author' do
          expect(new_issue.author).to eq user
        end

        it 'removes data that is invalid in new context' do
          expect(new_issue.milestone).to be_nil
          expect(new_issue.labels).to be_empty
        end

        it 'creates a new internal id for issue' do
          expect(new_issue.iid).to be 1
        end
94
      end
95

96
      context 'issue with notes' do
97 98 99 100 101
        let(:note_contents) do
          ['Some system note 1', 'Some comment', 'Some system note 2']
        end

        before do
102
          note_params = { noteable: old_issue, project: old_project, author: user }
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
          create(:system_note, note_params.merge(note: note_contents.first))
          create(:note, note_params.merge(note: note_contents.second))
          create(:system_note, note_params.merge(note: note_contents.third))
        end

        include_context 'issue move executed'

        let(:new_notes) { new_issue.notes.order('id ASC').pluck(:note) }

        it 'rewrites existing system notes in valid order' do
          expect(new_notes.first(3)).to eq note_contents
        end

        it 'adds a system note about move after rewritten notes' do
          expect(new_notes.last).to match /^Moved from/
        end
119
      end
120 121 122 123 124 125 126 127 128 129 130 131 132 133

      describe 'rewritting references' do
        include_context 'issue move executed'

        context 'issue reference' do
          let(:another_issue) { create(:issue, project: old_project) }
          let(:description) { "Some description #{another_issue.to_reference}" }

          it 'rewrites referenced issues creating cross project reference' do
            expect(new_issue.description)
              .to eq "Some description #{old_project.to_reference}#{another_issue.to_reference}"
          end
        end
      end
134
    end
135 136
  end

137
  context 'issue move not requested' do
138
    let(:new_project_id) { nil }
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

    describe '#move?' do
      subject { move_service.move? }

      context 'user do not have permissions to move issue' do
        it { is_expected.to be_falsey }
      end

      context 'user has permissions to move issue' do
        include_context 'user can move issue'
        it { is_expected.to be_falsey }
      end
    end
  end


  describe 'move permissions' do
    include_context 'issue move requested'

    describe '#move?' do
      subject { move_service.move? }

      context 'user is master in both projects' do
        include_context 'user can move issue'
        it { is_expected.to be_truthy }
      end

      context 'user is master only in new project' do
        before { new_project.team << [user, :master] }
        it { is_expected.to be_falsey }
      end

      context 'user is master only in old project' do
        before { old_project.team << [user, :master] }
        it { is_expected.to be_falsey }
      end

      context 'user is master in one project and developer in another' do
        before do
          new_project.team << [user, :developer]
          old_project.team << [user, :master]
        end
181

182 183
        it { is_expected.to be_falsey }
      end
184
    end
185 186
  end
end