Skip to content
This repository was archived by the owner on Apr 27, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/shopify_transporter/exporters/magento/soap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@ def call_with_retries(method, params, retry_count = 0)

def soap_client
@soap_client ||= Savon.client(
wsdl: "#{@hostname}/api/v2_soap?wsdl",
wsdl: "#{hostname_with_scheme}/api/v2_soap?wsdl",
open_timeout: 500,
read_timeout: 500,
)
end

def hostname_with_scheme
URI.parse(@hostname).scheme.present? ? @hostname : 'https://' + @hostname
end

def soap_session_id
return @soap_session_id if @soap_session_id.present?

Expand Down
70 changes: 57 additions & 13 deletions spec/shopify_transporter/exporters/magento/soap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module ShopifyTransporter
module Exporters
module Magento
RSpec.describe Soap do
let(:init_params) do
let(:init_params_https) do
{
hostname: 'https://example.com',
username: 'testuser',
Expand All @@ -14,9 +14,27 @@ module Magento
}
end

def stub_client_call(mock_client)
let(:init_params_http) do
{
hostname: 'http://example.com',
username: 'testuser',
api_key: 'testapikey',
batch_config: {},
}
end

let(:init_params_no_scheme) do
{
hostname: 'example.com',
username: 'testuser',
api_key: 'testapikey',
batch_config: {},
}
end

def stub_client_call(mock_client, scheme: 'https')
expect(Savon).to receive(:client).with(
wsdl: "https://example.com/api/v2_soap?wsdl",
wsdl: "#{scheme}://example.com/api/v2_soap?wsdl",
open_timeout: 500,
read_timeout: 500,
).and_return(mock_client)
Expand All @@ -39,11 +57,37 @@ def stub_login_call(mock_client, body: { login_response: { login_return: '123' }
end

describe '#call' do
context 'determining host connection protocol' do
it 'passes along https if it is provided' do
mock_client = spy('mock_client')
stub_client_call(mock_client, scheme: 'https')
stub_login_call(mock_client)

Soap.new(init_params_https).call(:test_call, {})
end

it 'passes along http if it is provided' do
mock_client = spy('mock_client')
stub_client_call(mock_client, scheme: 'http')
stub_login_call(mock_client)

Soap.new(init_params_http).call(:test_call, {})
end

it 'assumes https if no scheme is provided' do
mock_client = spy('mock_client')
stub_client_call(mock_client, scheme: 'https')
stub_login_call(mock_client)

Soap.new(init_params_no_scheme).call(:test_call, {})
end
end

it 'raises FailedLoginError if login response does not contain a session id' do
mock_client = spy('mock_client')
stub_client_call(mock_client)

expect { Soap.new(init_params).call(:test_call, {}) }.to raise_error(Soap::FailedLoginError)
expect { Soap.new(init_params_https).call(:test_call, {}) }.to raise_error(Soap::FailedLoginError)
end

it 'raises FailedLoginError with the right message and format' do
Expand All @@ -53,23 +97,23 @@ def stub_login_call(mock_client, body: { login_response: { login_return: '123' }

expected_error_message = "Unable to obtain SOAP session ID from server.\n\nDetails:\n{:not_the_right_key=>0}"

expect { Soap.new(init_params).call(:test_call, {}) }.to raise_error(Soap::FailedLoginError, expected_error_message)
expect { Soap.new(init_params_https).call(:test_call, {}) }.to raise_error(Soap::FailedLoginError, expected_error_message)
end

it 'creates a session correctly if the login response has keys :login_response and :login_return' do
mock_client = spy('mock_client')
stub_client_call(mock_client)
stub_login_call(mock_client)

Soap.new(init_params).call(:test_call, {})
Soap.new(init_params_https).call(:test_call, {})
end

it 'creates a session correctly if login response has keys :login_response_param and :result' do
mock_client = spy('mock_client')
stub_client_call(mock_client)
stub_login_call(mock_client, body: { login_response_param: { result: '456' } })

Soap.new(init_params).call(:test_call, {})
Soap.new(init_params_https).call(:test_call, {})
end

it 'calls Savon with the fn, session_id and params' do
Expand All @@ -84,15 +128,15 @@ def stub_login_call(mock_client, body: { login_response: { login_return: '123' }
},
)

Soap.new(init_params).call(:test_call, {})
Soap.new(init_params_https).call(:test_call, {})
end

it 'retries soap calls up to 4 times with a delay when there is a savon error' do
mock_client = spy('mock_client')
stub_client_call(mock_client)
stub_login_call(mock_client)

soap_instance = Soap.new(init_params)
soap_instance = Soap.new(init_params_https)

retries = 0
expect(mock_client).to receive(:call).with(
Expand Down Expand Up @@ -130,7 +174,7 @@ def expected_batching_filter(batch_key, batch_range_string)
end

it 'returns an enumerator with the results of #call for each batch specified' do
soap_client = Soap.new(init_params.merge(
soap_client = Soap.new(init_params_https.merge(
batch_config: {
'first_id' => 0,
'last_id' => 7,
Expand All @@ -151,7 +195,7 @@ def expected_batching_filter(batch_key, batch_range_string)
end

it 'works when first id and last id are the same' do
soap_client = Soap.new(init_params.merge(
soap_client = Soap.new(init_params_https.merge(
batch_config: {
'first_id' => 0,
'last_id' => 0,
Expand All @@ -170,7 +214,7 @@ def expected_batching_filter(batch_key, batch_range_string)
end

it 'correctly merges params passed in with the batching filter' do
soap_client = Soap.new(init_params.merge(
soap_client = Soap.new(init_params_https.merge(
batch_config: {
'first_id' => 0,
'last_id' => 0,
Expand Down Expand Up @@ -202,7 +246,7 @@ def expected_batching_filter(batch_key, batch_range_string)
end

it 'skips the batch and continues with the next batch if call raises an error' do
soap_client = Soap.new(init_params.merge(
soap_client = Soap.new(init_params_https.merge(
batch_config: {
'first_id' => 0,
'last_id' => 7,
Expand Down