Fix old tests

This commit is contained in:
Jure Grabnar 2019-07-15 08:27:34 +02:00
parent a372d4056d
commit 015dae1a05
2 changed files with 83 additions and 68 deletions

View file

@ -47,7 +47,7 @@ module ProtocolImporters
sort_mappings = CONSTANTS[:sort_mappings] sort_mappings = CONSTANTS[:sort_mappings]
query = CONSTANTS.dig(:endpoints, :protocols, :default_query_params) query = CONSTANTS.dig(:endpoints, :protocols, :default_query_params)
.stringify_keys .stringify_keys
.merge(query_params.except(:sort_by)) .merge(query_params.except(:sort_by).stringify_keys)
if sort_mappings[query_params[:sort_by]&.to_sym] if sort_mappings[query_params[:sort_by]&.to_sym]
query = query.merge(sort_mappings[query_params[:sort_by].to_sym].stringify_keys) query = query.merge(sort_mappings[query_params[:sort_by].to_sym].stringify_keys)

View file

@ -7,63 +7,74 @@ describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
TOKEN = 'test_token' TOKEN = 'test_token'
describe '#protocol_list' do describe '#protocol_list' do
context 'when search key is given' do
URL = "#{CONSTANTS[:base_uri]}protocols" URL = "#{CONSTANTS[:base_uri]}protocols"
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 let(:default_query_params) do
CONSTANTS.dig(:endpoints, :protocols, :default_query_params) CONSTANTS.dig(:endpoints, :protocols, :default_query_params)
end end
let(:key_query) do
{ key: 'key' }.stringify_keys
end
let(:default_query_params_with_key) do
default_query_params.merge(key_query)
end
let(:stub_protocols) do
stub_request(:get, URL).with(query: default_query_params_with_key)
end
let(:protocol_list_call) do
subject.protocol_list(key_query)
end
it 'returns 200 on successfull call' do it 'returns 200 on successfull call' do
stub_protocols stub_protocols.to_return(status: 200,
expect(subject.protocol_list.code).to eq 200 body: JSON.generate(status_code: 0),
headers: { 'Content-Type': 'application/json' })
expect(protocol_list_call.code).to eq 200
expect(stub_protocols).to have_been_requested expect(stub_protocols).to have_been_requested
end end
it 'raises NetworkError on timeout' do it 'raises NetworkError on timeout' do
stub_request(:get, URL).with(query: hash_including({})).to_timeout stub_protocols.to_timeout
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::NetworkError) expect { protocol_list_call }.to raise_error(ProtocolImporters::ProtocolsIO::V3::NetworkError)
end end
it 'raises ArgumentError when status_code = 1' do it 'raises ArgumentError when status_code = 1' do
stub_request(:get, URL).with(query: hash_including({})) stub_protocols.to_return(status: 200,
.to_return(status: 200,
body: JSON.generate(status_code: 1, error_message: 'Argument error'), body: JSON.generate(status_code: 1, error_message: 'Argument error'),
headers: { 'Content-Type': 'application/json' }) headers: { 'Content-Type': 'application/json' })
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::ArgumentError) expect { protocol_list_call }.to raise_error(ProtocolImporters::ProtocolsIO::V3::ArgumentError)
end end
it 'raises UnauthorizedError when status_code = 1218' do it 'raises UnauthorizedError when status_code = 1218' do
stub_request(:get, URL).with(query: hash_including({})) stub_protocols.to_return(status: 200,
.to_return(status: 200,
body: JSON.generate(status_code: 1218, error_message: 'Argument error'), body: JSON.generate(status_code: 1218, error_message: 'Argument error'),
headers: { 'Content-Type': 'application/json' }) headers: { 'Content-Type': 'application/json' })
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::UnauthorizedError) expect { protocol_list_call }.to raise_error(ProtocolImporters::ProtocolsIO::V3::UnauthorizedError)
end end
it 'raises UnauthorizedError when status_code = 1219' do it 'raises UnauthorizedError when status_code = 1219' do
stub_request(:get, URL).with(query: hash_including({})) stub_protocols.to_return(status: 200,
.to_return(status: 200,
body: JSON.generate(status_code: 1219, error_message: 'Argument error'), body: JSON.generate(status_code: 1219, error_message: 'Argument error'),
headers: { 'Content-Type': 'application/json' }) headers: { 'Content-Type': 'application/json' })
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::UnauthorizedError) expect { protocol_list_call }.to raise_error(ProtocolImporters::ProtocolsIO::V3::UnauthorizedError)
end end
it 'requests server with default query parameters if none are given' do it 'requests server with default query parameters if none are given' do
stub_protocols.with(query: default_query_params) stub_protocols.to_return(status: 200,
body: JSON.generate(status_code: 0),
headers: { 'Content-Type': 'application/json' })
subject.protocol_list subject.protocol_list(key_query)
expect(WebMock).to have_requested(:get, URL).with(query: default_query_params) expect(WebMock).to have_requested(:get, URL).with(query: default_query_params_with_key)
end end
it 'requests server with given query parameters' do it 'requests server with given query parameters' do
@ -77,6 +88,9 @@ describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
fields: 'somefields' fields: 'somefields'
} }
stub_protocols.with(query: query) stub_protocols.with(query: query)
.to_return(status: 200,
body: JSON.generate(status_code: 0),
headers: { 'Content-Type': 'application/json' })
subject.protocol_list(query) subject.protocol_list(query)
expect(WebMock).to have_requested(:get, URL).with(query: query) expect(WebMock).to have_requested(:get, URL).with(query: query)
@ -84,13 +98,14 @@ describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
it 'should send authorization token if provided on initialization' do it 'should send authorization token if provided on initialization' do
headers = { 'Authorization': "Bearer #{TOKEN}" } headers = { 'Authorization': "Bearer #{TOKEN}" }
stub_request(:get, URL).with(headers: headers, query: default_query_params) stub_protocols.with(headers: headers, query: default_query_params_with_key)
.to_return(status: 200, .to_return(status: 200,
body: JSON.generate(status_code: 0), body: JSON.generate(status_code: 0),
headers: { 'Content-Type': 'application/json' }) headers: { 'Content-Type': 'application/json' })
ProtocolImporters::ProtocolsIO::V3::ApiClient.new(TOKEN).protocol_list ProtocolImporters::ProtocolsIO::V3::ApiClient.new(TOKEN).protocol_list(key_query)
expect(WebMock).to have_requested(:get, URL).with(headers: headers, query: default_query_params) expect(WebMock).to have_requested(:get, URL).with(headers: headers, query: default_query_params_with_key)
end
end end
end end