Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes parent directories with relative path entries (..) from the S3 and Box providers #237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion browse-everything.gemspec
Expand Up @@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'ruby-box'
spec.add_dependency 'sass-rails'
spec.add_dependency 'signet', '~> 0.8'
spec.add_dependency 'thor', '~> 0.20'
spec.add_dependency 'thor', '~> 0.19'
spec.add_dependency 'typhoeus'

spec.add_development_dependency 'bixby', '>= 1.0'
Expand Down
14 changes: 2 additions & 12 deletions lib/browse_everything/driver/box.rb
Expand Up @@ -36,13 +36,8 @@ def validate_config
# @param [String] id of the file or folder in Box
# @return [Array<RubyBox::File>]
def contents(id = '')
if id.empty?
folder = box_client.root_folder
@entries = []
else
folder = box_client.folder_by_id(id)
@entries = [parent_directory(folder)]
end
folder = id.empty? ? box_client.root_folder : box_client.folder_by_id(id)
@entries = []

folder.items(ITEM_LIMIT, 0, %w[name size created_at]).collect do |f|
@entries << directory_entry(f)
Expand Down Expand Up @@ -138,11 +133,6 @@ def expiration_time
@token.fetch('expires_at', nil).to_i
end

# Used to represent the ".." parent directory of the folder
def parent_directory(folder)
BrowseEverything::FileEntry.new(Pathname(folder.name).join('..'), '', '..', 0, Time.current, true)
end

def directory_entry(file)
BrowseEverything::FileEntry.new(file.id, "#{key}:#{file.id}", file.name, file.size, file.created_at, file.type == 'folder')
end
Expand Down
15 changes: 4 additions & 11 deletions lib/browse_everything/driver/s3.rb
Expand Up @@ -8,7 +8,7 @@ module Driver
class S3 < Base
DEFAULTS = { response_type: :signed_url, expires_in: 14400 }.freeze
RESPONSE_TYPES = %i[signed_url public_url s3_uri].freeze
CONFIG_KEYS = %i[bucket].freeze
CONFIG_KEYS = %i[bucket region].freeze

class << self
attr_accessor :authentication_klass
Expand Down Expand Up @@ -45,7 +45,8 @@ def validate_config
# @return [Array<BrowseEverything::FileEntry>]
def contents(path = '')
path = File.join(path, '') unless path.empty?
init_entries(path)
@entries = []

generate_listing(path)
@sorter.call(@entries)
end
Expand Down Expand Up @@ -111,15 +112,6 @@ def client
@client ||= authenticate
end

def init_entries(path)
@entries = if path.empty?
[]
else
[BrowseEverything::FileEntry.new(Pathname(path).join('..').to_s, '', '..',
0, Time.current, true)]
end
end

def entry_for(name, size, date, dir)
BrowseEverything::FileEntry.new(name, [key, name].join(':'), File.basename(name), size, date, dir)
end
Expand All @@ -138,6 +130,7 @@ def add_files(listing, path)
end

