projects_spec.rb 14.3 KB
Newer Older
N
Nihad Abbasov 已提交
1 2 3
require 'spec_helper'

describe Gitlab::API do
4 5
  include ApiHelpers

6 7 8 9
  let(:user) { create(:user) }
  let(:user2) { create(:user) }
  let(:user3) { create(:user) }
  let!(:hook) { create(:project_hook, project: project, url: "http://example.com") }
10
  let!(:project) { create(:project, namespace: user.namespace ) }
11 12 13
  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) }
D
Dmitriy Zaporozhets 已提交
14
  before { project.team << [user, :reporter] }
N
Nihad Abbasov 已提交
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
N
Nihad Abbasov 已提交
22 23
    end

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

35
  describe "POST /projects" do
36
    it "should create new project without path" do
A
Alex Denisov 已提交
37
      expect { post api("/projects", user), name: 'foo' }.to change {Project.count}.by(1)
38
    end
A
Alex Denisov 已提交
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
A
Alex Denisov 已提交
48

49
    it "should respond with 400 if name is not given" do
A
Alex Denisov 已提交
50
      post api("/projects", user)
51
      response.status.should == 400
52
    end
A
Alex Denisov 已提交
53 54

    it "should assign attributes to project" do
55
      project = attributes_for(:project, {
A
Alex Denisov 已提交
56 57 58 59 60 61 62 63 64 65 66
        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|
67
        next if k == :path
A
Alex Denisov 已提交
68 69
        json_response[k.to_s].should == v
      end
70 71 72
    end
  end

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

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

    it "should return a 404 error if not found" do
R
Robert Speicher 已提交
88
      get api("/projects/42", user)
N
Nihad Abbasov 已提交
89
      response.status.should == 404
A
Alex Denisov 已提交
90
      json_response['message'].should == '404 Not Found'
N
Nihad Abbasov 已提交
91
    end
92 93 94 95 96 97

    it "should return a 404 error if user is not a member" do
      other_user = create(:user)
      get api("/projects/#{project.id}", other_user)
      response.status.should == 404
    end
N
Nihad Abbasov 已提交
98 99 100 101
  end

  describe "GET /projects/:id/repository/branches" do
    it "should return an array of project branches" do
102
      get api("/projects/#{project.id}/repository/branches", user)
N
Nihad Abbasov 已提交
103
      response.status.should == 200
N
Nihad Abbasov 已提交
104 105
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name
N
Nihad Abbasov 已提交
106 107 108
    end
  end

109 110
  describe "GET /projects/:id/repository/branches/:branch" do
    it "should return the branch information for a single branch" do
111
      get api("/projects/#{project.id}/repository/branches/new_design", user)
112 113 114 115
      response.status.should == 200

      json_response['name'].should == 'new_design'
      json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      json_response['protected'].should == false
    end
  end

  describe "PUT /projects/:id/repository/branches/:branch/protect" do
    it "should protect a single branch" do
      put api("/projects/#{project.id}/repository/branches/new_design/protect", user)
      response.status.should == 200

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

  describe "PUT /projects/:id/repository/branches/:branch/unprotect" do
    it "should unprotect a single branch" do
      put api("/projects/#{project.id}/repository/branches/new_design/unprotect", user)
      response.status.should == 200

      json_response['name'].should == 'new_design'
      json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
      json_response['protected'].should == false
139 140 141
    end
  end

N
Nihad Abbasov 已提交
142 143
  describe "GET /projects/:id/members" do
    it "should return project team members" do
144
      get api("/projects/#{project.id}/members", user)
M
miks 已提交
145 146
      response.status.should == 200
      json_response.should be_an Array
M
miks 已提交
147
      json_response.count.should == 2
N
Nihad Abbasov 已提交
148
      json_response.first['email'].should == user.email
M
miks 已提交
149
    end
V
Valeriy Sizov 已提交
150 151

    it "finds team members with query string" do
152
      get api("/projects/#{project.id}/members", user), query: user.username
V
Valeriy Sizov 已提交
153 154 155 156 157
      response.status.should == 200
      json_response.should be_an Array
      json_response.count.should == 1
      json_response.first['email'].should == user.email
    end
M
miks 已提交
158 159
  end

N
Nihad Abbasov 已提交
160 161
  describe "GET /projects/:id/members/:user_id" do
    it "should return project team member" do
162
      get api("/projects/#{project.id}/members/#{user.id}", user)
N
Nihad Abbasov 已提交
163 164 165
      response.status.should == 200
      json_response['email'].should == user.email
      json_response['access_level'].should == UsersProject::MASTER
M
miks 已提交
166 167 168
    end
  end

N
Nihad Abbasov 已提交
169 170
  describe "POST /projects/:id/members" do
    it "should add user to project team" do
M
miks 已提交
171
      expect {
172
        post api("/projects/#{project.id}/members", user), user_id: user2.id,
N
Nihad Abbasov 已提交
173 174 175 176 177 178 179
          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
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

    it "should return a 201 status if user is already project member" do
      post api("/projects/#{project.id}/members", user), user_id: user2.id,
        access_level: UsersProject::DEVELOPER
      expect {
        post api("/projects/#{project.id}/members", user), user_id: user2.id,
          access_level: UsersProject::DEVELOPER
      }.not_to change { UsersProject.count }.by(1)

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

    it "should return a 400 error when user id is not given" do
      post api("/projects/#{project.id}/members", user), access_level: UsersProject::MASTER
      response.status.should == 400
    end

    it "should return a 400 error when access level is not given" do
      post api("/projects/#{project.id}/members", user), user_id: user2.id
      response.status.should == 400
    end

    it "should return a 422 error when access level is not known" do
      post api("/projects/#{project.id}/members", user), user_id: user2.id, access_level: 1234
      response.status.should == 422
    end
N
Nihad Abbasov 已提交
208 209 210 211
  end

  describe "PUT /projects/:id/members/:user_id" do
    it "should update project team member" do
212
      put api("/projects/#{project.id}/members/#{user3.id}", user), access_level: UsersProject::MASTER
N
Nihad Abbasov 已提交
213 214 215
      response.status.should == 200
      json_response['email'].should == user3.email
      json_response['access_level'].should == UsersProject::MASTER
M
miks 已提交
216
    end
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

    it "should return a 404 error if user_id is not found" do
      put api("/projects/#{project.id}/members/1234", user), access_level: UsersProject::MASTER
      response.status.should == 404
    end

    it "should return a 400 error when access level is not given" do
      put api("/projects/#{project.id}/members/#{user3.id}", user)
      response.status.should == 400
    end

    it "should return a 422 error when access level is not known" do
      put api("/projects/#{project.id}/members/#{user3.id}", user), access_level: 123
      response.status.should == 422
    end
M
miks 已提交
232 233
  end

N
Nihad Abbasov 已提交
234 235
  describe "DELETE /projects/:id/members/:user_id" do
    it "should remove user from project team" do
M
miks 已提交
236
      expect {
237
        delete api("/projects/#{project.id}/members/#{user3.id}", user)
N
Nihad Abbasov 已提交
238
      }.to change { UsersProject.count }.by(-1)
M
miks 已提交
239
    end
240 241 242 243 244 245 246

    it "should return 200 if team member is not part of a project" do
      delete api("/projects/#{project.id}/members/#{user3.id}", user)
      expect {
        delete api("/projects/#{project.id}/members/#{user3.id}", user)
      }.to_not change { UsersProject.count }.by(1)
    end
M
miks 已提交
247 248
  end

249 250 251 252 253 254 255 256 257 258 259
  describe "DELETE /projects/:id/members/:user_id" do
    it "should return 200 OK when the user was not member" do
      expect {
        delete api("/projects/#{project.id}/members/1000000", user)
      }.to change { UsersProject.count }.by(0)
      response.status.should == 200
      json_response['message'].should == "Access revoked"
      json_response['id'].should == 1000000
    end
  end

M
miks 已提交
260 261
  describe "GET /projects/:id/hooks" do
    it "should return project hooks" do
262
      get api("/projects/#{project.id}/hooks", user)
M
miks 已提交
263 264 265 266 267 268 269 270 271

      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

272 273
  describe "GET /projects/:id/hooks/:hook_id" do
    it "should return a project hook" do
274
      get api("/projects/#{project.id}/hooks/#{hook.id}", user)
275 276 277
      response.status.should == 200
      json_response['url'].should == hook.url
    end
278 279 280 281 282

    it "should return a 404 error if hook id is not available" do
      get api("/projects/#{project.id}/hooks/1234", user)
      response.status.should == 404
    end
283 284 285
  end

  describe "POST /projects/:id/hooks" do
M
miks 已提交
286 287
    it "should add hook to project" do
      expect {
288
        post api("/projects/#{project.id}/hooks", user),
M
miks 已提交
289 290
          "url" => "http://example.com"
      }.to change {project.hooks.count}.by(1)
291
      response.status.should == 201
M
miks 已提交
292 293
    end
  end
N
Nihad Abbasov 已提交
294

295 296
  describe "PUT /projects/:id/hooks/:hook_id" do
    it "should update an existing project hook" do
297
      put api("/projects/#{project.id}/hooks/#{hook.id}", user),
N
Nihad Abbasov 已提交
298
        url: 'http://example.org'
299
      response.status.should == 200
N
Nihad Abbasov 已提交
300
      json_response['url'].should == 'http://example.org'
301
    end
N
Nihad Abbasov 已提交
302

303 304 305 306 307 308 309 310 311 312
    it "should return 404 error if hook id is not found" do
      put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org'
      response.status.should == 404
    end

    it "should return 400 error if url is not given" do
      put api("/projects/#{project.id}/hooks/#{hook.id}", user)
      response.status.should == 400
    end
  end
M
miks 已提交
313 314 315 316

  describe "DELETE /projects/:id/hooks" do
    it "should delete hook from project" do
      expect {
317
        delete api("/projects/#{project.id}/hooks", user),
M
miks 已提交
318 319
          hook_id: hook.id
      }.to change {project.hooks.count}.by(-1)
320
      response.status.should == 200
M
miks 已提交
321 322 323
    end
  end

N
Nihad Abbasov 已提交
324 325
  describe "GET /projects/:id/repository/tags" do
    it "should return an array of project tags" do
326
      get api("/projects/#{project.id}/repository/tags", user)
N
Nihad Abbasov 已提交
327
      response.status.should == 200
N
Nihad Abbasov 已提交
328 329
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name
N
Nihad Abbasov 已提交
330 331
    end
  end
N
Nihad Abbasov 已提交
332

333 334
  describe "GET /projects/:id/repository/commits" do
    context "authorized user" do
D
Dmitriy Zaporozhets 已提交
335
      before { project.team << [user2, :reporter] }
336 337

      it "should return project commits" do
338
        get api("/projects/#{project.id}/repository/commits", user)
339 340 341
        response.status.should == 200

        json_response.should be_an Array
D
Dmitriy Zaporozhets 已提交
342
        json_response.first['id'].should == project.repository.commit.id
343 344 345 346 347
      end
    end

    context "unauthorized user" do
      it "should not return project commits" do
348
        get api("/projects/#{project.id}/repository/commits")
349 350 351 352 353
        response.status.should == 401
      end
    end
  end

N
Nihad Abbasov 已提交
354
  describe "GET /projects/:id/snippets" do
N
Nihad Abbasov 已提交
355
    it "should return an array of project snippets" do
356
      get api("/projects/#{project.id}/snippets", user)
N
Nihad Abbasov 已提交
357 358 359 360 361 362
      response.status.should == 200
      json_response.should be_an Array
      json_response.first['title'].should == snippet.title
    end
  end

N
Nihad Abbasov 已提交
363 364
  describe "GET /projects/:id/snippets/:snippet_id" do
    it "should return a project snippet" do
365
      get api("/projects/#{project.id}/snippets/#{snippet.id}", user)
N
Nihad Abbasov 已提交
366
      response.status.should == 200
N
Nihad Abbasov 已提交
367
      json_response['title'].should == snippet.title
N
Nihad Abbasov 已提交
368 369 370 371 372
    end
  end

  describe "POST /projects/:id/snippets" do
    it "should create a new project snippet" do
373
      post api("/projects/#{project.id}/snippets", user),
374
        title: 'api test', file_name: 'sample.rb', code: 'test'
N
Nihad Abbasov 已提交
375
      response.status.should == 201
N
Nihad Abbasov 已提交
376
      json_response['title'].should == 'api test'
N
Nihad Abbasov 已提交
377 378 379
    end
  end

380
  describe "PUT /projects/:id/snippets/:shippet_id" do
381
    it "should update an existing project snippet" do
382
      put api("/projects/#{project.id}/snippets/#{snippet.id}", user),
383
        code: 'updated code'
384 385
      response.status.should == 200
      json_response['title'].should == 'example'
386
      snippet.reload.content.should == 'updated code'
387 388 389
    end
  end

N
Nihad Abbasov 已提交
390
  describe "DELETE /projects/:id/snippets/:snippet_id" do
M
m16a1 已提交
391
    it "should delete existing project snippet" do
N
Nihad Abbasov 已提交
392
      expect {
393
        delete api("/projects/#{project.id}/snippets/#{snippet.id}", user)
394
      }.to change { Snippet.count }.by(-1)
N
Nihad Abbasov 已提交
395 396
    end
  end
397 398 399

  describe "GET /projects/:id/snippets/:snippet_id/raw" do
    it "should get a raw project snippet" do
400
      get api("/projects/#{project.id}/snippets/#{snippet.id}/raw", user)
401 402 403
      response.status.should == 200
    end
  end
404 405 406

  describe "GET /projects/:id/:sha/blob" do
    it "should get the raw file contents" do
407
      get api("/projects/#{project.id}/repository/commits/master/blob?filepath=README.md", user)
408 409 410 411
      response.status.should == 200
    end

    it "should return 404 for invalid branch_name" do
412
      get api("/projects/#{project.id}/repository/commits/invalid_branch_name/blob?filepath=README.md", user)
413 414 415 416
      response.status.should == 404
    end

    it "should return 404 for invalid file" do
417
      get api("/projects/#{project.id}/repository/commits/master/blob?filepath=README.invalid", user)
418 419 420
      response.status.should == 404
    end
  end
N
Nihad Abbasov 已提交
421
end