Don't try to do put or delete requests to root dir in order to update etag

This commit is contained in:
Garret Alfert 2016-02-02 18:07:12 +01:00
parent 902917d3ad
commit c730333143
2 changed files with 56 additions and 18 deletions

View File

@ -411,10 +411,16 @@ module RemoteStorage
timestamp = (Time.now.to_f * 1000).to_i
parent_directories_for(directory).each do |dir|
# TODO check if we can actually do a put request to the root dir
res = do_put_request("#{url_for_directory(user, dir)}/", timestamp.to_s, "text/plain")
unless dir == ""
res = do_put_request("#{url_for_directory(user, dir)}/", timestamp.to_s, "text/plain")
etag = res.headers[:etag]
else
get_response = do_get_request("#{container_url_for(user)}/?format=json&path=")
etag = etag_for(get_response.body)
end
key = "rs_meta:#{user}:#{dir}/"
metadata = {etag: res.headers[:etag], modified: timestamp}
metadata = {etag: etag, modified: timestamp}
redis.hmset(key, *metadata)
redis.sadd "rs_meta:#{user}:#{parent_directory_for(dir)}:items", "#{top_directory(dir)}/"
end
@ -422,8 +428,9 @@ module RemoteStorage
true
rescue
parent_directories_for(directory).each do |dir|
# TODO check if we can actually do a delete request to the root dir
do_delete_request("#{url_for_directory(user, dir)}/") rescue false
unless dir == ""
do_delete_request("#{url_for_directory(user, dir)}/") rescue false
end
end
false
@ -438,15 +445,21 @@ module RemoteStorage
def delete_dir_objects(user, directory)
parent_directories_for(directory).each do |dir|
if dir_empty?(user, dir)
# TODO check if we can actually do a delete request to the root dir
do_delete_request("#{url_for_directory(user, dir)}/")
unless dir == ""
do_delete_request("#{url_for_directory(user, dir)}/")
end
redis.del "rs_meta:#{user}:#{directory}/"
redis.srem "rs_meta:#{user}:#{parent_directory_for(dir)}:items", "#{dir}/"
else
timestamp = (Time.now.to_f * 1000).to_i
# TODO check if we can actually do a put request to the root dir
res = do_put_request("#{url_for_directory(user, dir)}/", timestamp.to_s, "text/plain")
metadata = {etag: res.headers[:etag], modified: timestamp}
unless dir == ""
res = do_put_request("#{url_for_directory(user, dir)}/", timestamp.to_s, "text/plain")
etag = res.headers[:etag]
else
get_response = do_get_request("#{container_url_for(user)}/?format=json&path=")
etag = etag_for(get_response.body)
end
metadata = {etag: etag, modified: timestamp}
redis.hmset("rs_meta:#{user}:#{dir}/", *metadata)
end
end

View File

@ -40,13 +40,18 @@ describe "App" do
it "creates the directory objects metadata in redis" do
put_stub = OpenStruct.new(headers: {etag: "bla"})
get_stub = OpenStruct.new(body: "rootbody")
RestClient.stub :put, put_stub do
put "/phil/food/aguacate", "si"
put "/phil/food/camaron", "yummi"
RestClient.stub :get, get_stub do
RemoteStorage::Swift.stub_any_instance :etag_for, "rootetag" do
put "/phil/food/aguacate", "si"
put "/phil/food/camaron", "yummi"
end
end
end
metadata = redis.hgetall "rs_meta:phil:/"
metadata["etag"].must_equal "bla"
metadata["etag"].must_equal "rootetag"
metadata["modified"].length.must_equal 13
metadata = redis.hgetall "rs_meta:phil:food/"
@ -65,8 +70,13 @@ describe "App" do
describe "name collision checks" do
it "is successful when there is no name collision" do
put_stub = OpenStruct.new(headers: {etag: "bla"})
get_stub = OpenStruct.new(body: "rootbody")
RestClient.stub :put, put_stub do
put "/phil/food/aguacate", "si"
RestClient.stub :get, get_stub do
RemoteStorage::Swift.stub_any_instance :etag_for, "rootetag" do
put "/phil/food/aguacate", "si"
end
end
end
last_response.status.must_equal 200
@ -157,9 +167,14 @@ describe "App" do
it "deletes the metadata object in redis" do
put_stub = OpenStruct.new(headers: {etag: "bla"})
get_stub = OpenStruct.new(body: "rootbody")
RestClient.stub :put, put_stub do
RestClient.stub :delete, "" do
delete "/phil/food/aguacate"
RestClient.stub :get, get_stub do
RemoteStorage::Swift.stub_any_instance :etag_for, "rootetag" do
delete "/phil/food/aguacate"
end
end
end
end
@ -171,9 +186,14 @@ describe "App" do
old_metadata = redis.hgetall "rs_meta:phil:food/"
put_stub = OpenStruct.new(headers: {etag: "newetag"})
get_stub = OpenStruct.new(body: "rootbody")
RestClient.stub :put, put_stub do
RestClient.stub :delete, "" do
delete "/phil/food/aguacate"
RestClient.stub :get, get_stub do
RemoteStorage::Swift.stub_any_instance :etag_for, "rootetag" do
delete "/phil/food/aguacate"
end
end
end
end
@ -191,10 +211,15 @@ describe "App" do
it "deletes the parent directory objects metadata when deleting all items" do
put_stub = OpenStruct.new(headers: {etag: "bla"})
get_stub = OpenStruct.new(body: "rootbody")
RestClient.stub :put, put_stub do
RestClient.stub :delete, "" do
delete "/phil/food/aguacate"
delete "/phil/food/camaron"
RestClient.stub :get, get_stub do
RemoteStorage::Swift.stub_any_instance :etag_for, "rootetag" do
delete "/phil/food/aguacate"
delete "/phil/food/camaron"
end
end
end
end