From b1a723be21f1bc1a5b1e4b7d7e7c01706dfacd32 Mon Sep 17 00:00:00 2001 From: hexxa Date: Fri, 13 Aug 2021 15:37:17 +0800 Subject: [PATCH] test(files/sharings): add e2e tests --- src/client/files.go | 30 +++++++++++ src/server/server_files_test.go | 89 +++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/src/client/files.go b/src/client/files.go index e80d3d2..08424b4 100644 --- a/src/client/files.go +++ b/src/client/files.go @@ -168,3 +168,33 @@ func (cl *FilesClient) DelUploading(filepath string) (*http.Response, string, [] Param(fileshdr.FilePathQuery, filepath). End() } + +func (cl *FilesClient) AddSharing(dirpath string) (*http.Response, string, []error) { + return cl.r.Post(cl.url("/v1/fs/sharings")). + AddCookie(cl.token). + Send(fileshdr.SharingReq{SharingPath: dirpath}). + End() +} + +func (cl *FilesClient) DelSharing(dirpath string) (*http.Response, string, []error) { + return cl.r.Delete(cl.url("/v1/fs/sharings")). + AddCookie(cl.token). + Send(fileshdr.SharingReq{SharingPath: dirpath}). + End() +} + +func (cl *FilesClient) ListSharings() (*http.Response, *fileshdr.SharingResp, []error) { + resp, body, errs := cl.r.Get(cl.url("/v1/fs/sharings")). + AddCookie(cl.token). + End() + if len(errs) > 0 { + return nil, nil, errs + } + + shResp := &fileshdr.SharingResp{} + err := json.Unmarshal([]byte(body), shResp) + if err != nil { + return nil, nil, append(errs, err) + } + return resp, shResp, nil +} diff --git a/src/server/server_files_test.go b/src/server/server_files_test.go index f068506..f97532f 100644 --- a/src/server/server_files_test.go +++ b/src/server/server_files_test.go @@ -346,6 +346,95 @@ func TestFileHandlers(t *testing.T) { } }) + t.Run("test sharing APIs: Upload-AddSharing-ListSharings-List-Download-DelSharing-ListSharings", func(t *testing.T) { + files := map[string]string{ + "0/files/sharing/path1/f1": "123456", + "0/files/sharing/path2/path2": "12345678", + } + + for filePath, content := range files { + assertUploadOK(t, filePath, content, addr, token) + err = fs.Sync() + if err != nil { + t.Fatal(err) + } + } + + // add sharings + sharedPaths := map[string]bool{} + for filePath := range files { + dirPath := filepath.Dir(filePath) + sharedPaths[dirPath] = true + + res, _, errs := cl.AddSharing(dirPath) + if len(errs) > 0 { + t.Fatal(errs) + } else if res.StatusCode != 200 { + t.Fatal(res.StatusCode) + } + } + + // check listSharings + res, shRes, errs := cl.ListSharings() + if len(errs) > 0 { + t.Fatal(errs) + } else if res.StatusCode != 200 { + t.Fatal(res.StatusCode) + } + for _, dirPath := range shRes.SharingDirs { + if !sharedPaths[dirPath] { + t.Fatalf("sharing %s not found", dirPath) + } + } + + for _, dirPath := range shRes.SharingDirs { + res, lsResp, errs := cl.List(dirPath) + if len(errs) > 0 { + t.Fatal(errs) + } else if res.StatusCode != 200 { + t.Fatal(res.StatusCode) + } + if lsResp.Cwd != dirPath { + t.Fatalf("list sharing folder: incorrect cwd (%s)", lsResp.Cwd) + } + if len(lsResp.Metadatas) != 1 { + t.Fatalf("list sharing folder: incorrect metadata size (%d)", len(lsResp.Metadatas)) + } + } + + for filePath, content := range files { + assertDownloadOK(t, filePath, content, addr, token) + } + + i := 0 + for filePath := range files { + if i++; i%2 == 0 { + dirPath := filepath.Dir(filePath) + delete(sharedPaths, dirPath) + + res, _, errs := cl.DelSharing(dirPath) + if len(errs) > 0 { + t.Fatal(errs) + } else if res.StatusCode != 200 { + t.Fatal(res.StatusCode) + } + } + } + + // check listSharings + res, shRes, errs = cl.ListSharings() + if len(errs) > 0 { + t.Fatal(errs) + } else if res.StatusCode != 200 { + t.Fatal(res.StatusCode) + } + for _, dirPath := range shRes.SharingDirs { + if !sharedPaths[dirPath] { + t.Fatalf("sharing %s not found", dirPath) + } + } + }) + t.Run("test concurrently uploading & downloading", func(t *testing.T) { type mockFile struct { FilePath string