Commit 554e18ab authored by Z.J. van de Weg's avatar Z.J. van de Weg

Create service for enabling deploy keys

parent 460065b7
...@@ -12,8 +12,7 @@ class Projects::DeployKeysController < Projects::ApplicationController ...@@ -12,8 +12,7 @@ class Projects::DeployKeysController < Projects::ApplicationController
end end
def new def new
redirect_to namespace_project_deploy_keys_path(@project.namespace, redirect_to namespace_project_deploy_keys_path(@project.namespace, @project)
@project)
end end
def create def create
...@@ -21,19 +20,16 @@ class Projects::DeployKeysController < Projects::ApplicationController ...@@ -21,19 +20,16 @@ class Projects::DeployKeysController < Projects::ApplicationController
set_index_vars set_index_vars
if @key.valid? && @project.deploy_keys << @key if @key.valid? && @project.deploy_keys << @key
redirect_to namespace_project_deploy_keys_path(@project.namespace, redirect_to namespace_project_deploy_keys_path(@project.namespace, @project)
@project)
else else
render "index" render "index"
end end
end end
def enable def enable
@key = accessible_keys.find(params[:id]) EnableDeployKeyService.new(@project, current_user, params).execute
@project.deploy_keys << @key
redirect_to namespace_project_deploy_keys_path(@project.namespace, redirect_to namespace_project_deploy_keys_path(@project.namespace, @project)
@project)
end end
def disable def disable
...@@ -45,9 +41,9 @@ class Projects::DeployKeysController < Projects::ApplicationController ...@@ -45,9 +41,9 @@ class Projects::DeployKeysController < Projects::ApplicationController
protected protected
def set_index_vars def set_index_vars
@enabled_keys ||= @project.deploy_keys @enabled_keys ||= @project.deploy_keys
@available_keys ||= accessible_keys - @enabled_keys @available_keys ||= current_user.accessible_deploy_keys - @enabled_keys
@available_project_keys ||= current_user.project_deploy_keys - @enabled_keys @available_project_keys ||= current_user.project_deploy_keys - @enabled_keys
@available_public_keys ||= DeployKey.are_public - @enabled_keys @available_public_keys ||= DeployKey.are_public - @enabled_keys
...@@ -56,10 +52,6 @@ class Projects::DeployKeysController < Projects::ApplicationController ...@@ -56,10 +52,6 @@ class Projects::DeployKeysController < Projects::ApplicationController
@available_public_keys -= @available_project_keys @available_public_keys -= @available_project_keys
end end
def accessible_keys
@accessible_keys ||= current_user.accessible_deploy_keys
end
def deploy_key_params def deploy_key_params
params.require(:deploy_key).permit(:key, :title) params.require(:deploy_key).permit(:key, :title)
end end
......
class EnableDeployKeyService < BaseService
def execute
key = accessible_keys.find_by(id: params[:key_id] || params[:id])
project.deploy_keys << key if key
key
end
private
def accessible_keys
current_user.accessible_deploy_keys
end
end
...@@ -76,12 +76,12 @@ module API ...@@ -76,12 +76,12 @@ module API
requires :key_id, type: Integer, desc: 'The ID of the deploy key' requires :key_id, type: Integer, desc: 'The ID of the deploy key'
end end
post ":id/#{path}/:key_id/enable" do post ":id/#{path}/:key_id/enable" do
key = DeployKey.find(params[:key_id]) key = EnableDeployKeyService.new(user_project, current_user, declared(params)).execute
if user_project.deploy_keys << key if key
present key, with: Entities::SSHKey present key, with: Entities::SSHKey
else else
render_validation_error!(key) not_found!('Deploy Key')
end end
end end
......
...@@ -3,11 +3,14 @@ require 'spec_helper' ...@@ -3,11 +3,14 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
let(:user) { create(:user) } let(:user) { create(:user) }
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let(:project) { create(:project, creator_id: user.id) } let(:project) { create(:project, creator_id: user.id) }
let!(:deploy_keys_project) { create(:deploy_keys_project, project: project) } let(:deploy_key) { create(:deploy_key, public: true) }
let(:deploy_key) { deploy_keys_project.deploy_key }
let!(:deploy_keys_project) do
create(:deploy_keys_project, project: project, deploy_key: deploy_key)
end
describe 'GET /deploy_keys' do describe 'GET /deploy_keys' do
context 'when unauthenticated' do context 'when unauthenticated' do
......
require 'spec_helper'
describe EnableDeployKeyService, services: true do
let(:deploy_key) { create(:deploy_key, public: true) }
let(:project) { create(:empty_project) }
let(:user) { project.creator}
let!(:params) { { key_id: deploy_key.id } }
it 'enables the key' do
expect do
service.execute
end.to change { project.deploy_keys.count }.from(0).to(1)
end
context 'trying to add an unaccessable key' do
let(:another_key) { create(:another_key) }
let!(:params) { { key_id: another_key.id } }
it 'returns nil if the key cannot be added' do
expect(service.execute).to be nil
end
end
def service
EnableDeployKeyService.new(project, user, params)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment