From 279fecde223bec61bedda95cb8c60443f5ff6456 Mon Sep 17 00:00:00 2001 From: Dylan Waits Date: Mon, 13 Jun 2016 14:45:28 -0700 Subject: [PATCH] Add `copy` and `move` methods --- lib/dropbox/client.rb | 10 ++++++++ test/test_client.rb | 54 ++++++++++++++++++++++++++++++------------- test/test_metadata.rb | 15 ++++++++++++ 3 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 test/test_metadata.rb diff --git a/lib/dropbox/client.rb b/lib/dropbox/client.rb index fcfb5bb..f6a4cee 100644 --- a/lib/dropbox/client.rb +++ b/lib/dropbox/client.rb @@ -11,6 +11,11 @@ def initialize(access_token) @access_token = access_token end + def copy(from, to) + resp = request('/copy', from_path: from, to_path: to) + object_from_response(resp) + end + def create_folder(path) resp = request('/create_folder', path: path) FolderMetadata.new(resp['id'], resp['path_lower']) @@ -26,6 +31,11 @@ def list_folder(path) resp['entries'].map { |e| object_from_response(e) } end + def move(from, to) + resp = request('/move', from_path: from, to_path: to) + object_from_response(resp) + end + def search(query, path='', max=100) resp = request('/search', path: path, query: query, max_results: max) resp['matches'].map { |m| object_from_response(m['metadata']) } diff --git a/test/test_client.rb b/test/test_client.rb index 20f50fd..893fbd9 100644 --- a/test/test_client.rb +++ b/test/test_client.rb @@ -1,16 +1,12 @@ require 'minitest/autorun' require 'dropbox' -class DropboxTest < Minitest::Test +class DropboxClientTest < Minitest::Test def setup @client = Dropbox::Client.new(ENV['DROPBOX_SDK_ACCESS_TOKEN']) @nonce = Time.now.to_i.to_s end - def test_class - assert Dropbox::Client.is_a?(Class), 'Dropbox::Client is not a Class' - end - def test_client_initialize assert @client.is_a?(Dropbox::Client), 'Dropbox::Client did not initialize' end @@ -27,27 +23,35 @@ def test_client_initialize_error def test_invalid_access_token dbx = Dropbox::Client.new('12345678' * 8) + assert_raises(Dropbox::APIError) do dbx.list_folder('/somedir') end end - def test_folder_initialize - folder = Dropbox::FolderMetadata.new('id:123', '/parent/middle/child') - assert_equal 'child', folder.name + def test_copy + path = '/copied_folder' + folder = @client.copy('/folder_to_copy', path) + + assert_equal 'copied_folder', folder.name + + @client.delete(path) end - def test_file_initialize - file = Dropbox::FileMetadata.new('id:123', '/folder/file', 11) - assert_equal 'file', file.name - assert_equal 11, file.size + def test_copy_error + assert_raises(Dropbox::APIError) do + @client.copy('/folder_to_copy', '/folder_to_copy') + end end def test_create_folder - path = '/dropbox_ruby_sdk_test_dir_' + @nonce + path = '/temp_dir' folder = @client.create_folder(path) + assert folder.is_a?(Dropbox::FolderMetadata) assert_equal path, folder.path + + @client.delete(path) end def test_create_folder_error @@ -60,6 +64,7 @@ def test_delete_folder path = '/folder_to_delete' @client.create_folder(path) folder = @client.delete(path) + assert folder.is_a?(Dropbox::FolderMetadata) assert_equal path[1..-1], folder.name end @@ -72,6 +77,7 @@ def test_delete_folder_error def test_list_folder entries = @client.list_folder('/folder_to_list') + assert entries[0].is_a?(Dropbox::FolderMetadata) assert_equal 'subfolder', entries[0].name assert entries[1].is_a?(Dropbox::FileMetadata) @@ -85,11 +91,27 @@ def test_list_folder_error end end + def test_move + from, to = '/folder_to_move', '/moved_folder' + @client.create_folder(from) + folder = @client.move(from, to) + + assert_equal 'moved_folder', folder.name + + @client.delete(to) + end + + def test_move_error + assert_raises(Dropbox::APIError) do + @client.move('/does_not_exist', '/does_not_exist') + end + end + def test_search matches = @client.search('folder') - assert_equal 2, matches.length - assert matches[1].is_a?(Dropbox::FolderMetadata) - assert_equal 'folder_to_search', matches[1].name + assert_equal 3, matches.length + assert matches[2].is_a?(Dropbox::FolderMetadata) + assert_equal 'folder_to_search', matches[2].name matches = @client.search('sub', '/folder_to_search') assert_equal 2, matches.length diff --git a/test/test_metadata.rb b/test/test_metadata.rb new file mode 100644 index 0000000..af81602 --- /dev/null +++ b/test/test_metadata.rb @@ -0,0 +1,15 @@ +require 'minitest/autorun' +require 'dropbox' + +class DropboxMetadataTest < Minitest::Test + def test_folder_initialize + folder = Dropbox::FolderMetadata.new('id:123', '/parent/middle/child') + assert_equal 'child', folder.name + end + + def test_file_initialize + file = Dropbox::FileMetadata.new('id:123', '/folder/file', 11) + assert_equal 'file', file.name + assert_equal 11, file.size + end +end