def generate_listing(path)
client
listing = client.list_objects(bucket: config[:bucket], delimiter: '/', prefix: full_path(path))
add_directories(listing)
add_files(listing, path)
Expand Down
14 changes: 2 additions & 12 deletions spec/lib/browse_everything/driver/box_spec.rb
Expand Up @@ -164,9 +164,8 @@
context 'with files and folders in the SaS - Development Team directory' do
let(:folder_id) { '2459961273' }
let(:sas_directory) { provider.contents(folder_id) }
let(:parent_directory) { sas_directory[0] }
let(:apps_dir) { sas_directory[1] }
let(:equipment) { sas_directory[12] }
let(:apps_dir) { sas_directory[0] }
let(:equipment) { sas_directory[11] }
let(:folders_response_body) do
'{"type":"folder","id":"2459961273","sequence_id":"0","etag":"0","name":"SaS - Development Team","created_at":"2014-09-17T13:39:31-07:00","modified_at":"2017-03-17T08:41:40-07:00","description":"","size":152720753,"path_collection":{"total_count":1,"entries":[{"type":"folder","id":"0","sequence_id":null,"etag":null,"name":"All Files"}]},"created_by":{"type":"user","id":"191882215","name":"Daniel Coughlin","login":"dmc186@psu.edu"},"modified_by":{"type":"user","id":"190902775","name":"CAROLYN A COLE","login":"cam156@psu.edu"},"trashed_at":null,"purged_at":null,"content_created_at":"2014-09-17T13:39:31-07:00","content_modified_at":"2017-03-17T08:41:40-07:00","owned_by":{"type":"user","id":"191882215","name":"Daniel Coughlin","login":"dmc186@psu.edu"},"shared_link":{"url":"https:\/\/psu.box.com\/s\/8hb3e06nthwld39ehlg5","download_url":null,"vanity_url":null,"effective_access":"collaborators","is_password_enabled":false,"unshared_at":null,"download_count":0,"preview_count":0,"access":"collaborators","permissions":{"can_download":true,"can_preview":true}},"folder_upload_email":null,"parent":null,"item_status":"active","item_collection":{"total_count":17,"entries":[{"type":"folder","id":"2459974427","sequence_id":"0","etag":"0","name":"Apps&Int"},{"type":"folder","id":"11217040834","sequence_id":"0","etag":"0","name":"Credentials"},{"type":"folder","id":"5662231069","sequence_id":"0","etag":"0","name":"DevOps Presentation"},{"type":"folder","id":"8648394509","sequence_id":"0","etag":"0","name":"old"},{"type":"folder","id":"12445061821","sequence_id":"0","etag":"0","name":"Ops Plan"},{"type":"file","id":"136917954839","file_version":{"type":"file_version","id":"158227338595","sha1":"2e2124c0d6589dd05d03a854b9c5c3a68bbc4501"},"sequence_id":"17","etag":"17","sha1":"2e2124c0d6589dd05d03a854b9c5c3a68bbc4501","name":"2-17 Upcoming ScholarSphere changes.boxnote"},{"type":"file","id":"75228871930","file_version":{"type":"file_version","id":"79668751214","sha1":"43cd4ac728cef7767dc132c26b181133d867d78f"},"sequence_id":"1","etag":"1","sha1":"43cd4ac728cef7767dc132c26b181133d867d78f","name":"99bottles (1).epub"},{"type":"file","id":"72329914177","file_version":{"type":"file_version","id":"76429987577","sha1":"a3310fdad0e6c9654b19d88b35318b8cdc246379"},"sequence_id":"2","etag":"2","sha1":"a3310fdad0e6c9654b19d88b35318b8cdc246379","name":"Development Team Projects and Milestones (not downloaded).xlsx"},{"type":"file","id":"74507436394","file_version":{"type":"file_version","id":"78854145518","sha1":"589ab64efb1ba49537074fd2c56e1570a24a47b5"},"sequence_id":"7","etag":"7","sha1":"589ab64efb1ba49537074fd2c56e1570a24a47b5","name":"Development Team Projects and Milestones - Editable.xlsx"},{"type":"file","id":"72324078305","file_version":{"type":"file_version","id":"76423451525","sha1":"42c0a5a34717cd17e315653643b61d23e94b7cd7"},"sequence_id":"3","etag":"3","sha1":"42c0a5a34717cd17e315653643b61d23e94b7cd7","name":"Development Team Projects and Milestones.xlsx"},{"type":"file","id":"106651625938","file_version":{"type":"file_version","id":"114805197783","sha1":"0c1c90a0186bd3934ce3877e092a228dc1568e93"},"sequence_id":"2","etag":"2","sha1":"0c1c90a0186bd3934ce3877e092a228dc1568e93","name":"Digital Scholarship and Repository Development (DSRD) Org Chart 2016.pdf"},{"type":"file","id":"76960974625","file_version":{"type":"file_version","id":"106919307872","sha1":"74608bfb532feff774502c6132eafe9acb1e5217"},"sequence_id":"162","etag":"162","sha1":"74608bfb532feff774502c6132eafe9acb1e5217","name":"Equipment.boxnote"},{"type":"file","id":"68362464289","file_version":{"type":"file_version","id":"101137863811","sha1":"40a0a3f98926874f1ee62fd522621a4408e0058c"},"sequence_id":"8","etag":"8","sha1":"40a0a3f98926874f1ee62fd522621a4408e0058c","name":"migrationDesign.pptx"},{"type":"file","id":"99028795390","file_version":{"type":"file_version","id":"107183805224","sha1":"7044bd7f0892f915b77b914430c2915161f942d9"},"sequence_id":"37","etag":"37","sha1":"7044bd7f0892f915b77b914430c2915161f942d9","name":"Onboarding.boxnote"},{"type":"file","id":"134865535934","file_version":{"type":"file_version","id":"143971901576","sha1":"32cc33b2cd5cc46f0a3fb924b4779cc387a81018"},"sequence_id":"1","etag":"1","sha1":"32cc33b2cd5cc46f0a3fb924b4779cc387a81018","name":"passenger-test.boxnote"},{"type":"file","id":"92102699797","file_version":{"type":"file_version","id":"98754405909","sha1":"053b762f6d5752c261ef7ff6a3d776853cdb2ca3"},"sequence_id":"1","etag":"1","sha1":"053b762f6d5752c261ef7ff6a3d776853cdb2ca3","name":"sas_development_libraries_mou_2016_08_01.docx"},{"type":"file","id":"22182934895","file_version":{"type":"file_version","id":"50126669941","sha1":"8ca396b5b26ff90d625ea3a49f0cc69ad844476b"},"sequence_id":"41","etag":"41","sha1":"8ca396b5b26ff90d625ea3a49f0cc69ad844476b","name":"TimeBox.xlsx"}],"offset":0,"limit":100,"order":[{"by":"type","direction":"ASC"},{"by":"name","direction":"ASC"}]}}'
end
Expand Down Expand Up @@ -195,15 +194,6 @@
)
end

