Skip to content

Commit

Permalink
Merge pull request #145 from nmburgan/maint/master/fix_specs
Browse files Browse the repository at this point in the history
(maint) Use latest Faraday/webmock, update specs
  • Loading branch information
highb committed Jul 13, 2021
2 parents 302d52a + f6febc9 commit 3b40cbe
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ group :test do
gem 'rb-readline'
gem 'rspec', '~> 3.10.0'
gem 'rubocop', '~> 1.6'
gem 'webmock', '1.21.0'
gem 'webmock', '~> 3.13'
end
17 changes: 17 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'simplecov'
require 'coveralls'
require 'base64'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::HTMLFormatter,
Expand All @@ -26,3 +27,19 @@ def initialize(values = {})
config.tty = true
config.formatter = :documentation
end

def get_headers(username: nil, password: nil, token: nil, content_type: nil, content_length: nil)
headers = {
'Accept' => '*/*',
'Accept-Encoding' => /gzip/,
'User-Agent' => /Faraday/,
}
if username && password
auth = Base64.encode64("#{username}:#{password}").chomp
headers['Authorization'] = "Basic #{auth}"
end
headers['X-Auth-Token'] = token if token
headers['Content-Type'] = content_type if content_type
headers['Content-Length'] = content_length.to_s if content_length
headers
end
32 changes: 20 additions & 12 deletions spec/vmfloaty/abs/auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
require 'spec_helper'
require_relative '../../../lib/vmfloaty/auth'

user = 'first.last'
pass = 'password'

describe Pooler do

before :each do
@abs_url = 'https://abs.example.com/api/v2'
end
Expand All @@ -15,18 +19,20 @@
end

it 'returns a token from abs' do
stub_request(:post, 'https://first.last:password@abs.example.com/api/v2/token')
stub_request(:post, 'https://abs.example.com/api/v2/token')
.with(headers: get_headers(username: user, password: pass, content_length: 0))
.to_return(status: 200, body: @get_token_response, headers: {})

token = Auth.get_token(false, @abs_url, 'first.last', 'password')
token = Auth.get_token(false, @abs_url, user, pass)
expect(token).to eq @token
end

it 'raises a token error if something goes wrong' do
stub_request(:post, 'https://first.last:password@abs.example.com/api/v2/token')
stub_request(:post, 'https://abs.example.com/api/v2/token')
.with(headers: get_headers(username: user, password: pass, content_length: 0))
.to_return(status: 500, body: '{"ok":false}', headers: {})

expect { Auth.get_token(false, @abs_url, 'first.last', 'password') }.to raise_error(TokenError)
expect { Auth.get_token(false, @abs_url, user, pass) }.to raise_error(TokenError)
end
end

Expand All @@ -37,22 +43,24 @@
end

