issues_controller_spec.rb 26.8 KB
Newer Older
1 2 3
require('spec_helper')

describe Projects::IssuesController do
4
  let(:project) { create(:project_empty_repo) }
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
5 6
  let(:user)    { create(:user) }
  let(:issue)   { create(:issue, project: project) }
7

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
8
  describe "GET #index" do
9
    context 'external issue tracker' do
10 11 12 13
      before do
        sign_in(user)
        project.add_developer(user)
        create(:jira_service, project: project)
14 15
      end

16 17 18 19
      context 'when GitLab issues disabled' do
        it 'returns 404 status' do
          project.issues_enabled = false
          project.save!
20

21
          get :index, namespace_id: project.namespace, project_id: project
22

23 24 25 26 27 28 29 30 31 32 33
          expect(response).to have_http_status(404)
        end
      end

      context 'when GitLab issues enabled' do
        it 'renders the "index" template' do
          get :index, namespace_id: project.namespace, project_id: project

          expect(response).to have_http_status(200)
          expect(response).to render_template(:index)
        end
34
      end
35 36
    end

37 38 39 40 41
    context 'internal issue tracker' do
      before do
        sign_in(user)
        project.team << [user, :developer]
      end
42

43 44
      it_behaves_like "issuables list meta-data", :issue

45
      it "returns index" do
46
        get :index, namespace_id: project.namespace, project_id: project
47

48 49
        expect(response).to have_http_status(200)
      end
50

51
      it "returns 301 if request path doesn't match project path" do
52
        get :index, namespace_id: project.namespace, project_id: project.path.upcase
53

54
        expect(response).to redirect_to(project_issues_path(project))
55
      end
56

57 58
      it "returns 404 when issues are disabled" do
        project.issues_enabled = false
59
        project.save!
60

61
        get :index, namespace_id: project.namespace, project_id: project
62
        expect(response).to have_http_status(404)
63 64
      end
    end
65 66 67

    context 'with page param' do
      let(:last_page) { project.issues.page().total_pages }
68
      let!(:issue_list) { create_list(:issue, 2, project: project) }
69 70 71 72

      before do
        sign_in(user)
        project.team << [user, :developer]
73
        allow(Kaminari.config).to receive(:default_per_page).and_return(1)
74 75 76 77
      end

      it 'redirects to last_page if page number is larger than number of pages' do
        get :index,
78 79
          namespace_id: project.namespace.to_param,
          project_id: project,
80 81
          page: (last_page + 1).to_param

82
        expect(response).to redirect_to(namespace_project_issues_path(page: last_page, state: controller.params[:state], scope: controller.params[:scope]))
83 84 85 86
      end

      it 'redirects to specified page' do
        get :index,
87 88
          namespace_id: project.namespace.to_param,
          project_id: project,
89 90 91 92 93
          page: last_page.to_param

        expect(assigns(:issues).current_page).to eq(last_page)
        expect(response).to have_http_status(200)
      end
94 95 96 97 98 99 100 101 102 103 104

      it 'does not redirect to external sites when provided a host field' do
        external_host = "www.example.com"
        get :index,
          namespace_id: project.namespace.to_param,
          project_id: project,
          page: (last_page + 1).to_param,
          host: external_host

        expect(response).to redirect_to(namespace_project_issues_path(page: last_page, state: controller.params[:state], scope: controller.params[:scope]))
      end
105
    end
106 107 108
  end

  describe 'GET #new' do
109 110 111
    it 'redirects to signin if not logged in' do
      get :new, namespace_id: project.namespace, project_id: project

112
      expect(flash[:notice]).to eq 'Please sign in to create the new issue.'
113 114 115
      expect(response).to redirect_to(new_user_session_path)
    end

116 117 118 119 120 121 122
    context 'internal issue tracker' do
      before do
        sign_in(user)
        project.team << [user, :developer]
      end

      it 'builds a new issue' do
