Skip to content

Commit

Permalink
Generate UUID for system
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergeykot committed Apr 18, 2018
1 parent bcb2a29 commit b721ed8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -31,6 +31,7 @@ coverage/
config/secrets.yml.key
config/secrets.yml.enc
config/rmt.local.yml
config/system_uuid

.env
.tags*
31 changes: 25 additions & 6 deletions lib/suse/connect/api.rb
Expand Up @@ -7,30 +7,33 @@ module Connect
class Api

class InvalidCredentialsError < StandardError; end
SYSTEM_UUID_LOCATION = 'config/system_uuid'.freeze
ROOT_PATH = Pathname.new(File.dirname(__FILE__)).join('../', '../', '../').freeze
CONNECT_API_URL = 'https://scc.suse.com/connect'.freeze

def initialize(username, password)
@username = username
@password = password
end

def list_orders
make_paginated_request(:get, 'https://scc.suse.com/connect/organizations/orders')
make_paginated_request(:get, "#{CONNECT_API_URL}/organizations/orders")
end

def list_products
make_paginated_request(:get, 'https://scc.suse.com/connect/organizations/products')
make_paginated_request(:get, "#{CONNECT_API_URL}/organizations/products")
end

def list_products_unscoped
make_paginated_request(:get, 'https://scc.suse.com/connect/organizations/products/unscoped')
make_paginated_request(:get, "#{CONNECT_API_URL}/organizations/products/unscoped")
end

def list_repositories
make_paginated_request(:get, 'https://scc.suse.com/connect/organizations/repositories')
make_paginated_request(:get, "#{CONNECT_API_URL}/organizations/repositories")
end

def list_subscriptions
make_paginated_request(:get, 'https://scc.suse.com/connect/organizations/subscriptions')
make_paginated_request(:get, "#{CONNECT_API_URL}/organizations/subscriptions")
end

protected
Expand All @@ -47,7 +50,7 @@ def make_request(method, url, options)
options[:userpwd] = "#{@username}:#{@password}" unless options[:userpwd]
options[:method] = method
options[:accept_encoding] = 'gzip, deflate'
options[:headers] = { 'RMT' => @username }
options[:headers] = { 'RMT' => system_uuid }

response = RMT::HttpRequest.new(url, options).run
raise InvalidCredentialsError if (response.code == 401)
Expand Down Expand Up @@ -80,6 +83,22 @@ def make_paginated_request(method, url, options = {})
@entities
end

private

def system_uuid
@system_uuid ||= fetch_system_uuid
end

def fetch_system_uuid
uuid_file_location = ROOT_PATH.join(SYSTEM_UUID_LOCATION)
if File.exist?(uuid_file_location)
File.read(uuid_file_location)
else
uuid = SecureRandom.uuid
File.write(uuid_file_location, uuid)
uuid
end
end
end
end
end
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Expand Up @@ -26,6 +26,8 @@
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

File.delete(Rails.root.join('config', 'system_uuid')) if File.exist?(Rails.root.join('config', 'system_uuid'))

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
Expand Down
9 changes: 8 additions & 1 deletion spec/suse/connect/api_spec.rb
Expand Up @@ -78,7 +78,7 @@
{
'Authorization' => 'Basic ' + Base64.encode64("#{username}:#{password}").strip,
'User-Agent' => "RMT/#{RMT::VERSION}",
'RMT' => username
'RMT' => api_client.send(:system_uuid)
}
end
let(:response_data) { { foo: 'bar' } }
Expand All @@ -87,41 +87,48 @@
subject { api_client.send(:make_single_request, 'GET', 'http://example.org/api_method') }

it { is_expected.to eq(response_data) }
it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) }
end

describe '#make_paginated_request' do
subject { api_client.send(:make_paginated_request, 'GET', 'http://example.org/api_method') }

it { is_expected.to eq([response_data, response_data]) }
it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) }
end

describe '#list_orders' do
subject { api_client.list_orders }

it { is_expected.to eq([ { endpoint: 'organizations/orders' } ]) }
it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) }
end

describe '#list_products' do
subject { api_client.list_products }

it { is_expected.to eq([ { endpoint: 'organizations/products' } ]) }
it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) }
end

describe '#list_products_unscoped' do
subject { api_client.list_products_unscoped }

it { is_expected.to eq([ { endpoint: 'organizations/products/unscoped' } ]) }
it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) }
end

describe '#list_repositories' do
subject { api_client.list_repositories }

it { is_expected.to eq([ { endpoint: 'organizations/repositories' } ]) }
it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) }
end

describe '#list_subscriptions' do
subject { api_client.list_subscriptions }

it { is_expected.to eq([ { endpoint: 'organizations/subscriptions' } ]) }
it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) }
end
end

0 comments on commit b721ed8

Please sign in to comment.