Commit dd5c2677 authored by Sergey Gernyak's avatar Sergey Gernyak

Refactor SessionManager for multiaccounting

parent 03ffe1d2
......@@ -6,7 +6,7 @@ module BetfairApiNgRails
class Connection
attr_reader :account_name
delegate :app_key, to: :account
delegate :app_key, :username, to: :account
def initialize(account_name)
@account_name = account_name
......@@ -24,7 +24,7 @@ module BetfairApiNgRails
include Api::ConnectionExt::Formatting
def expire_provider
@_provider = nil
BetfairApiNgRails::Api::SessionManager.expire_ssoid username
end
protected
......@@ -34,7 +34,7 @@ module BetfairApiNgRails
end
def request_ssoid
BetfairApiNgRails::Api::SessionManager.new_ssoid
BetfairApiNgRails::Api::SessionManager.get_ssoid account
end
def hashing(params)
......
require 'betfair_api_ng_rails/api/concerns/errorable'
require 'active_support/core_ext/module/delegation'
module BetfairApiNgRails
module Api
......@@ -7,45 +8,47 @@ module BetfairApiNgRails
include Api::Concerns::Errorable
include Api::Constants
def ssoid
@ssoid ||= fetch_ssoid
def get_ssoid(account)
username = account.username
ssoid = BetfairApiNgRails.account_session_manager.get username
ssoid || SsoidRequester.new(account).get
end
def request_ssoid
ssoid
!has_errors?
def expire_ssoid(account)
username = account.username
BetfairApiNgRails.account_session_manager.expire username
end
end
def expire_ssoid
@ssoid = nil
end
class SsoidRequester < Struct.new(:account)
delegate :username, to: :account
def new_ssoid
expire_ssoid
ssoid
def get
session_token.tap do |token|
store_session token
end
end
private
def fetch_ssoid
get_login_response.session_token.tap { |sid| send_keep_alive(sid) }
def store_session(token)
BetfairApiNgRails.account_session_manager.store account_session(token)
end
def get_login_response
http_requester.do_request
def account_session(token)
BetfairApiNgRails::AccountSession.new(username, token)
end
def send_keep_alive(sid)
return unless Api::Config.keep_alive_session
keep_alive_requester(sid).do_request
def session_token
get_login_response.session_token
end
def http_requester
@_http_requester ||= Api::Http::Factory.session_requester
def get_login_response
http_requester.do_request
end
def keep_alive_requester(sid)
Api::Http::Factory.keep_alive_requester sid
def http_requester
@_http_requester ||= Api::Http::Factory.session_requester
end
end
end
......
......@@ -2,8 +2,14 @@ require 'spec_helper'
describe BetfairApiNgRails::Api::Connection do
let(:acc_name) { 'user001' }
let(:asm) { double(:account_session_manager) }
let(:account) { BetfairApiNgRails::Account.new(acc_name, 'pass', 'nfl43n7fg834', 'crt', 'key') }
subject(:connection) { described_class.new(acc_name) }
before do
allow(BetfairApiNgRails).to receive(:account_session_manager).and_return asm
end
describe "#request" do
let(:provider) { double(:provider) }
let(:parser) { double(:parser, process: true) }
......@@ -26,9 +32,10 @@ describe BetfairApiNgRails::Api::Connection do
end
describe "#expire_provider" do
it "resets provider and ssoid" do
it "resets account's ssoid" do
allow(connection).to receive(:username).and_return acc_name
expect(BetfairApiNgRails::Api::SessionManager).to receive(:expire_ssoid).with(acc_name)
connection.expire_provider
expect(connection.instance_variable_get(:@_provider)).to be_nil
end
end
......@@ -49,8 +56,12 @@ describe BetfairApiNgRails::Api::Connection do
end
describe "#request_ssoid" do
before do
allow(connection).to receive(:account).and_return account
end
it "requests new ssoid for new connection" do
expect(BetfairApiNgRails::Api::SessionManager).to receive(:new_ssoid).at_least(:once)
expect(BetfairApiNgRails::Api::SessionManager).to receive(:get_ssoid).with(account)
subject.send(:request_ssoid)
end
end
......
require 'spec_helper'
describe BetfairApiNgRails::Api::SessionManager do
subject { described_class }
describe "#ssoid" do
it "calls fetching ssoid" do
expect(subject).to receive(:fetch_ssoid)
subject.ssoid
end
end
let(:username) { 'user001' }
let(:asm) { double(:account_session_manager) }
let(:account) { BetfairApiNgRails::Account.new(username, 'password', '3cnt4ngt8oh3co', 'crt', 'key') }
describe "#request_ssoid" do
before(:each) { expect(subject).to receive(:ssoid) }
subject(:session_manager) { described_class }
context 'when no error occured' do
before(:each) { expect(subject).to receive(:has_errors?).and_return false }
its(:request_ssoid) { is_expected.to eq true }
before do
allow(BetfairApiNgRails).to receive(:account_session_manager).and_return asm
end
describe '.get_ssoid' do
before do
allow(BetfairApiNgRails).to receive_message_chain(:account_session_manager, :get).and_return ssoid
end
context 'when at least one error occured' do
before(:each) { expect(subject).to receive(:has_errors?).and_return true }
its(:request_ssoid) { is_expected.to eq false }
context 'when ASM has ssoid for passed account' do
let(:ssoid) { '2mf348f3fm3f3' }
it 'returns this one' do
expect(session_manager.get_ssoid(account)).to eq ssoid
end
end
end
context 'when ASM has not ssoid' do
let(:ssoid) { nil }
describe "#expire_ssoid" do
it "sets instance var ssoid to nil" do
subject.instance_variable_set :@ssoid, :value
expect(subject.ssoid).to eq :value
subject.expire_ssoid
expect(subject.instance_variable_get(:@ssoid)).to eq nil
it 'requests new ssoid and returns it' do
expect_any_instance_of(BetfairApiNgRails::Api::SessionManager::SsoidRequester).to receive(:session_token).and_return ssoid
expect(asm).to receive(:store)
expect(session_manager.get_ssoid(account)).to eq ssoid
end
end
end
describe "#new_ssoid" do
it "expires ssoid and calls fetching new one" do
expect(subject).to receive(:expire_ssoid)
expect(subject).to receive(:ssoid)
subject.new_ssoid
describe '.expire_ssoid' do
it 'expires ssoid for passed account' do
expect(asm).to receive(:expire).with username
session_manager.expire_ssoid account
end
end
end
\ No newline at end of file
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment