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

describe Gitlab::API do
4 5
  include ApiHelpers

N
Nihad Abbasov 已提交
6
  let(:user) { Factory :user }
M
miks 已提交
7 8
  let(:user2) { Factory.create(:user) }
  let(:user3) { Factory.create(:user) }
M
miks 已提交
9
  let!(:hook) { Factory :project_hook, project: project, url: "http://example.com" }
10 11
  let!(:project) { Factory :project, owner: user }
  let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' }
M
miks 已提交
12 13
  let!(:users_project) { Factory :users_project, user: user, project: project, project_access: UsersProject::MASTER  }
  let!(:users_project2) { Factory :users_project, user: user3, project: project, project_access: UsersProject::DEVELOPER  }
N
Nihad Abbasov 已提交
14
  before { project.add_access(user, :read) }
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 36
  describe "POST /projects" do
    it "should create new project without code and 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 50 51

    it "should repsond with 404 on failure" do
      post api("/projects", user)
      response.status.should == 404
52
    end
A
Alex Denisov 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    it "should assign attributes to project" do
      project = Factory.attributes(:project, {
        path: 'path',
        code: 'code',
        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
71 72 73
    end
  end

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

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

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

  describe "GET /projects/:id/repository/branches" do
    it "should return an array of project branches" do
R
Robert Speicher 已提交
97
      get api("/projects/#{project.code}/repository/branches", user)
N
Nihad Abbasov 已提交
98
      response.status.should == 200
N
Nihad Abbasov 已提交
99 100
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name
N
Nihad Abbasov 已提交
101 102 103
    end
  end

104 105
  describe "GET /projects/:id/repository/branches/:branch" do
    it "should return the branch information for a single branch" do
R
Robert Speicher 已提交
106
      get api("/projects/#{project.code}/repository/branches/new_design", user)
107 108 109 110 111 112 113
      response.status.should == 200

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

N
Nihad Abbasov 已提交
114 115 116
  describe "GET /projects/:id/members" do
    it "should return project team members" do
      get api("/projects/#{project.code}/members", user)
M
miks 已提交
117 118
      response.status.should == 200
      json_response.should be_an Array
M
miks 已提交
119
      json_response.count.should == 2
N
Nihad Abbasov 已提交
120
      json_response.first['email'].should == user.email
M
miks 已提交
121 122 123
    end
  end

N
Nihad Abbasov 已提交
124 125 126 127 128 129
  describe "GET /projects/:id/members/:user_id" do
    it "should return project team member" do
      get api("/projects/#{project.code}/members/#{user.id}", user)
      response.status.should == 200
      json_response['email'].should == user.email
      json_response['access_level'].should == UsersProject::MASTER
M
miks 已提交
130 131 132
    end
  end

N
Nihad Abbasov 已提交
133 134
  describe "POST /projects/:id/members" do
    it "should add user to project team" do
M
miks 已提交
135
      expect {
N
Nihad Abbasov 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        post api("/projects/#{project.code}/members", user), user_id: user2.id,
          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
      put api("/projects/#{project.code}/members/#{user3.id}", user), access_level: UsersProject::MASTER
      response.status.should == 200
      json_response['email'].should == user3.email
      json_response['access_level'].should == UsersProject::MASTER
M
miks 已提交
152 153 154
    end
  end

N
Nihad Abbasov 已提交
155 156
  describe "DELETE /projects/:id/members/:user_id" do
    it "should remove user from project team" do
M
miks 已提交
157
      expect {
N
Nihad Abbasov 已提交
158 159
        delete api("/projects/#{project.code}/members/#{user3.id}", user)
      }.to change { UsersProject.count }.by(-1)
M
miks 已提交
160 161 162
    end
  end

M
miks 已提交
163 164 165 166 167 168 169 170 171 172 173 174
  describe "GET /projects/:id/hooks" do
    it "should return project hooks" do
      get api("/projects/#{project.code}/hooks", user)

      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

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

  describe "POST /projects/:id/hooks" do
M
miks 已提交
184 185 186 187 188 189 190
    it "should add hook to project" do
      expect {
        post api("/projects/#{project.code}/hooks", user),
          "url" => "http://example.com"
      }.to change {project.hooks.count}.by(1)
    end
  end
191 192 193 194
  
  describe "PUT /projects/:id/hooks/:hook_id" do
    it "should update an existing project hook" do
      put api("/projects/#{project.code}/hooks/#{hook.id}", user),
195
        url: 'http://example.com'
196 197 198 199 200
      response.status.should == 200
      json_response['url'].should == 'http://example.com'
    end
  end
  
M
miks 已提交
201 202 203 204 205 206 207 208 209 210

  describe "DELETE /projects/:id/hooks" do
    it "should delete hook from project" do
      expect {
        delete api("/projects/#{project.code}/hooks", user),
          hook_id: hook.id
      }.to change {project.hooks.count}.by(-1)
    end
  end

N
Nihad Abbasov 已提交
211 212
  describe "GET /projects/:id/repository/tags" do
    it "should return an array of project tags" do
R
Robert Speicher 已提交
213
      get api("/projects/#{project.code}/repository/tags", user)
N
Nihad Abbasov 已提交
214
      response.status.should == 200
N
Nihad Abbasov 已提交
215 216
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name
N
Nihad Abbasov 已提交
217 218
    end
  end
N
Nihad Abbasov 已提交
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  describe "GET /projects/:id/repository/commits" do
    context "authorized user" do
      before { project.add_access(user2, :read) }

      it "should return project commits" do
        get api("/projects/#{project.code}/repository/commits", user)
        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
        get api("/projects/#{project.code}/repository/commits")
        response.status.should == 401
      end
    end
  end

N
Nihad Abbasov 已提交
241 242 243 244 245 246 247 248 249
  describe "GET /projects/:id/snippets" do
    it "should return a project snippet" do
      get api("/projects/#{project.code}/snippets", user)
      response.status.should == 200
      json_response.should be_an Array
      json_response.first['title'].should == snippet.title
    end
  end

N
Nihad Abbasov 已提交
250 251
  describe "GET /projects/:id/snippets/:snippet_id" do
    it "should return a project snippet" do
R
Robert Speicher 已提交
252
      get api("/projects/#{project.code}/snippets/#{snippet.id}", user)
N
Nihad Abbasov 已提交
253
      response.status.should == 200
N
Nihad Abbasov 已提交
254
      json_response['title'].should == snippet.title
N
Nihad Abbasov 已提交
255 256 257 258 259
    end
  end

  describe "POST /projects/:id/snippets" do
    it "should create a new project snippet" do
R
Robert Speicher 已提交
260
      post api("/projects/#{project.code}/snippets", user),
261
        title: 'api test', file_name: 'sample.rb', code: 'test'
N
Nihad Abbasov 已提交
262
      response.status.should == 201
N
Nihad Abbasov 已提交
263
      json_response['title'].should == 'api test'
N
Nihad Abbasov 已提交
264 265 266
    end
  end

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

N
Nihad Abbasov 已提交
277
  describe "DELETE /projects/:id/snippets/:snippet_id" do
M
m16a1 已提交
278
    it "should delete existing project snippet" do
N
Nihad Abbasov 已提交
279
      expect {
R
Robert Speicher 已提交
280
        delete api("/projects/#{project.code}/snippets/#{snippet.id}", user)
281
      }.to change { Snippet.count }.by(-1)
N
Nihad Abbasov 已提交
282 283
    end
  end
284 285 286

  describe "GET /projects/:id/snippets/:snippet_id/raw" do
    it "should get a raw project snippet" do
R
Robert Speicher 已提交
287
      get api("/projects/#{project.code}/snippets/#{snippet.id}/raw", user)
288 289 290
      response.status.should == 200
    end
  end
291 292 293

  describe "GET /projects/:id/:sha/blob" do
    it "should get the raw file contents" do
R
Robert Speicher 已提交
294
      get api("/projects/#{project.code}/repository/commits/master/blob?filepath=README.md", user)
295 296 297 298
      response.status.should == 200
    end

    it "should return 404 for invalid branch_name" do
R
Robert Speicher 已提交
299
      get api("/projects/#{project.code}/repository/commits/invalid_branch_name/blob?filepath=README.md", user)
300 301 302 303
      response.status.should == 404
    end

    it "should return 404 for invalid file" do
R
Robert Speicher 已提交
304
      get api("/projects/#{project.code}/repository/commits/master/blob?filepath=README.invalid", user)
305 306 307
      response.status.should == 404
    end
  end
N
Nihad Abbasov 已提交
308
end