commits_spec.rb 8.02 KB
Newer Older
1 2 3
require 'spec_helper'
require 'mime/types'

Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
4
describe API::API, api: true  do
5 6 7 8
  include ApiHelpers
  let(:user) { create(:user) }
  let(:user2) { create(:user) }
  let!(:project) { create(:project, creator_id: user.id) }
9 10
  let!(:master) { create(:project_member, :master, user: user, project: project) }
  let!(:guest) { create(:project_member, :guest, user: user2, project: project) }
11
  let!(:note) { create(:note_on_commit, author: user, project: project, commit_id: project.repository.commit.id, note: 'a comment on a commit') }
12
  let!(:another_note) { create(:note_on_commit, author: user, project: project, commit_id: project.repository.commit.id, note: 'another comment on a commit') }
13 14 15 16 17 18 19 20 21

  before { project.team << [user, :reporter] }

  describe "GET /projects/:id/repository/commits" do
    context "authorized user" do
      before { project.team << [user2, :reporter] }

      it "should return project commits" do
        get api("/projects/#{project.id}/repository/commits", user)
22
        expect(response).to have_http_status(200)
23

24 25
        expect(json_response).to be_an Array
        expect(json_response.first['id']).to eq(project.repository.commit.id)
26 27 28 29 30 31
      end
    end

    context "unauthorized user" do
      it "should not return project commits" do
        get api("/projects/#{project.id}/repository/commits")
32
        expect(response).to have_http_status(401)
33 34
      end
    end
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

    context "since optional parameter" do
      it "should return project commits since provided parameter" do
        commits = project.repository.commits("master")
        since = commits.second.created_at

        get api("/projects/#{project.id}/repository/commits?since=#{since.utc.iso8601}", user)

        expect(json_response.size).to eq 2
        expect(json_response.first["id"]).to eq(commits.first.id)
        expect(json_response.second["id"]).to eq(commits.second.id)
      end
    end

    context "until optional parameter" do
      it "should return project commits until provided parameter" do
        commits = project.repository.commits("master")
        before = commits.second.created_at

        get api("/projects/#{project.id}/repository/commits?until=#{before.utc.iso8601}", user)

        expect(json_response.size).to eq(commits.size - 1)
        expect(json_response.first["id"]).to eq(commits.second.id)
        expect(json_response.second["id"]).to eq(commits.third.id)
      end
    end

    context "invalid xmlschema date parameters" do
      it "should return an invalid parameter error message" do
        get api("/projects/#{project.id}/repository/commits?since=invalid-date", user)

66
        expect(response).to have_http_status(400)
67 68 69
        expect(json_response['message']).to include "\"since\" must be a timestamp in ISO 8601 format"
      end
    end
70 71 72 73 74 75
  end

  describe "GET /projects:id/repository/commits/:sha" do
    context "authorized user" do
      it "should return a commit by sha" do
        get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
76
        expect(response).to have_http_status(200)
77 78
        expect(json_response['id']).to eq(project.repository.commit.id)
        expect(json_response['title']).to eq(project.repository.commit.title)
79 80 81 82
      end

      it "should return a 404 error if not found" do
        get api("/projects/#{project.id}/repository/commits/invalid_sha", user)
83
        expect(response).to have_http_status(404)
84
      end
85

86
      it "should return nil for commit without CI" do
87
        get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
88
        expect(response).to have_http_status(200)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
89
        expect(json_response['status']).to be_nil
90 91 92
      end

      it "should return status for CI" do
93
        pipeline = project.ensure_pipeline(project.repository.commit.sha, 'master')
94
        get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
95
        expect(response).to have_http_status(200)
96
        expect(json_response['status']).to eq(pipeline.status)
97
      end
98 99 100 101 102
    end

    context "unauthorized user" do
      it "should not return the selected commit" do
        get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}")
103
        expect(response).to have_http_status(401)
104 105 106 107 108 109 110 111 112 113
      end
    end
  end

  describe "GET /projects:id/repository/commits/:sha/diff" do
    context "authorized user" do
      before { project.team << [user2, :reporter] }

      it "should return the diff of the selected commit" do
        get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff", user)
114
        expect(response).to have_http_status(200)
115

116 117 118
        expect(json_response).to be_an Array
        expect(json_response.length).to be >= 1
        expect(json_response.first.keys).to include "diff"
119 120 121 122
      end

      it "should return a 404 error if invalid commit" do
        get api("/projects/#{project.id}/repository/commits/invalid_sha/diff", user)
123
        expect(response).to have_http_status(404)
124 125 126 127 128 129
      end
    end

    context "unauthorized user" do
      it "should not return the diff of the selected commit" do
        get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff")
130
        expect(response).to have_http_status(401)
131 132 133
      end
    end
  end
134 135 136 137 138

  describe 'GET /projects:id/repository/commits/:sha/comments' do
    context 'authorized user' do
      it 'should return merge_request comments' do
        get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user)
139
        expect(response).to have_http_status(200)
140
        expect(json_response).to be_an Array
141
        expect(json_response.length).to eq(2)
142 143
        expect(json_response.first['note']).to eq('a comment on a commit')
        expect(json_response.first['author']['id']).to eq(user.id)
144 145 146 147
      end

      it 'should return a 404 error if merge_request_id not found' do
        get api("/projects/#{project.id}/repository/commits/1234ab/comments", user)
148
        expect(response).to have_http_status(404)
149 150 151 152 153 154
      end
    end

    context 'unauthorized user' do
      it 'should not return the diff of the selected commit' do
        get api("/projects/#{project.id}/repository/commits/1234ab/comments")
155
        expect(response).to have_http_status(401)
156 157 158 159 160 161 162 163
      end
    end
  end

  describe 'POST /projects:id/repository/commits/:sha/comments' do
    context 'authorized user' do
      it 'should return comment' do
        post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user), note: 'My comment'
164
        expect(response).to have_http_status(201)
165 166 167 168
        expect(json_response['note']).to eq('My comment')
        expect(json_response['path']).to be_nil
        expect(json_response['line']).to be_nil
        expect(json_response['line_type']).to be_nil
169 170 171 172
      end

      it 'should return the inline comment' do
        post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user), note: 'My comment', path: project.repository.commit.diffs.first.new_path, line: 7, line_type: 'new'
173
        expect(response).to have_http_status(201)
174 175 176 177
        expect(json_response['note']).to eq('My comment')
        expect(json_response['path']).to eq(project.repository.commit.diffs.first.new_path)
        expect(json_response['line']).to eq(7)
        expect(json_response['line_type']).to eq('new')
178 179 180 181
      end

      it 'should return 400 if note is missing' do
        post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user)
182
        expect(response).to have_http_status(400)
183 184 185 186
      end

      it 'should return 404 if note is attached to non existent commit' do
        post api("/projects/#{project.id}/repository/commits/1234ab/comments", user), note: 'My comment'
187
        expect(response).to have_http_status(404)
188 189 190 191 192 193
      end
    end

    context 'unauthorized user' do
      it 'should not return the diff of the selected commit' do
        post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments")
194
        expect(response).to have_http_status(401)
195 196 197
      end
    end
  end
198
end