Skip to content

Commit

Permalink
Add specs on auth v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mdouchement committed Jul 4, 2015
1 parent 2f9e926 commit 77d0523
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/swift_storage/auth/v2_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def auth_data

def storage_endpoint(service_catalog)
unless (swift = service_catalog.find { |service| service['type'] == 'object-store' })
fail 'No object-store service found'
fail SwiftStorage::Errors::NotFoundError.new 'No object-store service found'
end
yield swift['endpoints'].sample
end
Expand Down
132 changes: 115 additions & 17 deletions spec/swift/auth_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,123 @@
require "spec_helper"
require 'spec_helper'

RSpec.describe 'Auth' do
subject { swift_service }
it "success" do
headers(
h::STORAGE_URL => test_storage_url,
h::AUTH_TOKEN => 'auth_token',
h::STORAGE_TOKEN => 'storage_token'
)

expect{ subject.authenticate! }.to send_request(:get, '/auth/v1.0', :headers => {
h::AUTH_USER => 'test:testuser',
h::AUTH_KEY => 'testpassword'
})

expect(subject.auth_token).to eq('auth_token')
expect(subject.storage_url).to eq(test_storage_url)
expect(subject.storage_token).to eq('storage_token')

context 'when v1 is used' do
before do
headers(
h::STORAGE_URL => test_storage_url,
h::AUTH_TOKEN => 'auth_token',
h::STORAGE_TOKEN => 'storage_token'
)
end

it 'authenticates' do
expect { subject.authenticate! }.to send_request(:get, '/auth/v1.0',
headers: { h::AUTH_USER => 'test:testuser', h::AUTH_KEY => 'testpassword' })
end

it 'sets the authentication token' do
subject.authenticate!
expect(subject.auth_token).to eq('auth_token')
end

it 'sets the storage url' do
subject.authenticate!
expect(subject.storage_url).to eq(test_storage_url)
end

it 'sets the storage token' do
subject.authenticate!
expect(subject.storage_token).to eq('storage_token')
end
end

context 'when v2 is used' do
let(:credentials) do
{
auth: {
passwordCredentials: {
username: 'testuser',
password: 'testpassword'
},
subject.configuration.authtenant_type => 'test'
}
}
end
let(:body) do
{
'access' => {
'token' => {
'id' => 'auth_token'
},
'serviceCatalog' => [
{
'endpoints' => [
{
'id' => 'storage_token',
'publicURL' => test_storage_url
}
],
'type' => 'object-store'
}
]
}
}
end

before do
SwiftStorage.configuration.auth_version = '2.0'
allow(JSON).to receive(:parse).and_return(body)
end
after { SwiftStorage.configuration.auth_version = '1.0' }

it 'authenticates' do
expect { subject.authenticate! }.to send_request(:post, '/v2.0/tokens',
json_data: credentials.to_json)
end

it 'sets the authentication token' do
subject.authenticate!
expect(subject.auth_token).to eq('auth_token')
end

context 'when there is an object-store' do
it 'sets the storage url' do
subject.authenticate!
expect(subject.storage_url).to eq(test_storage_url)
end

it 'sets the storage token' do
subject.authenticate!
expect(subject.storage_token).to eq('storage_token')
end
end

context 'when there is no object-store' do
let(:bad_body) do
{
'access' => {
'token' => {
'id' => 'auth_token'
},
'serviceCatalog' => []
}
}
end

before do
allow(JSON).to receive(:parse).and_return(bad_body)
end

it 'raises an error' do
expect{ subject.authenticate! }
.to raise_error(SwiftStorage::Service::NotFoundError, 'No object-store service found')
end
end
end

it "failure" do
it 'failure' do
status(401)

expect{ subject.authenticate! }.to raise_error(SwiftStorage::Service::AuthError)
Expand Down

0 comments on commit 77d0523

Please sign in to comment.