interpret_service_spec.rb 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
require 'spec_helper'

describe SlashCommands::InterpretService, services: true do
  let(:project) { create(:project) }
  let(:user) { create(:user) }
  let(:issue) { create(:issue, project: project) }
  let(:milestone) { create(:milestone, project: project, title: '9.10') }
  let(:inprogress) { create(:label, project: project, title: 'In Progress') }
  let(:bug) { create(:label, project: project, title: 'Bug') }

  describe '#command_names' do
    subject { described_class.command_names }

    it 'returns the known commands' do
      is_expected.to match_array([
        :open, :reopen,
        :close,
R
Rémy Coutable 已提交
18
        :title,
19 20 21
        :assign, :reassign,
        :unassign, :remove_assignee,
        :milestone,
22
        :clear_milestone, :remove_milestone,
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
        :labels, :label,
        :unlabel, :remove_labels, :remove_label,
        :clear_labels, :clear_label,
        :todo,
        :done,
        :subscribe,
        :unsubscribe,
        :due_date,
        :clear_due_date
      ])
    end
  end

  describe '#execute' do
    let(:service) { described_class.new(project, user) }
38 39
    let(:issue) { create(:issue) }
    let(:merge_request) { create(:merge_request) }
40 41 42

    shared_examples 'open command' do
      it 'returns state_event: "open" if content contains /open' do
43
        changes = service.execute(content, issuable)
44 45 46 47 48 49 50

        expect(changes).to eq(state_event: 'reopen')
      end
    end

    shared_examples 'close command' do
      it 'returns state_event: "close" if content contains /open' do
51
        changes = service.execute(content, issuable)
52 53 54 55 56

        expect(changes).to eq(state_event: 'close')
      end
    end

R
Rémy Coutable 已提交
57 58 59 60 61 62 63 64
    shared_examples 'title command' do
      it 'populates title: "A brand new title" if content contains /title A brand new title' do
        changes = service.execute(content, issuable)

        expect(changes).to eq(title: 'A brand new title')
      end
    end

65 66
    shared_examples 'assign command' do
      it 'fetches assignee and populates assignee_id if content contains /assign' do
67
        changes = service.execute(content, issuable)
68 69 70 71 72

        expect(changes).to eq(assignee_id: user.id)
      end
    end

73 74 75 76 77 78 79 80
    shared_examples 'unassign command' do
      it 'populates assignee_id: nil if content contains /unassign' do
        changes = service.execute(content, issuable)

        expect(changes).to eq(assignee_id: nil)
      end
    end

81 82
    shared_examples 'milestone command' do
      it 'fetches milestone and populates milestone_id if content contains /milestone' do
83
        changes = service.execute(content, issuable)
84 85 86 87 88

        expect(changes).to eq(milestone_id: milestone.id)
      end
    end

89 90 91 92 93 94 95 96
    shared_examples 'clear_milestone command' do
      it 'populates milestone_id: nil if content contains /clear_milestone' do
        changes = service.execute(content, issuable)

        expect(changes).to eq(milestone_id: nil)
      end
    end

97 98
    shared_examples 'label command' do
      it 'fetches label ids and populates add_label_ids if content contains /label' do
99
        changes = service.execute(content, issuable)
100 101 102 103 104

        expect(changes).to eq(add_label_ids: [bug.id, inprogress.id])
      end
    end

105 106 107
    shared_examples 'unlabel command' do
      it 'fetches label ids and populates remove_label_ids if content contains /unlabel' do
        changes = service.execute(content, issuable)
108 109 110 111 112 113 114

        expect(changes).to eq(remove_label_ids: [inprogress.id])
      end
    end

    shared_examples 'clear_labels command' do
      it 'populates label_ids: [] if content contains /clear_labels' do
115
        changes = service.execute(content, issuable)
