diff --git a/app/controllers/object_manager_attributes_controller.rb b/app/controllers/object_manager_attributes_controller.rb index f1454cc98..fb563e9b0 100644 --- a/app/controllers/object_manager_attributes_controller.rb +++ b/app/controllers/object_manager_attributes_controller.rb @@ -32,7 +32,6 @@ class ObjectManagerAttributesController < ApplicationController raise Exceptions::UnprocessableEntity, 'already exists' if exists local_params = params.permit!.to_h - local_params[:data_option][:null] = true # set data option which can be null begin object_manager_attribute = ObjectManager::Attribute.add( object: local_params[:object], @@ -123,11 +122,9 @@ class ObjectManagerAttributesController < ApplicationController if params[:data_option][:null].nil? params[:data_option][:null] = true end - if params[:data_option][:options].nil? params[:data_option][:options] = '' end - if params[:data_option][:relation].nil? params[:data_option][:relation] = '' end @@ -139,5 +136,6 @@ class ObjectManagerAttributesController < ApplicationController null: true } end + end end diff --git a/app/models/object_manager/attribute.rb b/app/models/object_manager/attribute.rb index 56ae893ed..0cb2f3d5a 100644 --- a/app/models/object_manager/attribute.rb +++ b/app/models/object_manager/attribute.rb @@ -281,9 +281,11 @@ possible types if !force %i[name display data_type position active].each do |key| next if record[key] == data[key] + record[:data_option_new] = data[:data_option] if data[:data_option] # bring the data options over as well, when there are changes to the fields above data[:to_config] = true break end + if record[:data_option] != data[:data_option] # do we need a database migration? diff --git a/test/controllers/object_manager_attributes_controller.rb b/test/controllers/object_manager_attributes_controller.rb new file mode 100644 index 000000000..7415ff49d --- /dev/null +++ b/test/controllers/object_manager_attributes_controller.rb @@ -0,0 +1,449 @@ +# rubocop:disable Lint/BooleanSymbol +require 'test_helper' +require 'rake' + +class ObjectManagerAttributesControllerTest < ActionDispatch::IntegrationTest + setup do + + # set accept header + @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' } + + roles = Role.where(name: %w[Admin Agent]) + groups = Group.all + + UserInfo.current_user_id = 1 + @admin = User.create_or_update( + login: 'tickets-admin', + firstname: 'Tickets', + lastname: 'Admin', + email: 'tickets-admin@example.com', + password: 'adminpw', + active: true, + roles: roles, + groups: groups, + ) + + end + + test 'add new ticket text object' do + credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw') + + post '/api/v1/object_manager_attributes', params: {}, headers: @headers + token = @response.headers['CSRF-TOKEN'] + + # 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.to_json, headers: @headers.merge('Authorization' => credentials) + assert_response(201) + result = JSON.parse(@response.body) + assert(result) + assert(result['data_option']['null']) + assert_equal(result['data_option']['null'], true) + assert_equal(result['name'], 'test1') + end + + test 'add new ticket text object - no default' do + credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw') + + post '/api/v1/object_manager_attributes', params: {}, headers: @headers + token = @response.headers['CSRF-TOKEN'] + + # 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.to_json, headers: @headers.merge('Authorization' => credentials) + assert_response(201) + result = JSON.parse(@response.body) + assert(result) + assert(result['data_option']['null']) + assert_equal(result['data_option']['null'], true) + assert_equal(result['name'], 'test2') + end + + test 'update ticket text object' do + credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw') + + # add a new object + object = ObjectManager::Attribute.add( + name: 'test3', + object: 'Ticket', + display: 'Test 3', + active: true, + data_type: 'input', + data_option: { + default: 'test', + type: 'text', + maxlength: 120, + null: 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 + } + } + }, + position: 1550, + editable: true + ) + + migration = ObjectManager::Attribute.migration_execute + assert_equal(migration, true) + + post "/api/v1/object_manager_attributes/#{object.id}", params: {}, headers: @headers + token = @response.headers['CSRF-TOKEN'] + + # parameters for updating + params = { + 'name': 'test4', + '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.to_json, headers: @headers.merge('Authorization' => credentials) + assert_response(200) + result = JSON.parse(@response.body) + assert(result) + assert(result['data_option']['null']) + assert_equal(result['name'], 'test4') + assert_equal(result['display'], 'Test 4') + end + + test 'add new ticket boolean object' do + credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw') + + post '/api/v1/object_manager_attributes', params: {}, headers: @headers + token = @response.headers['CSRF-TOKEN'] + + # 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.to_json, headers: @headers.merge('Authorization' => credentials) + assert_response(201) + result = JSON.parse(@response.body) + assert(result) + assert(result['data_option']['null']) + assert_equal(result['data_option']['null'], true) + assert_equal(result['name'], 'bool2') + end + + test 'add new user select object' do + credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw') + + post '/api/v1/object_manager_attributes', params: {}, headers: @headers + token = @response.headers['CSRF-TOKEN'] + + # 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.to_json, headers: @headers.merge('Authorization' => credentials) + assert_response(201) + result = JSON.parse(@response.body) + assert(result) + assert(result['data_option']['null']) + assert_equal(result['data_option']['null'], true) + assert_equal(result['name'], 'test5') + end + + test 'update user select object' do + credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw') + + # add a new object + object = ObjectManager::Attribute.add( + active: true, + data_option: { + options: { + key1: 'foo' + }, + null: true, + default: '', + }, + data_type: 'select', + display: 'Test 6', + id: 'c-204', + name: 'test6', + 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 + } + } + }, + position: 1550, + editable: true + ) + + migration = ObjectManager::Attribute.migration_execute + assert_equal(migration, true) + + post "/api/v1/object_manager_attributes/#{object.id}", params: {}, headers: @headers + token = @response.headers['CSRF-TOKEN'] + + # parameters for updating + params = { + active: true, + data_option: { + options: { + key1: 'foo', + key2: 'bar' + } + }, + data_type: 'select', + display: 'Test 7', + id: 'c-204', + name: 'test7', + 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.to_json, headers: @headers.merge('Authorization' => credentials) + assert_response(200) + result = JSON.parse(@response.body) + assert(result) + assert(result['data_option']['options']) + assert_equal(result['name'], 'test7') + assert_equal(result['display'], 'Test 7') + end + +end