describe 'the first item' do
subject { parent_directory }

its(:name) { is_expected.to eq('..') }
its(:location) { is_expected.to be_empty }
its(:id) { is_expected.to be_kind_of(Pathname) }
it { is_expected.to be_container }
end

describe 'the second item' do
subject { apps_dir }

Expand Down
27 changes: 12 additions & 15 deletions spec/lib/browse_everything/driver/s3_spec.rb
Expand Up @@ -22,28 +22,32 @@
end

describe 'configuration' do
it '#validate_config' do
it 'rejects an empty config.' do
expect { described_class.new({}) }.to raise_error(BrowseEverything::InitializationError)
end

it 'rejects app_key if app_secret is missing' do
it 'rejects a missing region' do
expect { described_class.new(bucket: 'bucket', app_key: 'APP_KEY') }.to raise_error(BrowseEverything::InitializationError)
end

it 'rejects app_key if app_secret is missing' do
expect { described_class.new(bucket: 'bucket', region: 'us-east-1', app_key: 'APP_KEY') }.to raise_error(BrowseEverything::InitializationError)
end

it 'rejects app_secret if app_key is missing' do
expect { described_class.new(bucket: 'bucket', app_secret: 'APP_SECRET') }.to raise_error(BrowseEverything::InitializationError)
expect { described_class.new(bucket: 'bucket', region: 'us-east-1', app_secret: 'APP_SECRET') }.to raise_error(BrowseEverything::InitializationError)
end

it 'accepts app_key and app_secret together' do
expect { described_class.new(bucket: 'bucket', app_key: 'APP_KEY', app_secret: 'APP_SECRET') }.not_to raise_error
expect { described_class.new(bucket: 'bucket', region: 'us-east-1', app_key: 'APP_KEY', app_secret: 'APP_SECRET') }.not_to raise_error
end

it 'rejects an invalid response type' do
expect { described_class.new(bucket: 'bucket', response_type: :foo) }.to raise_error(BrowseEverything::InitializationError)
expect { described_class.new(bucket: 'bucket', region: 'us-east-1', response_type: :foo) }.to raise_error(BrowseEverything::InitializationError)
end

it 'deprecates :signed_url' do
driver = described_class.new(bucket: 'bucket', signed_url: false)
driver = described_class.new(bucket: 'bucket', region: 'us-east-1', signed_url: false)
expect(driver.config).not_to have_key(:signed_url)
expect(driver.config[:response_type]).to eq(:public_url)
end
Expand Down Expand Up @@ -95,15 +99,8 @@

let(:contents) { provider.contents('foo/') }

context 'with a single directory' do
subject { contents[0] }

its(:name) { is_expected.to eq('..') }
specify { is_expected.to be_container }
end

context 'with a JPEG asset' do
subject { contents[1] }
subject { contents[0] }

its(:name) { is_expected.to eq('baz.jpg') }
its(:location) { is_expected.to eq('s3:foo/baz.jpg') }
Expand All @@ -113,7 +110,7 @@
end

context 'with a PNG asset' do
subject { contents[2] }
subject { contents[1] }

its(:name) { is_expected.to eq('quux.png') }
its(:location) { is_expected.to eq('s3:foo/quux.png') }
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -79,7 +79,8 @@ def stub_configuration
's3' => {
app_key: 'S3AppKey',
app_secret: 'S3AppSecret',
bucket: 's3.bucket'
bucket: 's3.bucket',
region: 's3.region'
})
end

Expand Down