BigW Consortium Gitlab

repositories_spec.rb 6.55 KB
Newer Older
1
require 'spec_helper'
2
require 'mime/types'
3

4
describe API::API, api: true  do
5
  include ApiHelpers
6
  include RepoHelpers
Jacob Vosmaer committed
7
  include WorkhorseHelpers
8

9
  let(:user) { create(:user) }
10
  let(:user2) { create(:user) }
11
  let!(:project) { create(:project, creator_id: user.id) }
12 13
  let!(:master) { create(:project_member, :master, user: user, project: project) }
  let!(:guest) { create(:project_member, :guest, user: user2, project: project) }
14

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

19
      it "returns project commits" do
20
        get api("/projects/#{project.id}/repository/tree", user)
21
        expect(response).to have_http_status(200)
22

23
        expect(json_response).to be_an Array
24
        expect(json_response.first['name']).to eq('bar')
25 26
        expect(json_response.first['type']).to eq('tree')
        expect(json_response.first['mode']).to eq('040000')
27
      end
28

29
      it 'returns a 404 for unknown ref' do
30
        get api("/projects/#{project.id}/repository/tree?ref_name=foo", user)
31
        expect(response).to have_http_status(404)
32

33
        expect(json_response).to be_an Object
34 35
        json_response['message'] == '404 Tree Not Found'
      end
36 37 38
    end

    context "unauthorized user" do
39
      it "does not return project commits" do
40
        get api("/projects/#{project.id}/repository/tree")
41
        expect(response).to have_http_status(401)
42 43 44 45
      end
    end
  end

46
  describe "GET /projects/:id/repository/blobs/:sha" do
47
    it "gets the raw file contents" do
48
      get api("/projects/#{project.id}/repository/blobs/master?filepath=README.md", user)
49
      expect(response).to have_http_status(200)
50 51
    end

52
    it "returns 404 for invalid branch_name" do
53
      get api("/projects/#{project.id}/repository/blobs/invalid_branch_name?filepath=README.md", user)
54
      expect(response).to have_http_status(404)
55 56
    end

57
    it "returns 404 for invalid file" do
58
      get api("/projects/#{project.id}/repository/blobs/master?filepath=README.invalid", user)
59
      expect(response).to have_http_status(404)
60 61
    end

62
    it "returns a 400 error if filepath is missing" do
63
      get api("/projects/#{project.id}/repository/blobs/master", user)
64
      expect(response).to have_http_status(400)
65 66
    end
  end
67 68

  describe "GET /projects/:id/repository/commits/:sha/blob" do
69
    it "gets the raw file contents" do
70
      get api("/projects/#{project.id}/repository/commits/master/blob?filepath=README.md", user)
71
      expect(response).to have_http_status(200)
72 73 74
    end
  end

75
  describe "GET /projects/:id/repository/raw_blobs/:sha" do
76
    it "gets the raw file contents" do
77
      get api("/projects/#{project.id}/repository/raw_blobs/#{sample_blob.oid}", user)
78
      expect(response).to have_http_status(200)
79
    end
80

81
    it 'returns a 404 for unknown blob' do
82
      get api("/projects/#{project.id}/repository/raw_blobs/123456", user)
83
      expect(response).to have_http_status(404)
84

85
      expect(json_response).to be_an Object
86 87
      json_response['message'] == '404 Blob Not Found'
    end
88 89
  end

90
  describe "GET /projects/:id/repository/archive(.:format)?:sha" do
91
    it "gets the archive" do
92
      get api("/projects/#{project.id}/repository/archive", user)
93
      repo_name = project.repository.name.gsub("\.git", "")
94
      expect(response).to have_http_status(200)
Jacob Vosmaer committed
95 96 97
      type, params = workhorse_send_data
      expect(type).to eq('git-archive')
      expect(params['ArchivePath']).to match(/#{repo_name}\-[^\.]+\.tar.gz/)
98 99
    end

100
    it "gets the archive.zip" do
101 102
      get api("/projects/#{project.id}/repository/archive.zip", user)
      repo_name = project.repository.name.gsub("\.git", "")
103
      expect(response).to have_http_status(200)
Jacob Vosmaer committed
104 105 106
      type, params = workhorse_send_data
      expect(type).to eq('git-archive')
      expect(params['ArchivePath']).to match(/#{repo_name}\-[^\.]+\.zip/)
107 108
    end

109
    it "gets the archive.tar.bz2" do
110 111
      get api("/projects/#{project.id}/repository/archive.tar.bz2", user)
      repo_name = project.repository.name.gsub("\.git", "")
112
      expect(response).to have_http_status(200)
Jacob Vosmaer committed
113 114 115
      type, params = workhorse_send_data
      expect(type).to eq('git-archive')
      expect(params['ArchivePath']).to match(/#{repo_name}\-[^\.]+\.tar.bz2/)
116 117
    end

118
    it "returns 404 for invalid sha" do
119
      get api("/projects/#{project.id}/repository/archive/?sha=xxx", user)
120
      expect(response).to have_http_status(404)
121 122
    end
  end
123

124
  describe 'GET /projects/:id/repository/compare' do
125
    it "compares branches" do
126
      get api("/projects/#{project.id}/repository/compare", user), from: 'master', to: 'feature'
127
      expect(response).to have_http_status(200)
128 129
      expect(json_response['commits']).to be_present
      expect(json_response['diffs']).to be_present
130 131
    end

132
    it "compares tags" do
133
      get api("/projects/#{project.id}/repository/compare", user), from: 'v1.0.0', to: 'v1.1.0'
134
      expect(response).to have_http_status(200)
135 136
      expect(json_response['commits']).to be_present
      expect(json_response['diffs']).to be_present
137 138
    end

139
    it "compares commits" do
140
      get api("/projects/#{project.id}/repository/compare", user), from: sample_commit.id, to: sample_commit.parent_id
141
      expect(response).to have_http_status(200)
142 143 144
      expect(json_response['commits']).to be_empty
      expect(json_response['diffs']).to be_empty
      expect(json_response['compare_same_ref']).to be_falsey
145 146
    end

147
    it "compares commits in reverse order" do
148
      get api("/projects/#{project.id}/repository/compare", user), from: sample_commit.parent_id, to: sample_commit.id
149
      expect(response).to have_http_status(200)
150 151
      expect(json_response['commits']).to be_present
      expect(json_response['diffs']).to be_present
152 153
    end

154
    it "compares same refs" do
155
      get api("/projects/#{project.id}/repository/compare", user), from: 'master', to: 'master'
156
      expect(response).to have_http_status(200)
157 158 159
      expect(json_response['commits']).to be_empty
      expect(json_response['diffs']).to be_empty
      expect(json_response['compare_same_ref']).to be_truthy
160 161
    end
  end
162 163

  describe 'GET /projects/:id/repository/contributors' do
164
    it 'returns valid data' do
165
      get api("/projects/#{project.id}/repository/contributors", user)
166
      expect(response).to have_http_status(200)
167
      expect(json_response).to be_an Array
168
      contributor = json_response.first
169 170 171
      expect(contributor['email']).to eq('tiagonbotelho@hotmail.com')
      expect(contributor['name']).to eq('tiagonbotelho')
      expect(contributor['commits']).to eq(1)
172 173
      expect(contributor['additions']).to eq(0)
      expect(contributor['deletions']).to eq(0)
174 175
    end
  end
176
end