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]
query = CONSTANTS.dig(:endpoints, :protocols, :default_query_params)
.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]
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'
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
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
CONSTANTS.dig(:endpoints, :protocols, :default_query_params)
end
let(:default_query_params) do
CONSTANTS.dig(:endpoints, :protocols, :default_query_params)
end
let(:key_query) do
{ key: 'key' }.stringify_keys
end
it 'returns 200 on successfull call' do
stub_protocols
expect(subject.protocol_list.code).to eq 200
expect(stub_protocols).to have_been_requested
end
let(:default_query_params_with_key) do
default_query_params.merge(key_query)
end
it 'raises NetworkError on timeout' do
stub_request(:get, URL).with(query: hash_including({})).to_timeout
let(:stub_protocols) do
stub_request(:get, URL).with(query: default_query_params_with_key)
end
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::NetworkError)
end
let(:protocol_list_call) do
subject.protocol_list(key_query)
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' })
it 'returns 200 on successfull call' do
stub_protocols.to_return(status: 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
end
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::ArgumentError)
end
it 'raises NetworkError on timeout' do
stub_protocols.to_timeout
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 { protocol_list_call }.to raise_error(ProtocolImporters::ProtocolsIO::V3::NetworkError)
end
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::UnauthorizedError)
end
it 'raises ArgumentError when status_code = 1' do
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
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 { protocol_list_call }.to raise_error(ProtocolImporters::ProtocolsIO::V3::ArgumentError)
end
expect { subject.protocol_list }.to raise_error(ProtocolImporters::ProtocolsIO::V3::UnauthorizedError)
end
it 'raises UnauthorizedError when status_code = 1218' do
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
stub_protocols.with(query: default_query_params)
expect { protocol_list_call }.to raise_error(ProtocolImporters::ProtocolsIO::V3::UnauthorizedError)
end
subject.protocol_list
expect(WebMock).to have_requested(:get, URL).with(query: default_query_params)
end
it 'raises UnauthorizedError when status_code = 1219' do
stub_protocols.to_return(status: 200,
body: JSON.generate(status_code: 1219, error_message: 'Argument error'),
headers: { 'Content-Type': 'application/json' })
it 'requests server with given query parameters' do
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)
expect { protocol_list_call }.to raise_error(ProtocolImporters::ProtocolsIO::V3::UnauthorizedError)
end
subject.protocol_list(query)
expect(WebMock).to have_requested(:get, URL).with(query: query)
end
it 'requests server with default query parameters if none are given' do
stub_protocols.to_return(status: 200,
body: JSON.generate(status_code: 0),
headers: { 'Content-Type': 'application/json' })
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' })
subject.protocol_list(key_query)
expect(WebMock).to have_requested(:get, URL).with(query: default_query_params_with_key)
end
ProtocolImporters::ProtocolsIO::V3::ApiClient.new(TOKEN).protocol_list
expect(WebMock).to have_requested(:get, URL).with(headers: headers, query: default_query_params)
it 'requests server with given query parameters' do
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