BigW Consortium Gitlab

projects_spec.rb 9.97 KB
Newer Older
Nihad Abbasov committed
1 2 3
require 'spec_helper'

describe Gitlab::API do
4 5
  include ApiHelpers

6 7 8 9 10 11 12 13
  let(:user) { create(:user) }
  let(:user2) { create(:user) }
  let(:user3) { create(:user) }
  let!(:hook) { create(:project_hook, project: project, url: "http://example.com") }
  let!(:project) { create(:project, owner: user ) }
  let!(:snippet) { create(:snippet, author: user, project: project, title: 'example') }
  let!(:users_project) { create(:users_project, user: user, project: project, project_access: UsersProject::MASTER) }
  let!(:users_project2) { create(:users_project, user: user3, project: project, project_access: UsersProject::DEVELOPER) }
14
  before { project.add_access(user, :read) }
Nihad Abbasov committed
15 16

  describe "GET /projects" do
17 18 19 20 21
    context "when unauthenticated" do
      it "should return authentication error" do
        get api("/projects")
        response.status.should == 401
      end
Nihad Abbasov committed
22 23
    end

24
    context "when authenticated" do
Nihad Abbasov committed
25
      it "should return an array of projects" do
Robert Speicher committed
26
        get api("/projects", user)
Nihad Abbasov committed
27
        response.status.should == 200
Nihad Abbasov committed
28 29 30
        json_response.should be_an Array
        json_response.first['name'].should == project.name
        json_response.first['owner']['email'].should == user.email
Nihad Abbasov committed
31 32 33 34
      end
    end
  end

35
  describe "POST /projects" do
36
    it "should create new project without path" do
Alex Denisov committed
37
      expect { post api("/projects", user), name: 'foo' }.to change {Project.count}.by(1)
38
    end
Alex Denisov committed
39 40 41 42 43 44 45

    it "should not create new project without name" do
      expect { post api("/projects", user) }.to_not change {Project.count}
    end

    it "should respond with 201 on success" do
      post api("/projects", user), name: 'foo'
46
      response.status.should == 201
47
    end
Alex Denisov committed
48

Nihad Abbasov committed
49
    it "should respond with 404 on failure" do
Alex Denisov committed
50 51
      post api("/projects", user)
      response.status.should == 404
52
    end
Alex Denisov committed
53 54

    it "should assign attributes to project" do
55
      project = attributes_for(:project, {
Alex Denisov committed
56 57 58 59 60 61 62 63 64 65 66 67 68
        description: Faker::Lorem.sentence,
        default_branch: 'stable',
        issues_enabled: false,
        wall_enabled: false,
        merge_requests_enabled: false,
        wiki_enabled: false
      })

      post api("/projects", user), project

      project.each_pair do |k,v|
        json_response[k.to_s].should == v
      end
69 70 71
    end
  end

Nihad Abbasov committed
72 73
  describe "GET /projects/:id" do
    it "should return a project by id" do
Robert Speicher committed
74
      get api("/projects/#{project.id}", user)
Nihad Abbasov committed
75
      response.status.should == 200
Nihad Abbasov committed
76 77
      json_response['name'].should == project.name
      json_response['owner']['email'].should == user.email
Nihad Abbasov committed
78
    end
79

80 81
    it "should return a project by path name" do
      get api("/projects/#{project.path}", user)
82 83 84
      response.status.should == 200
      json_response['name'].should == project.name
    end
85 86

    it "should return a 404 error if not found" do
Robert Speicher committed
87
      get api("/projects/42", user)
88
      response.status.should == 404
89
      json_response['message'].should == '404 Not Found'
90
    end
Nihad Abbasov committed
91 92 93 94
  end

  describe "GET /projects/:id/repository/branches" do
    it "should return an array of project branches" do
95
      get api("/projects/#{project.path}/repository/branches", user)
Nihad Abbasov committed
96
      response.status.should == 200
