Skip to content

Commit 2bd9e80

Browse files
committed
Add validation for primary runtime namespace in project updates
1 parent 7776a3c commit 2bd9e80

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

app/graphql/mutations/namespaces/projects/update.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ def resolve(namespace_project_id:, **params)
2424
errors: [create_message_error('Invalid project')] }
2525
end
2626

27-
if params.key?(:primary_runtime_id)
28-
primary_runtime = SagittariusSchema.object_from_id(params[:primary_runtime_id])
29-
if primary_runtime.nil?
30-
return { namespace_project: nil, errors: [create_message_error('Could not find runtime')] }
31-
end
32-
33-
params[:primary_runtime] = primary_runtime
34-
params.delete(:primary_runtime_id)
35-
end
27+
params[:primary_runtime_id] = params[:primary_runtime_id]&.model_id if params.key?(:primary_runtime_id)
3628

3729
::Namespaces::Projects::UpdateService.new(
3830
current_authentication,

app/models/namespace_project.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ class NamespaceProject < ApplicationRecord
1919

2020
before_validation :strip_whitespace
2121

22+
validate :validate_primary_runtime, if: :primary_runtime_changed?
23+
24+
def validate_primary_runtime
25+
return if primary_runtime&.namespace.nil?
26+
return if primary_runtime.namespace == namespace
27+
28+
errors.add(:primary_runtime, :invalid_namespace, message: 'must belong to the same namespace as the project')
29+
end
30+
2231
private
2332

2433
def strip_whitespace

app/services/namespaces/projects/update_service.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def execute
1818
return ServiceResponse.error(message: 'Missing permission', payload: :missing_permission)
1919
end
2020

21+
params[:primary_runtime_id] = params.delete(:primary_runtime)&.id if params.key?(:primary_runtime)
22+
2123
transactional do |t|
2224
success = namespace_project.update(params)
2325
unless success
@@ -27,20 +29,6 @@ def execute
2729
)
2830
end
2931

30-
if params.key?(:primary_runtime)
31-
runtime = params[:primary_runtime]
32-
33-
if runtime.namespace != namespace_project.namespace
34-
return ServiceResponse.error(
35-
message: 'Primary runtime must belong to the same namespace as the project',
36-
payload: :invalid_primary_runtime
37-
)
38-
end
39-
40-
params[:primary_runtime_id] = runtime.id if runtime.is_a?(Runtime)
41-
params.delete(:primary_runtime)
42-
end
43-
4432
AuditService.audit(
4533
:namespace_project_updated,
4634
author_id: current_authentication.user.id,

spec/models/namespace_project_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'rails_helper'
44

55
RSpec.describe NamespaceProject do
6-
subject { create(:namespace_project) }
6+
subject(:project) { create(:namespace_project) }
77

88
describe 'associations' do
99
it { is_expected.to belong_to(:namespace).required }
@@ -25,5 +25,18 @@
2525
it { is_expected.to allow_value(' ').for(:description) }
2626
it { is_expected.to allow_value('').for(:description) }
2727
it { is_expected.not_to allow_value(nil).for(:description) }
28+
29+
it 'detects invalid primary runtime namespace' do
30+
runtime = create(:runtime, namespace: create(:namespace))
31+
project.primary_runtime = runtime
32+
is_expected.not_to be_valid
33+
expect(project.errors[:primary_runtime]).to include('must belong to the same namespace as the project')
34+
end
35+
36+
it 'allows primary runtime namespace to be nil' do
37+
runtime = create(:runtime, namespace: nil)
38+
project.primary_runtime = runtime
39+
is_expected.to be_valid
40+
end
2841
end
2942
end

0 commit comments

Comments
 (0)