ulozto_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package ulozto
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "time"
  7. "github.com/rclone/rclone/backend/ulozto/api"
  8. "github.com/rclone/rclone/fs"
  9. "github.com/rclone/rclone/fs/hash"
  10. "github.com/rclone/rclone/fs/operations"
  11. "github.com/rclone/rclone/fstest"
  12. "github.com/stretchr/testify/require"
  13. "github.com/rclone/rclone/fstest/fstests"
  14. )
  15. // TestIntegration runs integration tests against the remote
  16. func TestIntegration(t *testing.T) {
  17. fstests.Run(t, &fstests.Opt{
  18. RemoteName: "TestUlozto:",
  19. NilObject: (*Object)(nil),
  20. })
  21. }
  22. // TestListWithoutMetadata verifies that basic operations can be performed even if the remote file wasn't written by
  23. // rclone, or the serialized metadata can't be read.
  24. func TestListWithoutMetadata(t *testing.T) {
  25. const (
  26. remoteName = "TestUlozto:"
  27. payload = "42foobar42"
  28. sha256 = "d41f400003e93eb0891977f525e73ecedfa04272d2036f6137106168ecb196ab"
  29. md5 = "8ad32cfeb3dc0f5092261268f335e0a5"
  30. filesize = len(payload)
  31. )
  32. ctx := context.Background()
  33. fstest.Initialise()
  34. subRemoteName, subRemoteLeaf, err := fstest.RandomRemoteName(remoteName)
  35. require.NoError(t, err)
  36. f, err := fs.NewFs(ctx, subRemoteName)
  37. if errors.Is(err, fs.ErrorNotFoundInConfigFile) {
  38. t.Logf("Didn't find %q in config file - skipping tests", remoteName)
  39. return
  40. }
  41. require.NoError(t, err)
  42. file := fstest.Item{ModTime: time.UnixMilli(123456789), Path: subRemoteLeaf, Size: int64(filesize), Hashes: map[hash.Type]string{
  43. hash.SHA256: sha256,
  44. hash.MD5: md5,
  45. }}
  46. // Create a file with the given content and metadata
  47. obj := fstests.PutTestContents(ctx, t, f, &file, payload, false)
  48. // Verify the file has been uploaded
  49. fstest.CheckListing(t, f, []fstest.Item{file})
  50. // Now delete the description metadata
  51. uloztoObj := obj.(*Object)
  52. err = uloztoObj.updateFileProperties(ctx, api.UpdateDescriptionRequest{
  53. Description: "",
  54. })
  55. require.NoError(t, err)
  56. // Listing the file should still succeed, although with estimated mtime and no hashes
  57. fileWithoutDetails := fstest.Item{Path: subRemoteLeaf, Size: int64(filesize), ModTime: uloztoObj.remoteFsMtime, Hashes: map[hash.Type]string{
  58. hash.SHA256: "",
  59. hash.MD5: "",
  60. }}
  61. fstest.CheckListing(t, f, []fstest.Item{fileWithoutDetails})
  62. mtime := time.UnixMilli(987654321)
  63. // When we update the mtime it should be reflected but hashes should stay intact
  64. require.NoError(t, obj.SetModTime(ctx, mtime))
  65. updatedMtimeFile := fstest.Item{Path: subRemoteLeaf, Size: int64(filesize), ModTime: mtime, Hashes: map[hash.Type]string{
  66. hash.SHA256: "",
  67. hash.MD5: "",
  68. }}
  69. fstest.CheckListing(t, f, []fstest.Item{updatedMtimeFile})
  70. // Tear down
  71. require.NoError(t, operations.Purge(ctx, f, ""))
  72. }