Skip to content

Commit

Permalink
Merge 456b819 into b24fc42
Browse files Browse the repository at this point in the history
  • Loading branch information
felixsch committed Jun 7, 2018
2 parents b24fc42 + 456b819 commit 8a66b45
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
5 changes: 5 additions & 0 deletions lib/suse/connect/api.rb
Expand Up @@ -244,6 +244,11 @@ def package_search(product, query)
query = CGI.escape(query)

@connection.get(api + "?product_id=#{triplet}&query=#{query}")
rescue ApiError => e
raise e if e.code != 404
raise UnsupportedOperation, 'Package search is not supported by the '\
'registration proxy: Alternatively checkout'\
'the web version at https://scc.suse.com/packages/'
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/suse/connect/errors.rb
Expand Up @@ -12,6 +12,7 @@ class UnsupportedStatusFormat < StandardError; end
class NetworkError < StandardError; end
class SystemNotRegisteredError < StandardError; end
class BaseProductDeactivationError < StandardError; end
class UnsupportedOperation < StandardError; end

# Basic error for API interactions. Collects HTTP response (which includes
# status code and response body) for future showing to user via {Cli}
Expand Down
63 changes: 52 additions & 11 deletions spec/connect/api_spec.rb
Expand Up @@ -522,21 +522,62 @@

describe '#package_search' do
let(:product) { SUSE::Connect::Zypper::Product.new name: 'SLES', version: '15', arch: 'x86_64' }
let(:query) { 'https://example.com/api/package_search/packages?product_id=SLES/15/x86_64&query=vim' }

let!(:stubbed_request) do
stub_request(:get, query).with(headers: {
'Accept' => 'application/json,application/vnd.scc.suse.com.v4+json',
'Content-Type' => 'application/json'
}).to_return(status: 200, body: "[{}]", headers: {})
context 'supported' do
let(:query) { 'https://example.com/api/package_search/packages?product_id=SLES/15/x86_64&query=vim' }

let!(:stubbed_request) do
stub_request(:get, query).with(headers: {
'Accept' => 'application/json,application/vnd.scc.suse.com.v4+json',
'Content-Type' => 'application/json'
}).to_return(status: 200, body: "[{}]", headers: {})
end

it 'performs the request and set the query params correctly' do
expect(CGI).to receive(:escape).twice.and_call_original
expect(product).to receive(:to_triplet).and_call_original

subject.new(config).package_search(product, "vim")
expect(stubbed_request).to have_been_made
end
end

it 'performs the request and set the query params correctly' do
expect(CGI).to receive(:escape).twice.and_call_original
expect(product).to receive(:to_triplet).and_call_original
context 'unsupported' do
let(:query) { 'https://example.com/api/package_search/packages?product_id=SLES/15/x86_64&query=docker' }

subject.new(config).package_search(product, "vim")
expect(stubbed_request).to have_been_made
let!(:stubbed_request) do
stub_request(:get, query).with(headers: {
'Accept' => 'application/json,application/vnd.scc.suse.com.v4+json',
'Content-Type' => 'application/json'
}).to_return(status: 404, body: "{}", headers: {})
end

it 'performs the request and raises an error' do
expect(CGI).to receive(:escape).twice.and_call_original
expect(product).to receive(:to_triplet).and_call_original

expect { subject.new(config).package_search(product, "docker") }.to raise_error(SUSE::Connect::UnsupportedOperation)
expect(stubbed_request).to have_been_made
end
end

context 'not working host' do
let(:query) { 'https://example.com/api/package_search/packages?product_id=SLES/15/x86_64&query=libguestfs' }

let!(:stubbed_request) do
stub_request(:get, query).with(headers: {
'Accept' => 'application/json,application/vnd.scc.suse.com.v4+json',
'Content-Type' => 'application/json'
}).to_return(status: 500, body: "{}", headers: {})
end

it 'performs the request and raises an error' do
expect(CGI).to receive(:escape).twice.and_call_original
expect(product).to receive(:to_triplet).and_call_original

expect { subject.new(config).package_search(product, "libguestfs") }.to raise_error(SUSE::Connect::ApiError)
expect(stubbed_request).to have_been_made
end
end
end
end

0 comments on commit 8a66b45

Please sign in to comment.