projects_spec.rb 12.7 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 37 38 39 40 41 42 43 44 45 46 47 48 49
    context "maximum number of projects reached" do
      before do
        (1..user2.projects_limit).each do |project|
          post api("/projects", user2), name: "foo#{project}"
        end
      end

      it "should not create new project" do
        expect {
          post api("/projects", user2), name: 'foo'
        }.to change {Project.count}.by(0)
      end
    end

50
    it "should create new project without path" do
A
Alex Denisov 已提交
51
      expect { post api("/projects", user), name: 'foo' }.to change {Project.count}.by(1)
52
    end
A
Alex Denisov 已提交
53 54 55 56 57

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

58 59 60 61 62 63
    it "should create last project before reaching project limit" do
      (1..user2.projects_limit-1).each { |p| post api("/projects", user2), name: "foo#{p}" }
      post api("/projects", user2), name: "foo"
      response.status.should == 201
    end

A
Alex Denisov 已提交
64 65
    it "should respond with 201 on success" do
      post api("/projects", user), name: 'foo'
66
      response.status.should == 201
67
    end
A
Alex Denisov 已提交
68

N
Nihad Abbasov 已提交
69
    it "should respond with 404 on failure" do
A
Alex Denisov 已提交
70 71
      post api("/projects", user)
      response.status.should == 404
72
    end
A
Alex Denisov 已提交
73 74

    it "should assign attributes to project" do