123
        get :new, namespace_id: project.namespace, project_id: project
124 125 126 127 128

        expect(assigns(:issue)).to be_a_new(Issue)
      end

      it 'fills in an issue for a merge request' do
129
        project_with_repository = create(:project, :repository)
130 131 132
        project_with_repository.team << [user, :developer]
        mr = create(:merge_request_with_diff_notes, source_project: project_with_repository)

Bob Van Landuyt's avatar
Bob Van Landuyt committed
133
        get :new, namespace_id: project_with_repository.namespace, project_id: project_with_repository, merge_request_to_resolve_discussions_of: mr.iid
134 135 136 137

        expect(assigns(:issue).title).not_to be_empty
        expect(assigns(:issue).description).not_to be_empty
      end
138 139 140 141

      it 'fills in an issue for a discussion' do
        note = create(:note_on_merge_request, project: project)

Bob Van Landuyt's avatar
Bob Van Landuyt committed
142
        get :new, namespace_id: project.namespace.path, project_id: project, merge_request_to_resolve_discussions_of: note.noteable.iid, discussion_to_resolve: note.discussion_id
143 144 145 146

        expect(assigns(:issue).title).not_to be_empty
        expect(assigns(:issue).description).not_to be_empty
      end
147 148
    end

149
    context 'external issue tracker' do
150 151 152 153
      let!(:service) do
        create(:custom_issue_tracker_service, project: project, title: 'Custom Issue Tracker', new_issue_url: 'http://test.com')
      end

154 155 156
      before do
        sign_in(user)
        project.team << [user, :developer]
157 158 159

        external = double
        allow(project).to receive(:external_issue_tracker).and_return(external)
160 161
      end

162 163 164 165
      context 'when GitLab issues disabled' do
        it 'returns 404 status' do
          project.issues_enabled = false
          project.save!
166

167 168 169 170 171 172 173 174 175
          get :new, namespace_id: project.namespace, project_id: project

          expect(response).to have_http_status(404)
        end
      end

      context 'when GitLab issues enabled' do
        it 'renders the "new" template' do
          get :new, namespace_id: project.namespace, project_id: project
176

177 178 179
          expect(response).to have_http_status(200)
          expect(response).to render_template(:new)
        end
180
      end
181
    end
182 183
  end

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  describe 'Redirect after sign in' do
    context 'with an AJAX request' do
      it 'does not store the visited URL' do
        xhr :get,
          :show,
          format: :json,
          namespace_id: project.namespace,
          project_id: project,
          id: issue.iid

        expect(session['user_return_to']).to be_blank
      end
    end

    context 'without an AJAX request' do
      it 'stores the visited URL' do
        get :show,
          namespace_id: project.namespace.to_param,
          project_id: project,
          id: issue.iid

        expect(session['user_return_to']).to eq("/#{project.namespace.to_param}/#{project.to_param}/issues/#{issue.iid}")
      end
    end
  end

210
  describe 'PUT #update' do
211 212 213 214 215 216 217
    before do
      sign_in(user)
      project.team << [user, :developer]
    end

    it_behaves_like 'update invalid issuable', Issue

218 219 220 221 222 223 224 225 226
    context 'changing the assignee' do
      it 'limits the attributes exposed on the assignee' do
        assignee = create(:user)
        project.add_developer(assignee)

        put :update,
          namespace_id: project.namespace.to_param,
          project_id: project,
          id: issue.iid,
227
          issue: { assignee_ids: [assignee.id] },
228 229 230
          format: :json
        body = JSON.parse(response.body)

231
        expect(body['assignees'].first.keys)
Phil Hughes's avatar
Phil Hughes committed
232
          .to match_array(%w(id name username avatar_url state web_url))
233 234 235
      end
    end

236
    context 'when moving issue to another private project' do
237
      let(:another_project) { create(:project, :private) }
238 239

      context 'when user has access to move issue' do