116 117 118 119 120

        expect(changes).to eq(label_ids: [])
      end
    end

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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
    shared_examples 'todo command' do
      it 'populates todo_event: "mark" if content contains /todo' do
        changes = service.execute(content, issuable)

        expect(changes).to eq(todo_event: 'mark')
      end
    end

    shared_examples 'done command' do
      it 'populates todo_event: "done" if content contains /done' do
        changes = service.execute(content, issuable)

        expect(changes).to eq(todo_event: 'done')
      end
    end

    shared_examples 'subscribe command' do
      it 'populates subscription_event: "subscribe" if content contains /subscribe' do
        changes = service.execute(content, issuable)

        expect(changes).to eq(subscription_event: 'subscribe')
      end
    end

    shared_examples 'unsubscribe command' do
      it 'populates subscription_event: "unsubscribe" if content contains /unsubscribe' do
        changes = service.execute(content, issuable)

        expect(changes).to eq(subscription_event: 'unsubscribe')
      end
    end

    shared_examples 'due_date command' do
      it 'populates due_date: Date.new(2016, 8, 28) if content contains /due_date 2016-08-28' do
        changes = service.execute(content, issuable)

        expect(changes).to eq(due_date: Date.new(2016, 8, 28))
      end
    end

    shared_examples 'clear_due_date command' do
      it 'populates due_date: nil if content contains /clear_due_date' do
        changes = service.execute(content, issuable)

        expect(changes).to eq(due_date: nil)
      end
    end

    shared_examples 'empty command' do
      it 'populates {} if content contains an unsupported command' do
        changes = service.execute(content, issuable)
172 173 174 175 176 177 178

        expect(changes).to be_empty
      end
    end

    it_behaves_like 'open command' do
      let(:content) { '/open' }
179 180 181 182 183 184
      let(:issuable) { issue }
    end

    it_behaves_like 'open command' do
      let(:content) { '/open' }
      let(:issuable) { merge_request }
185 186 187 188
    end

    it_behaves_like 'open command' do
      let(:content) { '/reopen' }
189
      let(:issuable) { issue }
190 191 192 193
    end

    it_behaves_like 'close command' do
      let(:content) { '/close' }
194 195 196 197 198 199
      let(:issuable) { issue }
    end

    it_behaves_like 'close command' do
      let(:content) { '/close' }
      let(:issuable) { merge_request }
200 201
    end

R
Rémy Coutable 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    it_behaves_like 'title command' do
      let(:content) { '/title A brand new title' }
      let(:issuable) { issue }
    end

    it_behaves_like 'title command' do
      let(:content) { '/title A brand new title' }
      let(:issuable) { merge_request }
    end

    it_behaves_like 'empty command' do
      let(:content) { '/title' }
      let(:issuable) { issue }
    end

217 218
    it_behaves_like 'assign command' do
      let(:content) { "/assign @#{user.username}" }
219 220 221 222 223 224
      let(:issuable) { issue }
    end

    it_behaves_like 'assign command' do
      let(:content) { "/assign @#{user.username}" }
      let(:issuable) { merge_request }
225 226
    end

R
Rémy Coutable 已提交
227 228 229
    it_behaves_like 'empty command' do
      let(:content) { '/assign @abcd1234' }
      let(:issuable) { issue }
230 231
    end

R
Rémy Coutable 已提交
232 233 234
    it_behaves_like 'empty command' do
      let(:content) { '/assign' }
      let(:issuable) { issue }
235 236
    end

237 238 239 240 241 242 243 244 245 246 247 248 249 250
    it_behaves_like 'unassign command' do
      let(:content) { '/unassign' }
      let(:issuable) { issue }
    end

    it_behaves_like 'unassign command' do
      let(:content) { '/unassign' }
      let(:issuable) { merge_request }
    end

    it_behaves_like 'unassign command' do
      let(:content) { '/remove_assignee' }
      let(:issuable) { issue }
    end
251

252 253 254
    it_behaves_like 'milestone command' do
      let(:content) { "/milestone %#{milestone.title}" }
      let(:issuable) { issue }
255 256 257 258
    end

    it_behaves_like 'milestone command' do
      let(:content) { "/milestone %#{milestone.title}" }
259 260 261 262 263 264 265 266 267 268 269
      let(:issuable) { merge_request }
    end

    it_behaves_like 'clear_milestone command' do
      let(:content) { '/clear_milestone' }
      let(:issuable) { issue }
    end

    it_behaves_like 'clear_milestone command' do
      let(:content) { '/clear_milestone' }
      let(:issuable) { merge_request }
