related_issues_spec.rb 17.3 KB
Newer Older
1 2
# frozen_string_literal: true

3
require 'spec_helper'
4

5
describe 'Related issues', :js do
6
  let(:user) { create(:user) }
7 8 9 10 11 12 13 14 15 16
  let(:project) { create(:project_empty_repo, :public) }
  let(:project_b) { create(:project_empty_repo, :public) }
  let(:project_unauthorized) { create(:project_empty_repo, :public) }
  let(:issue_a) { create(:issue, project: project) }
  let(:issue_b) { create(:issue, project: project) }
  let(:issue_c) { create(:issue, project: project) }
  let(:issue_d) { create(:issue, project: project) }
  let(:issue_project_b_a) { create(:issue, project: project_b) }
  let(:issue_project_unauthorized_a) { create(:issue, project: project_unauthorized) }

17 18 19 20 21 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
  context 'widget visibility' do
    before do
      stub_licensed_features(related_issues: true)
    end

    context 'when not logged in' do
      it 'does not show widget when internal project' do
        project = create :project_empty_repo, :internal
        issue = create :issue, project: project

        visit project_issue_path(project, issue)

        expect(page).not_to have_css('.related-issues-block')
      end

      it 'does not show widget when private project' do
        project = create :project_empty_repo, :private
        issue = create :issue, project: project

        visit project_issue_path(project, issue)

        expect(page).not_to have_css('.related-issues-block')
      end

      it 'shows widget when public project' do
        project = create :project_empty_repo, :public
        issue = create :issue, project: project

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
48
        expect(page).not_to have_selector('.js-issue-count-badge-add-button')
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
      end
    end

    context 'when logged in but not a member' do
      before do
        gitlab_sign_in(user)
      end

      it 'shows widget when internal project' do
        project = create :project_empty_repo, :internal
        issue = create :issue, project: project

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
64
        expect(page).not_to have_selector('.js-issue-count-badge-add-button')
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      end

      it 'does not show widget when private project' do
        project = create :project_empty_repo, :private
        issue = create :issue, project: project

        visit project_issue_path(project, issue)

        expect(page).not_to have_css('.related-issues-block')
      end

      it 'shows widget when public project' do
        project = create :project_empty_repo, :public
        issue = create :issue, project: project

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
83 84 85 86 87 88 89 90 91 92 93
        expect(page).not_to have_selector('.js-issue-count-badge-add-button')
      end

      it 'shows widget on their own public issue' do
        project = create :project_empty_repo, :public
        issue = create :issue, project: project, author: user

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
        expect(page).not_to have_selector('.js-issue-count-badge-add-button')
94 95 96
      end
    end

97
    context 'when logged in and a guest' do
98 99 100 101 102 103 104 105 106 107 108 109
      before do
        gitlab_sign_in(user)
      end

      it 'shows widget when internal project' do
        project = create :project_empty_repo, :internal
        issue = create :issue, project: project
        project.add_guest(user)

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
110
        expect(page).not_to have_selector('.js-issue-count-badge-add-button')
111 112 113 114 115 116 117 118 119 120
      end

      it 'shows widget when private project' do
        project = create :project_empty_repo, :private
        issue = create :issue, project: project
        project.add_guest(user)

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
121
        expect(page).not_to have_selector('.js-issue-count-badge-add-button')
122 123 124 125 126 127 128 129 130 131
      end

      it 'shows widget when public project' do
        project = create :project_empty_repo, :public
        issue = create :issue, project: project
        project.add_guest(user)

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
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 172 173 174 175 176 177 178 179 180 181 182
        expect(page).not_to have_selector('.js-issue-count-badge-add-button')
      end
    end

    context 'when logged in and a reporter' do
      before do
        gitlab_sign_in(user)
      end

      it 'shows widget when internal project' do
        project = create :project_empty_repo, :internal
        issue = create :issue, project: project
        project.add_reporter(user)

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
        expect(page).to have_selector('.js-issue-count-badge-add-button')
      end

      it 'shows widget when private project' do
        project = create :project_empty_repo, :private
        issue = create :issue, project: project
        project.add_reporter(user)

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
        expect(page).to have_selector('.js-issue-count-badge-add-button')
      end

      it 'shows widget when public project' do
        project = create :project_empty_repo, :public
        issue = create :issue, project: project
        project.add_reporter(user)

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
        expect(page).to have_selector('.js-issue-count-badge-add-button')
      end

      it 'shows widget on their own public issue' do
        project = create :project_empty_repo, :public
        issue = create :issue, project: project, author: user
        project.add_reporter(user)

        visit project_issue_path(project, issue)

        expect(page).to have_css('.related-issues-block')
        expect(page).to have_selector('.js-issue-count-badge-add-button')
