rclone/backend/ulozto/ulozto_test.go
iotmaestro 4b5c10f72e
Add a new backend for uloz.to
Note that this temporarily skips uploads of files over 2.5 GB.

See https://github.com/rclone/rclone/pull/7552#issuecomment-1956316492
for details.
2024-03-26 09:46:47 +00:00

118 lines
3.7 KiB
Go

package ulozto
import (
"bytes"
"context"
"errors"
"testing"
"time"
"github.com/rclone/rclone/backend/ulozto/api"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/fs/object"
"github.com/rclone/rclone/fs/operations"
"github.com/rclone/rclone/fstest"
"github.com/stretchr/testify/require"
"github.com/rclone/rclone/fstest/fstests"
)
// TestIntegration runs integration tests against the remote
func TestIntegration(t *testing.T) {
fstests.Run(t, &fstests.Opt{
RemoteName: "TestUlozto:",
NilObject: (*Object)(nil),
})
}
// TestListWithoutMetadata verifies that basic operations can be performed even if the remote file wasn't written by
// rclone, or the serialized metadata can't be read.
func TestListWithoutMetadata(t *testing.T) {
const (
remoteName = "TestUlozto:"
payload = "42foobar42"
sha256 = "d41f400003e93eb0891977f525e73ecedfa04272d2036f6137106168ecb196ab"
md5 = "8ad32cfeb3dc0f5092261268f335e0a5"
filesize = len(payload)
)
ctx := context.Background()
fstest.Initialise()
subRemoteName, subRemoteLeaf, err := fstest.RandomRemoteName(remoteName)
require.NoError(t, err)
f, err := fs.NewFs(ctx, subRemoteName)
if errors.Is(err, fs.ErrorNotFoundInConfigFile) {
t.Logf("Didn't find %q in config file - skipping tests", remoteName)
return
}
require.NoError(t, err)
file := fstest.Item{ModTime: time.UnixMilli(123456789), Path: subRemoteLeaf, Size: int64(filesize), Hashes: map[hash.Type]string{
hash.SHA256: sha256,
hash.MD5: md5,
}}
// Create a file with the given content and metadata
obj := fstests.PutTestContents(ctx, t, f, &file, payload, false)
// Verify the file has been uploaded
fstest.CheckListing(t, f, []fstest.Item{file})
// Now delete the description metadata
uloztoObj := obj.(*Object)
err = uloztoObj.updateFileProperties(ctx, api.UpdateDescriptionRequest{
Description: "",
})
require.NoError(t, err)
// Listing the file should still succeed, although with estimated mtime and no hashes
fileWithoutDetails := fstest.Item{Path: subRemoteLeaf, Size: int64(filesize), ModTime: uloztoObj.remoteFsMtime, Hashes: map[hash.Type]string{
hash.SHA256: "",
hash.MD5: "",
}}
fstest.CheckListing(t, f, []fstest.Item{fileWithoutDetails})
mtime := time.UnixMilli(987654321)
// When we update the mtime it should be reflected but hashes should stay intact
require.NoError(t, obj.SetModTime(ctx, mtime))
updatedMtimeFile := fstest.Item{Path: subRemoteLeaf, Size: int64(filesize), ModTime: mtime, Hashes: map[hash.Type]string{
hash.SHA256: "",
hash.MD5: "",
}}
fstest.CheckListing(t, f, []fstest.Item{updatedMtimeFile})
// Tear down
require.NoError(t, operations.Purge(ctx, f, ""))
}
// TestUploadLargeFile verifies that files over the supported threshold are not uploaded.
func TestUploadLargeFile(t *testing.T) {
const (
remoteName = "TestUlozto:"
payload = "foobar"
filesize = maxFileSizeBytes + 1
)
ctx := context.Background()
fstest.Initialise()
subRemoteName, subRemoteLeaf, err := fstest.RandomRemoteName(remoteName)
require.NoError(t, err)
f, err := fs.NewFs(ctx, subRemoteName)
if errors.Is(err, fs.ErrorNotFoundInConfigFile) {
t.Logf("Didn't find %q in config file - skipping tests", remoteName)
return
}
require.NoError(t, err)
file := fstest.Item{ModTime: time.UnixMilli(123456789), Path: subRemoteLeaf, Size: int64(filesize)}
obji := object.NewStaticObjectInfo(file.Path, file.ModTime, file.Size, true, nil, nil)
// The payload buffer is just a placeholder which shouldn't be used
_, err = f.Put(ctx, bytes.NewBufferString(payload), obji)
require.Error(t, err, "File size over the supported max threshold.")
// Verify the remote stayed intact
fstest.CheckListing(t, f, []fstest.Item{})
}