Support directory listings

This commit is contained in:
2012-09-26 11:02:12 +02:00
parent 5155d36de4
commit ebe499211a
4 changed files with 292 additions and 12 deletions

153
spec/directories_spec.rb Normal file
View File

@@ -0,0 +1,153 @@
require_relative "spec_helper"
describe "Directories" do
include Rack::Test::Methods
include RemoteStorage::Riak
before do
purge_all_buckets
auth = auth_bucket.new("jimmy:123")
auth.data = ["documents:r", "tasks:rw"]
auth.store
header "Authorization", "Bearer 123"
end
describe "GET listing" do
before do
put "/jimmy/tasks/home/foo", "do the laundry"
put "/jimmy/tasks/home/bar", "do the laundry"
end
it "lists the objects with a timestamp of the last modification" do
get "/jimmy/tasks/home/"
# File.open("stacktrace.html", "w") do |f|
# f.write last_response.body
# end
last_response.status.must_equal 200
last_response.content_type.must_equal "application/json"
content = JSON.parse(last_response.body)
content["foo"].to_s.must_match /\d+/
content["foo"].to_s.length.must_be :>=, 10
end
it "has a Last-Modifier header set" do
get "/jimmy/tasks/home/"
last_response.headers["Last-Modified"].wont_be_nil
end
it "has CORS headers set" do
get "/jimmy/tasks/home/"
last_response.headers["Access-Control-Allow-Origin"].must_equal "*"
last_response.headers["Access-Control-Allow-Methods"].must_equal "GET, PUT, DELETE"
last_response.headers["Access-Control-Allow-Headers"].must_equal "Authorization, Content-Type, Origin"
end
context "with sub-directories" do
before do
put "/jimmy/tasks/home/laundry", "do the laundry"
end
end
it "lists the containing objects as well as the direct sub-directories" do
get "/jimmy/tasks/"
last_response.status.must_equal 200
content = JSON.parse(last_response.body)
# p content
# dir = directory_bucket.get("jimmy:tasks/home")
# puts dir.indexes.inspect
content["home/"].to_s.must_match /\d+/
end
context "for a sub-directory" do
before do
put "/jimmy/tasks/home/laundry", "do the laundry"
end
it "lists the objects with timestamp" do
get "/jimmy/tasks/home/"
last_response.status.must_equal 200
content = JSON.parse(last_response.body)
content["laundry"].to_s.must_match /\d+/
content["laundry"].to_s.length.must_be :>=, 10
end
end
describe "for an empty or absent directory" do
it "returns an empty listing" do
get "/jimmy/documents/notfound/"
last_response.status.must_equal 200
last_response.body.must_equal "{}"
end
end
end
describe "directory object" do
describe "PUT file" do
context "no existing directory object" do
it "creates a new directory object" do
put "/jimmy/tasks/home/trash", "take out the trash"
object = directory_bucket.get("jimmy:tasks/home")
object.last_modified.wont_be_nil
end
it "sets the correct index for the directory object" do
put "/jimmy/tasks/home/trash", "take out the trash"
object = directory_bucket.get("jimmy:tasks/home")
object.indexes["directory_bin"].must_include "tasks"
end
end
context "existing directory object" do
before do
@directory = directory_bucket.new("jimmy:tasks/home")
@directory.content_type = "text/plain"
@directory.raw_data = ""
@directory.store
@old_timestamp = @directory.reload.last_modified
end
it "updates the timestamp of the directory" do
sleep 1
put "/jimmy/tasks/home/trash", "take out the trash"
@directory.reload
@directory.last_modified.must_be :>, @old_timestamp
end
end
end
describe "DELETE file" do
context "last file in directory" do
before do
directory_bucket.delete("jimmy:tasks")
put "/jimmy/tasks/trash", "take out the trash"
end
it "deletes the directory object" do
delete "/jimmy/tasks/trash"
last_response.status.must_equal 204
lambda {
directory_bucket.get("jimmy:tasks")
}.must_raise Riak::HTTPFailedRequest
end
end
end
end
end

View File

@@ -14,3 +14,33 @@ ENV["RACK_ENV"] = "test"
config = File.read(File.expand_path('../config.yml', File.dirname(__FILE__)))
riak_config = YAML.load(config)[ENV['RACK_ENV']]['riak'].symbolize_keys
set :riak_config, riak_config
::Riak.disable_list_keys_warnings = true
def app
LiquorCabinet
end
def storage_client
@storage_client ||= ::Riak::Client.new(settings.riak_config)
end
def data_bucket
@data_bucket ||= storage_client.bucket("user_data")
end
def auth_bucket
@auth_bucket ||= storage_client.bucket("authorizations")
end
def directory_bucket
@directory_bucket ||= storage_client.bucket("rs_directories")
end
def purge_all_buckets
[data_bucket, directory_bucket, auth_bucket].each do |bucket|
bucket.keys.each {|key| bucket.delete key}
end
end
alias context describe