183 184 185 186
      end
    end
  end

187 188 189 190
  context 'when user has no permission to manage related issues' do
    let!(:issue_link_b) { create :issue_link, source: issue_a, target: issue_b }
    let!(:issue_link_c) { create :issue_link, source: issue_a, target: issue_c }

191
    before do
192
      stub_licensed_features(related_issues: true)
193 194
      project.add_guest(user)
      gitlab_sign_in(user)
195 196
    end

197
    context 'visiting some issue someone else created' do
198
      before do
199 200
        visit project_issue_path(project, issue_a)
        wait_for_requests
201 202
      end

203 204 205 206
      it 'shows related issues count' do
        expect(find('.js-related-issues-header-issue-count')).to have_content('2')
      end
    end
207

208 209 210 211 212
    context 'visiting issue_b which was targeted by issue_a' do
      before do
        visit project_issue_path(project, issue_b)
        wait_for_requests
      end
213

214 215
      it 'shows related issues count' do
        expect(find('.js-related-issues-header-issue-count')).to have_content('1')
216 217 218 219
      end
    end
  end

220
  context 'when user has permission to manage related issues' do
221
    before do
222 223
      project.add_maintainer(user)
      project_b.add_maintainer(user)
224
      gitlab_sign_in(user)
225 226 227 228 229 230 231
    end

    context 'with related_issues disabled' do
      let!(:issue_link_b) { create :issue_link, source: issue_a, target: issue_b }
      let!(:issue_link_c) { create :issue_link, source: issue_a, target: issue_c }

      before do
232
        visit project_issue_path(project, issue_a)
233 234 235 236 237 238 239 240 241 242
        wait_for_requests
      end

      it 'does not show the related issues block' do
        expect(page).not_to have_selector('.js-related-issues-root')
      end
    end

    context 'with related_issues enabled' do
      before do
243
        stub_licensed_features(related_issues: true)
244 245 246 247
      end

      context 'without existing related issues' do
        before do
248
          visit project_issue_path(project, issue_a)
249 250 251 252 253 254 255 256 257 258 259 260 261 262
          wait_for_requests
        end

        it 'shows related issues count' do
          expect(find('.js-related-issues-header-issue-count')).to have_content('0')
        end

        it 'add related issue' do
          find('.js-issue-count-badge-add-button').click
          find('.js-add-issuable-form-input').set "#{issue_b.to_reference(project)} "
          find('.js-add-issuable-form-add-button').click

          wait_for_requests

263
          items = all('.item-title a')
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

          # Form gets hidden after submission
          expect(page).not_to have_selector('.js-add-related-issues-form-area')
          # Check if related issues are present
          expect(items.count).to eq(1)
          expect(items[0].text).to eq(issue_b.title)
          expect(find('.js-related-issues-header-issue-count')).to have_content('1')
        end

        it 'add cross-project related issue' do
          find('.js-issue-count-badge-add-button').click
          find('.js-add-issuable-form-input').set "#{issue_project_b_a.to_reference(project)} "
          find('.js-add-issuable-form-add-button').click

          wait_for_requests

280
          items = all('.item-title a')
281

282 283 284 285 286 287 288 289 290 291 292 293
          expect(items.count).to eq(1)
          expect(items[0].text).to eq(issue_project_b_a.title)
          expect(find('.js-related-issues-header-issue-count')).to have_content('1')
        end

        it 'pressing enter should submit the form' do
          find('.js-issue-count-badge-add-button').click
          find('.js-add-issuable-form-input').set "#{issue_project_b_a.to_reference(project)} "
          find('.js-add-issuable-form-input').native.send_key(:enter)

          wait_for_requests

294
          items = all('.item-title a')
295

296 297 298 299
          expect(items.count).to eq(1)
          expect(items[0].text).to eq(issue_project_b_a.title)
          expect(find('.js-related-issues-header-issue-count')).to have_content('1')
        end
300 301 302 303 304 305 306 307 308 309 310 311 312 313

        it 'disallows duplicate entries' do
          find('.js-issue-count-badge-add-button').click
          find('.js-add-issuable-form-input').set 'duplicate duplicate duplicate'

          items = all('.js-add-issuable-form-token-list-item')
          expect(items.count).to eq(1)
          expect(items[0].text).to eq('duplicate')

          # Pending issues aren't counted towards the related issue count
          expect(find('.js-related-issues-header-issue-count')).to have_content('0')
        end

        it 'allows us to remove pending issues' do
314
          # Tests against https://gitlab.com/gitlab-org/gitlab/issues/11625
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
          find('.js-issue-count-badge-add-button').click
          find('.js-add-issuable-form-input').set 'issue1 issue2 issue3 '

          items = all('.js-add-issuable-form-token-list-item')
          expect(items.count).to eq(3)
          expect(items[0].text).to eq('issue1')
          expect(items[1].text).to eq('issue2')
          expect(items[2].text).to eq('issue3')

          # Remove pending issues left to right to make sure none get stuck
          items[0].find('.js-issue-token-remove-button').click
          items = all('.js-add-issuable-form-token-list-item')
          expect(items.count).to eq(2)
          expect(items[0].text).to eq('issue2')
          expect(items[1].text).to eq('issue3')

          items[0].find('.js-issue-token-remove-button').click
          items = all('.js-add-issuable-form-token-list-item')
          expect(items.count).to eq(1)
          expect(items[0].text).to eq('issue3')

          items[0].find('.js-issue-token-remove-button').click
          items = all('.js-add-issuable-form-token-list-item')
          expect(items.count).to eq(0)
        end
340 341 342 343 344 345 346
      end

      context 'with existing related issues' do
        let!(:issue_link_b) { create :issue_link, source: issue_a, target: issue_b }
        let!(:issue_link_c) { create :issue_link, source: issue_a, target: issue_c }

        before do
347
          visit project_issue_path(project, issue_a)
348 349 350 351 352 353 354 355
          wait_for_requests
        end

        it 'shows related issues count' do
          expect(find('.js-related-issues-header-issue-count')).to have_content('2')
        end

        it 'shows related issues' do
356
          items = all('.item-title a')
357 358 359 360 361 362 363

          expect(items.count).to eq(2)
          expect(items[0].text).to eq(issue_b.title)
          expect(items[1].text).to eq(issue_c.title)
        end

        it 'allows us to remove a related issues' do
364
          items_before = all('.item-title a')
365 366 367

          expect(items_before.count).to eq(2)

Clement Ho's avatar
Clement Ho committed
368
          first('.js-issue-item-remove-button').click
369 370 371

          wait_for_requests

372
          items_after = all('.item-title a')
373 374 375 376 377 378 379 380 381 382 383

          expect(items_after.count).to eq(1)
        end

        it 'add related issue' do
          find('.js-issue-count-badge-add-button').click
          find('.js-add-issuable-form-input').set "##{issue_d.iid} "
          find('.js-add-issuable-form-add-button').click

          wait_for_requests

384
          items = all('.item-title a')
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

          expect(items.count).to eq(3)
          expect(items[0].text).to eq(issue_b.title)
          expect(items[1].text).to eq(issue_c.title)
          expect(items[2].text).to eq(issue_d.title)
          expect(find('.js-related-issues-header-issue-count')).to have_content('3')
        end

        it 'add invalid related issue' do
          find('.js-issue-count-badge-add-button').click
          find('.js-add-issuable-form-input').set "#9999999 "
          find('.js-add-issuable-form-add-button').click

          wait_for_requests