Nihad Abbasov committed
97 98
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name
Nihad Abbasov committed
99 100 101
    end
  end

102 103
  describe "GET /projects/:id/repository/branches/:branch" do
    it "should return the branch information for a single branch" do
104
      get api("/projects/#{project.path}/repository/branches/new_design", user)
105 106 107 108 109 110 111
      response.status.should == 200

      json_response['name'].should == 'new_design'
      json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
    end
  end

112 113
  describe "GET /projects/:id/members" do
    it "should return project team members" do
114
      get api("/projects/#{project.path}/members", user)
miks committed
115 116
      response.status.should == 200
      json_response.should be_an Array
miks committed
117
      json_response.count.should == 2
118
      json_response.first['email'].should == user.email
miks committed
119 120 121
    end
  end

122 123
  describe "GET /projects/:id/members/:user_id" do
    it "should return project team member" do
124
      get api("/projects/#{project.path}/members/#{user.id}", user)
125 126 127
      response.status.should == 200
      json_response['email'].should == user.email
      json_response['access_level'].should == UsersProject::MASTER
128 129 130
    end
  end

131 132
  describe "POST /projects/:id/members" do
    it "should add user to project team" do
133
      expect {
134
        post api("/projects/#{project.path}/members", user), user_id: user2.id,
135 136 137 138 139 140 141 142 143 144 145
          access_level: UsersProject::DEVELOPER
      }.to change { UsersProject.count }.by(1)

      response.status.should == 201
      json_response['email'].should == user2.email
      json_response['access_level'].should == UsersProject::DEVELOPER
    end
  end

  describe "PUT /projects/:id/members/:user_id" do
    it "should update project team member" do
146
      put api("/projects/#{project.path}/members/#{user3.id}", user), access_level: UsersProject::MASTER
147 148 149
      response.status.should == 200
      json_response['email'].should == user3.email
      json_response['access_level'].should == UsersProject::MASTER
150 151 152
    end
  end

153 154
  describe "DELETE /projects/:id/members/:user_id" do
    it "should remove user from project team" do
155
      expect {
156
        delete api("/projects/#{project.path}/members/#{user3.id}", user)
157
      }.to change { UsersProject.count }.by(-1)
158 159 160
    end
  end

miks committed
161 162
  describe "GET /projects/:id/hooks" do
    it "should return project hooks" do
163
      get api("/projects/#{project.path}/hooks", user)
miks committed
164 165 166 167 168 169 170 171 172

      response.status.should == 200

      json_response.should be_an Array
      json_response.count.should == 1
      json_response.first['url'].should == "http://example.com"
    end
  end

173 174
  describe "GET /projects/:id/hooks/:hook_id" do
    it "should return a project hook" do
175
      get api("/projects/#{project.path}/hooks/#{hook.id}", user)
176 177 178 179 180 181
      response.status.should == 200
      json_response['url'].should == hook.url
    end
  end

  describe "POST /projects/:id/hooks" do
miks committed
182 183
    it "should add hook to project" do
      expect {
184
        post api("/projects/#{project.path}/hooks", user),
miks committed
185 186 187 188
          "url" => "http://example.com"
      }.to change {project.hooks.count}.by(1)
    end
  end
Nihad Abbasov committed
189

190 191
  describe "PUT /projects/:id/hooks/:hook_id" do
    it "should update an existing project hook" do
192
      put api("/projects/#{project.path}/hooks/#{hook.id}", user),
Nihad Abbasov committed
193
        url: 'http://example.org'
194
      response.status.should == 200
Nihad Abbasov committed
195
      json_response['url'].should == 'http://example.org'
196 197
    end
  end
Nihad Abbasov committed
198

miks committed
199 200 201 202

  describe "DELETE /projects/:id/hooks" do
    it "should delete hook from project" do
      expect {
203
        delete api("/projects/#{project.path}/hooks", user),
miks committed
204 205 206 207 208
          hook_id: hook.id
      }.to change {project.hooks.count}.by(-1)
    end
  end

