2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2019-03-13 23:51:22 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'models/object_manager/attribute/validation/backend_examples'
|
|
|
|
|
|
|
|
RSpec.describe ::ObjectManager::Attribute::Validation::Required do
|
|
|
|
|
|
|
|
subject do
|
|
|
|
described_class.new(
|
|
|
|
record: record,
|
|
|
|
attribute: attribute
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-23 07:52:08 +00:00
|
|
|
let(:record) { build(:ticket_article) }
|
2019-04-15 01:41:17 +00:00
|
|
|
let(:attribute) { build(:object_manager_attribute_date) }
|
|
|
|
|
2020-08-19 15:24:19 +00:00
|
|
|
it_behaves_like 'validate backend'
|
2019-03-13 23:51:22 +00:00
|
|
|
|
|
|
|
context 'when validation should be performed' do
|
|
|
|
|
|
|
|
let(:value) { nil }
|
|
|
|
|
|
|
|
shared_examples 'a permission based validator' do |permission:|
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
let(:performing_user) { create(:agent) }
|
2019-03-13 23:51:22 +00:00
|
|
|
|
|
|
|
before { UserInfo.current_user_id = performing_user.id }
|
|
|
|
|
|
|
|
context "for applying permission (#{permission})" do
|
|
|
|
|
|
|
|
let(:permission) { permission }
|
|
|
|
|
|
|
|
before do
|
|
|
|
attribute.screens = {
|
|
|
|
action => {
|
|
|
|
permission => {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-09-22 12:57:53 +00:00
|
|
|
context 'when boolean field with false values' do
|
|
|
|
let(:value) { false }
|
|
|
|
let(:attribute) { build(:object_manager_attribute_boolean) }
|
|
|
|
let(:action) { 'create_middle' }
|
|
|
|
|
|
|
|
it_behaves_like 'a validation without errors'
|
|
|
|
end
|
|
|
|
|
2019-03-13 23:51:22 +00:00
|
|
|
context 'when action is edit' do
|
|
|
|
|
|
|
|
let(:action) { 'edit' }
|
2021-09-23 07:52:08 +00:00
|
|
|
let(:record) { create(:ticket_article) }
|
2019-03-13 23:51:22 +00:00
|
|
|
|
|
|
|
it_behaves_like 'a validation with errors'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action is create_...' do
|
|
|
|
|
|
|
|
let(:action) { 'create_middle' }
|
|
|
|
|
|
|
|
it_behaves_like 'a validation with errors'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a permission based validator', permission: 'ticket.agent'
|
|
|
|
it_behaves_like 'a permission based validator', permission: '-all-'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when validation should not be performed' do
|
|
|
|
|
|
|
|
context 'for present value' do
|
|
|
|
|
|
|
|
let(:value) { 'some_value' }
|
|
|
|
|
|
|
|
it_behaves_like 'a validation without errors'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when value is actually blank' do
|
|
|
|
|
|
|
|
let(:value) { nil }
|
|
|
|
|
|
|
|
context "when action wasn't performed by a user" do
|
|
|
|
context 'for blank UserInfo.current_user_id', current_user_id: nil do
|
|
|
|
it_behaves_like 'a validation without errors'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for system UserInfo.current_user_id', current_user_id: 1 do
|
|
|
|
it_behaves_like 'a validation without errors'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for required => false' do
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
let(:performing_user) { create(:agent) }
|
2019-03-13 23:51:22 +00:00
|
|
|
|
|
|
|
before { UserInfo.current_user_id = performing_user.id }
|
|
|
|
|
|
|
|
context 'for applying permission' do
|
|
|
|
|
|
|
|
let(:permission) { 'ticket.agent' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
attribute.screens = {
|
|
|
|
action => {
|
|
|
|
permission => {
|
|
|
|
required: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action is edit' do
|
|
|
|
|
|
|
|
let(:action) { 'edit' }
|
2021-09-23 07:52:08 +00:00
|
|
|
let(:record) { create(:ticket_article) }
|
2019-03-13 23:51:22 +00:00
|
|
|
|
|
|
|
it_behaves_like 'a validation without errors'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action is create_...' do
|
|
|
|
|
|
|
|
let(:action) { 'create_middle' }
|
|
|
|
|
|
|
|
it_behaves_like 'a validation without errors'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for not applying permission' do
|
|
|
|
|
|
|
|
let(:permission) { 'ticket.customer' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
attribute.screens = {
|
|
|
|
action => {
|
|
|
|
permission => {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action is edit' do
|
|
|
|
|
|
|
|
let(:action) { 'edit' }
|
2021-09-23 07:52:08 +00:00
|
|
|
let(:record) { create(:ticket_article) }
|
2019-03-13 23:51:22 +00:00
|
|
|
|
|
|
|
it_behaves_like 'a validation without errors'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action is create_...' do
|
|
|
|
|
|
|
|
let(:action) { 'create_middle' }
|
|
|
|
|
|
|
|
it_behaves_like 'a validation without errors'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|