|
|
|
@ -54,12 +54,11 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
let(:schema_writable) { true } |
|
|
|
|
let(:model) { build_stubbed(:work_package) } |
|
|
|
|
let(:schema) do |
|
|
|
|
double('WorkPackageSchema', |
|
|
|
|
project_id: 42, |
|
|
|
|
model:, |
|
|
|
|
defines_assignable_values?: true, |
|
|
|
|
available_custom_fields: [custom_field], |
|
|
|
|
writable?: schema_writable) |
|
|
|
|
instance_double(API::V3::WorkPackages::Schema::SpecificWorkPackageSchema, |
|
|
|
|
project_id: 42, |
|
|
|
|
model:, |
|
|
|
|
available_custom_fields: [custom_field], |
|
|
|
|
writable?: schema_writable) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
subject { modified_class.new(schema, current_user: nil, form_embedded: true).to_json } |
|
|
|
@ -112,7 +111,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
# meaning they won't as no values are specified |
|
|
|
|
it_behaves_like 'indicates length requirements' |
|
|
|
|
|
|
|
|
|
context 'custom field is not required' do |
|
|
|
|
context 'when custom field is not required' do |
|
|
|
|
let(:custom_field) { build(:custom_field, is_required: false) } |
|
|
|
|
|
|
|
|
|
it 'marks the field as not required' do |
|
|
|
@ -120,7 +119,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'custom field has regex' do |
|
|
|
|
context 'when custom field has regex' do |
|
|
|
|
let(:custom_field) { build(:custom_field, regexp: 'Foo+bar') } |
|
|
|
|
|
|
|
|
|
it 'renders the regular expression' do |
|
|
|
@ -128,7 +127,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'custom field has minimum length' do |
|
|
|
|
context 'when custom field has minimum length' do |
|
|
|
|
let(:custom_field) { build(:custom_field, min_length: 5) } |
|
|
|
|
|
|
|
|
|
it_behaves_like 'indicates length requirements' do |
|
|
|
@ -136,7 +135,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'custom field has maximum length' do |
|
|
|
|
context 'when custom field has maximum length' do |
|
|
|
|
let(:custom_field) { build(:custom_field, max_length: 5) } |
|
|
|
|
|
|
|
|
|
it_behaves_like 'indicates length requirements' do |
|
|
|
@ -295,11 +294,10 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
|
|
|
|
|
describe 'user custom field on new project' do |
|
|
|
|
let(:schema) do |
|
|
|
|
double('ProjectSchema', |
|
|
|
|
id: nil, |
|
|
|
|
model: Project.new, |
|
|
|
|
defines_assignable_values?: true, |
|
|
|
|
available_custom_fields: [custom_field]) |
|
|
|
|
instance_double(API::V3::WorkPackages::Schema::SpecificWorkPackageSchema, |
|
|
|
|
id: nil, |
|
|
|
|
model: Project.new, |
|
|
|
|
available_custom_fields: [custom_field]) |
|
|
|
|
end |
|
|
|
|
let(:custom_field) do |
|
|
|
|
build(:custom_field, |
|
|
|
@ -331,13 +329,14 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it 'on writing it sets on the represented' do |
|
|
|
|
expected = { custom_field.id => expected_setter } |
|
|
|
|
expect(represented) |
|
|
|
|
.to receive(custom_field.attribute_setter) |
|
|
|
|
.with(expected_setter) |
|
|
|
|
allow(represented).to receive(custom_field.attribute_setter) |
|
|
|
|
modified_class |
|
|
|
|
.new(represented, current_user: nil) |
|
|
|
|
.from_json({ cf_path => json_value }.to_json) |
|
|
|
|
|
|
|
|
|
expect(represented) |
|
|
|
|
.to have_received(custom_field.attribute_setter) |
|
|
|
|
.with(expected_setter) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
@ -348,7 +347,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
available_custom_fields: [custom_field], |
|
|
|
|
custom_field.attribute_name => value) |
|
|
|
|
end |
|
|
|
|
let(:custom_value) { double('CustomValue', value: raw_value, typed_value:) } |
|
|
|
|
let(:custom_value) { instance_double(CustomValue, value: raw_value, typed_value:) } |
|
|
|
|
let(:raw_value) { nil } |
|
|
|
|
let(:typed_value) { raw_value } |
|
|
|
|
let(:value) { '' } |
|
|
|
@ -361,7 +360,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
allow(represented).to receive(:custom_value_for).with(custom_field).and_return(custom_value) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'user custom field' do |
|
|
|
|
context 'for user custom field' do |
|
|
|
|
let(:raw_value) { value.id.to_s } |
|
|
|
|
let(:typed_value) { value } |
|
|
|
|
let(:field_format) { 'user' } |
|
|
|
@ -383,7 +382,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'value is nil' do |
|
|
|
|
context 'when value is nil' do |
|
|
|
|
let(:value) { nil } |
|
|
|
|
let(:raw_value) { '' } |
|
|
|
|
|
|
|
|
@ -393,7 +392,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'version custom field' do |
|
|
|
|
context 'for version custom field' do |
|
|
|
|
let(:value) { build_stubbed(:version, id: 2) } |
|
|
|
|
let(:raw_value) { value.id.to_s } |
|
|
|
|
let(:typed_value) { value } |
|
|
|
@ -410,7 +409,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
expect(subject).to be_json_eql(value.name.to_json).at_path("_embedded/#{cf_path}/name") |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'value is nil' do |
|
|
|
|
context 'when value is nil' do |
|
|
|
|
let(:value) { nil } |
|
|
|
|
let(:raw_value) { '' } |
|
|
|
|
|
|
|
|
@ -420,7 +419,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'list custom field' do |
|
|
|
|
context 'for list custom field' do |
|
|
|
|
let(:value) { build_stubbed(:custom_option) } |
|
|
|
|
let(:typed_value) { value.value } |
|
|
|
|
let(:raw_value) { value.id.to_s } |
|
|
|
@ -432,7 +431,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
let(:title) { value.value } |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'value is nil' do |
|
|
|
|
context 'when value is nil' do |
|
|
|
|
let(:value) { nil } |
|
|
|
|
let(:raw_value) { '' } |
|
|
|
|
let(:typed_value) { '' } |
|
|
|
@ -442,7 +441,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'value is some invalid string' do |
|
|
|
|
context 'when value is some invalid string' do |
|
|
|
|
let(:value) { 'some invalid string' } |
|
|
|
|
let(:raw_value) { 'some invalid string' } |
|
|
|
|
let(:typed_value) { 'some invalid string not found' } |
|
|
|
@ -461,7 +460,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'string custom field' do |
|
|
|
|
context 'for string custom field' do |
|
|
|
|
it_behaves_like 'injects property custom field' do |
|
|
|
|
let(:field_format) { 'string' } |
|
|
|
|
let(:value) { 'Foobar' } |
|
|
|
@ -470,7 +469,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'int custom field' do |
|
|
|
|
context 'for int custom field' do |
|
|
|
|
it_behaves_like 'injects property custom field' do |
|
|
|
|
let(:field_format) { 'int' } |
|
|
|
|
let(:value) { 42 } |
|
|
|
@ -479,7 +478,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'float custom field' do |
|
|
|
|
context 'for float custom field' do |
|
|
|
|
it_behaves_like 'injects property custom field' do |
|
|
|
|
let(:field_format) { 'float' } |
|
|
|
|
let(:value) { 3.14 } |
|
|
|
@ -488,7 +487,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'bool custom field' do |
|
|
|
|
context 'for bool custom field' do |
|
|
|
|
it_behaves_like 'injects property custom field' do |
|
|
|
|
let(:field_format) { 'bool' } |
|
|
|
|
let(:value) { true } |
|
|
|
@ -497,16 +496,16 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'date custom field' do |
|
|
|
|
context 'for date custom field' do |
|
|
|
|
it_behaves_like 'injects property custom field' do |
|
|
|
|
let(:field_format) { 'date' } |
|
|
|
|
let(:value) { Date.today.to_date } |
|
|
|
|
let(:value) { Date.current } |
|
|
|
|
let(:json_value) { value.to_date.iso8601 } |
|
|
|
|
let(:expected_setter) { json_value } |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'text custom field' do |
|
|
|
|
context 'for text custom field' do |
|
|
|
|
it_behaves_like 'injects property custom field' do |
|
|
|
|
let(:field_format) { 'text' } |
|
|
|
|
let(:value) { '**Foobar**' } |
|
|
|
@ -530,7 +529,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
let(:represented) do |
|
|
|
|
double('represented', available_custom_fields: [custom_field]) |
|
|
|
|
end |
|
|
|
|
let(:custom_value) { double('CustomValue', value:, typed_value:) } |
|
|
|
|
let(:custom_value) { instance_double(CustomValue, value:, typed_value:) } |
|
|
|
|
let(:value) { '' } |
|
|
|
|
let(:user) { build_stubbed(:user) } |
|
|
|
|
let(:typed_value) { value } |
|
|
|
@ -542,7 +541,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
allow(represented).to receive(custom_field.attribute_getter).and_return(typed_value) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'reading' do |
|
|
|
|
context 'for reading' do |
|
|
|
|
let(:value) { '2' } |
|
|
|
|
let(:field_format) { 'user' } |
|
|
|
|
|
|
|
|
@ -558,7 +557,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'value is nil' do |
|
|
|
|
context 'when value is nil' do |
|
|
|
|
let(:value) { nil } |
|
|
|
|
let(:typed_value) { nil } |
|
|
|
|
|
|
|
|
@ -568,7 +567,7 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'writing' do |
|
|
|
|
context 'for writing' do |
|
|
|
|
let(:value) { nil } |
|
|
|
|
let(:field_format) { 'user' } |
|
|
|
|
|
|
|
|
@ -578,8 +577,10 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
json = { cf_path => { href: path } }.to_json |
|
|
|
|
expected = ['2'] |
|
|
|
|
|
|
|
|
|
expect(represented).to receive(custom_field.attribute_setter).with(expected) |
|
|
|
|
allow(represented).to receive(custom_field.attribute_setter) |
|
|
|
|
modified_class.new(represented, current_user: nil).from_json(json) |
|
|
|
|
|
|
|
|
|
expect(represented).to have_received(custom_field.attribute_setter).with(expected) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
@ -587,8 +588,10 @@ describe API::V3::Utilities::CustomFieldInjector do |
|
|
|
|
json = { cf_path => { href: nil } }.to_json |
|
|
|
|
expected = [] |
|
|
|
|
|
|
|
|
|
expect(represented).to receive(custom_field.attribute_setter).with(expected) |
|
|
|
|
allow(represented).to receive(custom_field.attribute_setter) |
|
|
|
|
modified_class.new(represented, current_user: nil).from_json(json) |
|
|
|
|
|
|
|
|
|
expect(represented).to have_received(custom_field.attribute_setter).with(expected) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|