diff --git a/app/models/organization.rb b/app/models/organization.rb index 8e560b28f..a58186f7a 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -19,7 +19,8 @@ class Organization < ApplicationModel before_create :domain_cleanup before_update :domain_cleanup - validates :name, presence: true + validates :name, presence: true + validates :domain, presence: { message: 'required when Domain Based Assignment is enabled' }, if: :domain_assignment activity_stream_permission 'admin.role' diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index 71bb0277a..ec31a4836 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -41,4 +41,30 @@ RSpec.describe Organization, type: :model do end end end + + describe '#domain_assignment' do + it 'fails if enabled and domain is missing' do + organization.domain_assignment = true + organization.domain = nil + organization.valid? + + expect(organization.errors[:domain]).to be_present + end + + it 'succeeds if enabled and domain is present' do + organization.domain_assignment = true + organization.domain = 'example.org' + organization.valid? + + expect(organization.errors[:domain]).to be_empty + end + + it 'succeeds if disabled and domain is missing' do + organization.domain_assignment = false + organization.domain = nil + organization.valid? + + expect(organization.errors[:domain]).to be_empty + end + end end