Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Tryba committed Jul 8, 2014
1 parent ec0ef95 commit 26214a3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
17 changes: 7 additions & 10 deletions lib/bundler/s3_fetcher.rb
Expand Up @@ -8,21 +8,12 @@ def fetch(uri, counter = 0)
super(sign(uri), counter)
end

protected

# The s3 fetcher does not use the username and password for basic auth,
# so this is a no-op
def add_basic_auth(req)
end

private

# Instead of taking a dependency on aws-sdk, use a method modeled on
# the signing method in https://github.com/rubygems/rubygems/pull/856
def sign(uri, expiration = default_expiration)
uri = uri.dup
unless uri.user && uri.password
raise AuthenticationRequiredError.new("credentials needed in s3 source, like s3://key:secret@bucket-name/", uri.to_s)
raise AuthenticationRequiredError.new("credentials needed in s3 source, like s3://key:secret@bucket-name/")
end

payload = "GET\n\n\n#{expiration}\n/#{uri.host}#{uri.path}"
Expand All @@ -44,4 +35,10 @@ def default_expiration

BASE64_URI_TRANSLATE = { '+' => '%2B', '/' => '%2F', '=' => '%3D' }.freeze
end

protected
# The s3 fetcher does not use the username and password for basic auth,
# so this is a no-op
def add_basic_auth(req)
end
end
1 change: 0 additions & 1 deletion lib/bundler/source/rubygems.rb
Expand Up @@ -164,7 +164,6 @@ def replace_remotes(source)
true
end

protected
def remotes_to_fetchers(remotes)
remotes.map do |uri|
case uri.scheme
Expand Down
29 changes: 29 additions & 0 deletions spec/bundler/s3_fetcher_spec.rb
@@ -0,0 +1,29 @@
require 'spec_helper'

describe Bundler::S3Fetcher do
before do
allow(Bundler).to receive(:root){ Pathname.new("root") }
end

describe "sign" do
it "requires authentication" do
url = "s3://foo"
expect { Bundler::S3Fetcher.new(url).sign(URI(url))}.to raise_error(Bundler::Fetcher::AuthenticationRequiredError)
end

it "signs S3 requests" do
accessId = "a"
secretKey = "b"
url = "s3://#{accessId}:#{secretKey}@foo"
time = Time.utc(2014,6,1).to_i

actual = Bundler::S3Fetcher.new(url).sign(URI(url),time)
expect(actual.host).to eq "foo.s3.amazonaws.com"
expect(actual.scheme).to eq "https"
query = CGI.parse(actual.query)
expect(query['AWSAccessKeyId']).to eq [accessId]
expect(query['Expires']).to eq [time.to_s]
expect(query['Signature']).to eq ["2ZFX8vg7E04u/UqUH9F/cKiQjJA="]
end
end
end
9 changes: 9 additions & 0 deletions spec/bundler/source_spec.rb
Expand Up @@ -22,4 +22,13 @@
end
end
end

describe "remotes_to_fetchers" do
it "turns s3 paths into S3Fetcher objects and other paths into Fetcher objects" do
result = subject.remotes_to_fetchers([URI("s3://foo"),
URI("http://foo")])
expect(result.first).to be_an_instance_of Bundler::S3Fetcher
expect(result.last).to be_an_instance_of Bundler::Fetcher
end
end
end

0 comments on commit 26214a3

Please sign in to comment.