Commit 7fd9e39d authored by Tiago Botelho's avatar Tiago Botelho

When a Service templates are invalid newly created projects will have them inactive

parent bf4a3af0
......@@ -273,6 +273,7 @@ class Service < ActiveRecord::Base
def self.build_from_template(project_id, template)
service = template.dup
service.active = false unless service.valid?
service.template = false
service.project_id = project_id
service
......
......@@ -133,8 +133,10 @@ module Projects
def fail(error:)
message = "Unable to save project. Error: #{error}"
log_message = message.dup
Rails.logger.error(message)
log_message << " Project ID: #{@project.id}" if @project&.id
Rails.logger.error(log_message)
if @project
@project.errors.add(:base, message)
......
......@@ -58,6 +58,21 @@ describe Service do
end
describe "Template" do
describe '.build_from_template' do
context 'when template is invalid' do
it 'sets service template to inactive when template is invalid' do
project = create(:project)
template = JiraService.new(template: true, active: true)
template.save(validate: false)
service = described_class.build_from_template(project.id, template)
expect(service).to be_valid
expect(service.active).to be false
end
end
end
describe "for pushover service" do
let!(:service_template) do
PushoverService.create(
......
......@@ -71,14 +71,14 @@ describe Projects::CreateService, '#execute' do
expect(create_project(user, opts)).to eq(nil)
end
it 'handles invalid service' do
it 'sets invalid service as inactive' do
create(:service, type: 'JiraService', project: nil, template: true, active: true)
project = create_project(user, opts)
service = project.services.first
expect(project).not_to be_persisted
expect(project.errors.full_messages_for(:base).first).to match(/Unable to save project. Error: Unable to save JiraService/)
expect(project.services.count).to eq 0
expect(project).to be_persisted
expect(service.active).to be false
end
end
......@@ -242,14 +242,15 @@ describe Projects::CreateService, '#execute' do
end
context 'when a bad service template is created' do
it 'reports an error in the imported project' do
it 'sets service to be inactive' do
opts[:import_url] = 'http://www.gitlab.com/gitlab-org/gitlab-ce'
create(:service, type: 'DroneCiService', project: nil, template: true, active: true)
project = create_project(user, opts)
service = project.services.first
expect(project.errors.full_messages_for(:base).first).to match(/Unable to save project. Error: Unable to save DroneCiService/)
expect(project.services.count).to eq 0
expect(project).to be_persisted
expect(service.active).to be false
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