notes_controller_spec.rb 5.97 KB
Newer Older
1
require 'spec_helper'
2 3 4

describe Projects::NotesController do
  let(:user)    { create(:user) }
5
  let(:project) { create(:empty_project) }
6 7 8
  let(:issue)   { create(:issue, project: project) }
  let(:note)    { create(:note, noteable: issue, project: project) }

Douwe Maan's avatar
Douwe Maan committed
9 10 11 12 13 14 15 16
  let(:request_params) do
    {
      namespace_id: project.namespace,
      project_id: project,
      id: note
    }
  end

17 18
  describe 'POST create' do
    let(:merge_request) { create(:merge_request) }
19
    let(:project) { merge_request.source_project }
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    let(:request_params) do
      {
        note: { note: 'some note', noteable_id: merge_request.id, noteable_type: 'MergeRequest' },
        namespace_id: project.namespace,
        project_id: project,
        merge_request_diff_head_sha: 'sha'
      }
    end

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

    it "returns status 302 for html" do
      post :create, request_params

      expect(response).to have_http_status(302)
    end

    it "returns status 200 for json" do
      post :create, request_params.merge(format: :json)

      expect(response).to have_http_status(200)
    end

    context 'when merge_request_diff_head_sha present' do
      before do
        service_params = {
          note: 'some note',
          noteable_id: merge_request.id.to_s,
          noteable_type: 'MergeRequest',
          merge_request_diff_head_sha: 'sha'
        }

        expect(Notes::CreateService).to receive(:new).with(project, user, service_params).and_return(double(execute: true))
      end

      it "returns status 302 for html" do
        post :create, request_params

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

66
  describe 'POST toggle_award_emoji' do
67 68 69 70 71 72 73
    before do
      sign_in(user)
      project.team << [user, :developer]
    end

    it "toggles the award emoji" do
      expect do
Douwe Maan's avatar
Douwe Maan committed
74
        post(:toggle_award_emoji, request_params.merge(name: "thumbsup"))
Z.J. van de Weg's avatar
Z.J. van de Weg committed
75
      end.to change { note.award_emoji.count }.by(1)
76

77
      expect(response).to have_http_status(200)
78 79
    end

Z.J. van de Weg's avatar
Z.J. van de Weg committed
80
    it "removes the already awarded emoji" do
Douwe Maan's avatar
Douwe Maan committed
81
      post(:toggle_award_emoji, request_params.merge(name: "thumbsup"))
82 83

      expect do
Douwe Maan's avatar
Douwe Maan committed
84
        post(:toggle_award_emoji, request_params.merge(name: "thumbsup"))
85 86
      end.to change { AwardEmoji.count }.by(-1)

87
      expect(response).to have_http_status(200)
88 89
    end
  end
90

Douwe Maan's avatar
Douwe Maan committed
91
  describe "resolving and unresolving" do
92
    let(:project) { create(:project, :repository) }
Douwe Maan's avatar
Douwe Maan committed
93 94
    let(:merge_request) { create(:merge_request, source_project: project) }
    let(:note) { create(:diff_note_on_merge_request, noteable: merge_request, project: project) }
95

Douwe Maan's avatar
Douwe Maan committed
96
    describe 'POST resolve' do
97
      before do
Douwe Maan's avatar
Douwe Maan committed
98
        sign_in user
99 100
      end

Douwe Maan's avatar
Douwe Maan committed
101
      context "when the user is not authorized to resolve the note" do
102
        it "returns status 404" do
Douwe Maan's avatar
Douwe Maan committed
103
          post :resolve, request_params
104 105 106 107 108

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

Douwe Maan's avatar
Douwe Maan committed
109 110 111
      context "when the user is authorized to resolve the note" do
        before do
          project.team << [user, :developer]
112 113
        end

Douwe Maan's avatar
Douwe Maan committed
114 115 116 117
        context "when the note is not resolvable" do
          before do
            note.update(system: true)
          end
118

Douwe Maan's avatar
Douwe Maan committed
119 120 121 122 123
          it "returns status 404" do
            post :resolve, request_params

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

Douwe Maan's avatar
Douwe Maan committed
126 127 128
        context "when the note is resolvable" do
          it "resolves the note" do
            post :resolve, request_params
129

Douwe Maan's avatar
Douwe Maan committed
130 131 132
            expect(note.reload.resolved?).to be true
            expect(note.reload.resolved_by).to eq(user)
          end
133

Douwe Maan's avatar
Douwe Maan committed
134 135
          it "sends notifications if all discussions are resolved" do
            expect_any_instance_of(MergeRequests::ResolvedDiscussionNotificationService).to receive(:execute).with(merge_request)
136

Douwe Maan's avatar
Douwe Maan committed
137 138
            post :resolve, request_params
          end
139

Douwe Maan's avatar
Douwe Maan committed
140 141 142 143 144
          it "returns the name of the resolving user" do
            post :resolve, request_params

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

Douwe Maan's avatar
Douwe Maan committed
146 147
          it "returns status 200" do
            post :resolve, request_params
148

Douwe Maan's avatar
Douwe Maan committed
149 150 151
            expect(response).to have_http_status(200)
          end
        end
152 153 154
      end
    end

Douwe Maan's avatar
Douwe Maan committed
155
    describe 'DELETE unresolve' do
156
      before do
Douwe Maan's avatar
Douwe Maan committed
157 158 159
        sign_in user

        note.resolve!(user)
160 161
      end

Douwe Maan's avatar
Douwe Maan committed
162
      context "when the user is not authorized to resolve the note" do
163
        it "returns status 404" do
Douwe Maan's avatar
Douwe Maan committed
164
          delete :unresolve, request_params
165 166 167 168 169

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

Douwe Maan's avatar
Douwe Maan committed
170 171 172 173 174 175 176 177 178
      context "when the user is authorized to resolve the note" do
        before do
          project.team << [user, :developer]
        end

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

Douwe Maan's avatar
Douwe Maan committed
180 181
          it "returns status 404" do
            delete :unresolve, request_params
182

Douwe Maan's avatar
Douwe Maan committed
183 184
            expect(response).to have_http_status(404)
          end
185 186
        end

Douwe Maan's avatar
Douwe Maan committed
187 188 189 190 191 192 193 194 195
        context "when the note is resolvable" do
          it "unresolves the note" do
            delete :unresolve, request_params

            expect(note.reload.resolved?).to be false
          end

          it "returns status 200" do
            delete :unresolve, request_params
196

Douwe Maan's avatar
Douwe Maan committed
197 198
            expect(response).to have_http_status(200)
          end
199 200 201 202
        end
      end
    end
  end
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

  describe 'GET index' do
    let(:last_fetched_at) { '1487756246' }
    let(:request_params) do
      {
        namespace_id: project.namespace,
        project_id: project,
        target_type: 'issue',
        target_id: issue.id
      }
    end

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

    it 'passes last_fetched_at from headers to NotesFinder' do
      request.headers['X-Last-Fetched-At'] = last_fetched_at

      expect(NotesFinder).to receive(:new)
        .with(anything, anything, hash_including(last_fetched_at: last_fetched_at))
        .and_call_original

      get :index, request_params
    end
  end
230
end