Skip to content

Commit

Permalink
Refactorizo la lógica de cacheo en disco del ta y lo paso a tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
eeng committed Aug 20, 2018
1 parent 87bf7e7 commit a52b135
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 74 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pkg/*
.bundle
spec/manual/*.key
spec/manual/*.crt
.DS_Store
.DS_Store
tmp
40 changes: 24 additions & 16 deletions lib/afipws/wsaa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ def initialize options = {}
@ttl = options[:ttl] || 2400
@cuit = options[:cuit]
@client = Client.new Hash(options[:savon]).reverse_merge(wsdl: WSDL[@env])
@ta_path = File.join(Dir.pwd, 'storage', "#{@cuit}-#{@env}-ta.dump")

restore_ta
@ta_path = File.join(Dir.pwd, 'tmp', "#{@cuit}-#{@env}-ta.dump")
end

def generar_tra service, ttl
Expand All @@ -28,7 +26,6 @@ def generar_tra service, ttl
xml.header do
xml.uniqueId Time.now.to_i
xml.generationTime xsd_datetime Time.now - ttl
# TODO me parece que no le da mucha bola el WS al expirationTime
xml.expirationTime xsd_datetime Time.now + ttl
end
xml.service service
Expand All @@ -52,25 +49,36 @@ def tra key, cert, service, ttl
def login
response = @client.raw_request :login_cms, in0: tra(@key, @cert, @service, @ttl)
ta = Nokogiri::XML(Nokogiri::XML(response.to_xml).text)
ta = { token: ta.css('token').text, sign: ta.css('sign').text,
{
token: ta.css('token').text,
sign: ta.css('sign').text,
generation_time: from_xsd_datetime(ta.css('generationTime').text),
expiration_time: from_xsd_datetime(ta.css('expirationTime').text) }
persist_ta(ta)
ta
expiration_time: from_xsd_datetime(ta.css('expirationTime').text)
}
rescue Savon::SOAPFault => f
raise WSError, f.message
end

# Obtiene un TA, lo cachea hasta que expire, y devuelve el hash Auth listo para pasarle al Client
# en los otros WS.
# Obtiene un TA, lo cachea hasta que expire, y devuelve el hash Auth listo para pasarle al Client en los otros WS
def auth
@ta = login if ta_expirado?
{ auth: { token: @ta[:token], sign: @ta[:sign], cuit: @cuit } }
ta = obtener_y_cachear_ta
{auth: {token: ta[:token], sign: ta[:sign], cuit: @cuit}}
end

private
def ta_expirado?
@ta.nil? or @ta[:expiration_time] <= Time.now

# Previene el error 'El CEE ya posee un TA valido para el acceso al WSN solicitado' que se genera cuando se pide el token varias veces en poco tiempo
def obtener_y_cachear_ta
@ta ||= restore_ta
if ta_expirado? @ta
@ta = login
persist_ta @ta
end
@ta
end

def ta_expirado? ta
ta.nil? || ta[:expiration_time] <= Time.now
end

def xsd_datetime time
Expand All @@ -82,10 +90,10 @@ def from_xsd_datetime str
end

def restore_ta
@ta = Marshal.load(File.read(@ta_path)) if File.exists?(@ta_path)
Marshal.load(File.read(@ta_path)) if File.exists?(@ta_path)
end

def persist_ta(ta)
def persist_ta ta
dirname = File.dirname(@ta_path)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
Expand Down
3 changes: 1 addition & 2 deletions lib/afipws/wsfe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class WSFE
def initialize options = {}
@env = (options[:env] || :test).to_sym
@wsaa = options[:wsaa] || WSAA.new(options.merge(service: 'wsfe'))
ssl_version = :TLSv1
@client = Client.new Hash(options[:savon]).reverse_merge(wsdl: WSDL[@env], ssl_version: ssl_version, convert_request_keys_to: :camelcase)
@client = Client.new Hash(options[:savon]).reverse_merge(wsdl: WSDL[@env], ssl_version: :TLSv1, convert_request_keys_to: :camelcase)
end

def dummy
Expand Down
20 changes: 14 additions & 6 deletions spec/afipws/wsaa_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,21 @@
end

context "auth" do
before { Time.stubs(:now).returns(now = Time.local(2010,1,1)) }
before do
FileUtils.rm_rf Dir.glob('tmp/*ta.dump')
Time.stubs(:now).returns(Time.local(2010, 1, 1))
end

it "debería cachear TA" do
subject.expects(:login).once.returns(ta = {token: 'token', sign: 'sign', expiration_time: Time.now + 60})
subject.auth
subject.auth
subject.ta.should equal ta
it "debería cachear TA en la instancia y disco" do
ws = Afipws::WSAA.new
ws.expects(:login).once.returns(ta = {token: 'token', sign: 'sign', expiration_time: Time.now + 60})
ws.auth
ws.auth
ws.ta.should equal ta

ws = Afipws::WSAA.new
ws.auth
ws.ta.should == ta
end

it "si el TA expiró debería ejecutar solicitar uno nuevo" do
Expand Down
Loading

0 comments on commit a52b135

Please sign in to comment.