Commit 61ffcab60fef2efcd54b8496aa09de79ee999a2c

Authored by Angus MacArthur
Committed by Angus MacArthur
1 parent d5663e14

Additional Admin APIs

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') }
... ... @@ -90,6 +91,46 @@ describe Gitlab::API do
90 91 end
91 92 end
92 93  
  94 + describe "POST /projects/user/:id" do
  95 + before { admin }
  96 +
  97 + it "should create new project without path" do
  98 + expect { post api("/projects/user/#{user.id}", admin), name: 'foo' }.to change {Project.count}.by(1)
  99 + end
  100 +
  101 + it "should not create new project without name" do
  102 + expect { post api("/projects/user/#{user.id}", admin) }.to_not change {Project.count}
  103 + end
  104 +
  105 + it "should respond with 201 on success" do
  106 + post api("/projects/user/#{user.id}", admin), name: 'foo'
  107 + response.status.should == 201
  108 + end
  109 +
  110 + it "should respond with 404 on failure" do
  111 + post api("/projects/user/#{user.id}", admin)
  112 + response.status.should == 404
  113 + end
  114 +
  115 + it "should assign attributes to project" do
  116 + project = attributes_for(:project, {
  117 + description: Faker::Lorem.sentence,
  118 + default_branch: 'stable',
  119 + issues_enabled: false,
  120 + wall_enabled: false,
  121 + merge_requests_enabled: false,
  122 + wiki_enabled: false
  123 + })
  124 +
  125 + post api("/projects/user/#{user.id}", admin), project
  126 +
  127 + project.each_pair do |k,v|
  128 + next if k == :path
  129 + json_response[k.to_s].should == v
  130 + end
  131 + end
  132 + end
  133 +
93 134 describe "GET /projects/:id" do
94 135 it "should return a project by id" do
95 136 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  
... ...