240 241 242
        before do
          another_project.team << [user, :reporter]
        end
243 244 245 246 247

        it 'moves issue to another project' do
          move_issue

          expect(response).to have_http_status :found
248
          expect(another_project.issues).not_to be_empty
249 250 251 252 253 254 255 256 257 258 259
        end
      end

      context 'when user does not have access to move issue' do
        it 'responds with 404' do
          move_issue

          expect(response).to have_http_status :not_found
        end
      end

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      context 'Akismet is enabled' do
        let(:project) { create(:project_empty_repo, :public) }

        before do
          stub_application_setting(recaptcha_enabled: true)
          allow_any_instance_of(SpamService).to receive(:check_for_spam?).and_return(true)
        end

        context 'when an issue is not identified as spam' do
          before do
            allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(false)
            allow_any_instance_of(AkismetService).to receive(:is_spam?).and_return(false)
          end

          it 'normally updates the issue' do
            expect { update_issue(title: 'Foo') }.to change { issue.reload.title }.to('Foo')
          end
        end

        context 'when an issue is identified as spam' do
280 281 282
          before do
            allow_any_instance_of(AkismetService).to receive(:is_spam?).and_return(true)
          end
283 284 285 286 287 288

          context 'when captcha is not verified' do
            def update_spam_issue
              update_issue(title: 'Spam Title', description: 'Spam lives here')
            end

289 290 291
            before do
              allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(false)
            end
292 293

            it 'rejects an issue recognized as a spam' do
294
              expect(Gitlab::Recaptcha).to receive(:load_configurations!).and_return(true)
295
              expect { update_spam_issue }.not_to change { issue.reload.title }
296 297 298 299 300
            end

            it 'rejects an issue recognized as a spam when recaptcha disabled' do
              stub_application_setting(recaptcha_enabled: false)

301
              expect { update_spam_issue }.not_to change { issue.reload.title }
302 303 304 305 306 307 308 309 310 311 312 313
            end

            it 'creates a spam log' do
              update_spam_issue

              spam_logs = SpamLog.all

              expect(spam_logs.count).to eq(1)
              expect(spam_logs.first.title).to eq('Spam Title')
              expect(spam_logs.first.recaptcha_verified).to be_falsey
            end

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
            context 'as HTML' do
              it 'renders verify template' do
                update_spam_issue

                expect(response).to render_template(:verify)
              end
            end

            context 'as JSON' do
              before do
                update_issue({ title: 'Spam Title', description: 'Spam lives here' }, format: :json)
              end

              it 'renders json errors' do
                expect(json_response)
                  .to eql("errors" => ["Your issue has been recognized as spam. Please, change the content or solve the reCAPTCHA to proceed."])
              end
331

332 333 334
              it 'returns 422 status' do
                expect(response).to have_http_status(422)
              end
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
            end
          end

          context 'when captcha is verified' do
            let(:spammy_title) { 'Whatever' }
            let!(:spam_logs) { create_list(:spam_log, 2, user: user, title: spammy_title) }

            def update_verified_issue
              update_issue({ title: spammy_title },
                           { spam_log_id: spam_logs.last.id,
                             recaptcha_verification: true })
            end

            before do
              allow_any_instance_of(described_class).to receive(:verify_recaptcha)
                .and_return(true)
            end

            it 'redirect to issue page' do
              update_verified_issue

356
              expect(response)
357
                .to redirect_to(project_issue_path(project, issue))
358 359 360
            end

            it 'accepts an issue after recaptcha is verified' do
361
              expect { update_verified_issue }.to change { issue.reload.title }.to(spammy_title)
362 363 364 365 366 367 368 369 370
            end

            it 'marks spam log as recaptcha_verified' do
              expect { update_verified_issue }.to change { SpamLog.last.recaptcha_verified }.from(false).to(true)
            end

            it 'does not mark spam log as recaptcha_verified when it does not belong to current_user' do
              spam_log = create(:spam_log)

