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

describe Projects::IssuesController do
4
  let(:project) { create(:project_empty_repo) }
5 6
  let(:user)    { create(:user) }
  let(:issue)   { create(:issue, project: project) }
7

8
  describe "GET #index" do
9 10
    context 'external issue tracker' do
      it 'redirects to the external issue tracker' do
11
        external = double(project_path: 'https://example.com/project')
12 13
        allow(project).to receive(:external_issue_tracker).and_return(external)
        controller.instance_variable_set(:@project, project)
14

15
        get :index, namespace_id: project.namespace.path, project_id: project
16

17
        expect(response).to redirect_to('https://example.com/project')
18
      end
19 20
    end

21 22 23 24 25
    context 'internal issue tracker' do
      before do
        sign_in(user)
        project.team << [user, :developer]
      end
26

27 28
      it_behaves_like "issuables list meta-data", :issue

29 30
      it "returns index" do
        get :index, namespace_id: project.namespace.path, project_id: project.path
31

32 33
        expect(response).to have_http_status(200)
      end
34

35
      it "returns 301 if request path doesn't match project path" do
36 37 38 39
        get :index, namespace_id: project.namespace.path, project_id: project.path.upcase

        expect(response).to redirect_to(namespace_project_issues_path(project.namespace, project))
      end
40

41 42 43
      it "returns 404 when issues are disabled" do
        project.issues_enabled = false
        project.save
44

45 46 47 48 49 50 51 52 53 54
        get :index, namespace_id: project.namespace.path, project_id: project.path
        expect(response).to have_http_status(404)
      end

      it "returns 404 when external issue tracker is enabled" do
        controller.instance_variable_set(:@project, project)
        allow(project).to receive(:default_issues_tracker?).and_return(false)

        get :index, namespace_id: project.namespace.path, project_id: project.path
        expect(response).to have_http_status(404)
55 56
      end
    end
57 58 59

    context 'with page param' do
      let(:last_page) { project.issues.page().total_pages }
60
      let!(:issue_list) { create_list(:issue, 2, project: project) }
61 62 63 64

      before do
        sign_in(user)
        project.team << [user, :developer]
65
        allow(Kaminari.config).to receive(:default_per_page).and_return(1)
66 67 68 69 70 71 72 73
      end

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

74
        expect(response).to redirect_to(namespace_project_issues_path(page: last_page, state: controller.params[:state], scope: controller.params[:scope]))
75 76 77 78 79 80 81 82 83 84 85 86
      end

      it 'redirects to specified page' do
        get :index,
          namespace_id: project.namespace.path.to_param,
          project_id: project.path.to_param,
          page: last_page.to_param

        expect(assigns(:issues).current_page).to eq(last_page)
        expect(response).to have_http_status(200)
      end
    end
87 88 89
  end

  describe 'GET #new' do
90 91 92 93 94 95 96 97 98 99 100 101 102
    context 'internal issue tracker' do
      before do
        sign_in(user)
        project.team << [user, :developer]
      end

      it 'builds a new issue' do
        get :new, namespace_id: project.namespace.path, project_id: project

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

      it 'fills in an issue for a merge request' do
103
        project_with_repository = create(:project, :repository)
104 105 106 107 108 109 110 111 112 113
        project_with_repository.team << [user, :developer]
        mr = create(:merge_request_with_diff_notes, source_project: project_with_repository)

        get :new, namespace_id: project_with_repository.namespace.path, project_id: project_with_repository, merge_request_for_resolving_discussions: mr.iid

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

114 115 116 117 118 119 120 121 122
    context 'external issue tracker' do
      it 'redirects to the external issue tracker' do
        external = double(new_issue_path: 'https://example.com/issues/new')
        allow(project).to receive(:external_issue_tracker).and_return(external)
        controller.instance_variable_set(:@project, project)

        get :new, namespace_id: project.namespace.path, project_id: project

        expect(response).to redirect_to('https://example.com/issues/new')
123
      end
124
    end
125 126
  end

127 128
  describe 'PUT #update' do
    context 'when moving issue to another private project' do
129
      let(:another_project) { create(:empty_project, :private) }
130 131 132 133 134 135 136 137 138 139 140 141 142

      before do
        sign_in(user)
        project.team << [user, :developer]
      end

      context 'when user has access to move issue' do
        before { another_project.team << [user, :reporter] }

        it 'moves issue to another project' do
          move_issue

          expect(response).to have_http_status :found
143
          expect(another_project.issues).not_to be_empty
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        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

      def move_issue
        put :update,
          namespace_id: project.namespace.to_param,
          project_id: project.to_param,
          id: issue.iid,
          issue: { title: 'New title' },
          move_to_project_id: another_project.id
      end
    end
  end

166
  describe 'Confidential Issues' do
167
    let(:project) { create(:project_empty_repo, :public) }
168 169 170 171 172 173 174 175 176 177
    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) }
    let!(:request_forgery_timing_attack) { create(:issue, :confidential, project: project, assignee: assignee) }

    describe 'GET #index' do
178
      it 'does not list confidential issues for guests' do
179 180 181 182 183 184
        sign_out(:user)
        get_issues

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

185
      it 'does not list confidential issues for non project members' do
186 187 188 189 190 191
        sign_in(non_member)
        get_issues

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

192
      it 'does not list confidential issues for project members with guest role' do
193 194 195 196 197 198 199 200
        sign_in(member)
        project.team << [member, :guest]

        get_issues

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

201
      it 'lists confidential issues for author' do
202 203 204 205 206 207 208
        sign_in(author)
        get_issues

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

209
      it 'lists confidential issues for assignee' do
210 211 212 213 214 215 216
        sign_in(assignee)
        get_issues

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

217
      it 'lists confidential issues for project members' do
218 219 220 221 222 223 224 225 226
        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

227
      it 'lists confidential issues for admin' do
228 229 230 231 232 233 234 235 236 237 238 239 240
        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,
          project_id: project.to_param
      end
    end
241

242 243
    shared_examples_for 'restricted action' do |http_status|
      it 'returns 404 for guests' do
244
        sign_out(:user)
245 246 247 248 249 250 251 252 253
        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)

254 255 256 257 258 259 260 261
        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)

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        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,
          project_id: project.to_param,
          id: id
      end
    end

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

      def go(id:)
        get :edit,
          namespace_id: project.namespace.to_param,
          project_id: project.to_param,
          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,
          project_id: project.to_param,
          id: id,
          issue: { title: 'New title' }
      end
    end
328
  end
329

330
  describe 'POST #create' do
331
    def post_new_issue(issue_attrs = {}, additional_params = {})
332 333 334 335 336 337 338
      sign_in(user)
      project = create(:empty_project, :public)
      project.team << [user, :developer]

      post :create, {
        namespace_id: project.namespace.to_param,
        project_id: project.to_param,
339 340
        issue: { title: 'Title', description: 'Description' }.merge(issue_attrs)
      }.merge(additional_params)
341 342 343 344

      project.issues.first
    end

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    context 'resolving discussions in MergeRequest' do
      let(:discussion) { Discussion.for_diff_notes([create(:diff_note_on_merge_request)]).first }
      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
        { merge_request_for_resolving_discussions: merge_request.iid }
      end

      def post_issue(issue_params)
        post :create, namespace_id: project.namespace.to_param, project_id: project.to_param, issue: issue_params, merge_request_for_resolving_discussions: merge_request.iid
      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
    end

381 382
    context 'Akismet is enabled' do
      before do
383
        stub_application_setting(recaptcha_enabled: true)
384
        allow_any_instance_of(SpamService).to receive(:check_for_spam?).and_return(true)
385 386
      end

387 388 389 390 391
      context 'when an issue is not identified as a 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
392

393 394 395
        it 'does not create an issue' do
          expect { post_new_issue(title: '') }.not_to change(Issue, :count)
        end
396 397
      end

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 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
      context 'when an issue is identified as a spam' do
        before { allow_any_instance_of(AkismetService).to receive(:is_spam?).and_return(true) }

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

          before { allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(false) }

          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)

            expect { post_new_issue({}, { spam_log_id: spam_log.id, recaptcha_verification: true } ) }.
              not_to change { SpamLog.last.recaptcha_verified }
          end
        end
458 459
      end
    end
460 461 462 463 464 465

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

466
      it 'creates a user agent detail' do
467
        expect { post_new_issue }.to change(UserAgentDetail, :count).by(1)
468 469 470 471 472
      end
    end

    context 'when description has slash commands' do
      before do
473 474 475
        sign_in(user)
      end

476 477 478 479 480 481 482 483 484 485
      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)
486 487
      end
    end
488 489
  end

490 491 492
  describe 'POST #mark_as_spam' do
    context 'properly submits to Akismet' do
      before do
493
        allow_any_instance_of(AkismetService).to receive_messages(submit_spam: true)
494
        allow_any_instance_of(ApplicationSetting).to receive_messages(akismet_enabled: true)
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
      end

      def post_spam
        admin = create(:admin)
        create(:user_agent_detail, subject: issue)
        project.team << [admin, :master]
        sign_in(admin)
        post :mark_as_spam, {
          namespace_id: project.namespace.path,
          project_id: project.path,
          id: issue.iid
        }
      end

      it 'updates issue' do
        post_spam
511
        expect(issue.submittable_as_spam?).to be_falsey
512 513 514 515
      end
    end
  end

516
  describe "DELETE #destroy" do
517 518 519 520
    context "when the user is a developer" do
      before { sign_in(user) }
      it "rejects a developer to destroy an issue" do
        delete :destroy, namespace_id: project.namespace.path, project_id: project.path, id: issue.iid
521
        expect(response).to have_http_status(404)
522
      end
523 524
    end

525 526 527
    context "when the user is owner" do
      let(:owner)     { create(:user) }
      let(:namespace) { create(:namespace, owner: owner) }
528
      let(:project)   { create(:empty_project, namespace: namespace) }
529

530
      before { sign_in(owner) }
531

532
      it "deletes the issue" do
533 534
        delete :destroy, namespace_id: project.namespace.path, project_id: project.path, id: issue.iid

535
        expect(response).to have_http_status(302)
536
        expect(controller).to set_flash[:notice].to(/The issue was successfully deleted\./).now
537
      end
538 539 540 541 542 543

      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

        delete :destroy, namespace_id: project.namespace.path, project_id: project.path, id: issue.iid
      end
544 545
    end
  end
546 547 548 549 550 551 552

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

553
    it "toggles the award emoji" do
Z.J. van de Weg's avatar
Z.J. van de Weg committed
554
      expect do
555
        post(:toggle_award_emoji, namespace_id: project.namespace.path,
Z.J. van de Weg's avatar
Z.J. van de Weg committed
556 557
                                  project_id: project.path, id: issue.iid, name: "thumbsup")
      end.to change { issue.award_emoji.count }.by(1)
558

559
      expect(response).to have_http_status(200)
560 561
    end
  end
562
end