400
          items = all('.item-title a')
401 402 403 404 405 406 407 408 409 410 411 412 413 414

          expect(items.count).to eq(2)
          expect(items[0].text).to eq(issue_b.title)
          expect(items[1].text).to eq(issue_c.title)
          expect(find('.js-related-issues-header-issue-count')).to have_content('2')
        end

        it 'add unauthorized related issue' do
          find('.js-issue-count-badge-add-button').click
          find('.js-add-issuable-form-input').set "#{issue_project_unauthorized_a.to_reference(project)} "
          find('.js-add-issuable-form-add-button').click

          wait_for_requests

415
          items = all('.item-title a')
416 417 418 419 420 421 422

          expect(items.count).to eq(2)
          expect(items[0].text).to eq(issue_b.title)
          expect(items[1].text).to eq(issue_c.title)
          expect(find('.js-related-issues-header-issue-count')).to have_content('2')
        end
      end
423

424
      context 'with "Relates to", "Blocks", "Is blocked by" groupings' do
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
        def add_linked_issue(issue, radio_input_value)
          find('.js-issue-count-badge-add-button').click
          find('.js-add-issuable-form-input').set "#{issue.to_reference(project)} "
          find("input[name=\"linked-issue-type-radio\"][value=\"#{radio_input_value}\"]").click
          find('.js-add-issuable-form-add-button').click

          wait_for_requests
        end

        before do
          visit project_issue_path(project, issue_a)
          wait_for_requests
        end

        context 'when adding a "relates_to" issue' do
          before do
            add_linked_issue(issue_b, "relates_to")
          end

          it 'shows "Relates to" heading' do
            headings = all('.linked-issues-card-body h4')

            expect(headings.count).to eq(1)
            expect(headings[0].text).to eq("Relates to")
          end

          it 'shows the added issue' do
            items = all('.item-title a')

            expect(items[0].text).to eq(issue_b.title)
            expect(find('.js-related-issues-header-issue-count')).to have_content('1')
          end
        end

        context 'when adding a "blocks" issue' do
          before do
            add_linked_issue(issue_b, "blocks")
          end

          it 'shows "Blocks" heading' do
            headings = all('.linked-issues-card-body h4')

            expect(headings.count).to eq(1)
            expect(headings[0].text).to eq("Blocks")
          end

          it 'shows the added issue' do
            items = all('.item-title a')

            expect(items[0].text).to eq(issue_b.title)
            expect(find('.js-related-issues-header-issue-count')).to have_content('1')
          end
        end

        context 'when adding an "is_blocked_by" issue' do
          before do
            add_linked_issue(issue_b, "is_blocked_by")
          end

          it 'shows "Is blocked by" heading' do
            headings = all('.linked-issues-card-body h4')

            expect(headings.count).to eq(1)
            expect(headings[0].text).to eq("Is blocked by")
          end

          it 'shows the added issue' do
            items = all('.item-title a')

            expect(items[0].text).to eq(issue_b.title)
            expect(find('.js-related-issues-header-issue-count')).to have_content('1')
          end
        end

        context 'when adding "relates_to", "blocks", and "is_blocked_by" issues' do
          before do
            add_linked_issue(issue_b, "relates_to")
            add_linked_issue(issue_c, "blocks")
            add_linked_issue(issue_d, "is_blocked_by")
          end

          it 'shows "Blocks", "Is blocked by", and "Relates to" headings' do
            headings = all('.linked-issues-card-body h4')

            expect(headings.count).to eq(3)
            expect(headings[0].text).to eq("Blocks")
            expect(headings[1].text).to eq("Is blocked by")
            expect(headings[2].text).to eq("Relates to")
          end

          it 'shows all added issues' do
            items = all('.item-title a')

            expect(items.count).to eq(3)
            expect(find('.js-related-issues-header-issue-count')).to have_content('3')
          end
        end
      end
523 524 525
    end
  end
end