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