test(files/sharings): add e2e tests

This commit is contained in:
hexxa 2021-08-13 15:37:17 +08:00 committed by Hexxa
parent df0264ecfd
commit b1a723be21
2 changed files with 119 additions and 0 deletions

View file

@ -168,3 +168,33 @@ func (cl *FilesClient) DelUploading(filepath string) (*http.Response, string, []
Param(fileshdr.FilePathQuery, filepath). Param(fileshdr.FilePathQuery, filepath).
End() 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
}

View file

@ -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) { t.Run("test concurrently uploading & downloading", func(t *testing.T) {
type mockFile struct { type mockFile struct {
FilePath string FilePath string