Skip to content

Commit

Permalink
Merge pull request #4 from DEFRA/add-end-to-end-test-coverage
Browse files Browse the repository at this point in the history
Add end to end test coverage and main entry point for bucket
  • Loading branch information
cintamani authored Jun 7, 2019
2 parents 28e6bb9 + c2746d9 commit dde4342
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 12 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,43 @@ Or install it yourself as:

$ gem install defra_ruby_aws

## Configuration

Add a new bucket with:

```
# config/initializers/defra_ruby_aws.rb
require "defra_ruby/aws"
DefraRuby::Aws.configure do |config|
config.buckets = [{
# bucket's name, required
name: "defra-ruby-aws",
# AWS bucket access credentials, required
credentials: {
access_key_id: "ACCESS_KEY_ID",
secret_access_key: "SECRET_ACCESS_KEY"
},
# optional - Default to "eu-west-1"
region: "eu-west-1"
}]
end
```

## Usage

TBD
```
file_to_upload = Tempfile.new("test-upload-file.csv")
bucket = DefraRuby::Aws.get_bucket("defra-ruby-aws")
response = bucket.load(file_to_upload)
if response.successful?
# Do something
else
response.error # return the failure error
# Do something else
end
```

## Contributing to this project

Expand Down
1 change: 1 addition & 0 deletions defra_ruby_aws.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rubocop"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "webmock"
end
21 changes: 21 additions & 0 deletions lib/defra_ruby/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,24 @@
require_relative "aws/configuration"

require_relative "aws/services/bucket_loader_service"

module DefraRuby
module Aws
class << self
attr_accessor :configuration

def configure
require "aws-sdk-s3"

self.configuration ||= Configuration.new
yield(configuration)
end

def get_bucket(bucket_name)
configuration.buckets.select do |bucket|
bucket.bucket_name == bucket_name
end.first
end
end
end
end
11 changes: 0 additions & 11 deletions lib/defra_ruby/aws/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@

module DefraRuby
module Aws
class << self
attr_accessor :configuration
end

def self.configure
require "aws-sdk-s3"

self.configuration ||= Configuration.new
yield(configuration)
end

class Configuration
attr_reader :buckets

Expand Down
51 changes: 51 additions & 0 deletions spec/features/upload_file_to_bucket_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe "Defra Ruby AWS" do
it "Upload a file to an AWS bucket" do
configure_gem
stub_successful_request

bucket = DefraRuby::Aws.get_bucket("bulk-test")
response = bucket.load(Tempfile.new("test-bucket-load.test"))

expect(response).to be_successful
end

it "fails gracefully" do
configure_gem
stub_failing_request

bucket = DefraRuby::Aws.get_bucket("bulk-test")
response = bucket.load(Tempfile.new("test-bucket-load.test"))

expect(response).to_not be_successful
expect(response.error).to be_a(Aws::S3::Errors::Forbidden)
end

def configure_gem
DefraRuby::Aws.configure do |config|
config.buckets = [{
name: "bulk-test",
credentials: {
access_key_id: "ACCESS_KEY_ID",
secret_access_key: "SECRET_ACCESS_KEY"
}
}]
end
end

def stub_successful_request
stub_request(:put, /https:\/\/bulk-test\.s3\.eu-west-1\.amazonaws\.com\/test-bucket-load\..+/)
end

def stub_failing_request
stub_request(
:put,
/https:\/\/bulk-test\.s3\.eu-west-1\.amazonaws\.com\/test-bucket-load\..+/
).to_return(
status: 403
)
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "pry-byebug"
require "aws-sdk-s3"
require "webmock/rspec"

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
Expand Down

0 comments on commit dde4342

Please sign in to comment.