issuable_slash_commands_shared_examples.rb 8.36 KB
Newer Older
1 2 3 4
# Specifications for behavior common to all objects with executable attributes.
# It takes a `issuable_type`, and expect an `issuable`.

shared_examples 'issuable record that supports slash commands in its description and notes' do |issuable_type|
5
  include SlashCommandsHelpers
6 7
  include WaitForAjax

8
  let(:master) { create(:user) }
9
  let(:assignee) { create(:user, username: 'bob') }
10
  let(:guest) { create(:user) }
11 12 13 14 15 16 17
  let(:project) { create(:project, :public) }
  let!(:milestone) { create(:milestone, project: project, title: 'ASAP') }
  let!(:label_bug) { create(:label, project: project, title: 'bug') }
  let!(:label_feature) { create(:label, project: project, title: 'feature') }
  let(:new_url_opts) { {} }

  before do
18
    project.team << [master, :master]
19
    project.team << [assignee, :developer]
20 21
    project.team << [guest, :guest]
    login_with(master)
22 23
  end

24 25 26 27 28
  after do
    # Ensure all outstanding Ajax requests are complete to avoid database deadlocks
    wait_for_ajax
  end

29 30 31 32 33 34 35 36 37 38
  describe "new #{issuable_type}" do
    context 'with commands in the description' do
      it "creates the #{issuable_type} and interpret commands accordingly" do
        visit public_send("new_namespace_project_#{issuable_type}_path", project.namespace, project, new_url_opts)
        fill_in "#{issuable_type}_title", with: 'bug 345'
        fill_in "#{issuable_type}_description", with: "bug description\n/label ~bug\n/milestone %\"ASAP\""
        click_button "Submit #{issuable_type}".humanize

        issuable = project.public_send(issuable_type.to_s.pluralize).first

39
        expect(issuable.description).to eq "bug description"
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        expect(issuable.labels).to eq [label_bug]
        expect(issuable.milestone).to eq milestone
        expect(page).to have_content 'bug 345'
        expect(page).to have_content 'bug description'
      end
    end
  end

  describe "note on #{issuable_type}" do
    before do
      visit public_send("namespace_project_#{issuable_type}_path", project.namespace, project, issuable)
    end

    context 'with a note containing commands' do
      it 'creates a note without the commands and interpret the commands accordingly' do
55
        write_note("Awesome!\n/assign @bob\n/label ~bug\n/milestone %\"ASAP\"")
56 57 58 59 60 61 62 63 64

        expect(page).to have_content 'Awesome!'
        expect(page).not_to have_content '/assign @bob'
        expect(page).not_to have_content '/label ~bug'
        expect(page).not_to have_content '/milestone %"ASAP"'

        issuable.reload
        note = issuable.notes.user.first

65
        expect(note.note).to eq "Awesome!"
66 67 68 69 70 71 72 73
        expect(issuable.assignee).to eq assignee
        expect(issuable.labels).to eq [label_bug]
        expect(issuable.milestone).to eq milestone
      end
    end

    context 'with a note containing only commands' do
      it 'does not create a note but interpret the commands accordingly' do
74
        write_note("/assign @bob\n/label ~bug\n/milestone %\"ASAP\"")
75 76 77 78

        expect(page).not_to have_content '/assign @bob'
        expect(page).not_to have_content '/label ~bug'
        expect(page).not_to have_content '/milestone %"ASAP"'
79
        expect(page).to have_content 'Commands applied'
80 81 82 83 84 85 86 87 88 89

        issuable.reload

        expect(issuable.notes.user).to be_empty
        expect(issuable.assignee).to eq assignee
        expect(issuable.labels).to eq [label_bug]
        expect(issuable.milestone).to eq milestone
      end
    end

90 91 92 93 94 95 96
    context "with a note closing the #{issuable_type}" do
      before do
        expect(issuable).to be_open
      end

      context "when current user can close #{issuable_type}" do
        it "closes the #{issuable_type}" do
97
          write_note("/close")
98 99

          expect(page).not_to have_content '/close'
100
          expect(page).to have_content 'Commands applied'
101 102 103 104 105 106 107 108 109 110 111 112 113

          expect(issuable.reload).to be_closed
        end
      end

      context "when current user cannot close #{issuable_type}" do
        before do
          logout
          login_with(guest)
          visit public_send("namespace_project_#{issuable_type}_path", project.namespace, project, issuable)
        end

        it "does not close the #{issuable_type}" do
114
          write_note("/close")
115 116

          expect(page).not_to have_content '/close'
