Skip to content

Commit

Permalink
Refactor current state for tests to pass. Increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kalabiyau committed Apr 2, 2014
1 parent 81f5939 commit 0ae6c09
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 117 deletions.
2 changes: 1 addition & 1 deletion lib/suse/connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Connect

require 'suse/connect/version'
require 'suse/connect/logger'
require 'suse/connect/utilities'
require 'suse/connect/errors'
require 'suse/connect/client'
require 'suse/connect/system'
Expand All @@ -18,7 +19,6 @@ module Connect
require 'suse/connect/source'
require 'suse/connect/connection'
require 'suse/connect/api'
require 'suse/connect/utilities'
require 'suse/connect/yast'
end
end
5 changes: 1 addition & 4 deletions lib/suse/connect/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def activate_subscription(auth, product)
:token => product[:token] ? product[:token] : @client.options[:token]
}
@connection.post('/connect/systems/products', :auth => auth, :params => payload)

end

# List all publicly available products. This includes a list of all repositories for each product.
Expand All @@ -75,9 +74,7 @@ def products
# @return [OpenStruct] responding to body(response from SCC) and code(natural HTTP response code).
#
def addons(auth, product)
payload = {
:product_ident => product[:name]
}
payload = { :product_ident => product[:name] }
@connection.get('/connect/systems/products', :auth => auth, :params => payload)
end

Expand Down
43 changes: 14 additions & 29 deletions lib/suse/connect/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ module SUSE
module Connect
# Client to interact with API
class Client
# TODO: drop this in favor of clear auth implementation
include ::Net::HTTPHeader

DEFAULT_URL = 'https://scc.suse.com'
include SUSE::Connect::Utilities

attr_reader :options, :url, :api

Expand All @@ -20,35 +19,24 @@ def initialize(opts)
@api = Api.new(self)
end

def token_auth(token)
raise CannotBuildTokenAuth, 'token auth requested, but no token provided' unless token
"Token token=#{token}"
end

def basic_auth

username, password = System.credentials
# TODO: find new name because this is specific to cli usage
def execute!

if username && password
basic_encode(username, password)
else
raise CannotBuildBasicAuth, 'cannot get proper username and password'
unless System.registered?
login, password = announce_system
Zypper.write_base_credentials(login, password)
end

end

# TODO find new name because this is specific to cli usage
def execute!
login, password = announce_system unless System.registered?
Zypper.write_base_credentials(login, password)

service = activate_subscription(Zypper.base_product)
System.add_service(service)
end

# Announce system via SCC/Registration Proxy
#
# @returns: [Array] login, password tuple. Those credentials are given by SCC/Registration Proxy
def announce_system
response = @api.announce_system(token_auth)
return response.body['login'], response.body['password']
response = @api.announce_system(token_auth(@options[:token]))
[response.body['login'], response.body['password']]
end

def activate_subscription(product)
Expand All @@ -58,15 +46,12 @@ def activate_subscription(product)

# @param product [Hash] product to query extensions for
def products_for(product)
response = @api.addons(basic_auth, product)
extensions = response.each do |extension|
Extension.new(extension['name'], '', '', extension['zypper_name'])
response = @api.addons(basic_auth, product).body
response.map do |extension|
Yast::Extension.new(extension['name'], '', '', extension['zypper_name'])
end
extensions
end

private

end
end
end
2 changes: 1 addition & 1 deletion lib/suse/connect/product.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class SUSE::Connect::YaST::Product
#TODO replace hash representing products used elsewhere
# TODO: replace hash representing products used elsewhere
end
2 changes: 0 additions & 2 deletions lib/suse/connect/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ def initialize(name, url)
@url = url.is_a?(String) ? URI(url) : url
end
end


30 changes: 15 additions & 15 deletions lib/suse/connect/utilities.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# utility methods
module SUSE
module Connect
class Utilities
class << self
include ::Net::HTTPHeader
# utility methods
module Utilities

def token_auth(token)
raise CannotBuildTokenAuth, 'token auth requested, but no token provided' unless token
"Token token=#{token}"
end
include ::Net::HTTPHeader

def basic_auth
def token_auth(token)
raise CannotBuildTokenAuth, 'token auth requested, but no token provided' unless token
"Token token=#{token}"
end

username, password = System.credentials
def basic_auth

if username && password
basic_encode(username, password)
else
raise CannotBuildBasicAuth, 'cannot get proper username and password'
end
username, password = System.credentials

if username && password
basic_encode(username, password)
else
raise CannotBuildBasicAuth, 'cannot get proper username and password'
end

end

end
end
end
24 changes: 16 additions & 8 deletions lib/suse/connect/yast/extension.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
class SUSE::Connect::YaST::Extension
module SUSE
module Connect
module Yast
# Yast Extension class
class Extension

attr_reader :short_name, :long_name, :description, :product_ident
attr_reader :short_name, :long_name, :description, :product_ident

# Constructor
def initialize(short_name, long_name, description, product_ident)
@short_name = short_name
@long_name = long_name
@description = description
@product_ident = product_ident
# Constructor
def initialize(short_name, long_name, description, product_ident)
@short_name = short_name
@long_name = long_name
@description = description
@product_ident = product_ident
end

end
end
end
end
30 changes: 30 additions & 0 deletions spec/connect/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,34 @@

end

describe 'products' do