270 271
    end

272 273 274 275
    it_behaves_like 'clear_milestone command' do
      let(:content) { '/remove_milestone' }
      let(:issuable) { issue }
    end
276

277 278 279
    it_behaves_like 'label command' do
      let(:content) { %(/label ~"#{inprogress.title}" ~#{bug.title} ~unknown) }
      let(:issuable) { issue }
280 281 282 283
    end

    it_behaves_like 'label command' do
      let(:content) { %(/label ~"#{inprogress.title}" ~#{bug.title} ~unknown) }
284
      let(:issuable) { merge_request }
285 286 287 288
    end

    it_behaves_like 'label command' do
      let(:content) { %(/labels ~"#{inprogress.title}" ~#{bug.title} ~unknown) }
289
      let(:issuable) { issue }
290 291
    end

292
    it_behaves_like 'unlabel command' do
293
      let(:content) { %(/unlabel ~"#{inprogress.title}") }
294
      let(:issuable) { issue }
295 296
    end

297 298 299 300 301 302
    it_behaves_like 'unlabel command' do
      let(:content) { %(/unlabel ~"#{inprogress.title}") }
      let(:issuable) { merge_request }
    end

    it_behaves_like 'unlabel command' do
303
      let(:content) { %(/remove_labels ~"#{inprogress.title}") }
304
      let(:issuable) { issue }
305 306
    end

307
    it_behaves_like 'unlabel command' do
308
      let(:content) { %(/remove_label ~"#{inprogress.title}") }
309
      let(:issuable) { issue }
310 311 312 313
    end

    it_behaves_like 'clear_labels command' do
      let(:content) { '/clear_labels' }
314
      let(:issuable) { issue }
315 316 317
    end

    it_behaves_like 'clear_labels command' do
318 319
      let(:content) { '/clear_labels' }
      let(:issuable) { merge_request }
320 321
    end

322 323 324 325
    it_behaves_like 'clear_labels command' do
      let(:content) { '/clear_label' }
      let(:issuable) { issue }
    end
326

327 328 329
    it_behaves_like 'todo command' do
      let(:content) { '/todo' }
      let(:issuable) { issue }
330 331
    end

332 333 334 335
    it_behaves_like 'todo command' do
      let(:content) { '/todo' }
      let(:issuable) { merge_request }
    end
336

337 338 339
    it_behaves_like 'done command' do
      let(:content) { '/done' }
      let(:issuable) { issue }
340 341
    end

342 343 344 345
    it_behaves_like 'done command' do
      let(:content) { '/done' }
      let(:issuable) { merge_request }
    end
346

347 348 349
    it_behaves_like 'subscribe command' do
      let(:content) { '/subscribe' }
      let(:issuable) { issue }
350 351
    end

352 353 354 355
    it_behaves_like 'subscribe command' do
      let(:content) { '/subscribe' }
      let(:issuable) { merge_request }
    end
356

357 358 359
    it_behaves_like 'unsubscribe command' do
      let(:content) { '/unsubscribe' }
      let(:issuable) { issue }
360 361
    end

362 363 364 365
    it_behaves_like 'unsubscribe command' do
      let(:content) { '/unsubscribe' }
      let(:issuable) { merge_request }
    end
366

367 368 369
    it_behaves_like 'due_date command' do
      let(:content) { '/due_date 2016-08-28' }
      let(:issuable) { issue }
370 371
    end

372 373 374 375
    it_behaves_like 'empty command' do
      let(:content) { '/due_date foo bar' }
      let(:issuable) { issue }
    end
376

377 378 379
    it_behaves_like 'empty command' do
      let(:content) { '/due_date 2016-08-28' }
      let(:issuable) { merge_request }
380 381
    end

382 383 384 385
    it_behaves_like 'clear_due_date command' do
      let(:content) { '/clear_due_date' }
      let(:issuable) { issue }
    end
386

387 388 389
    it_behaves_like 'empty command' do
      let(:content) { '/clear_due_date' }
      let(:issuable) { merge_request }
390 391 392
    end
  end
end