123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113 |
- # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- RSpec.describe 'ObjectManager Attributes', type: :request do
- let(:admin) do
- create(:admin)
- end
- describe 'request handling' do
- it 'does add new ticket text object' do
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: {}, as: :json
- # token based on headers
- params = {
- name: 'test1',
- object: 'Ticket',
- display: 'Test 1',
- active: true,
- data_type: 'input',
- data_option: {
- default: 'test',
- type: 'text',
- maxlength: 120
- },
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- },
- id: 'c-196'
- }
- post '/api/v1/object_manager_attributes', params: params, as: :json
- expect(response).to have_http_status(:created)
- expect(json_response).to be_truthy
- expect(json_response['data_option']['null']).to be_truthy
- expect(json_response['data_option']['null']).to be(true)
- expect(json_response['name']).to eq('test1')
- end
- it 'does add new ticket text object - no default' do
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: {}, as: :json
- # token based on headers
- params = {
- name: 'test2',
- object: 'Ticket',
- display: 'Test 2',
- active: true,
- data_type: 'input',
- data_option: {
- type: 'text',
- maxlength: 120
- },
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- },
- id: 'c-196'
- }
- post '/api/v1/object_manager_attributes', params: params, as: :json
- expect(response).to have_http_status(:created)
- expect(json_response).to be_truthy
- expect(json_response['data_option']['null']).to be_truthy
- expect(json_response['data_option']['null']).to be(true)
- expect(json_response['name']).to eq('test2')
- end
- it 'does update ticket text object', db_strategy: :reset do
- # add a new object
- object = create(:object_manager_attribute_text)
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- authenticated_as(admin)
- post "/api/v1/object_manager_attributes/#{object.id}", params: {}, as: :json
- # parameters for updating
- params = {
- name: object.name,
- object: 'Ticket',
- display: 'Test 4',
- active: true,
- data_type: 'input',
- data_option: {
- default: 'test',
- type: 'text',
- maxlength: 120
- },
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- },
- id: 'c-196'
- }
- # update the object
- put "/api/v1/object_manager_attributes/#{object.id}", params: params, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_truthy
- expect(json_response['data_option']['null']).to be_truthy
- expect(json_response['name']).to eq(object.name)
- expect(json_response['display']).to eq('Test 4')
- end
- it 'does add new ticket boolean object' do
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: {}, as: :json
- # token based on headers
- params = {
- active: true,
- data_option: {
- options: {
- false: 'no',
- true: 'yes'
- }
- },
- data_type: 'boolean',
- display: 'Boolean 2',
- id: 'c-200',
- name: 'bool2',
- object: 'Ticket',
- screens: {
- create_middle: {
- 'ticket.agent' => {
- item_class: 'column',
- shown: true
- },
- 'ticket.customer' => {
- item_class: 'column',
- shown: true
- }
- },
- edit: {
- 'ticket.agent' => {
- shown: true
- },
- 'ticket.customer' => {
- shown: true
- }
- }
- }
- }
- post '/api/v1/object_manager_attributes', params: params, as: :json
- expect(response).to have_http_status(:created)
- expect(json_response).to be_truthy
- expect(json_response['data_option']['null']).to be_truthy
- expect(json_response['data_option']['null']).to be(true)
- expect(json_response['name']).to eq('bool2')
- end
- it 'does add new user select object' do
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: {}, as: :json
- # token based on headers
- params = {
- active: true,
- data_option: {
- options: {
- key1: 'foo'
- }
- },
- data_type: 'select',
- display: 'Test 5',
- id: 'c-204',
- name: 'test5',
- object: 'User',
- screens: {
- create: {
- 'admin.user' => {
- shown: true
- },
- 'ticket.agent' => {
- shown: true
- },
- 'ticket.customer' => {
- shown: true
- }
- },
- edit: {
- 'admin.user' => {
- shown: true
- },
- 'ticket.agent' => {
- shown: true
- }
- },
- view: {
- 'admin.user' => {
- shown: true
- },
- 'ticket.agent' => {
- shown: true
- },
- 'ticket.customer' => {
- shown: true
- }
- }
- }
- }
- post '/api/v1/object_manager_attributes', params: params, as: :json
- expect(response).to have_http_status(:created)
- expect(json_response).to be_truthy
- expect(json_response['data_option']['null']).to be_truthy
- expect(json_response['data_option']['null']).to be(true)
- expect(json_response['name']).to eq('test5')
- end
- it 'does update user select object', authenticated_as: -> { admin }, db_strategy: :reset do
- # add a new object
- object = create(:object_manager_attribute_text, object_name: 'User')
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- post "/api/v1/object_manager_attributes/#{object.id}", params: {}, as: :json
- # parameters for updating
- params = {
- active: true,
- data_option: {
- options: {
- key1: 'foo',
- key2: 'bar'
- }
- },
- data_type: 'select',
- display: 'Test 7',
- id: 'c-204',
- name: object.name,
- object: 'User',
- screens: {
- create: {
- 'admin.user' => {
- shown: true
- },
- 'ticket.agent' => {
- shown: true
- },
- 'ticket.customer' => {
- shown: true
- }
- },
- edit: {
- 'admin.user' => {
- shown: true
- },
- 'ticket.agent' => {
- shown: true
- }
- },
- view: {
- 'admin.user' => {
- shown: true
- },
- 'ticket.agent' => {
- shown: true
- },
- 'ticket.customer' => {
- shown: true
- }
- }
- }
- }
- # update the object
- put "/api/v1/object_manager_attributes/#{object.id}", params: params, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_truthy
- expect(json_response['data_option']['options']).to be_truthy
- expect(json_response['name']).to eq(object.name)
- expect(json_response['display']).to eq('Test 7')
- end
- it 'does converts string to boolean for default value for boolean data type with true (01)', db_strategy: :reset do
- params = {
- name: "customerdescription#{SecureRandom.uuid.tr('-', '_')}",
- object: 'Ticket',
- display: "custom description#{SecureRandom.uuid.tr('-', '_')}",
- active: true,
- data_type: 'boolean',
- data_option: {
- options: {
- true: '',
- false: '',
- },
- default: 'true',
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- }
- },
- id: 'c-201'
- }
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: params, as: :json
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- expect(response).to have_http_status(:created) # created
- expect(json_response).to be_truthy
- expect(json_response['data_option']['default']).to be_truthy
- expect(json_response['data_option']['default']).to be(true)
- expect(json_response['data_type']).to eq('boolean')
- end
- it 'does converts string to boolean for default value for boolean data type with false (02)', db_strategy: :reset do
- params = {
- name: "customerdescription_#{SecureRandom.uuid.tr('-', '_')}",
- object: 'Ticket',
- display: "custom description #{SecureRandom.uuid.tr('-', '_')}",
- active: true,
- data_type: 'boolean',
- data_option: {
- options: {
- true: '',
- false: '',
- },
- default: 'false',
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- }
- },
- }
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: params, as: :json
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- expect(response).to have_http_status(:created) # created
- expect(json_response).to be_truthy
- expect(json_response['data_option']['default']).to be_falsey
- expect(json_response['data_option']['default']).to be(false)
- expect(json_response['data_type']).to eq('boolean')
- end
- it 'does ticket attributes cannot be removed when it is referenced by an overview (03)', db_strategy: :reset do
- # 1. create a new ticket attribute and execute migration
- ObjectManager::Attribute.migration_execute
- params = {
- name: 'test_attribute_referenced_by_an_overview',
- object: 'Ticket',
- display: 'Test Attribute',
- active: true,
- data_type: 'input',
- data_option: {
- default: '',
- type: 'text',
- maxlength: 120,
- null: true,
- options: {},
- relation: ''
- },
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- },
- }
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: params, as: :json
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- # 2. create an overview that uses the attribute
- params = {
- name: 'test_overview',
- roles: Role.where(name: 'Agent').pluck(:name),
- condition: {
- 'ticket.state_id': {
- operator: 'is',
- value: Ticket::State.all.pluck(:id),
- },
- 'ticket.test_attribute_referenced_by_an_overview': {
- operator: 'contains',
- value: 'DUMMY'
- },
- },
- order: {
- by: 'created_at',
- direction: 'DESC',
- },
- view: {
- d: %w[title customer state created_at],
- s: %w[number title customer state created_at],
- m: %w[number title customer state created_at],
- view_mode_default: 's',
- },
- user_ids: [ '1' ],
- }
- if Overview.where('name like ?', '%test%').empty?
- post '/api/v1/overviews', params: params, as: :json
- expect(response).to have_http_status(:created)
- expect(Hash).to eq(json_response.class)
- expect('test_overview').to eq(json_response['name'])
- end
- # 3. attempt to delete the ticket attribute
- get '/api/v1/object_manager_attributes', as: :json
- expect(response).to have_http_status(:ok)
- target_attribute = json_response.select { |x| x['name'] == 'test_attribute_referenced_by_an_overview' && x['object'] == 'Ticket' }
- expect(target_attribute.size).to eq(1)
- target_id = target_attribute[0]['id']
- delete "/api/v1/object_manager_attributes/#{target_id}", as: :json
- expect(response).to have_http_status(:unprocessable_entity)
- expect(response.body).to include('Overview')
- expect(response.body).to include('test_overview')
- expect(response.body).to include('cannot be deleted!')
- end
- it 'does ticket attributes cannot be removed when it is referenced by a trigger (04)', db_strategy: :reset do
- # 1. create a new ticket attribute and execute migration
- ObjectManager::Attribute.migration_execute
- params = {
- name: 'test_attribute_referenced_by_a_trigger',
- object: 'Ticket',
- display: 'Test Attribute',
- active: true,
- data_type: 'input',
- data_option: {
- default: '',
- type: 'text',
- maxlength: 120,
- null: true,
- options: {},
- relation: ''
- },
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- },
- }
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: params, as: :json
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- # 2. create an trigger that uses the attribute
- params = {
- name: 'test_trigger',
- condition: {
- 'ticket.test_attribute_referenced_by_a_trigger': {
- operator: 'contains',
- value: 'DUMMY'
- }
- },
- perform: {
- 'ticket.state_id': {
- value: '2'
- }
- },
- active: true,
- id: 'c-3'
- }
- if Trigger.where('name like ?', '%test%').empty?
- post '/api/v1/triggers', params: params, as: :json
- expect(response).to have_http_status(:created)
- expect(Hash).to eq(json_response.class)
- expect('test_trigger').to eq(json_response['name'])
- end
- # 3. attempt to delete the ticket attribute
- get '/api/v1/object_manager_attributes', as: :json
- expect(response).to have_http_status(:ok)
- target_attribute = json_response.select { |x| x['name'] == 'test_attribute_referenced_by_a_trigger' && x['object'] == 'Ticket' }
- expect(target_attribute.size).to eq(1)
- target_id = target_attribute[0]['id']
- delete "/api/v1/object_manager_attributes/#{target_id}", as: :json
- expect(response).to have_http_status(:unprocessable_entity)
- expect(response.body).to include('Trigger')
- expect(response.body).to include('test_trigger')
- expect(response.body).to include('cannot be deleted!')
- end
- it 'does ticket attributes cannot be removed when it is referenced by a scheduler (05)', db_strategy: :reset do
- # 1. create a new ticket attribute and execute migration
- ObjectManager::Attribute.migration_execute
- params = {
- name: 'test_attribute_referenced_by_a_scheduler',
- object: 'Ticket',
- display: 'Test Attribute',
- active: true,
- data_type: 'input',
- data_option: {
- default: '',
- type: 'text',
- maxlength: 120,
- null: true,
- options: {},
- relation: ''
- },
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- },
- }
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: params, as: :json
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- # 2. create a scheduler that uses the attribute
- params = {
- name: 'test_scheduler',
- timeplan: {
- days: {
- Mon: true,
- Tue: false,
- Wed: false,
- Thu: false,
- Fri: false,
- Sat: false,
- Sun: false
- },
- hours: {
- '0': true,
- '1': false,
- '2': false,
- '3': false,
- '4': false,
- '5': false,
- '6': false,
- '7': false,
- '8': false,
- '9': false,
- '10': false,
- '11': false,
- '12': false,
- '13': false,
- '14': false,
- '15': false,
- '16': false,
- '17': false,
- '18': false,
- '19': false,
- '20': false,
- '21': false,
- '22': false,
- '23': false
- },
- minutes: {
- '0': true,
- '10': false,
- '20': false,
- '30': false,
- '40': false,
- '50': false
- }
- },
- condition: {
- 'ticket.test_attribute_referenced_by_a_scheduler': {
- operator: 'contains',
- value: 'DUMMY'
- }
- },
- perform: {
- 'ticket.state_id': {
- value: '2'
- }
- },
- disable_notification: true,
- note: '',
- active: true,
- id: 'c-0'
- }
- if Job.where('name like ?', '%test%').empty?
- post '/api/v1/jobs', params: params, as: :json
- expect(response).to have_http_status(:created)
- expect(Hash).to eq(json_response.class)
- expect('test_scheduler').to eq(json_response['name'])
- end
- # 3. attempt to delete the ticket attribute
- get '/api/v1/object_manager_attributes', as: :json
- expect(response).to have_http_status(:ok)
- target_attribute = json_response.select { |x| x['name'] == 'test_attribute_referenced_by_a_scheduler' && x['object'] == 'Ticket' }
- expect(target_attribute.size).to eq(1)
- target_id = target_attribute[0]['id']
- delete "/api/v1/object_manager_attributes/#{target_id}", as: :json
- expect(response).to have_http_status(:unprocessable_entity)
- expect(response.body).to include('Job')
- expect(response.body).to include('test_scheduler')
- expect(response.body).to include('cannot be deleted!')
- end
- it 'does ticket attributes can be removed when it is referenced by an overview but by user object (06)', db_strategy: :reset do
- # 1. create a new ticket attribute and execute migration
- ObjectManager::Attribute.migration_execute
- params = {
- name: 'test_attribute_referenced_by_an_overview',
- object: 'Ticket',
- display: 'Test Attribute',
- active: true,
- data_type: 'input',
- data_option: {
- default: '',
- type: 'text',
- maxlength: 120,
- null: true,
- options: {},
- relation: ''
- },
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- },
- }
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: params, as: :json
- params = {
- name: 'test_attribute_referenced_by_an_overview',
- object: 'User',
- display: 'Test Attribute',
- active: true,
- data_type: 'input',
- data_option: {
- default: '',
- type: 'text',
- maxlength: 120,
- null: true,
- options: {},
- relation: ''
- },
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- },
- }
- post '/api/v1/object_manager_attributes', params: params, as: :json
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- # 2. create an overview that uses the attribute
- params = {
- name: 'test_overview',
- roles: Role.where(name: 'Agent').pluck(:name),
- condition: {
- 'ticket.state_id': {
- operator: 'is',
- value: Ticket::State.all.pluck(:id),
- },
- 'ticket.test_attribute_referenced_by_an_overview': {
- operator: 'contains',
- value: 'DUMMY'
- },
- },
- order: {
- by: 'created_at',
- direction: 'DESC',
- },
- view: {
- d: %w[title customer state created_at],
- s: %w[number title customer state created_at],
- m: %w[number title customer state created_at],
- view_mode_default: 's',
- },
- user_ids: [ '1' ],
- }
- if Overview.where('name like ?', '%test%').empty?
- post '/api/v1/overviews', params: params, as: :json
- expect(response).to have_http_status(:created)
- expect(Hash).to eq(json_response.class)
- expect('test_overview').to eq(json_response['name'])
- end
- # 3. attempt to delete the ticket attribute
- get '/api/v1/object_manager_attributes', as: :json
- expect(response).to have_http_status(:ok)
- all_json_response = json_response
- target_attribute = all_json_response.select { |x| x['name'] == 'test_attribute_referenced_by_an_overview' && x['object'] == 'User' }
- expect(target_attribute.size).to eq(1)
- target_id = target_attribute[0]['id']
- delete "/api/v1/object_manager_attributes/#{target_id}", as: :json
- expect(response).to have_http_status(:ok)
- target_attribute = all_json_response.select { |x| x['name'] == 'test_attribute_referenced_by_an_overview' && x['object'] == 'Ticket' }
- expect(target_attribute.size).to eq(1)
- target_id = target_attribute[0]['id']
- delete "/api/v1/object_manager_attributes/#{target_id}", as: :json
- expect(response).to have_http_status(:unprocessable_entity)
- expect(response.body).to include('Overview')
- expect(response.body).to include('test_overview')
- expect(response.body).to include('cannot be deleted!')
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- end
- it 'does verify if attribute type can not be changed (07)', db_strategy: :reset do
- params = {
- name: "customerdescription_#{SecureRandom.uuid.tr('-', '_')}",
- object: 'Ticket',
- display: "custom description #{SecureRandom.uuid.tr('-', '_')}",
- active: true,
- data_type: 'boolean',
- data_option: {
- options: {
- true: '',
- false: '',
- },
- default: 'false',
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- }
- },
- }
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: params, as: :json
- expect(response).to have_http_status(:created) # created
- expect(json_response).to be_truthy
- expect(json_response['data_option']['default']).to be_falsey
- expect(json_response['data_option']['default']).to be(false)
- expect(json_response['data_type']).to eq('boolean')
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- params['data_type'] = 'input'
- params['data_option'] = {
- default: 'test',
- type: 'text',
- maxlength: 120
- }
- put "/api/v1/object_manager_attributes/#{json_response['id']}", params: params, as: :json
- expect(response).to have_http_status(:unprocessable_entity)
- expect(json_response).to be_truthy
- expect(json_response['error']).to be_truthy
- end
- it 'does verify if attribute type can be changed (08)', db_strategy: :reset do
- params = {
- name: "customerdescription_#{SecureRandom.uuid.tr('-', '_')}",
- object: 'Ticket',
- display: "custom description #{SecureRandom.uuid.tr('-', '_')}",
- active: true,
- data_type: 'input',
- data_option: {
- default: 'test',
- type: 'text',
- maxlength: 120,
- },
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- },
- },
- }
- authenticated_as(admin)
- post '/api/v1/object_manager_attributes', params: params, as: :json
- expect(response).to have_http_status(:created) # created
- expect(json_response).to be_truthy
- expect(json_response['data_option']['default']).to eq('test')
- expect(json_response['data_type']).to eq('input')
- migration = ObjectManager::Attribute.migration_execute
- expect(migration).to be(true)
- params['data_type'] = 'select'
- params['data_option'] = {
- default: 'fuu',
- options: {
- key1: 'foo',
- key2: 'fuu',
- }
- }
- put "/api/v1/object_manager_attributes/#{json_response['id']}", params: params, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_truthy
- expect(json_response['data_option']['default']).to eq('test')
- expect(json_response['data_option_new']['default']).to eq('fuu')
- expect(json_response['data_type']).to eq('select')
- end
- it "doesn't let to update item that doesn't exist", authenticated_as: -> { admin } do
- params = {
- active: true,
- data_option: {
- type: 'text',
- maxlength: 200
- },
- data_type: 'input',
- display: 'Test 7',
- name: 'attribute_that_doesnt_exist',
- object: 'User',
- }
- # update the object
- put '/api/v1/object_manager_attributes/abc', params: params, as: :json
- expect(response).to have_http_status(:unprocessable_entity)
- end
- context 'position handling', authenticated_as: -> { admin } do
- let(:base_params) do
- {
- name: "customerdescription_#{SecureRandom.uuid.tr('-', '_')}",
- object: 'Ticket',
- display: "custom description #{SecureRandom.uuid.tr('-', '_')}",
- active: true,
- data_type: 'input',
- data_option: {
- default: 'test',
- type: 'text',
- maxlength: 120,
- },
- }
- end
- let(:new_attribute_id) { json_response['id'] }
- let(:new_attribute_object) { ObjectManager::Attribute.find new_attribute_id }
- before { post '/api/v1/object_manager_attributes', params: params, as: :json }
- context 'when creating a new attribute' do
- let(:params) { base_params }
- context 'with no position attribute provided' do
- let(:maximum_position) do
- ObjectManager::Attribute
- .for_object(params[:object])
- .maximum(:position)
- end
- it 'defaults to the maximum available position' do
- expect(new_attribute_object.position).to eq maximum_position
- end
- end
- context 'with a position attribute given' do
- let(:position) { 50 }
- let(:params) { base_params.merge(position: position) }
- it 'defaults to given position' do
- expect(new_attribute_object.position).to eq position
- end
- end
- end
- context 'when updating an existing attribute' do
- let(:alternative_position) { 123 }
- let(:alternative_display) { 'another description' }
- let(:params) { base_params }
- let(:alternative_params) { base_params.merge(display: alternative_display) }
- before do
- new_attribute_object.update! position: alternative_position
- put "/api/v1/object_manager_attributes/#{new_attribute_id}", params: alternative_params, as: :json
- new_attribute_object.reload
- end
- # confirm that test build up was correct
- it 'request succeeds' do
- expect(new_attribute_object.display).to eq alternative_display
- end
- # https://github.com/zammad/zammad/issues/3044
- it 'position did not reset' do
- expect(new_attribute_object.position).to eq alternative_position
- end
- end
- end
- end
- end
|