before do
stub_addons_call
end

it 'is authenticated via basic auth' do
payload = [
'/connect/systems/products',
:auth => 'Basic: encodedgibberish',
:params => { :product_ident => 'rodent' }
]
Connection.any_instance.should_receive(:get)
.with(*payload)
.and_call_original
subject.new(client).addons('Basic: encodedgibberish', :name => 'rodent')
end

it 'respond with proper status code' do
response = subject.new(client).addons('Basic: encodedgibberish', :name => 'rodent')
response.code.should eq 200
end

it 'returns array of extensions' do
body = subject.new(client).addons('Basic: encodedgibberish', :name => 'rodent').body
body.should be_kind_of Array
end

end

end
83 changes: 37 additions & 46 deletions spec/connect/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
end

describe '#announce_system' do
subject { SUSE::Connect::Client.new({:token => "blabla"}) }

subject { SUSE::Connect::Client.new(:token => 'blabla') }

before do
api_response = double('api_response')
Expand All @@ -44,15 +45,7 @@
subject.stub(:token_auth => true)
end

#TODO push this into execute! test
#it 'writes credentials file' do
# Zypper.should_receive(:write_base_credentials).with('lg', 'pw')
# subject.stub(:api)
# subject.execute!
#end

it 'calls underlying api' do
#Zypper.stub(:write_base_credentials)
Api.any_instance.should_receive(:announce_system)
subject.announce_system
end
Expand All @@ -72,10 +65,10 @@

end

#it 'selects product' do
# Zypper.should_receive(:base_product).and_return(:name => 'SLE_BASE')
# subject.activate_subscription(Zypper.base_product)
#end
it 'selects product' do
Zypper.should_receive(:base_product).and_return(:name => 'SLE_BASE')
subject.activate_subscription(Zypper.base_product)
end

it 'gets login and password from system' do
subject.should_receive(:basic_auth)
Expand All @@ -88,37 +81,26 @@
subject.activate_subscription(Zypper.base_product)
end

#it 'adds a service' do
# System.should_receive(:add_service)
# subject.activate_subscription(Zypper.base_product)
#end

end

describe '#execute!' do

before do
# This is where the stubbing is wrong
# I don't know why we would stub the methods that are being tested below
# But this was already present
#Client.any_instance.stub(:announce_system)
#Client.any_instance.stub(:activate_subscription => Service.new( [Source.new("foo", "flub")], true, true))
Zypper.stub(:base_product => { :name => 'SLE_BASE' })
Zypper.stub(:add_service => true)
System.stub(:add_service => true)
Zypper.stub(:write_base_credentials)
subject.stub(:activate_subscription)
subject.class.any_instance.stub(:basic_auth => true)
subject.class.any_instance.stub(:token_auth => true)
end

it 'should call announce if system not registered' do
System.stub(:registered? => false)
#System.stub(:add_service => true)
subject.should_receive(:announce_system)
subject.execute!
end

it 'should not call announce on api if system registered' do

System.stub(:registered? => true)
subject.should_not_receive(:announce_system)
subject.execute!
Expand All @@ -130,34 +112,43 @@
subject.execute!
end

end

describe '?token_auth' do

it 'returns string for auth header' do
subject.send(:token_auth, 'lambada').should eq 'Token token=lambada'
it 'writes credentials file' do
System.stub(:registered? => false)
subject.stub(:announce_system => %w{ lg pw })
Zypper.should_receive(:write_base_credentials).with('lg', 'pw')
subject.execute!
end

# I think this test can be deleted as we would now get ArgumentError if calling token_auth without args
#it 'raise if no token passed, but method requested' do
# expect { subject.send(:token_auth, nil) }
# .to raise_error CannotBuildTokenAuth, 'token auth requested, but no token provided'
#end
it 'adds service after product activation' do
System.stub(:registered? => true)
System.should_receive(:add_service)
subject.execute!
end

end

describe '?basic_auth' do
describe '#products_for' do

let(:stubbed_response) do
OpenStruct.new(
:code => 200,
:body => [{ 'name' => 'short_name', 'zypper_name' => 'zypper_name' }],
:success => true
)
end

before do
subject.stub(:basic_auth => 'Basic: encodedstring')
end

it 'returns string for auth header' do
System.stub(:credentials => %w{bob dylan})
base64_line = 'Basic Ym9iOmR5bGFu'
subject.send(:basic_auth).should eq base64_line
it 'collects data from api response' do
subject.api.should_receive(:addons).with('Basic: encodedstring', 'SLES').and_return stubbed_response
subject.products_for('SLES')
end

it 'raise if cannot get credentials' do
System.stub(:credentials => nil)
expect { subject.send(:basic_auth) }
.to raise_error CannotBuildBasicAuth, 'cannot get proper username and password'
it 'returns array of extension products returned from api' do
subject.api.should_receive(:addons).with('Basic: encodedstring', 'SLES').and_return stubbed_response
pp subject.products_for('SLES').first.should be_kind_of Yast::Extension
end

end
Expand Down
3 changes: 2 additions & 1 deletion spec/connect/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
let :sources_mock do
{ 'a' => 'foo', 'b' => 'bar' }
end

let :sources_output do
[Source.new("foo")]
[Source.new('foo')]
end

it 'assigns sources' do
Expand Down

0 comments on commit 0ae6c09

Please sign in to comment.