Commit 7c408960ce9cf8a20941c99fd64aa5b1f472f9a0

Authored by Dmitriy Zaporozhets
2 parents 52bf5b0e 47abdc10

Merge pull request #3146 from amacarthur/AdminAPIs

Additional Admin APIs
doc/api/groups.md
... ... @@ -43,3 +43,14 @@ Parameters:
43 43  
44 44 Will return created group with status `201 Created` on success, or `404 Not found` on fail.
45 45  
  46 +## Transfer project to group
  47 +
  48 +Transfer a project to the Group namespace. Available only for admin
  49 +
  50 +```
  51 +POST /groups/:id/projects/:project_id
  52 +```
  53 +
  54 +Parameters:
  55 ++ `id` (required) - The ID of a group
  56 ++ `project_id (required) - The ID of a project
... ...
doc/api/projects.md
... ... @@ -113,6 +113,28 @@ Parameters:
113 113 Will return created project with status `201 Created` on success, or `404 Not
114 114 found` on fail.
115 115  
  116 +## Create project for user
  117 +
  118 +Create new project owned by user. Available only for admin
  119 +
  120 +```
  121 +POST /projects/user/:user_id
  122 +```
  123 +
  124 +Parameters:
  125 +
  126 ++ `user_id` (required) - user_id of owner
  127 ++ `name` (required) - new project name
  128 ++ `description` (optional) - short project description
  129 ++ `default_branch` (optional) - 'master' by default
  130 ++ `issues_enabled` (optional) - enabled by default
  131 ++ `wall_enabled` (optional) - enabled by default
  132 ++ `merge_requests_enabled` (optional) - enabled by default
  133 ++ `wiki_enabled` (optional) - enabled by default
  134 +
  135 +Will return created project with status `201 Created` on success, or `404 Not
  136 +found` on fail.
  137 +
116 138 ## List project team members
117 139  
118 140 Get a list of project team members.
... ... @@ -352,4 +374,4 @@ Parameters:
352 374  
353 375 + `id` (required) - Deploy key ID
354 376  
355   -Will return `200 OK` on success, or `404 Not Found` on fail.
356 377 \ No newline at end of file
  378 +Will return `200 OK` on success, or `404 Not Found` on fail.
... ...
doc/api/users.md
... ... @@ -220,6 +220,23 @@ Parameters:
220 220 Will return created key with status `201 Created` on success, or `404 Not
221 221 found` on fail.
222 222  
  223 +## Add SSH key for user
  224 +
  225 +Create new key owned by specified user. Available only for admin
  226 +
  227 +```
  228 +POST /users/:id/keys
  229 +```
  230 +
  231 +Parameters:
  232 +
  233 ++ `id` (required) - id of specified user
  234 ++ `title` (required) - new SSH Key's title
  235 ++ `key` (required) - new SSH key
  236 +
  237 +Will return created key with status `201 Created` on success, or `404 Not
  238 +found` on fail.
  239 +
223 240 ## Delete SSH key
224 241  
225 242 Delete key owned by currently authenticated user
... ...
lib/api/groups.rb
... ... @@ -51,6 +51,24 @@ module Gitlab
51 51 not_found!
52 52 end
53 53 end
  54 +
  55 + # Transfer a project to the Group namespace
  56 + #
  57 + # Parameters:
  58 + # id - group id
  59 + # project_id - project id
  60 + # Example Request:
  61 + # POST /groups/:id/projects/:project_id
  62 + post ":id/projects/:project_id" do
  63 + authenticated_as_admin!
  64 + @group = Group.find(params[:id])
  65 + project = Project.find(params[:project_id])
  66 + if project.transfer(@group)
  67 + present @group
  68 + else
  69 + not_found!
  70 + end
  71 + end
54 72 end
55 73 end
56 74 end
... ...
lib/api/projects.rb
... ... @@ -52,6 +52,38 @@ module Gitlab
52 52 end
53 53 end
54 54  
  55 + # Create new project for a specified user. Only available to admin users.
  56 + #
  57 + # Parameters:
  58 + # user_id (required) - The ID of a user
  59 + # name (required) - name for new project
  60 + # description (optional) - short project description
  61 + # default_branch (optional) - 'master' by default
  62 + # issues_enabled (optional) - enabled by default
  63 + # wall_enabled (optional) - enabled by default
  64 + # merge_requests_enabled (optional) - enabled by default
  65 + # wiki_enabled (optional) - enabled by default
  66 + # Example Request
  67 + # POST /projects/user/:user_id
  68 + post "user/:user_id" do
  69 + authenticated_as_admin!
  70 + user = User.find(params[:user_id])
  71 + attrs = attributes_for_keys [:name,
  72 + :description,
  73 + :default_branch,
  74 + :issues_enabled,
  75 + :wall_enabled,
  76 + :merge_requests_enabled,
  77 + :wiki_enabled]
  78 + @project = ::Projects::CreateContext.new(user, attrs).execute
  79 + if @project.saved?
  80 + present @project, with: Entities::Project
  81 + else
  82 + not_found!
  83 + end
  84 + end
  85 +
  86 +
55 87 # Get a project team members
56 88 #
57 89 # Parameters:
... ...
lib/api/users.rb
... ... @@ -77,6 +77,26 @@ module Gitlab
77 77 end
78 78 end
79 79  
  80 + # Add ssh key to a specified user. Only available to admin users.
  81 + #
  82 + # Parameters:
  83 + # id (required) - The ID of a user
  84 + # key (required) - New SSH Key
  85 + # title (required) - New SSH Key's title
  86 + # Example Request:
  87 + # POST /users/:id/keys
  88 + post ":id/keys" do
  89 + authenticated_as_admin!
  90 + user = User.find(params[:id])
  91 + attrs = attributes_for_keys [:title, :key]
  92 + key = user.keys.new attrs
  93 + if key.save
  94 + present key, with: Entities::SSHKey
  95 + else
  96 + not_found!
  97 + end
  98 + end
  99 +
80 100 # Delete user. Available only for admin
81 101 #
82 102 # Example Request:
... ...
spec/requests/api/groups_spec.rb
... ... @@ -90,4 +90,27 @@ describe Gitlab::API do
90 90 end
91 91 end
92 92 end
  93 +
  94 + describe "POST /groups/:id/projects/:project_id" do
  95 + let(:project) { create(:project) }
  96 + before(:each) do
  97 + project.stub!(:transfer).and_return(true)
  98 + Project.stub(:find).and_return(project)
  99 + end
  100 +
  101 +
  102 + context "when authenticated as user" do
  103 + it "should not transfer project to group" do
  104 + post api("/groups/#{group1.id}/projects/#{project.id}", user2)
  105 + response.status.should == 403
  106 + end
  107 + end
  108 +
  109 + context "when authenticated as admin" do
  110 + it "should transfer project to group" do
  111 + project.should_receive(:transfer)
  112 + post api("/groups/#{group1.id}/projects/#{project.id}", admin)
  113 + end
  114 + end
  115 + end
93 116 end
... ...
spec/requests/api/projects_spec.rb
... ... @@ -6,6 +6,7 @@ describe Gitlab::API do
6 6 let(:user) { create(:user) }
7 7 let(:user2) { create(:user) }
8 8 let(:user3) { create(:user) }
  9 + let(:admin) { create(:admin) }
9 10 let!(:hook) { create(:project_hook, project: project, url: "http://example.com") }
10 11 let!(:project) { create(:project, namespace: user.namespace ) }
11 12 let!(:snippet) { create(:snippet, author: user, project: project, title: 'example') }
... ... @@ -92,6 +93,46 @@ describe Gitlab::API do
92 93 end
93 94 end
94 95  
  96 + describe "POST /projects/user/:id" do
  97 + before { admin }
  98 +
  99 + it "should create new project without path" do
  100 + expect { post api("/projects/user/#{user.id}", admin), name: 'foo' }.to change {Project.count}.by(1)
  101 + end
  102 +
  103 + it "should not create new project without name" do
  104 + expect { post api("/projects/user/#{user.id}", admin) }.to_not change {Project.count}
  105 + end
  106 +
  107 + it "should respond with 201 on success" do
  108 + post api("/projects/user/#{user.id}", admin), name: 'foo'
  109 + response.status.should == 201
  110 + end
  111 +
  112 + it "should respond with 404 on failure" do
  113 + post api("/projects/user/#{user.id}", admin)
  114 + response.status.should == 404
  115 + end
  116 +
  117 + it "should assign attributes to project" do
  118 + project = attributes_for(:project, {
  119 + description: Faker::Lorem.sentence,
  120 + default_branch: 'stable',
  121 + issues_enabled: false,
  122 + wall_enabled: false,
  123 + merge_requests_enabled: false,
  124 + wiki_enabled: false
  125 + })
  126 +
  127 + post api("/projects/user/#{user.id}", admin), project
  128 +
  129 + project.each_pair do |k,v|
  130 + next if k == :path
  131 + json_response[k.to_s].should == v
  132 + end
  133 + end
  134 + end
  135 +
95 136 describe "GET /projects/:id" do
96 137 it "should return a project by id" do
97 138 get api("/projects/#{project.id}", user)
... ...
spec/requests/api/users_spec.rb
... ... @@ -105,6 +105,22 @@ describe Gitlab::API do
105 105 end
106 106 end
107 107  
  108 + describe "POST /users/:id/keys" do
  109 + before { admin }
  110 +
  111 + it "should not create invalid ssh key" do
  112 + post api("/users/#{user.id}/keys", admin), { title: "invalid key" }
  113 + response.status.should == 404
  114 + end
  115 +
  116 + it "should create ssh key" do
  117 + key_attrs = attributes_for :key
  118 + expect {
  119 + post api("/users/#{user.id}/keys", admin), key_attrs
  120 + }.to change{ user.keys.count }.by(1)
  121 + end
  122 + end
  123 +
108 124 describe "DELETE /users/:id" do
109 125 before { admin }
110 126  
... ...