BigW Consortium Gitlab

notes_spec.rb 7.4 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::API, api: true  do
4 5
  include ApiHelpers
  let(:user) { create(:user) }
6
  let!(:project) { create(:project, namespace: user.namespace ) }
7
  let!(:issue) { create(:issue, project: project, author: user) }
8
  let!(:merge_request) { create(:merge_request, source_project: project, target_project: project, author: user) }
Andrew8xx8 committed
9
  let!(:snippet) { create(:project_snippet, project: project, author: user) }
10
  let!(:issue_note) { create(:note, noteable: issue, project: project, author: user) }
11
  let!(:merge_request_note) { create(:note, noteable: merge_request, project: project, author: user) }
12
  let!(:snippet_note) { create(:note, noteable: snippet, project: project, author: user) }
Dmitriy Zaporozhets committed
13
  before { project.team << [user, :reporter] }
14 15 16

  describe "GET /projects/:id/noteable/:noteable_id/notes" do
    context "when noteable is an Issue" do
Nihad Abbasov committed
17
      it "should return an array of issue notes" do
18
        get api("/projects/#{project.id}/issues/#{issue.id}/notes", user)
19 20 21
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first['body']).to eq(issue_note.note)
22
      end
23 24 25

      it "should return a 404 error when issue id not found" do
        get api("/projects/#{project.id}/issues/123/notes", user)
26
        expect(response.status).to eq(404)
27
      end
28 29 30
    end

    context "when noteable is a Snippet" do
Nihad Abbasov committed
31
      it "should return an array of snippet notes" do
32
        get api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user)
33 34 35
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first['body']).to eq(snippet_note.note)
36
      end
37 38 39

      it "should return a 404 error when snippet id not found" do
        get api("/projects/#{project.id}/snippets/42/notes", user)
40
        expect(response.status).to eq(404)
41
      end
42
    end
43 44 45 46

    context "when noteable is a Merge Request" do
      it "should return an array of merge_requests notes" do
        get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/notes", user)
47 48 49
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.first['body']).to eq(merge_request_note.note)
50
      end
51 52 53

      it "should return a 404 error if merge request id not found" do
        get api("/projects/#{project.id}/merge_requests/4444/notes", user)
54
        expect(response.status).to eq(404)
55 56
      end
    end
57
  end
Nihad Abbasov committed
58 59 60 61 62

  describe "GET /projects/:id/noteable/:noteable_id/notes/:note_id" do
    context "when noteable is an Issue" do
      it "should return an issue note by id" do
        get api("/projects/#{project.id}/issues/#{issue.id}/notes/#{issue_note.id}", user)
63 64
        expect(response.status).to eq(200)
        expect(json_response['body']).to eq(issue_note.note)
Nihad Abbasov committed
65
      end
66 67 68

      it "should return a 404 error if issue note not found" do
        get api("/projects/#{project.id}/issues/#{issue.id}/notes/123", user)
69
        expect(response.status).to eq(404)
70
      end
Nihad Abbasov committed
71 72 73 74 75
    end

    context "when noteable is a Snippet" do
      it "should return a snippet note by id" do
        get api("/projects/#{project.id}/snippets/#{snippet.id}/notes/#{snippet_note.id}", user)
76 77
        expect(response.status).to eq(200)
        expect(json_response['body']).to eq(snippet_note.note)
Nihad Abbasov committed
78
      end
79 80 81

      it "should return a 404 error if snippet note not found" do
        get api("/projects/#{project.id}/snippets/#{snippet.id}/notes/123", user)
82
        expect(response.status).to eq(404)
83
      end
Nihad Abbasov committed
84 85
    end
  end
Nihad Abbasov committed
86 87 88 89 90

  describe "POST /projects/:id/noteable/:noteable_id/notes" do
    context "when noteable is an Issue" do
      it "should create a new issue note" do
        post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!'
91 92 93
        expect(response.status).to eq(201)
        expect(json_response['body']).to eq('hi!')
        expect(json_response['author']['username']).to eq(user.username)
Nihad Abbasov committed
94
      end
95 96 97

      it "should return a 400 bad request error if body not given" do
        post api("/projects/#{project.id}/issues/#{issue.id}/notes", user)
98
        expect(response.status).to eq(400)
99 100 101 102
      end

      it "should return a 401 unauthorized error if user not authenticated" do
        post api("/projects/#{project.id}/issues/#{issue.id}/notes"), body: 'hi!'
103
        expect(response.status).to eq(401)
104
      end
Nihad Abbasov committed
105 106 107 108 109
    end

    context "when noteable is a Snippet" do
      it "should create a new snippet note" do
        post api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user), body: 'hi!'
110 111 112
        expect(response.status).to eq(201)
        expect(json_response['body']).to eq('hi!')
        expect(json_response['author']['username']).to eq(user.username)
Nihad Abbasov committed
113
      end
114 115 116

      it "should return a 400 bad request error if body not given" do
        post api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user)
117
        expect(response.status).to eq(400)
118 119 120 121
      end

      it "should return a 401 unauthorized error if user not authenticated" do
        post api("/projects/#{project.id}/snippets/#{snippet.id}/notes"), body: 'hi!'
122
        expect(response.status).to eq(401)
123 124
      end
    end
Nihad Abbasov committed
125
  end
126 127 128

  describe "POST /projects/:id/noteable/:noteable_id/notes to test observer on create" do
    it "should create an activity event when an issue note is created" do
129
      expect(Event).to receive(:create)
130 131 132 133

      post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!'
    end
  end
134 135 136 137 138 139

  describe 'PUT /projects/:id/noteable/:noteable_id/notes/:note_id' do
    context 'when noteable is an Issue' do
      it 'should return modified note' do
        put api("/projects/#{project.id}/issues/#{issue.id}/"\
                  "notes/#{issue_note.id}", user), body: 'Hello!'
140 141
        expect(response.status).to eq(200)
        expect(json_response['body']).to eq('Hello!')
142 143 144 145 146
      end

      it 'should return a 404 error when note id not found' do
        put api("/projects/#{project.id}/issues/#{issue.id}/notes/123", user),
                body: 'Hello!'
147
        expect(response.status).to eq(404)
148 149 150 151 152
      end

      it 'should return a 400 bad request error if body not given' do
        put api("/projects/#{project.id}/issues/#{issue.id}/"\
                  "notes/#{issue_note.id}", user)
153
        expect(response.status).to eq(400)
154 155 156 157 158 159 160
      end
    end

    context 'when noteable is a Snippet' do
      it 'should return modified note' do
        put api("/projects/#{project.id}/snippets/#{snippet.id}/"\
                  "notes/#{snippet_note.id}", user), body: 'Hello!'
161 162
        expect(response.status).to eq(200)
        expect(json_response['body']).to eq('Hello!')
163 164 165 166 167
      end

      it 'should return a 404 error when note id not found' do
        put api("/projects/#{project.id}/snippets/#{snippet.id}/"\
                  "notes/123", user), body: "Hello!"
168
        expect(response.status).to eq(404)
169 170 171 172 173 174 175
      end
    end

    context 'when noteable is a Merge Request' do
      it 'should return modified note' do
        put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/"\
                  "notes/#{merge_request_note.id}", user), body: 'Hello!'
176 177
        expect(response.status).to eq(200)
        expect(json_response['body']).to eq('Hello!')
178 179 180 181 182
      end

      it 'should return a 404 error when note id not found' do
        put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/"\
                  "notes/123", user), body: "Hello!"
183
        expect(response.status).to eq(404)
184 185 186 187
      end
    end
  end

188
end