371 372
              expect { update_issue(spam_log_id: spam_log.id, recaptcha_verification: true) }
                .not_to change { SpamLog.last.recaptcha_verified }
373 374 375 376 377 378 379 380
            end
          end
        end
      end

      def update_issue(issue_params = {}, additional_params = {})
        params = {
          namespace_id: project.namespace.to_param,
381
          project_id: project,
382 383 384 385 386 387 388
          id: issue.iid,
          issue: issue_params
        }.merge(additional_params)

        put :update, params
      end

389 390 391
      def move_issue
        put :update,
          namespace_id: project.namespace.to_param,
392
          project_id: project,
393 394 395 396 397 398 399
          id: issue.iid,
          issue: { title: 'New title' },
          move_to_project_id: another_project.id
      end
    end
  end

400
  describe 'Confidential Issues' do
401
    let(:project) { create(:project_empty_repo, :public) }
402 403 404 405 406 407 408
    let(:assignee) { create(:assignee) }
    let(:author) { create(:user) }
    let(:non_member) { create(:user) }
    let(:member) { create(:user) }
    let(:admin) { create(:admin) }
    let!(:issue) { create(:issue, project: project) }
    let!(:unescaped_parameter_value) { create(:issue, :confidential, project: project, author: author) }
409
    let!(:request_forgery_timing_attack) { create(:issue, :confidential, project: project, assignees: [assignee]) }
410 411

    describe 'GET #index' do
412
      it 'does not list confidential issues for guests' do
413 414 415 416 417 418
        sign_out(:user)
        get_issues

        expect(assigns(:issues)).to eq [issue]
      end

419
      it 'does not list confidential issues for non project members' do
420 421 422 423 424 425
        sign_in(non_member)
        get_issues

        expect(assigns(:issues)).to eq [issue]
      end

426
      it 'does not list confidential issues for project members with guest role' do
427 428 429 430 431 432 433 434
        sign_in(member)
        project.team << [member, :guest]

        get_issues

        expect(assigns(:issues)).to eq [issue]
      end

435
      it 'lists confidential issues for author' do
436 437 438 439 440 441 442
        sign_in(author)
        get_issues

        expect(assigns(:issues)).to include unescaped_parameter_value
        expect(assigns(:issues)).not_to include request_forgery_timing_attack
      end

443
      it 'lists confidential issues for assignee' do
444 445 446 447 448 449 450
        sign_in(assignee)
        get_issues

        expect(assigns(:issues)).not_to include unescaped_parameter_value
        expect(assigns(:issues)).to include request_forgery_timing_attack
      end

451
      it 'lists confidential issues for project members' do
452 453 454 455 456 457 458 459 460
        sign_in(member)
        project.team << [member, :developer]

        get_issues

        expect(assigns(:issues)).to include unescaped_parameter_value
        expect(assigns(:issues)).to include request_forgery_timing_attack
      end

461
      it 'lists confidential issues for admin' do
462 463 464 465 466 467 468 469 470 471
        sign_in(admin)
        get_issues

        expect(assigns(:issues)).to include unescaped_parameter_value
        expect(assigns(:issues)).to include request_forgery_timing_attack
      end

      def get_issues
        get :index,
          namespace_id: project.namespace.to_param,
472
          project_id: project
473 474
      end
    end
475

476 477
    shared_examples_for 'restricted action' do |http_status|
      it 'returns 404 for guests' do
478
        sign_out(:user)
479 480 481 482 483 484 485 486 487
        go(id: unescaped_parameter_value.to_param)

        expect(response).to have_http_status :not_found
      end

      it 'returns 404 for non project members' do
        sign_in(non_member)
        go(id: unescaped_parameter_value.to_param)

