Decompose EppSession#data

#700
This commit is contained in:
Artur Beljajev 2018-02-07 02:17:48 +02:00
parent ba341ee411
commit 940613ae34
8 changed files with 13 additions and 57 deletions

2
.reek
View file

@ -538,7 +538,6 @@ IrresponsibleModule:
- DomainStatus
- DomainTransfer
- Epp::Contact
- EppSession
- Invoice
- InvoiceItem
- Keyrelay
@ -1027,7 +1026,6 @@ PrimaDonnaMethod:
- Contact
- Domain
- Epp::Domain
- EppSession
- RegistrantVerification
- Registrar
BooleanParameter:

View file

@ -655,11 +655,6 @@ Performance/StringReplacement:
- 'app/models/directo.rb'
- 'app/models/dnskey.rb'
# Offense count: 1
Security/MarshalLoad:
Exclude:
- 'app/models/epp_session.rb'
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.

View file

@ -91,7 +91,7 @@ class Epp::SessionsController < EppController
end
end
epp_session[:api_user_id] = @api_user.id
epp_session.user = @api_user
epp_session.update_column(:registrar_id, @api_user.registrar_id)
render_epp_response('login_success')
else

View file

@ -115,7 +115,7 @@ class EppController < ApplicationController
end
def current_user
@current_user ||= ApiUser.find_by_id(epp_session[:api_user_id])
@current_user ||= epp_session.user
# by default PaperTrail uses before filter and at that
# time current_user is not yet present
::PaperTrail.whodunnit = user_log_str(@current_user)

View file

@ -1,38 +1,6 @@
class EppSession < ActiveRecord::Base
before_save :marshal_data!
belongs_to :user, required: true
belongs_to :registrar
validates :session_id, presence: true
belongs_to :registrar
# rubocop: disable Rails/ReadWriteAttribute
# Turned back to read_attribute, thus in Rails 4
# there is differences between self[:data] and read_attribute.
def data
@data ||= self.class.unmarshal(read_attribute(:data)) || {}
end
# rubocop: enable Rails/ReadWriteAttribute
def [](key)
data[key.to_sym]
end
def []=(key, value)
data[key.to_sym] = value
save!
end
def marshal_data!
self.data = self.class.marshal(data)
end
class << self
def marshal(data)
::Base64.encode64(Marshal.dump(data)) if data
end
def unmarshal(data)
return data unless data.is_a? String
Marshal.load(::Base64.decode64(data)) if data
end
end
end

View file

@ -52,7 +52,7 @@ namespace :dev do
epp_session = EppSession.new
epp_session.session_id = 'test'
epp_session.registrar = registrar
epp_session[:api_user_id] = api_user.id
epp_session.user = api_user
epp_session.save!
domain_counter = 1.step

View file

@ -1,9 +1,9 @@
api_bestnames:
session_id: 1
user: api_bestnames
registrar: bestnames
data: <%= Base64.encode64(Marshal.dump({api_user_id: ActiveRecord::Fixtures.identify(:api_bestnames)})) %>
api_goodnames:
session_id: 2
user: api_goodnames
registrar: goodnames
data: <%= Base64.encode64(Marshal.dump({api_user_id: ActiveRecord::Fixtures.identify(:api_goodnames)})) %>

View file

@ -9,20 +9,15 @@ class EppSessionTest < ActiveSupport::TestCase
assert @epp_session.valid?
end
def test_api_user_id_serialization
epp_session = EppSession.new
epp_session.session_id = 'test'
epp_session.registrar = registrars(:bestnames)
epp_session[:api_user_id] = ActiveRecord::Fixtures.identify(:api_bestnames)
epp_session.save!
epp_session.reload
assert_equal ActiveRecord::Fixtures.identify(:api_bestnames), epp_session[:api_user_id]
end
def test_session_id_presence_validation
@epp_session.session_id = nil
@epp_session.validate
assert @epp_session.invalid?
end
def test_user_presence_validation
@epp_session.user = nil
@epp_session.validate
assert @epp_session.invalid?
end
end