Add tests, show papertrail whodunnit properly on repp api

This commit is contained in:
Karl Erik Õunapuu 2020-11-18 12:35:11 +02:00
parent 7d08e22f6f
commit 38f86225ca
No known key found for this signature in database
GPG key ID: C9DD647298A34764
3 changed files with 28 additions and 3 deletions

View file

@ -4,9 +4,10 @@ module Repp
rescue_from ActiveRecord::RecordNotFound, with: :not_found_error
before_action :authenticate_user
before_action :check_ip_restriction
before_action :set_paper_trail_whodunnit
attr_reader :current_user
before_action :set_paper_trail_whodunnit
rescue_from ActionController::ParameterMissing do |exception|
render json: { code: 2003, message: exception }, status: :bad_request
end
@ -23,6 +24,10 @@ module Repp
private
def set_paper_trail_whodunnit
::PaperTrail.request.whodunnit = current_user
end
def render_success(code: nil, message: nil, data: nil)
@response = { code: code || 1000, message: message || 'Command completed successfully',
data: data || {} }

View file

@ -1,10 +1,9 @@
module ObjectVersionsHelper
def attach_existing_fields(version, new_object)
destroy_event = version.event == 'destroy'
version.object_changes.to_h.each do |key, value|
method_name = "#{key}=".to_sym
if new_object.respond_to?(method_name)
new_object.public_send(method_name, destroy_event ? value.first : value.last)
new_object.public_send(method_name, event_value(event, value))
end
end
end
@ -13,4 +12,10 @@ module ObjectVersionsHelper
field_names = model.column_names
version.object.to_h.select { |key, _value| field_names.include?(key) }
end
private
def event_value(version, val)
version.event == 'destroy' ? val.first : val.last
end
end

View file

@ -101,4 +101,19 @@ class ReppV1ContactsUpdateTest < ActionDispatch::IntegrationTest
@contact.reload
assert @contact.legal_documents.any?
end
def test_returns_error_if_ident_wrong_format
request_body = {
"contact": {
"ident": "123"
}
}
put "/repp/v1/contacts/#{@contact.code}", headers: @auth_headers, params: request_body
json = JSON.parse(response.body, symbolize_names: true)
assert_response :bad_request
assert_equal 2308, json[:code]
assert_equal 'Ident update is not allowed. Consider creating new contact object', json[:message]
end
end