feat(multi-home): enable separated home dir for each user (#64)

* feat(files): make files service supporting home dir

* fix(files): add path access control and avoid redirecting path in the backend

* feat(files): add ListHome API

* fix(server): fix access control issues

* feat(client/web): support multi-home

* feat(server): cleanup

* fix(server): failed to init admin folder
This commit is contained in:
Hexxa 2021-07-24 21:05:36 -05:00 committed by GitHub
parent 9748d0cab4
commit 81da97650b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 527 additions and 212 deletions

View file

@ -14,6 +14,7 @@ import (
"testing"
"github.com/ihexxa/quickshare/src/client"
q "github.com/ihexxa/quickshare/src/handlers"
"github.com/ihexxa/quickshare/src/handlers/fileshdr"
)
@ -22,7 +23,9 @@ func TestFileHandlers(t *testing.T) {
root := "testData"
config := `{
"users": {
"enableAuth": false
"enableAuth": true,
"minUserNameLen": 2,
"minPwdLen": 4
},
"server": {
"debug": true
@ -32,6 +35,11 @@ func TestFileHandlers(t *testing.T) {
}
}`
adminName := "qs"
adminPwd := "quicksh@re"
os.Setenv("DEFAULTADMIN", adminName)
os.Setenv("DEFAULTADMINPWD", adminPwd)
os.RemoveAll(root)
err := os.MkdirAll(root, 0700)
if err != nil {
@ -42,14 +50,23 @@ func TestFileHandlers(t *testing.T) {
srv := startTestServer(config)
defer srv.Shutdown()
fs := srv.depsFS()
cl := client.NewFilesClient(addr)
if !waitForReady(addr) {
t.Fatal("fail to start server")
}
usersCl := client.NewSingleUserClient(addr)
resp, _, errs := usersCl.Login(adminName, adminPwd)
if len(errs) > 0 {
t.Fatal(errs)
} else if resp.StatusCode != 200 {
t.Fatal(resp.StatusCode)
}
token := client.GetCookie(resp.Cookies(), q.TokenCookie)
cl := client.NewFilesClient(addr, token)
assertUploadOK := func(t *testing.T, filePath, content string) bool {
cl := client.NewFilesClient(addr)
cl := client.NewFilesClient(addr, token)
fileSize := int64(len([]byte(content)))
res, _, errs := cl.Create(filePath, fileSize)
@ -82,7 +99,7 @@ func TestFileHandlers(t *testing.T) {
fileSize = int64(len([]byte(content)))
)
cl := client.NewFilesClient(addr)
// cl := client.NewFilesClient(addr)
rd := rand.Intn(3)
switch rd {
@ -151,12 +168,12 @@ func TestFileHandlers(t *testing.T) {
t.Run("test uploading files with duplicated names", func(t *testing.T) {
files := map[string]string{
"dupdir/dup_file1": "12345678",
"dupdir/dup_file2.ext": "12345678",
"0/dupdir/dup_file1": "12345678",
"0/dupdir/dup_file2.ext": "12345678",
}
renames := map[string]string{
"dupdir/dup_file1": "dupdir/dup_file1_1",
"dupdir/dup_file2.ext": "dupdir/dup_file2_1.ext",
"0/dupdir/dup_file1": "0/dupdir/dup_file1_1",
"0/dupdir/dup_file2.ext": "0/dupdir/dup_file2_1.ext",
}
for filePath, content := range files {
@ -183,8 +200,8 @@ func TestFileHandlers(t *testing.T) {
t.Run("test files APIs: Create-UploadChunk-UploadStatus-Metadata-Delete", func(t *testing.T) {
for filePath, content := range map[string]string{
"path1/f1.md": "1111 1111 1111 1111",
"path1/path2/f2.md": "1010 1010 1111 0000 0010",
"0/path1/f1.md": "1111 1111 1111 1111",
"0/path1/path2/f2.md": "1010 1010 1111 0000 0010",
} {
fileSize := int64(len([]byte(content)))
// create a file
@ -196,7 +213,7 @@ func TestFileHandlers(t *testing.T) {
}
// check uploading file
uploadFilePath := path.Join(fileshdr.UploadDir, fmt.Sprintf("%x", sha1.Sum([]byte(filePath))))
uploadFilePath := path.Join(q.UploadDir, "0", fmt.Sprintf("%x", sha1.Sum([]byte(filePath))))
info, err := fs.Stat(uploadFilePath)
if err != nil {
t.Fatal(err)
@ -242,12 +259,12 @@ func TestFileHandlers(t *testing.T) {
}
// check uploaded file
fsFilePath := filepath.Join(fileshdr.FsDir, filePath)
info, err = fs.Stat(fsFilePath)
// fsFilePath := filepath.Join("0", filePath)
info, err = fs.Stat(filePath)
if err != nil {
t.Fatal(err)
} else if info.Name() != filepath.Base(fsFilePath) {
t.Fatal(info.Name(), filepath.Base(fsFilePath))
} else if info.Name() != filepath.Base(filePath) {
t.Fatal(info.Name(), filepath.Base(filePath))
}
// metadata
@ -273,11 +290,11 @@ func TestFileHandlers(t *testing.T) {
t.Run("test dirs APIs: Mkdir-Create-UploadChunk-List", func(t *testing.T) {
for dirPath, files := range map[string]map[string]string{
"dir/path1": map[string]string{
"0/dir/path1": map[string]string{
"f1.md": "11111",
"f2.md": "22222222222",
},
"dir/path2/path2": map[string]string{
"0/dir/path2/path2": map[string]string{
"f3.md": "3333333",
},
} {
@ -314,8 +331,8 @@ func TestFileHandlers(t *testing.T) {
})
t.Run("test operation APIs: Mkdir-Create-UploadChunk-Move-List", func(t *testing.T) {
srcDir := "move/src"
dstDir := "move/dst"
srcDir := "0/move/src"
dstDir := "0/move/dst"
for _, dirPath := range []string{srcDir, dstDir} {
res, _, errs := cl.Mkdir(dirPath)
@ -366,8 +383,8 @@ func TestFileHandlers(t *testing.T) {
t.Run("test download APIs: Download(normal, ranges)", func(t *testing.T) {
for filePath, content := range map[string]string{
"download/path1/f1": "123456",
"download/path1/path2": "12345678",
"0/download/path1/f1": "123456",
"0/download/path1/path2": "12345678",
} {
assertUploadOK(t, filePath, content)
@ -431,8 +448,8 @@ func TestFileHandlers(t *testing.T) {
t.Run("test uploading APIs: Create, ListUploadings, DelUploading", func(t *testing.T) {
files := map[string]string{
"uploadings/path1/f1": "123456",
"uploadings/path1/path2": "12345678",
"0/uploadings/path1/f1": "123456",
"0/uploadings/path1/path2": "12345678",
}
for filePath, content := range files {
@ -487,10 +504,10 @@ func TestFileHandlers(t *testing.T) {
})
t.Run("test uploading APIs: Create, Stop, UploadChunk", func(t *testing.T) {
cl := client.NewFilesClient(addr)
// cl := client.NewFilesClient(addr)
files := map[string]string{
"uploadings/path1/f1": "12345678",
"0/uploadings/path1/f1": "12345678",
}
for filePath, content := range files {
@ -542,12 +559,12 @@ func TestFileHandlers(t *testing.T) {
})
t.Run("test uploading APIs: Create and UploadChunk randomly", func(t *testing.T) {
cl := client.NewFilesClient(addr)
// cl := client.NewFilesClient(addr)
files := map[string]string{
"uploadings/random/path1/f1": "12345678",
"uploadings/random/path1/f2": "87654321",
"uploadings/random/path1/f3": "17654321",
"0/uploadings/random/path1/f1": "12345678",
"0/uploadings/random/path1/f2": "87654321",
"0/uploadings/random/path1/f3": "17654321",
}
for filePath, content := range files {
@ -598,7 +615,7 @@ func TestFileHandlers(t *testing.T) {
t.Fatal("incorrect uploaded size", mRes)
}
isEqual, err := compareFileContent(fs, filePath, content)
isEqual, err := compareFileContent(fs, "0", filePath, content)
if err != nil {
t.Fatalf("err comparing content: %s", err)
} else if !isEqual {
@ -608,4 +625,11 @@ func TestFileHandlers(t *testing.T) {
assetDownloadOK(t, filePath, content)
}
})
resp, _, errs = usersCl.Logout(token)
if len(errs) > 0 {
t.Fatal(errs)
} else if resp.StatusCode != 200 {
t.Fatal(resp.StatusCode)
}
}