BigW Consortium Gitlab

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

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, user: user, project: project, access_level: ProjectMember::MASTER) }
  let!(:guest) { create(:project_member, user: user2, project: project, access_level: ProjectMember::GUEST) }
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.status).to eq(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.status).to eq(401)
33 34 35 36 37 38 39 40
      end
    end
  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)
41 42 43
        expect(response.status).to eq(200)
        expect(json_response['id']).to eq(project.repository.commit.id)
        expect(json_response['title']).to eq(project.repository.commit.title)
44 45 46 47
      end

      it "should return a 404 error if not found" do
        get api("/projects/#{project.id}/repository/commits/invalid_sha", user)
48
        expect(response.status).to eq(404)
49
      end
50 51 52 53 54 55 56 57 58 59 60 61 62

      it "should return not_found for CI status" do
        get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
        expect(response.status).to eq(200)
        expect(json_response['status']).to eq('not_found')
      end

      it "should return status for CI" do
        ci_commit = project.ensure_ci_commit(project.repository.commit.sha)
        get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
        expect(response.status).to eq(200)
        expect(json_response['status']).to eq(ci_commit.status)
      end
63 64 65 66 67
    end

    context "unauthorized user" do
      it "should not return the selected commit" do
        get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}")
68
        expect(response.status).to eq(401)
69 70 71 72 73 74 75 76 77 78
      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)
79
        expect(response.status).to eq(200)
80

81 82 83
        expect(json_response).to be_an Array
        expect(json_response.length).to be >= 1
        expect(json_response.first.keys).to include "diff"
84 85 86 87
      end

      it "should return a 404 error if invalid commit" do
        get api("/projects/#{project.id}/repository/commits/invalid_sha/diff", user)
88
        expect(response.status).to eq(404)
89 90 91 92 93 94
      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")
95
        expect(response.status).to eq(401)
96 97 98
      end
    end
  end
99 100 101 102 103

  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)
104 105
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
106
        expect(json_response.length).to eq(2)
107 108
        expect(json_response.first['note']).to eq('a comment on a commit')
        expect(json_response.first['author']['id']).to eq(user.id)
109 110 111 112
      end

      it 'should return a 404 error if merge_request_id not found' do
        get api("/projects/#{project.id}/repository/commits/1234ab/comments", user)
113
        expect(response.status).to eq(404)
114 115 116 117 118 119
      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")
120
        expect(response.status).to eq(401)
121 122 123 124 125 126 127 128
      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'
129 130 131 132 133
        expect(response.status).to eq(201)
        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
134 135 136 137
      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'
138 139 140 141 142
        expect(response.status).to eq(201)
        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')
143 144 145 146
      end

      it 'should return 400 if note is missing' do
        post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user)
147
        expect(response.status).to eq(400)
148 149 150 151
      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'
152
        expect(response.status).to eq(404)
153 154 155 156 157 158
      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")
159
        expect(response.status).to eq(401)
160 161 162
      end
    end
  end
163
end