it 'deletes the specified token' do
stub_request(:delete, 'https://first.last:password@abs.example.com/api/v2/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
stub_request(:delete, 'https://abs.example.com/api/v2/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
.with(headers: get_headers(username: user, password: pass))
.to_return(status: 200, body: @delete_token_response, headers: {})

expect(Auth.delete_token(false, @abs_url, 'first.last', 'password',
@token)).to eq JSON.parse(@delete_token_response)
expect(Auth.delete_token(false, @abs_url, user, pass,
@token)).to eq JSON.parse(@delete_token_response)
end

it 'raises a token error if something goes wrong' do
stub_request(:delete, 'https://first.last:password@abs.example.com/api/v2/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
stub_request(:delete, 'https://abs.example.com/api/v2/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
.with(headers: get_headers(username: user, password: pass))
.to_return(status: 500, body: '{"ok":false}', headers: {})

expect { Auth.delete_token(false, @abs_url, 'first.last', 'password', @token) }.to raise_error(TokenError)
expect { Auth.delete_token(false, @abs_url, user, pass, @token) }.to raise_error(TokenError)
end

it 'raises a token error if no token provided' do
expect { Auth.delete_token(false, @abs_url, 'first.last', 'password', nil) }.to raise_error(TokenError)
expect { Auth.delete_token(false, @abs_url, user, pass, nil) }.to raise_error(TokenError)
end
end

Expand All @@ -64,15 +72,15 @@

it 'checks the status of a token' do
stub_request(:get, "#{@abs_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y")
.with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3' })
.with(headers: get_headers)
.to_return(status: 200, body: @token_status_response, headers: {})

expect(Auth.token_status(false, @abs_url, @token)).to eq JSON.parse(@token_status_response)
end

it 'raises a token error if something goes wrong' do
stub_request(:get, "#{@abs_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y")
.with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3' })
.with(headers: get_headers)
.to_return(status: 500, body: '{"ok":false}', headers: {})

expect { Auth.token_status(false, @abs_url, @token) }.to raise_error(TokenError)
Expand Down
8 changes: 6 additions & 2 deletions spec/vmfloaty/abs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@
[
{ "state":"allocated", "last_processed":"2020-01-17 22:29:13 +0000", "allocated_resources":[{"hostname":"craggy-chord.delivery.puppetlabs.net", "type":"centos-7-x86_64", "engine":"vmpooler"}, {"hostname":"visible-revival.delivery.puppetlabs.net", "type":"centos-7-x86_64", "engine":"vmpooler"}], "audit_log":{"2020-01-17 22:28:45 +0000":"Allocated craggy-chord.delivery.puppetlabs.net, visible-revival.delivery.puppetlabs.net for job 1579300120799"}, "request":{"resources":{"centos-7-x86_64":2}, "job":{"id":"1579300120799", "tags":{"user":"test-user"}, "user":"test-user", "time-received":1579300120}, "priority":3}}
]'
@return_request = { '{"job_id":"1579300120799","hosts":{"hostname":"craggy-chord.delivery.puppetlabs.net","type":"centos-7-x86_64","engine":"vmpooler"},{"hostname":"visible-revival.delivery.puppetlabs.net","type":"centos-7-x86_64","engine":"vmpooler"}}' => true }
@return_request = {
"job_id" => "1579300120799",
"hosts" => [{"hostname"=>"craggy-chord.delivery.puppetlabs.net","type"=>"centos-7-x86_64","engine"=>"vmpooler"},
{"hostname"=>"visible-revival.delivery.puppetlabs.net","type"=>"centos-7-x86_64","engine"=>"vmpooler"}]
}
# rubocop:enable Layout/LineLength
@token = 'utpg2i2xswor6h8ttjhu3d47z53yy47y'
@test_user = 'test-user'
Expand All @@ -164,7 +168,7 @@
stub_request(:get, 'https://abs.example.com/api/v2/status/queue')
.to_return(status: 200, body: @active_requests_response, headers: {})
stub_request(:post, 'https://abs.example.com/api/v2/return')
.with(body: @return_request)
.with(headers: get_headers(content_type: 'application/x-www-form-urlencoded', token: @token), body: @return_request.to_json)
.to_return(status: 200, body: 'OK', headers: {})

ret = ABS.delete(false, @abs_url, @hosts, @token, @test_user)
Expand Down
23 changes: 16 additions & 7 deletions spec/vmfloaty/auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
require 'spec_helper'
require_relative '../../lib/vmfloaty/auth'

user = 'first.last'
pass = 'password'

describe Pooler do
before :each do
@vmpooler_url = 'https://vmpooler.example.com'
Expand All @@ -15,18 +18,20 @@
end

it 'returns a token from vmpooler' do
stub_request(:post, 'https://first.last:password@vmpooler.example.com/token')
stub_request(:post, 'https://vmpooler.example.com/token')
.with(headers: get_headers(username: user, password: pass, content_length: 0))
.to_return(status: 200, body: @get_token_response, headers: {})

token = Auth.get_token(false, @vmpooler_url, 'first.last', 'password')
token = Auth.get_token(false, @vmpooler_url, user, pass)
expect(token).to eq @token
end

it 'raises a token error if something goes wrong' do
stub_request(:post, 'https://first.last:password@vmpooler.example.com/token')
stub_request(:post, 'https://vmpooler.example.com/token')
.with(headers: get_headers(username: user, password: pass, content_length: 0))
.to_return(status: 500, body: '{"ok":false}', headers: {})

expect { Auth.get_token(false, @vmpooler_url, 'first.last', 'password') }.to raise_error(TokenError)
expect { Auth.get_token(false, @vmpooler_url, user, pass) }.to raise_error(TokenError)
end
end

Expand All @@ -37,15 +42,17 @@
end

it 'deletes the specified token' do
stub_request(:delete, 'https://first.last:password@vmpooler.example.com/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
stub_request(:delete, 'https://vmpooler.example.com/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
.with(headers: get_headers(username: user, password: pass))
.to_return(status: 200, body: @delete_token_response, headers: {})

expect(Auth.delete_token(false, @vmpooler_url, 'first.last', 'password',
expect(Auth.delete_token(false, @vmpooler_url, user, pass,
@token)).to eq JSON.parse(@delete_token_response)
end

it 'raises a token error if something goes wrong' do
stub_request(:delete, 'https://first.last:password@vmpooler.example.com/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
stub_request(:delete, 'https://vmpooler.example.com/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
.with(headers: get_headers(username: user, password: pass))
.to_return(status: 500, body: '{"ok":false}', headers: {})

expect { Auth.delete_token(false, @vmpooler_url, 'first.last', 'password', @token) }.to raise_error(TokenError)
Expand All @@ -64,13 +71,15 @@

it 'checks the status of a token' do
stub_request(:get, "#{@vmpooler_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y")
.with(headers: get_headers)
.to_return(status: 200, body: @token_status_response, headers: {})

expect(Auth.token_status(false, @vmpooler_url, @token)).to eq JSON.parse(@token_status_response)
end

it 'raises a token error if something goes wrong' do
stub_request(:get, "#{@vmpooler_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y")
.with(headers: get_headers)
.to_return(status: 500, body: '{"ok":false}', headers: {})

expect { Auth.token_status(false, @vmpooler_url, @token) }.to raise_error(TokenError)
Expand Down
8 changes: 3 additions & 5 deletions spec/vmfloaty/nonstandard_pooler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
describe NonstandardPooler do
before :each do
@nspooler_url = 'https://nspooler.example.com'
@auth_token_headers = {
'X-Auth-Token' => 'token-value'
}
@auth_token_headers = get_headers(token: 'token-value')
end

describe '#list' do
Expand Down Expand Up @@ -121,7 +119,7 @@

it 'raises an AuthError if the token is invalid' do
stub_request(:post, "#{@nspooler_url}/host/solaris-11-sparc")
.with(headers: @auth_token_headers)
.with(headers: get_headers(token: 'token-value'))
.to_return(status: 401, body: '{"ok":false,"reason": "token: token-value does not exist"}', headers: {})

vm_hash = { 'solaris-11-sparc' => 1 }
Expand Down Expand Up @@ -174,7 +172,7 @@
end

it 'modifies the reason of a vm' do
modify_request_body = { '{"reserved_for_reason":"testing"}' => true }
modify_request_body = { '{"reserved_for_reason":"testing"}' => nil }
stub_request(:put, "#{@nspooler_url}/host/myfakehost")
.with(body: modify_request_body,
headers: @auth_token_headers)
Expand Down
4 changes: 2 additions & 2 deletions spec/vmfloaty/pooler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
it 'modifies the TTL of a vm' do
modify_hash = { lifetime: 12 }
stub_request(:put, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6")
.with(body: { '{"lifetime":12}' => true },
headers: { 'Content-Type' => 'application/x-www-form-urlencoded', 'X-Auth-Token' => 'mytokenfile' })
.with(body: { '{"lifetime":12}' => nil },
headers: get_headers(content_type: 'application/x-www-form-urlencoded', token: 'mytokenfile'))
.to_return(status: 200, body: @modify_response_body_success, headers: {})

modify_req = Pooler.modify(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', modify_hash)
Expand Down
2 changes: 1 addition & 1 deletion vmfloaty.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ Gem::Specification.new do |s|

s.add_dependency 'colorize', '~> 0.8.1'
s.add_dependency 'commander', '>= 4.4.3', '< 4.6.0'
s.add_dependency 'faraday', '~> 0.17.0'
s.add_dependency 'faraday', '~> 1.5', '>= 1.5.1'
end

0 comments on commit 3b40cbe

Please sign in to comment.