Commit f1857590 authored by Etienne Baqué's avatar Etienne Baqué

Fixed deploy token creation

Fixed case when a deploy token is created without scope.
Updated rspec accordingly.
Did a bit of refactoring for the DeployToken create services classes.
parent fbf2cde3
...@@ -43,7 +43,7 @@ module Groups ...@@ -43,7 +43,7 @@ module Groups
end end
def create_deploy_token def create_deploy_token
result = Projects::DeployTokens::CreateService.new(@group, current_user, deploy_token_params).execute result = Groups::DeployTokens::CreateService.new(@group, current_user, deploy_token_params).execute
@new_deploy_token = result[:deploy_token] @new_deploy_token = result[:deploy_token]
if result[:status] == :success if result[:status] == :success
......
...@@ -14,4 +14,12 @@ module DeployTokenMethods ...@@ -14,4 +14,12 @@ module DeployTokenMethods
deploy_token.destroy deploy_token.destroy
end end
def create_deploy_token_payload_for(deploy_token)
if deploy_token.persisted?
success(deploy_token: deploy_token, http_status: :created)
else
error(deploy_token.errors.full_messages.to_sentence, :bad_request).merge(deploy_token: deploy_token)
end
end
end end
...@@ -8,11 +8,7 @@ module Groups ...@@ -8,11 +8,7 @@ module Groups
def execute def execute
deploy_token = create_deploy_token_for(@group, params) deploy_token = create_deploy_token_for(@group, params)
if deploy_token.persisted? create_deploy_token_payload_for(deploy_token)
success(deploy_token: deploy_token, http_status: :created)
else
error(deploy_token.errors.full_messages.to_sentence, :bad_request)
end
end end
end end
end end
......
...@@ -8,11 +8,7 @@ module Projects ...@@ -8,11 +8,7 @@ module Projects
def execute def execute
deploy_token = create_deploy_token_for(@project, params) deploy_token = create_deploy_token_for(@project, params)
if deploy_token.persisted? create_deploy_token_payload_for(deploy_token)
success(deploy_token: deploy_token, http_status: :created)
else
error(deploy_token.errors.full_messages.to_sentence, :bad_request)
end
end end
end end
end end
......
# frozen_string_literal: true # frozen_string_literal: true
RSpec.shared_examples 'a created deploy token' do RSpec.shared_examples 'a created deploy token' do
let(:read_repository) { '1' }
let(:deploy_token_params) do let(:deploy_token_params) do
{ {
name: 'deployer_token', name: 'deployer_token',
expires_at: 1.month.from_now.to_date.to_s, expires_at: 1.month.from_now.to_date.to_s,
username: 'deployer', username: 'deployer',
read_repository: '1', read_repository: read_repository,
deploy_token_type: deploy_token_type deploy_token_type: deploy_token_type
} }
end end
...@@ -19,4 +20,15 @@ RSpec.shared_examples 'a created deploy token' do ...@@ -19,4 +20,15 @@ RSpec.shared_examples 'a created deploy token' do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:show) expect(response).to render_template(:show)
end end
context 'when no scope is selected' do
let(:read_repository) { '0' }
it 'creates a variable with a errored deploy token' do
expect { create_deploy_token }.not_to change { DeployToken.active.count }
expect(assigns(:new_deploy_token)).to be_a(DeployToken)
expect(assigns(:new_deploy_token).errors.full_messages.first).to eq('Scopes can\'t be blank')
end
end
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