Skip to content

Commit

Permalink
Add more tests for WebDAV/NextCloud
Browse files Browse the repository at this point in the history
  • Loading branch information
nono committed May 15, 2024
1 parent 4608054 commit 0ae5839
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
3 changes: 2 additions & 1 deletion model/nextcloud/nextcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ func (nc *NextCloud) Downstream(path, dirID string, cozyMetadata *vfs.FilesCozyM
}
defer dl.Content.Close()

size, _ := strconv.Atoi(dl.Length)
mime, class := vfs.ExtractMimeAndClass(dl.Mime)
doc, err := vfs.NewFileDoc(
filepath.Base(path),
dirID,
0, // size
int64(size),
nil, // md5sum
mime,
class,
Expand Down
45 changes: 42 additions & 3 deletions tests/system/lib/nextcloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,65 @@ def list(path)
JSON.parse(res.body)
end

def move(path, to)
opts = {
authorization: "Bearer #{@token}"
}
@inst.client["#{@base_path}/move#{encode_path path}?To=#{to}"].post nil, opts
end

def copy(path, name)
opts = {
authorization: "Bearer #{@token}"
}
@inst.client["#{@base_path}/copy#{encode_path path}?Name=#{name}"].post nil, opts
end

def mkdir(path)
opts = {
accept: :json,
authorization: "Bearer #{@token}"
}
@inst.client["#{@base_path}#{encode_path path}?Type=directory"].put nil, opts
end

def upload(path, filename)
mime = MiniMime.lookup_by_filename(filename).content_type
ap mime
content = File.read filename
opts = {
accept: :json,
authorization: "Bearer #{@token}",
:"content-type" => mime
}
@inst.client["#{@base_path}#{encode_path path}?Type=file"].put content, opts
end

def download(path)
opts = {
authorization: "Bearer #{@token}"
}
@inst.client["#{@base_path}#{encode_path path}?Dl=1"].get(opts).body
end

def upstream(path, from)
opts = {
authorization: "Bearer #{@token}"
}
@inst.client["#{@base_path}/upstream#{encode_path path}?From=#{from}"].post nil, opts
end

def downstream(path, to)
opts = {
authorization: "Bearer #{@token}"
}
@inst.client["#{@base_path}/downstream#{encode_path path}?To=#{to}"].post nil, opts
end

def delete(path)
opts = {
authorization: "Bearer #{@token}"
}
@inst.client["#{@base_path}#{encode_path path}"].delete opts
end

def encode_path(path)
path.split("/").map { |s| ERB::Util.url_encode s }.join("/")
end
Expand Down
33 changes: 31 additions & 2 deletions tests/system/tests/webdav_nextcloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,45 @@
dir_name = "#{Faker::Superhero.name} ⚡️"
nextcloud.mkdir "/#{dir_name}"

file_name = "#{Faker::Science.science}.jpg"
file_name = "1. #{Faker::Science.science}.jpg"
file_path = "../fixtures/wet-cozy_20160910__M4Dz.jpg"
nextcloud.upload "/#{dir_name}/#{file_name}", file_path
expected = File.read(file_path, encoding: Encoding::ASCII_8BIT)
content = nextcloud.download "/#{dir_name}/#{file_name}"
assert_equal content, expected

opts = CozyFile.options_from_fixture("README.md")
file = CozyFile.create inst, opts
other_name = "2. #{file.name}"
nextcloud.upstream "/#{dir_name}/#{other_name}", file.couch_id

list = nextcloud.list "/#{dir_name}"
assert_equal 1, list.dig("meta", "count")
assert_equal 2, list.dig("meta", "count")
assert_equal "file", list.dig("data", 0, "attributes", "type")
assert_equal file_name, list.dig("data", 0, "attributes", "name")
assert_equal File.size(file_path), list.dig("data", 0, "attributes", "size")
assert_equal "image/jpeg", list.dig("data", 0, "attributes", "mime")
assert_equal "image", list.dig("data", 0, "attributes", "class")
assert_equal "file", list.dig("data", 1, "attributes", "type")
assert_equal other_name, list.dig("data", 1, "attributes", "name")
assert_equal File.size("README.md"), list.dig("data", 1, "attributes", "size")
assert_equal "text/markdown", list.dig("data", 1, "attributes", "mime")
assert_equal "text", list.dig("data", 1, "attributes", "class")

copy_name = "#{Faker::Mountain.name}.jpg"
nextcloud.copy "/#{dir_name}/#{file_name}", copy_name
nextcloud.move "/#{dir_name}/#{copy_name}", "/#{copy_name}"
content = nextcloud.download "/#{copy_name}"
assert_equal content, expected

nextcloud.downstream "/#{dir_name}/#{file_name}", Folder::ROOT_DIR
f = CozyFile.find_by_path inst, "/#{file_name}"
assert_equal File.size(file_path), f.size.to_i
assert_equal "image/jpeg", f.mime

nextcloud.delete "/#{dir_name}/#{other_name}"
list = nextcloud.list "/#{dir_name}"
assert_equal 0, list.dig("meta", "count")
end

container.remove
Expand Down

0 comments on commit 0ae5839

Please sign in to comment.