117
          expect(page).not_to have_content 'Commands applied'
118 119 120 121 122 123 124 125 126 127 128 129 130 131

          expect(issuable).to be_open
        end
      end
    end

    context "with a note reopening the #{issuable_type}" do
      before do
        issuable.close
        expect(issuable).to be_closed
      end

      context "when current user can reopen #{issuable_type}" do
        it "reopens the #{issuable_type}" do
132
          write_note("/reopen")
133 134

          expect(page).not_to have_content '/reopen'
135
          expect(page).to have_content 'Commands applied'
136 137 138 139 140 141 142 143 144 145 146 147 148

          expect(issuable.reload).to be_open
        end
      end

      context "when current user cannot reopen #{issuable_type}" do
        before do
          logout
          login_with(guest)
          visit public_send("namespace_project_#{issuable_type}_path", project.namespace, project, issuable)
        end

        it "does not reopen the #{issuable_type}" do
149
          write_note("/reopen")
150 151

          expect(page).not_to have_content '/reopen'
152
          expect(page).not_to have_content 'Commands applied'
153 154 155 156 157 158

          expect(issuable).to be_closed
        end
      end
    end

159 160 161
    context "with a note changing the #{issuable_type}'s title" do
      context "when current user can change title of #{issuable_type}" do
        it "reopens the #{issuable_type}" do
162
          write_note("/title Awesome new title")
163 164

          expect(page).not_to have_content '/title'
165
          expect(page).to have_content 'Commands applied'
166 167 168 169 170 171 172 173 174 175 176 177 178

          expect(issuable.reload.title).to eq 'Awesome new title'
        end
      end

      context "when current user cannot change title of #{issuable_type}" do
        before do
          logout
          login_with(guest)
          visit public_send("namespace_project_#{issuable_type}_path", project.namespace, project, issuable)
        end

        it "does not reopen the #{issuable_type}" do
179
          write_note("/title Awesome new title")
180 181

          expect(page).not_to have_content '/title'
182
          expect(page).not_to have_content 'Commands applied'
183 184 185 186 187 188

          expect(issuable.reload.title).not_to eq 'Awesome new title'
        end
      end
    end

189 190
    context "with a note marking the #{issuable_type} as todo" do
      it "creates a new todo for the #{issuable_type}" do
191
        write_note("/todo")
192 193

        expect(page).not_to have_content '/todo'
194
        expect(page).to have_content 'Commands applied'
195

196
        todos = TodosFinder.new(master).execute
197 198 199 200 201
        todo = todos.first

        expect(todos.size).to eq 1
        expect(todo).to be_pending
        expect(todo.target).to eq issuable
202 203
        expect(todo.author).to eq master
        expect(todo.user).to eq master
204 205 206 207 208
      end
    end

    context "with a note marking the #{issuable_type} as done" do
      before do
209
        TodoService.new.mark_todo(issuable, master)
210 211 212
      end

      it "creates a new todo for the #{issuable_type}" do
213
        todos = TodosFinder.new(master).execute
214 215 216 217 218
        todo = todos.first

        expect(todos.size).to eq 1
        expect(todos.first).to be_pending
        expect(todo.target).to eq issuable
219 220
        expect(todo.author).to eq master
        expect(todo.user).to eq master
221

222
        write_note("/done")
223 224

        expect(page).not_to have_content '/done'
225
        expect(page).to have_content 'Commands applied'
226 227 228 229 230 231 232

        expect(todo.reload).to be_done
      end
    end

    context "with a note subscribing to the #{issuable_type}" do
      it "creates a new todo for the #{issuable_type}" do
233
        expect(issuable.subscribed?(master, project)).to be_falsy
234

235
        write_note("/subscribe")
236 237

        expect(page).not_to have_content '/subscribe'
238
        expect(page).to have_content 'Commands applied'
239

240
        expect(issuable.subscribed?(master, project)).to be_truthy
241 242 243 244 245
      end
    end

    context "with a note unsubscribing to the #{issuable_type} as done" do
      before do
246
        issuable.subscribe(master, project)
247 248 249
      end

      it "creates a new todo for the #{issuable_type}" do
250
        expect(issuable.subscribed?(master, project)).to be_truthy
251

252
        write_note("/unsubscribe")
253 254

        expect(page).not_to have_content '/unsubscribe'
255
        expect(page).to have_content 'Commands applied'
256

257
        expect(issuable.subscribed?(master, project)).to be_falsy
258 259 260 261
      end
    end
  end
end