75
      project = attributes_for(:project, {
A
Alex Denisov 已提交
76 77 78 79 80 81 82 83 84 85 86
        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|
87
        next if k == :path
A
Alex Denisov 已提交
88 89
        json_response[k.to_s].should == v
      end
90 91 92
    end
  end

N
Nihad Abbasov 已提交
93 94
  describe "GET /projects/:id" do
    it "should return a project by id" do
R
Robert Speicher 已提交
95
      get api("/projects/#{project.id}", user)
N
Nihad Abbasov 已提交
96
      response.status.should == 200
N
Nihad Abbasov 已提交
97 98
      json_response['name'].should == project.name
      json_response['owner']['email'].should == user.email
N
Nihad Abbasov 已提交
99
    end
100

101
    it "should return a project by path name" do
102
      get api("/projects/#{project.id}", user)
103 104 105
      response.status.should == 200
      json_response['name'].should == project.name
    end
N
Nihad Abbasov 已提交
106 107

    it "should return a 404 error if not found" do
R
Robert Speicher 已提交
108
      get api("/projects/42", user)
N
Nihad Abbasov 已提交
109
      response.status.should == 404
A
Alex Denisov 已提交
110
      json_response['message'].should == '404 Not Found'
N
Nihad Abbasov 已提交
111
    end
N
Nihad Abbasov 已提交
112 113 114 115
  end

  describe "GET /projects/:id/repository/branches" do
    it "should return an array of project branches" do
116
      get api("/projects/#{project.id}/repository/branches", user)
N
Nihad Abbasov 已提交
117
      response.status.should == 200
N
Nihad Abbasov 已提交
118 119
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name
N
Nihad Abbasov 已提交
120 121 122
    end
  end

123 124
  describe "GET /projects/:id/repository/branches/:branch" do
    it "should return the branch information for a single branch" do
125
      get api("/projects/#{project.id}/repository/branches/new_design", user)
126 127 128 129
      response.status.should == 200

      json_response['name'].should == 'new_design'
      json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
130 131
      json_response['protected'].should == false
    end
132 133 134 135 136

    it "should return a 404 error if branch is not available" do
      get api("/projects/#{project.id}/repository/branches/unknown", user)
      response.status.should == 404
    end
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  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
158 159 160
    end
  end

N
Nihad Abbasov 已提交
161 162
  describe "GET /projects/:id/members" do
    it "should return project team members" do
163
      get api("/projects/#{project.id}/members", user)
M
miks 已提交
164 165
      response.status.should == 200
      json_response.should be_an Array
M
miks 已提交
166
      json_response.count.should == 2
N
Nihad Abbasov 已提交
167
      json_response.first['email'].should == user.email
M
miks 已提交
168
    end
V
Valeriy Sizov 已提交
169 170

    it "finds team members with query string" do
171
      get api("/projects/#{project.id}/members", user), query: user.username
V
Valeriy Sizov 已提交
172 173 174 175 176
      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 已提交
177 178
  end

N
Nihad Abbasov 已提交
179 180
  describe "GET /projects/:id/members/:user_id" do
    it "should return project team member" do
181
      get api("/projects/#{project.id}/members/#{user.id}", user)
N
Nihad Abbasov 已提交
182 183 184
      response.status.should == 200
      json_response['email'].should == user.email
      json_response['access_level'].should == UsersProject::MASTER
M
miks 已提交
185 186 187
    end
  end

N
Nihad Abbasov 已提交
188 189
  describe "POST /projects/:id/members" do
    it "should add user to project team" do
M
miks 已提交
190
      expect {
191
        post api("/projects/#{project.id}/members", user), user_id: user2.id,
N
Nihad Abbasov 已提交
192 193 194 195 196 197 198 199 200 201 202
          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
203
      put api("/projects/#{project.id}/members/#{user3.id}", user), access_level: UsersProject::MASTER
N
Nihad Abbasov 已提交
204 205 206
      response.status.should == 200
      json_response['email'].should == user3.email
      json_response['access_level'].should == UsersProject::MASTER
M
miks 已提交
207 208 209
    end
  end

N
Nihad Abbasov 已提交
210 211
  describe "DELETE /projects/:id/members/:user_id" do
    it "should remove user from project team" do
M
miks 已提交
212
      expect {
213
        delete api("/projects/#{project.id}/members/#{user3.id}", user)
N
Nihad Abbasov 已提交
214
      }.to change { UsersProject.count }.by(-1)
M
miks 已提交
215 216 217
    end
  end

M
miks 已提交
218
  describe "GET /projects/:id/hooks" do
219 220 221 222
    context "authorized user" do
      it "should return project hooks" do
        get api("/projects/#{project.id}/hooks", user)
        response.status.should == 200
M
miks 已提交
223

224 225 226 227 228
        json_response.should be_an Array
        json_response.count.should == 1
        json_response.first['url'].should == "http://example.com"
      end
    end
M
miks 已提交
229

230 231 232 233 234
    context "unauthorized user" do
      it "should not access project hooks" do
        get api("/projects/#{project.id}/hooks", user3)
        response.status.should == 403
      end
M
miks 已提交
235 236 237
    end
  end

238
  describe "GET /projects/:id/hooks/:hook_id" do
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    context "authorized user" do
      it "should return a project hook" do
        get api("/projects/#{project.id}/hooks/#{hook.id}", user)
        response.status.should == 200
        json_response['url'].should == hook.url
      end

      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
    end

    context "unauthorized user" do
      it "should not access an existing hook" do
        get api("/projects/#{project.id}/hooks/#{hook.id}", user3)
        response.status.should == 403
      end
257 258 259 260
    end
  end

  describe "POST /projects/:id/hooks" do
M
miks 已提交
261 262
    it "should add hook to project" do
      expect {
263
        post api("/projects/#{project.id}/hooks", user),
M
miks 已提交
264 265 266 267
          "url" => "http://example.com"
      }.to change {project.hooks.count}.by(1)
    end
  end
N
Nihad Abbasov 已提交
268

269 270
  describe "PUT /projects/:id/hooks/:hook_id" do
    it "should update an existing project hook" do
271
      put api("/projects/#{project.id}/hooks/#{hook.id}", user),
N
Nihad Abbasov 已提交
272
        url: 'http://example.org'
273
      response.status.should == 200
N
Nihad Abbasov 已提交
274
      json_response['url'].should == 'http://example.org'
275 276
    end
  end
N
Nihad Abbasov 已提交
277

278
  describe "DELETE /projects/:id/hooks/:hook_id" do
M
miks 已提交
279 280
    it "should delete hook from project" do
      expect {
281
        delete api("/projects/#{project.id}/hooks/#{hook.id}", user),
M
miks 已提交
282 283 284 285 286
          hook_id: hook.id
      }.to change {project.hooks.count}.by(-1)
    end
  end

N
Nihad Abbasov 已提交
287 288
  describe "GET /projects/:id/repository/tags" do
    it "should return an array of project tags" do
289
      get api("/projects/#{project.id}/repository/tags", user)
N
Nihad Abbasov 已提交
290
      response.status.should == 200
N
Nihad Abbasov 已提交
291 292
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name
N
Nihad Abbasov 已提交
293 294
    end
  end
N
Nihad Abbasov 已提交
295

296 297
  describe "GET /projects/:id/repository/commits" do
    context "authorized user" do
D
Dmitriy Zaporozhets 已提交
298
      before { project.team << [user2, :reporter] }
299 300

      it "should return project commits" do
301
        get api("/projects/#{project.id}/repository/commits", user)
302 303 304
        response.status.should == 200

        json_response.should be_an Array
D
Dmitriy Zaporozhets 已提交
305
        json_response.first['id'].should == project.repository.commit.id
306 307 308 309 310
      end
    end

    context "unauthorized user" do
      it "should not return project commits" do
311
        get api("/projects/#{project.id}/repository/commits")
312 313 314 315 316
        response.status.should == 401
      end
    end
  end

N
Nihad Abbasov 已提交
317
  describe "GET /projects/:id/snippets" do
N
Nihad Abbasov 已提交
318
    it "should return an array of project snippets" do
319
      get api("/projects/#{project.id}/snippets", user)
N
Nihad Abbasov 已提交
320 321 322 323 324 325
      response.status.should == 200
      json_response.should be_an Array
      json_response.first['title'].should == snippet.title
    end
  end

N
Nihad Abbasov 已提交
326 327
  describe "GET /projects/:id/snippets/:snippet_id" do
    it "should return a project snippet" do
328
      get api("/projects/#{project.id}/snippets/#{snippet.id}", user)
N
Nihad Abbasov 已提交
329
      response.status.should == 200
N
Nihad Abbasov 已提交
330
      json_response['title'].should == snippet.title
N
Nihad Abbasov 已提交
331 332 333 334 335
    end
  end

  describe "POST /projects/:id/snippets" do
    it "should create a new project snippet" do
336
      post api("/projects/#{project.id}/snippets", user),
337
        title: 'api test', file_name: 'sample.rb', code: 'test'
N
Nihad Abbasov 已提交
338
      response.status.should == 201
N
Nihad Abbasov 已提交
339
      json_response['title'].should == 'api test'
N
Nihad Abbasov 已提交
340 341 342
    end
  end

343
  describe "PUT /projects/:id/snippets/:shippet_id" do
344
    it "should update an existing project snippet" do
345
      put api("/projects/#{project.id}/snippets/#{snippet.id}", user),
346
        code: 'updated code'
347 348
      response.status.should == 200
      json_response['title'].should == 'example'
349
      snippet.reload.content.should == 'updated code'
350 351 352
    end
  end

N
Nihad Abbasov 已提交
353
  describe "DELETE /projects/:id/snippets/:snippet_id" do
M
m16a1 已提交
354
    it "should delete existing project snippet" do
N
Nihad Abbasov 已提交
355
      expect {
356
        delete api("/projects/#{project.id}/snippets/#{snippet.id}", user)
357
      }.to change { Snippet.count }.by(-1)
N
Nihad Abbasov 已提交
358 359
    end
  end
360 361 362

  describe "GET /projects/:id/snippets/:snippet_id/raw" do
    it "should get a raw project snippet" do
363
      get api("/projects/#{project.id}/snippets/#{snippet.id}/raw", user)
364 365 366
      response.status.should == 200
    end
  end
367 368 369

  describe "GET /projects/:id/:sha/blob" do
    it "should get the raw file contents" do
370
      get api("/projects/#{project.id}/repository/commits/master/blob?filepath=README.md", user)
371 372 373 374
      response.status.should == 200
    end

    it "should return 404 for invalid branch_name" do
375
      get api("/projects/#{project.id}/repository/commits/invalid_branch_name/blob?filepath=README.md", user)
376 377 378 379
      response.status.should == 404
    end

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