488 489 490 491 492 493 494 495
        expect(response).to have_http_status :not_found
      end

      it 'returns 404 for project members with guest role' do
        sign_in(member)
        project.team << [member, :guest]
        go(id: unescaped_parameter_value.to_param)

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 523 524 525 526 527 528 529 530 531 532 533 534
        expect(response).to have_http_status :not_found
      end

      it "returns #{http_status[:success]} for author" do
        sign_in(author)
        go(id: unescaped_parameter_value.to_param)

        expect(response).to have_http_status http_status[:success]
      end

      it "returns #{http_status[:success]} for assignee" do
        sign_in(assignee)
        go(id: request_forgery_timing_attack.to_param)

        expect(response).to have_http_status http_status[:success]
      end

      it "returns #{http_status[:success]} for project members" do
        sign_in(member)
        project.team << [member, :developer]
        go(id: unescaped_parameter_value.to_param)

        expect(response).to have_http_status http_status[:success]
      end

      it "returns #{http_status[:success]} for admin" do
        sign_in(admin)
        go(id: unescaped_parameter_value.to_param)

        expect(response).to have_http_status http_status[:success]
      end
    end

    describe 'GET #show' do
      it_behaves_like 'restricted action', success: 200

      def go(id:)
        get :show,
          namespace_id: project.namespace.to_param,
535
          project_id: project,
536 537 538 539
          id: id
      end
    end

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    describe 'GET #realtime_changes' do
      it_behaves_like 'restricted action', success: 200

      def go(id:)
        get :realtime_changes,
          namespace_id: project.namespace.to_param,
          project_id: project,
          id: id
      end

      context 'when an issue was edited by a deleted user' do
        let(:deleted_user) { create(:user) }

        before do
          project.team << [user, :developer]

          issue.update!(last_edited_by: deleted_user, last_edited_at: Time.now)

          deleted_user.destroy
          sign_in(user)
        end

        it 'returns 200' do
          go(id: issue.iid)

          expect(response).to have_http_status(200)
        end
      end
    end

570 571 572 573 574 575
    describe 'GET #edit' do
      it_behaves_like 'restricted action', success: 200

      def go(id:)
        get :edit,
          namespace_id: project.namespace.to_param,
576
          project_id: project,
577 578 579 580 581 582 583 584 585 586
          id: id
      end
    end

    describe 'PUT #update' do
      it_behaves_like 'restricted action', success: 302

      def go(id:)
        put :update,
          namespace_id: project.namespace.to_param,
587
          project_id: project,
588 589 590 591
          id: id,
          issue: { title: 'New title' }
      end
    end
592
  end
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
593

594
  describe 'POST #create' do
595
    def post_new_issue(issue_attrs = {}, additional_params = {})
596
      sign_in(user)
597
      project = create(:project, :public)
598 599 600 601
      project.team << [user, :developer]

      post :create, {
        namespace_id: project.namespace.to_param,
602
        project_id: project,
603 604
        issue: { title: 'Title', description: 'Description' }.merge(issue_attrs)
      }.merge(additional_params)
605 606 607 608

      project.issues.first
    end

609
    context 'resolving discussions in MergeRequest' do
610
      let(:discussion) { create(:diff_note_on_merge_request).to_discussion }
611 612 613 614 615 616 617 618 619
      let(:merge_request) { discussion.noteable }
      let(:project) { merge_request.source_project }

      before do
        project.team << [user, :master]
        sign_in user
      end

      let(:merge_request_params) do
Bob Van Landuyt's avatar
Bob Van Landuyt committed
620
        { merge_request_to_resolve_discussions_of: merge_request.iid }
621 622
      end

623
      def post_issue(issue_params, other_params: {})
Bob Van Landuyt's avatar
Bob Van Landuyt committed
624
        post :create, { namespace_id: project.namespace.to_param, project_id: project, issue: issue_params, merge_request_to_resolve_discussions_of: merge_request.iid }.merge(other_params)
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
      end

      it 'creates an issue for the project' do
        expect { post_issue({ title: 'Hello' }) }.to change { project.issues.reload.size }.by(1)
      end

      it "doesn't overwrite given params" do
        post_issue(description: 'Manually entered description')

        expect(assigns(:issue).description).to eq('Manually entered description')
      end

      it 'resolves the discussion in the merge_request' do
        post_issue(title: 'Hello')
        discussion.first_note.reload

        expect(discussion.resolved?).to eq(true)
      end
