projects_spec.rb 7.21 KB
Newer Older
Nihad Abbasov's avatar
Nihad Abbasov committed
1 2 3
require 'spec_helper'

describe Gitlab::API do
4 5
  include ApiHelpers

Nihad Abbasov's avatar
Nihad Abbasov committed
6
  let(:user) { Factory :user }
miks's avatar
miks committed
7 8
  let(:user2) { Factory.create(:user) }
  let(:user3) { Factory.create(:user) }
9 10
  let!(:project) { Factory :project, owner: user }
  let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' }
miks's avatar
miks committed
11 12
  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  }
13
  before { project.add_access(user, :read) }
Nihad Abbasov's avatar
Nihad Abbasov committed
14 15 16

  describe "GET /projects" do
    it "should return authentication error" do
Robert Speicher's avatar
Robert Speicher committed
17
      get api("/projects")
Nihad Abbasov's avatar
Nihad Abbasov committed
18 19 20 21 22
      response.status.should == 401
    end

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

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

    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'
43
      response.status.should == 201
44
    end
Alex Denisov's avatar
Alex Denisov committed
45 46 47 48

    it "should repsond with 404 on failure" do
      post api("/projects", user)
      response.status.should == 404
49
    end
Alex Denisov's avatar
Alex Denisov committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    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
68 69 70
    end
  end

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

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

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

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

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

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

miks's avatar
miks committed
111 112 113 114 115 116 117
  describe "GET /projects/:id/users" do
    it "should return project users" do
      get api("/projects/#{project.code}/users", user)

      response.status.should == 200

      json_response.should be_an Array
miks's avatar
miks committed
118
      json_response.count.should == 2
miks's avatar
miks committed
119 120 121 122
      json_response.first['user']['id'].should == user.id
    end
  end

miks's avatar
miks committed
123 124 125 126
  describe "POST /projects/:id/users" do
    it "should add users to project" do
      expect {
        post api("/projects/#{project.code}/users", user),
miks's avatar
miks committed
127 128
          user_ids: {"0" => user2.id}, project_access: UsersProject::DEVELOPER
      }.to change {project.users_projects.where(:project_access => UsersProject::DEVELOPER).count}.by(1)
miks's avatar
miks committed
129 130 131 132 133 134 135
    end
  end

  describe "PUT /projects/:id/users" do
    it "should update users to new access role" do
      expect {
        put api("/projects/#{project.code}/users", user),
miks's avatar
miks committed
136 137
          user_ids: {"0" => user3.id}, project_access: UsersProject::MASTER
      }.to change {project.users_projects.where(:project_access => UsersProject::MASTER).count}.by(1)
miks's avatar
miks committed
138 139 140 141 142 143 144
    end
  end

  describe "DELETE /projects/:id/users" do
    it "should delete users from project" do
      expect {
        delete api("/projects/#{project.code}/users", user),
miks's avatar
miks committed
145 146
          user_ids: {"0" => user3.id}
      }.to change {project.users_projects.count}.by(-1)
miks's avatar
miks committed
147 148 149
    end
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
150 151
  describe "GET /projects/:id/repository/tags" do
    it "should return an array of project tags" do
Robert Speicher's avatar
Robert Speicher committed
152
      get api("/projects/#{project.code}/repository/tags", user)
Nihad Abbasov's avatar
Nihad Abbasov committed
153
      response.status.should == 200
Nihad Abbasov's avatar
Nihad Abbasov committed
154 155
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name
Nihad Abbasov's avatar
Nihad Abbasov committed
156 157
    end
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
158 159 160

  describe "GET /projects/:id/snippets/:snippet_id" do
    it "should return a project snippet" do
Robert Speicher's avatar
Robert Speicher committed
161
      get api("/projects/#{project.code}/snippets/#{snippet.id}", user)
Nihad Abbasov's avatar
Nihad Abbasov committed
162
      response.status.should == 200
Nihad Abbasov's avatar
Nihad Abbasov committed
163
      json_response['title'].should == snippet.title
Nihad Abbasov's avatar
Nihad Abbasov committed
164 165 166 167 168
    end
  end

  describe "POST /projects/:id/snippets" do
    it "should create a new project snippet" do
Robert Speicher's avatar
Robert Speicher committed
169
      post api("/projects/#{project.code}/snippets", user),
170
        title: 'api test', file_name: 'sample.rb', code: 'test'
Nihad Abbasov's avatar
Nihad Abbasov committed
171
      response.status.should == 201
Nihad Abbasov's avatar
Nihad Abbasov committed
172
      json_response['title'].should == 'api test'
Nihad Abbasov's avatar
Nihad Abbasov committed
173 174 175
    end
  end

176 177
  describe "PUT /projects/:id/snippets" do
    it "should update an existing project snippet" do
Robert Speicher's avatar
Robert Speicher committed
178
      put api("/projects/#{project.code}/snippets/#{snippet.id}", user),
179
        code: 'updated code'
180 181 182 183 184 185
      response.status.should == 200
      json_response['title'].should == 'example'
      snippet.reload.content.should == 'updated code'
    end
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
186
  describe "DELETE /projects/:id/snippets/:snippet_id" do
m16a1's avatar
m16a1 committed
187
    it "should delete existing project snippet" do
Nihad Abbasov's avatar
Nihad Abbasov committed
188
      expect {
Robert Speicher's avatar
Robert Speicher committed
189
        delete api("/projects/#{project.code}/snippets/#{snippet.id}", user)
190
      }.to change { Snippet.count }.by(-1)
Nihad Abbasov's avatar
Nihad Abbasov committed
191 192
    end
  end
193 194 195

  describe "GET /projects/:id/snippets/:snippet_id/raw" do
    it "should get a raw project snippet" do
Robert Speicher's avatar
Robert Speicher committed
196
      get api("/projects/#{project.code}/snippets/#{snippet.id}/raw", user)
197 198 199
      response.status.should == 200
    end
  end
200 201 202

  describe "GET /projects/:id/:sha/blob" do
    it "should get the raw file contents" do
Robert Speicher's avatar
Robert Speicher committed
203
      get api("/projects/#{project.code}/repository/commits/master/blob?filepath=README.md", user)
204 205 206 207
      response.status.should == 200
    end

    it "should return 404 for invalid branch_name" do
Robert Speicher's avatar
Robert Speicher committed
208
      get api("/projects/#{project.code}/repository/commits/invalid_branch_name/blob?filepath=README.md", user)
209 210 211 212
      response.status.should == 404
    end

    it "should return 404 for invalid file" do
Robert Speicher's avatar
Robert Speicher committed
213
      get api("/projects/#{project.code}/repository/commits/master/blob?filepath=README.invalid", user)
214 215 216
      response.status.should == 404
    end
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
217
end