mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-20 14:45:56 +08:00
Update ApiClient tests
Closes SCI-3434
This commit is contained in:
parent
8a7a2eb097
commit
06b8d1fda7
|
@ -80,11 +80,11 @@ module ProtocolImporters
|
|||
end
|
||||
|
||||
def api_errors
|
||||
"ProtocolImporters::#{endpoint_name}::V3Errors::Error".constantize
|
||||
"ProtocolImporters::#{endpoint_name}::Error".constantize
|
||||
end
|
||||
|
||||
def normalizer_errors
|
||||
"ProtocolImporters::#{endpoint_name}::V3Errors::NormalizerError".constantize
|
||||
"ProtocolImporters::#{endpoint_name}::NormalizerError".constantize
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -43,41 +43,41 @@ module ProtocolImporters
|
|||
# id of page.
|
||||
# Default is 1.
|
||||
def protocol_list(query_params = {})
|
||||
with_handle_errors do
|
||||
response = with_handle_network_errors do
|
||||
query = CONSTANTS.dig(:endpoints, :protocols, :default_query_params)
|
||||
.merge(query_params)
|
||||
|
||||
check_for_api_errors(self.class.get('/protocols', query: query))
|
||||
self.class.get('/protocols', query: query)
|
||||
end
|
||||
check_for_response_errors(response)
|
||||
end
|
||||
|
||||
# Returns full representation of given protocol ID
|
||||
def single_protocol(id)
|
||||
with_handle_errors do
|
||||
check_for_api_errors(self.class.get("/protocols/#{id}"))
|
||||
response = with_handle_network_errors do
|
||||
self.class.get("/protocols/#{id}")
|
||||
end
|
||||
check_for_response_errors(response)
|
||||
end
|
||||
|
||||
# Returns html preview for given protocol
|
||||
# This endpoint is outside the scope of API but is listed here for the
|
||||
# sake of clarity
|
||||
def protocol_html_preview(uri)
|
||||
with_handle_errors do
|
||||
with_handle_network_errors do
|
||||
self.class.get("https://www.protocols.io/view/#{uri}.html", headers: {})
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def with_handle_errors
|
||||
def with_handle_network_errors
|
||||
yield
|
||||
rescue SocketError, HTTParty::Error => e
|
||||
raise ProtocolImporters::ProtocolsIO::V3::NetworkError.new(:network_error), e.message
|
||||
rescue StandardError => e
|
||||
raise ProtocolImporters::ProtocolsIO::V3::Error.new(e.class.to_s.downcase.to_sym), e.message
|
||||
raise ProtocolImporters::ProtocolsIO::V3::NetworkError.new(e.class), e.message
|
||||
end
|
||||
|
||||
def check_for_api_errors(response)
|
||||
def check_for_response_errors(response)
|
||||
error_message = response.parsed_response['error_message']
|
||||
|
||||
case response.parsed_response['status_code']
|
||||
|
@ -90,7 +90,7 @@ module ProtocolImporters
|
|||
when 1219
|
||||
raise ProtocolImporters::ProtocolsIO::V3::UnauthorizedError.new(:token_expires), error_message
|
||||
else
|
||||
raise ProtocolImporters::ProtocolsIO::V3::Error.new(:api_error), error_message
|
||||
raise ProtocolImporters::ProtocolsIO::V3::Error.new(e.class), error_message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -26,7 +26,7 @@ describe ProtocolImporters::BuildProtocolFromClientService do
|
|||
it 'return api errors' do
|
||||
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ApiClient)
|
||||
.to(receive(:single_protocol)
|
||||
.and_raise(ProtocolImporters::ProtocolsIO::V3::V3Errors::ArgumentError
|
||||
.and_raise(ProtocolImporters::ProtocolsIO::V3::ArgumentError
|
||||
.new(:missing_or_empty_parameters), 'Missing Or Empty Parameters Error'))
|
||||
|
||||
expect(service_call.errors).to have_key(:missing_or_empty_parameters)
|
||||
|
@ -43,7 +43,7 @@ describe ProtocolImporters::BuildProtocolFromClientService do
|
|||
|
||||
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ProtocolNormalizer)
|
||||
.to(receive(:normalize_protocol).with(client_data)
|
||||
.and_raise(ProtocolImporters::ProtocolsIO::V3::V3Errors::NormalizerError.new(:nil_protocol), 'Nil Protocol'))
|
||||
.and_raise(ProtocolImporters::ProtocolsIO::V3::NormalizerError.new(:nil_protocol), 'Nil Protocol'))
|
||||
|
||||
expect(service_call.errors).to have_key(:nil_protocol)
|
||||
end
|
||||
|
|
|
@ -36,7 +36,7 @@ describe ProtocolImporters::SearchProtocolsService do
|
|||
it 'return api errors' do
|
||||
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ApiClient)
|
||||
.to(receive(:protocol_list)
|
||||
.and_raise(ProtocolImporters::ProtocolsIO::V3::V3Errors::ArgumentError
|
||||
.and_raise(ProtocolImporters::ProtocolsIO::V3::ArgumentError
|
||||
.new(:missing_or_empty_parameters), 'Missing Or Empty Parameters Error'))
|
||||
|
||||
expect(service_call.errors).to have_key(:missing_or_empty_parameters)
|
||||
|
@ -53,7 +53,7 @@ describe ProtocolImporters::SearchProtocolsService do
|
|||
|
||||
allow_any_instance_of(ProtocolImporters::ProtocolsIO::V3::ProtocolNormalizer)
|
||||
.to(receive(:normalize_list).with(client_data)
|
||||
.and_raise(ProtocolImporters::ProtocolsIO::V3::V3Errors::NormalizerError.new(:nil_protocol), 'Nil Protocol'))
|
||||
.and_raise(ProtocolImporters::ProtocolsIO::V3::NormalizerError.new(:nil_protocol), 'Nil Protocol'))
|
||||
|
||||
expect(service_call.errors).to have_key(:nil_protocol)
|
||||
end
|
||||
|
|
|
@ -11,6 +11,9 @@ describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
|
|||
|
||||
let(:stub_protocols) do
|
||||
stub_request(:get, URL).with(query: hash_including({}))
|
||||
.to_return(status: 200,
|
||||
body: JSON.generate(status_code: 0),
|
||||
headers: { 'Content-Type': 'application/json' })
|
||||
end
|
||||
|
||||
let(:default_query_params) do
|
||||
|
@ -18,20 +21,46 @@ describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
|
|||
end
|
||||
|
||||
it 'returns 200 on successfull call' do
|
||||
stub_protocols.to_return(status: 200, body: '[]', headers: {})
|
||||
|
||||
stub_protocols
|
||||
expect(subject.protocol_list.code).to eq 200
|
||||
expect(stub_protocols).to have_been_requested
|
||||
end
|
||||
|
||||
it 'raises OpenTimeout error on timeout' do
|
||||
stub_protocols.to_timeout
|
||||
it 'raises NetworkError on timeout' do
|
||||
stub_request(:get, URL).with(query: hash_including({})).to_timeout
|
||||
|
||||
expect { subject.protocol_list }.to raise_error(Net::OpenTimeout)
|
||||
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::NetworkError)
|
||||
end
|
||||
|
||||
it 'raises ArgumentError when status_code = 1' do
|
||||
stub_request(:get, URL).with(query: hash_including({}))
|
||||
.to_return(status: 200,
|
||||
body: JSON.generate(status_code: 1, error_message: 'Argument error'),
|
||||
headers: { 'Content-Type': 'application/json' })
|
||||
|
||||
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::ArgumentError)
|
||||
end
|
||||
|
||||
it 'raises UnauthorizedError when status_code = 1218' do
|
||||
stub_request(:get, URL).with(query: hash_including({}))
|
||||
.to_return(status: 200,
|
||||
body: JSON.generate(status_code: 1218, error_message: 'Argument error'),
|
||||
headers: { 'Content-Type': 'application/json' })
|
||||
|
||||
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::UnauthorizedError)
|
||||
end
|
||||
|
||||
it 'raises UnauthorizedError when status_code = 1219' do
|
||||
stub_request(:get, URL).with(query: hash_including({}))
|
||||
.to_return(status: 200,
|
||||
body: JSON.generate(status_code: 1219, error_message: 'Argument error'),
|
||||
headers: { 'Content-Type': 'application/json' })
|
||||
|
||||
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::UnauthorizedError)
|
||||
end
|
||||
|
||||
it 'requests server with default query parameters if none are given' do
|
||||
stub_request(:get, URL).with(query: default_query_params)
|
||||
stub_protocols.with(query: default_query_params)
|
||||
|
||||
subject.protocol_list
|
||||
expect(WebMock).to have_requested(:get, URL).with(query: default_query_params)
|
||||
|
@ -47,7 +76,7 @@ describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
|
|||
page_size: 15,
|
||||
fields: 'somefields'
|
||||
}
|
||||
stub_request(:get, URL).with(query: query)
|
||||
stub_protocols.with(query: query)
|
||||
|
||||
subject.protocol_list(query)
|
||||
expect(WebMock).to have_requested(:get, URL).with(query: query)
|
||||
|
@ -56,6 +85,9 @@ describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
|
|||
it 'should send authorization token if provided on initialization' do
|
||||
headers = { 'Authorization': "Bearer #{TOKEN}" }
|
||||
stub_request(:get, URL).with(headers: headers, query: default_query_params)
|
||||
.to_return(status: 200,
|
||||
body: JSON.generate(status_code: 0),
|
||||
headers: { 'Content-Type': 'application/json' })
|
||||
|
||||
ProtocolImporters::ProtocolsIO::V3::ApiClient.new(TOKEN).protocol_list
|
||||
expect(WebMock).to have_requested(:get, URL).with(headers: headers, query: default_query_params)
|
||||
|
@ -67,20 +99,24 @@ describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
|
|||
SINGLE_PROTOCOL_URL = "#{CONSTANTS[:base_uri]}protocols/#{PROTOCOL_ID}"
|
||||
|
||||
let(:stub_single_protocol) do
|
||||
stub_request(:get, SINGLE_PROTOCOL_URL)
|
||||
stub_request(:get, SINGLE_PROTOCOL_URL).to_return(
|
||||
status: 200,
|
||||
body: JSON.generate(status_code: 0),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns 200 on successfull call' do
|
||||
stub_single_protocol.to_return(status: 200, body: '[]', headers: {})
|
||||
stub_single_protocol
|
||||
|
||||
expect(subject.single_protocol(PROTOCOL_ID).code).to eq 200
|
||||
expect(stub_single_protocol).to have_been_requested
|
||||
end
|
||||
|
||||
it 'raises OpenTimeout error on timeout' do
|
||||
stub_single_protocol.to_timeout
|
||||
it 'raises NetworkError on timeout' do
|
||||
stub_request(:get, SINGLE_PROTOCOL_URL).to_timeout
|
||||
|
||||
expect { subject.single_protocol(PROTOCOL_ID) }.to raise_error(Net::OpenTimeout)
|
||||
expect { subject.single_protocol(PROTOCOL_ID) }.to raise_error(ProtocolImporters::ProtocolsIO::V3::NetworkError)
|
||||
end
|
||||
|
||||
it 'should send authorization token if provided on initialization' do
|
||||
|
@ -106,10 +142,11 @@ describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
|
|||
expect(stub_html_preview).to have_been_requested
|
||||
end
|
||||
|
||||
it 'raises OpenTimeout error on timeout' do
|
||||
it 'raises NetworkErrorr on timeout' do
|
||||
stub_html_preview.to_timeout
|
||||
|
||||
expect { subject.protocol_html_preview(PROTOCOL_URI) }.to raise_error(Net::OpenTimeout)
|
||||
expect { subject.protocol_html_preview(PROTOCOL_URI) }
|
||||
.to raise_error(ProtocolImporters::ProtocolsIO::V3::NetworkError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue