BigW Consortium Gitlab

discussions_controller_spec.rb 3.17 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe Projects::DiscussionsController do
  let(:user)    { create(:user) }
  let(:project) { create(:project) }
  let(:merge_request) { create(:merge_request, source_project: project) }
  let(:note) { create(:diff_note_on_merge_request, noteable: merge_request, project: project) }
Douwe Maan committed
8 9 10 11 12 13 14 15 16 17
  let(:discussion) { note.discussion }

  let(:request_params) do
    {
      namespace_id: project.namespace,
      project_id: project,
      merge_request_id: merge_request,
      id: note.discussion_id
    }
  end
18 19 20 21 22 23 24 25

  describe 'POST resolve' do
    before do
      sign_in user
    end

    context "when the user is not authorized to resolve the discussion" do
      it "returns status 404" do
Douwe Maan committed
26
        post :resolve, request_params
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

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

    context "when the user is authorized to resolve the discussion" do
      before do
        project.team << [user, :developer]
      end

      context "when the discussion is not resolvable" do
        before do
          note.update(system: true)
        end

        it "returns status 404" do
Douwe Maan committed
43
          post :resolve, request_params
44 45 46 47 48 49 50

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

      context "when the discussion is resolvable" do
        it "resolves the discussion" do
Douwe Maan committed
51
          post :resolve, request_params
52

Douwe Maan committed
53 54
          expect(note.reload.discussion.resolved?).to be true
          expect(note.reload.discussion.resolved_by).to eq(user)
55 56
        end

Douwe Maan committed
57 58
        it "sends notifications if all discussions are resolved" do
          expect_any_instance_of(MergeRequests::ResolvedDiscussionNotificationService).to receive(:execute).with(merge_request)
59

Douwe Maan committed
60
          post :resolve, request_params
61 62 63
        end

        it "returns the name of the resolving user" do
Douwe Maan committed
64
          post :resolve, request_params
65 66 67 68 69

          expect(JSON.parse(response.body)["resolved_by"]).to eq(user.name)
        end

        it "returns status 200" do
Douwe Maan committed
70
          post :resolve, request_params
71 72 73 74 75 76 77 78 79 80

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

  describe 'DELETE unresolve' do
    before do
      sign_in user
Douwe Maan committed
81 82

      note.discussion.resolve!(user)
83 84 85 86
    end

    context "when the user is not authorized to resolve the discussion" do
      it "returns status 404" do
Douwe Maan committed
87
        delete :unresolve, request_params
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

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

    context "when the user is authorized to resolve the discussion" do
      before do
        project.team << [user, :developer]
      end

      context "when the discussion is not resolvable" do
        before do
          note.update(system: true)
        end

        it "returns status 404" do
Douwe Maan committed
104
          delete :unresolve, request_params
105 106 107 108 109 110 111

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

      context "when the discussion is resolvable" do
        it "unresolves the discussion" do
Douwe Maan committed
112
          delete :unresolve, request_params
113

Douwe Maan committed
114
          expect(note.reload.discussion.resolved?).to be false
115 116 117
        end

        it "returns status 200" do
Douwe Maan committed
118
          delete :unresolve, request_params
119 120 121 122 123 124 125

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