Nihad Abbasov committed
209 210
  describe "GET /projects/:id/repository/tags" do
    it "should return an array of project tags" do
211
      get api("/projects/#{project.path}/repository/tags", user)
Nihad Abbasov committed
212
      response.status.should == 200
Nihad Abbasov committed
213 214
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name
Nihad Abbasov committed
215 216
    end
  end
Nihad Abbasov committed
217

218 219 220 221 222
  describe "GET /projects/:id/repository/commits" do
    context "authorized user" do
      before { project.add_access(user2, :read) }

      it "should return project commits" do
223
        get api("/projects/#{project.path}/repository/commits", user)
224 225 226 227 228 229 230 231 232
        response.status.should == 200

        json_response.should be_an Array
        json_response.first['id'].should == project.commit.id
      end
    end

    context "unauthorized user" do
      it "should not return project commits" do
233
        get api("/projects/#{project.path}/repository/commits")
234 235 236 237 238
        response.status.should == 401
      end
    end
  end

239
  describe "GET /projects/:id/snippets" do
Nihad Abbasov committed
240
    it "should return an array of project snippets" do
241
      get api("/projects/#{project.path}/snippets", user)
242 243 244 245 246 247
      response.status.should == 200
      json_response.should be_an Array
      json_response.first['title'].should == snippet.title
    end
  end

Nihad Abbasov committed
248 249
  describe "GET /projects/:id/snippets/:snippet_id" do
    it "should return a project snippet" do
250
      get api("/projects/#{project.path}/snippets/#{snippet.id}", user)
Nihad Abbasov committed
251
      response.status.should == 200
Nihad Abbasov committed
252
      json_response['title'].should == snippet.title
Nihad Abbasov committed
253 254 255 256 257
    end
  end

  describe "POST /projects/:id/snippets" do
    it "should create a new project snippet" do
258
      post api("/projects/#{project.path}/snippets", user),
259
        title: 'api test', file_name: 'sample.rb', code: 'test'
Nihad Abbasov committed
260
      response.status.should == 201
Nihad Abbasov committed
261
      json_response['title'].should == 'api test'
Nihad Abbasov committed
262 263 264
    end
  end

265
  describe "PUT /projects/:id/snippets/:shippet_id" do
266
    it "should update an existing project snippet" do
267
      put api("/projects/#{project.path}/snippets/#{snippet.id}", user),
268
        code: 'updated code'
269 270
      response.status.should == 200
      json_response['title'].should == 'example'
271
      snippet.reload.content.should == 'updated code'
272 273 274
    end
  end

Nihad Abbasov committed
275
  describe "DELETE /projects/:id/snippets/:snippet_id" do
m16a1 committed
276
    it "should delete existing project snippet" do
Nihad Abbasov committed
277
      expect {
278
        delete api("/projects/#{project.path}/snippets/#{snippet.id}", user)
279
      }.to change { Snippet.count }.by(-1)
Nihad Abbasov committed
280 281
    end
  end
282 283 284

  describe "GET /projects/:id/snippets/:snippet_id/raw" do
    it "should get a raw project snippet" do
285
      get api("/projects/#{project.path}/snippets/#{snippet.id}/raw", user)
286 287 288
      response.status.should == 200
    end
  end
289 290 291

  describe "GET /projects/:id/:sha/blob" do
    it "should get the raw file contents" do
292
      get api("/projects/#{project.path}/repository/commits/master/blob?filepath=README.md", user)
293 294 295 296
      response.status.should == 200
    end

    it "should return 404 for invalid branch_name" do
297
      get api("/projects/#{project.path}/repository/commits/invalid_branch_name/blob?filepath=README.md", user)
298 299 300 301
      response.status.should == 404
    end

    it "should return 404 for invalid file" do
302
      get api("/projects/#{project.path}/repository/commits/master/blob?filepath=README.invalid", user)
303 304 305
      response.status.should == 404
    end
  end
Nihad Abbasov committed
306
end