643

644 645 646
      it 'sets a flash message' do
        post_issue(title: 'Hello')

647
        expect(flash[:notice]).to eq('Resolved all discussions.')
648 649
      end

650 651 652 653 654 655
      describe "resolving a single discussion" do
        before do
          post_issue({ title: 'Hello' }, other_params: { discussion_to_resolve: discussion.id })
        end
        it 'resolves a single discussion' do
          discussion.first_note.reload
656

657 658
          expect(discussion.resolved?).to eq(true)
        end
659

660 661 662
        it 'sets a flash message that one discussion was resolved' do
          expect(flash[:notice]).to eq('Resolved 1 discussion.')
        end
663
      end
664 665
    end

666 667
    context 'Akismet is enabled' do
      before do
668
        stub_application_setting(recaptcha_enabled: true)
669
        allow_any_instance_of(SpamService).to receive(:check_for_spam?).and_return(true)
670 671
      end

672
      context 'when an issue is not identified as spam' do
673 674 675 676
        before do
          allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(false)
          allow_any_instance_of(AkismetService).to receive(:is_spam?).and_return(false)
        end
677

678 679 680
        it 'does not create an issue' do
          expect { post_new_issue(title: '') }.not_to change(Issue, :count)
        end
681 682
      end

683
      context 'when an issue is identified as spam' do
684 685 686
        before do
          allow_any_instance_of(AkismetService).to receive(:is_spam?).and_return(true)
        end
687 688 689 690 691 692

        context 'when captcha is not verified' do
          def post_spam_issue
            post_new_issue(title: 'Spam Title', description: 'Spam lives here')
          end

693 694 695
          before do
            allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(false)
          end
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

          it 'rejects an issue recognized as a spam' do
            expect { post_spam_issue }.not_to change(Issue, :count)
          end

          it 'creates a spam log' do
            post_spam_issue
            spam_logs = SpamLog.all

            expect(spam_logs.count).to eq(1)
            expect(spam_logs.first.title).to eq('Spam Title')
            expect(spam_logs.first.recaptcha_verified).to be_falsey
          end

          it 'does not create an issue when it is not valid' do
            expect { post_new_issue(title: '') }.not_to change(Issue, :count)
          end

          it 'does not create an issue when recaptcha is not enabled' do
            stub_application_setting(recaptcha_enabled: false)

            expect { post_spam_issue }.not_to change(Issue, :count)
          end
        end

        context 'when captcha is verified' do
          let!(:spam_logs) { create_list(:spam_log, 2, user: user, title: 'Title') }

          def post_verified_issue
            post_new_issue({}, { spam_log_id: spam_logs.last.id, recaptcha_verification: true } )
          end

          before do
            allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(true)
          end

          it 'accepts an issue after recaptcha is verified' do
            expect { post_verified_issue }.to change(Issue, :count)
          end

          it 'marks spam log as recaptcha_verified' do
            expect { post_verified_issue }.to change { SpamLog.last.recaptcha_verified }.from(false).to(true)
          end

          it 'does not mark spam log as recaptcha_verified when it does not belong to current_user' do
            spam_log = create(:spam_log)

743 744
            expect { post_new_issue({}, { spam_log_id: spam_log.id, recaptcha_verification: true } ) }
              .not_to change { SpamLog.last.recaptcha_verified }
745 746
          end
        end
747 748
      end
    end
749 750 751 752 753 754

    context 'user agent details are saved' do
      before do
        request.env['action_dispatch.remote_ip'] = '127.0.0.1'
      end

755
      it 'creates a user agent detail' do
756
        expect { post_new_issue }.to change(UserAgentDetail, :count).by(1)
757 758 759
      end
    end

760
    context 'when description has quick actions' do
761
      before do
762 763 764
        sign_in(user)
      end

765 766 767 768 769 770 771 772 773 774
      it 'can add spent time' do
        issue = post_new_issue(description: '/spend 1h')

        expect(issue.total_time_spent).to eq(3600)
      end

      it 'can set the time estimate' do
        issue = post_new_issue(description: '/estimate 2h')

        expect(issue.time_estimate).to eq(7200)
775 776
      end
    end
777 778
  end

779 780 781
  describe 'POST #mark_as_spam' do
    context 'properly submits to Akismet' do
      before do
782
        allow_any_instance_of(AkismetService).to receive_messages(submit_spam: true)
783
        allow_any_instance_of(ApplicationSetting).to receive_messages(akismet_enabled: true)
784 785 786 787 788 789 790 791
      end

      def post_spam
        admin = create(:admin)
        create(:user_agent_detail, subject: issue)
        project.team << [admin, :master]
        sign_in(admin)
        post :mark_as_spam, {
792 793
          namespace_id: project.namespace,
          project_id: project,
794 795 796 797 798 799
          id: issue.iid
        }
      end

      it 'updates issue' do
        post_spam
800
        expect(issue.submittable_as_spam?).to be_falsey
801 802 803 804
      end
    end
  end

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
805
  describe "DELETE #destroy" do
806
    context "when the user is a developer" do
807 808 809 810
      before do
        sign_in(user)
      end

811
      it "rejects a developer to destroy an issue" do
812
        delete :destroy, namespace_id: project.namespace, project_id: project, id: issue.iid
813
        expect(response).to have_http_status(404)
814
      end
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
815 816
    end

817 818 819
    context "when the user is owner" do
      let(:owner)     { create(:user) }
      let(:namespace) { create(:namespace, owner: owner) }
820
      let(:project)   { create(:project, namespace: namespace) }
821

822 823 824
      before do
        sign_in(owner)
      end
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
825

826
      it "deletes the issue" do
827
        delete :destroy, namespace_id: project.namespace, project_id: project, id: issue.iid
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
828

829
        expect(response).to have_http_status(302)
830
        expect(controller).to set_flash[:notice].to(/The issue was successfully deleted\./)
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
831
      end
832 833 834 835

      it 'delegates the update of the todos count cache to TodoService' do
        expect_any_instance_of(TodoService).to receive(:destroy_issue).with(issue, owner).once

836
        delete :destroy, namespace_id: project.namespace, project_id: project, id: issue.iid
837
      end
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
838 839
    end
  end
840 841 842 843 844 845 846

  describe 'POST #toggle_award_emoji' do
    before do
      sign_in(user)
      project.team << [user, :developer]
    end

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
847
    it "toggles the award emoji" do
Z.J. van de Weg's avatar
Z.J. van de Weg committed
848
      expect do
849 850
        post(:toggle_award_emoji, namespace_id: project.namespace,
                                  project_id: project, id: issue.iid, name: "thumbsup")
Z.J. van de Weg's avatar
Z.J. van de Weg committed
851
      end.to change { issue.award_emoji.count }.by(1)
852

853
      expect(response).to have_http_status(200)
854 855
    end
  end
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

  describe 'POST create_merge_request' do
    before do
      project.add_developer(user)
      sign_in(user)
    end

    it 'creates a new merge request' do
      expect { create_merge_request }.to change(project.merge_requests, :count).by(1)
    end

    it 'render merge request as json' do
      create_merge_request

      expect(response).to match_response_schema('merge_request')
    end

    def create_merge_request
      post :create_merge_request, namespace_id: project.namespace.to_param,
                                  project_id: project.to_param,
                                  id: issue.to_param,
                                  format: :json
    end
  end
880
end