2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-26 09:34:44 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-08 19:42:07 +00:00
|
|
|
"context"
|
2019-07-26 09:34:44 +00:00
|
|
|
"fmt"
|
2019-10-09 09:48:54 +00:00
|
|
|
"html/template"
|
2020-02-03 23:08:00 +00:00
|
|
|
"io/ioutil"
|
2019-07-26 09:34:44 +00:00
|
|
|
"net/http"
|
2019-08-08 19:42:07 +00:00
|
|
|
"net/http/httptest"
|
2020-01-31 18:04:00 +00:00
|
|
|
"net/url"
|
2020-01-19 06:41:05 +00:00
|
|
|
"os"
|
2020-02-03 23:08:00 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2020-01-31 18:04:00 +00:00
|
|
|
"strings"
|
2019-07-26 09:34:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2020-01-19 06:41:05 +00:00
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-02-23 10:30:26 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-07-26 09:34:44 +00:00
|
|
|
)
|
|
|
|
|
2019-08-03 11:19:00 +00:00
|
|
|
const (
|
|
|
|
invalidURL = "http://foo\x7f.com/"
|
|
|
|
inactiveURL = "http://127.0.0.1:12345"
|
|
|
|
)
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestGetRespStatus(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
err = &dataprovider.MethodDisabledError{}
|
|
|
|
respStatus := getRespStatus(err)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, respStatus)
|
2019-07-26 09:34:44 +00:00
|
|
|
err = fmt.Errorf("generic error")
|
|
|
|
respStatus = getRespStatus(err)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, respStatus)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckResponse(t *testing.T) {
|
2019-08-04 19:37:31 +00:00
|
|
|
err := checkResponse(http.StatusOK, http.StatusCreated)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-08-04 19:37:31 +00:00
|
|
|
err = checkResponse(http.StatusBadRequest, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestCheckFolder(t *testing.T) {
|
|
|
|
expected := &vfs.BaseVirtualFolder{}
|
|
|
|
actual := &vfs.BaseVirtualFolder{}
|
|
|
|
err := checkFolder(expected, actual)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expected.ID = 1
|
|
|
|
actual.ID = 2
|
|
|
|
err = checkFolder(expected, actual)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expected.ID = 2
|
|
|
|
actual.ID = 2
|
|
|
|
expected.MappedPath = "path"
|
|
|
|
err = checkFolder(expected, actual)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expected.MappedPath = ""
|
|
|
|
expected.LastQuotaUpdate = 1
|
|
|
|
err = checkFolder(expected, actual)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expected.LastQuotaUpdate = 0
|
|
|
|
expected.UsedQuotaFiles = 1
|
|
|
|
err = checkFolder(expected, actual)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expected.UsedQuotaFiles = 0
|
|
|
|
expected.UsedQuotaSize = 1
|
|
|
|
err = checkFolder(expected, actual)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expected.UsedQuotaSize = 0
|
|
|
|
expected.Users = append(expected.Users, "user1")
|
|
|
|
err = checkFolder(expected, actual)
|
|
|
|
assert.Error(t, err)
|
|
|
|
actual.Users = append(actual.Users, "user2")
|
|
|
|
err = checkFolder(expected, actual)
|
|
|
|
assert.Error(t, err)
|
|
|
|
expected.Users = nil
|
|
|
|
actual.Users = nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestCheckUser(t *testing.T) {
|
2019-12-30 17:37:50 +00:00
|
|
|
expected := &dataprovider.User{}
|
|
|
|
actual := &dataprovider.User{}
|
2019-07-26 09:34:44 +00:00
|
|
|
actual.Password = "password"
|
|
|
|
err := checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
actual.Password = ""
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.ID = 1
|
|
|
|
actual.ID = 2
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.ID = 2
|
|
|
|
actual.ID = 2
|
2019-12-25 17:20:19 +00:00
|
|
|
expected.Permissions = make(map[string][]string)
|
|
|
|
expected.Permissions["/"] = []string{dataprovider.PermCreateDirs, dataprovider.PermDelete, dataprovider.PermDownload}
|
|
|
|
actual.Permissions = make(map[string][]string)
|
2019-12-30 17:37:50 +00:00
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
actual.Permissions["/"] = []string{dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks}
|
2019-07-26 09:34:44 +00:00
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
expected.Permissions["/"] = append(expected.Permissions["/"], dataprovider.PermRename)
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
expected.Permissions = make(map[string][]string)
|
|
|
|
expected.Permissions["/somedir"] = []string{dataprovider.PermAny}
|
|
|
|
actual.Permissions = make(map[string][]string)
|
|
|
|
actual.Permissions["/otherdir"] = []string{dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks}
|
2019-07-26 09:34:44 +00:00
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-12-30 17:37:50 +00:00
|
|
|
expected.Permissions = make(map[string][]string)
|
|
|
|
actual.Permissions = make(map[string][]string)
|
2020-02-19 21:39:30 +00:00
|
|
|
actual.FsConfig.Provider = 1
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
actual.FsConfig.Provider = 0
|
|
|
|
expected.VirtualFolders = append(expected.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: os.TempDir(),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
actual.VirtualFolders = append(actual.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: os.TempDir(),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
})
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-19 21:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompareUserFilters(t *testing.T) {
|
|
|
|
expected := &dataprovider.User{}
|
|
|
|
actual := &dataprovider.User{}
|
|
|
|
actual.ID = 1
|
|
|
|
expected.ID = 1
|
2019-12-30 17:37:50 +00:00
|
|
|
expected.Filters.AllowedIP = []string{}
|
|
|
|
actual.Filters.AllowedIP = []string{"192.168.1.2/32"}
|
2020-02-19 21:39:30 +00:00
|
|
|
err := checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-12-30 17:37:50 +00:00
|
|
|
expected.Filters.AllowedIP = []string{"192.168.1.3/32"}
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-12-30 17:37:50 +00:00
|
|
|
expected.Filters.AllowedIP = []string{}
|
|
|
|
actual.Filters.AllowedIP = []string{}
|
|
|
|
expected.Filters.DeniedIP = []string{}
|
|
|
|
actual.Filters.DeniedIP = []string{"192.168.1.2/32"}
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-12-30 17:37:50 +00:00
|
|
|
expected.Filters.DeniedIP = []string{"192.168.1.3/32"}
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
expected.Filters.DeniedIP = []string{}
|
|
|
|
actual.Filters.DeniedIP = []string{}
|
2020-02-19 21:39:30 +00:00
|
|
|
expected.Filters.DeniedLoginMethods = []string{}
|
|
|
|
actual.Filters.DeniedLoginMethods = []string{dataprovider.SSHLoginMethodPublicKey}
|
2020-01-19 06:41:05 +00:00
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-19 21:39:30 +00:00
|
|
|
expected.Filters.DeniedLoginMethods = []string{dataprovider.SSHLoginMethodPassword}
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-03-01 21:10:29 +00:00
|
|
|
expected.Filters.DeniedLoginMethods = []string{}
|
|
|
|
actual.Filters.DeniedLoginMethods = []string{}
|
|
|
|
expected.Filters.FileExtensions = append(expected.Filters.FileExtensions, dataprovider.ExtensionsFilter{
|
|
|
|
Path: "/",
|
|
|
|
AllowedExtensions: []string{".jpg", ".png"},
|
|
|
|
DeniedExtensions: []string{".zip", ".rar"},
|
|
|
|
})
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-03-01 21:10:29 +00:00
|
|
|
actual.Filters.FileExtensions = append(actual.Filters.FileExtensions, dataprovider.ExtensionsFilter{
|
|
|
|
Path: "/sub",
|
|
|
|
AllowedExtensions: []string{".jpg", ".png"},
|
|
|
|
DeniedExtensions: []string{".zip", ".rar"},
|
|
|
|
})
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-03-01 21:10:29 +00:00
|
|
|
actual.Filters.FileExtensions[0] = dataprovider.ExtensionsFilter{
|
|
|
|
Path: "/",
|
|
|
|
AllowedExtensions: []string{".jpg"},
|
|
|
|
DeniedExtensions: []string{".zip", ".rar"},
|
|
|
|
}
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-03-01 21:10:29 +00:00
|
|
|
actual.Filters.FileExtensions[0] = dataprovider.ExtensionsFilter{
|
|
|
|
Path: "/",
|
|
|
|
AllowedExtensions: []string{".tiff", ".png"},
|
|
|
|
DeniedExtensions: []string{".zip", ".rar"},
|
|
|
|
}
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-03-01 21:10:29 +00:00
|
|
|
actual.Filters.FileExtensions[0] = dataprovider.ExtensionsFilter{
|
|
|
|
Path: "/",
|
|
|
|
AllowedExtensions: []string{".jpg", ".png"},
|
|
|
|
DeniedExtensions: []string{".tar.gz", ".rar"},
|
|
|
|
}
|
|
|
|
err = checkUser(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompareUserFields(t *testing.T) {
|
2019-12-30 17:37:50 +00:00
|
|
|
expected := &dataprovider.User{}
|
|
|
|
actual := &dataprovider.User{}
|
2019-12-25 17:20:19 +00:00
|
|
|
expected.Permissions = make(map[string][]string)
|
|
|
|
actual.Permissions = make(map[string][]string)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.Username = "test"
|
|
|
|
err := compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.Username = ""
|
|
|
|
expected.HomeDir = "homedir"
|
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.HomeDir = ""
|
|
|
|
expected.UID = 1
|
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.UID = 0
|
|
|
|
expected.GID = 1
|
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.GID = 0
|
|
|
|
expected.MaxSessions = 2
|
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.MaxSessions = 0
|
|
|
|
expected.QuotaSize = 4096
|
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.QuotaSize = 0
|
|
|
|
expected.QuotaFiles = 2
|
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.QuotaFiles = 0
|
2019-12-25 17:20:19 +00:00
|
|
|
expected.Permissions["/"] = []string{dataprovider.PermCreateDirs}
|
2019-07-26 09:34:44 +00:00
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.Permissions = nil
|
|
|
|
expected.UploadBandwidth = 64
|
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
expected.UploadBandwidth = 0
|
|
|
|
expected.DownloadBandwidth = 128
|
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-11-13 10:36:21 +00:00
|
|
|
expected.DownloadBandwidth = 0
|
|
|
|
expected.Status = 1
|
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-11-13 10:36:21 +00:00
|
|
|
expected.Status = 0
|
|
|
|
expected.ExpirationDate = 123
|
|
|
|
err = compareEqualsUserFields(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
2019-08-03 11:19:00 +00:00
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
func TestCompareUserFsConfig(t *testing.T) {
|
|
|
|
expected := &dataprovider.User{}
|
|
|
|
actual := &dataprovider.User{}
|
|
|
|
expected.FsConfig.Provider = 1
|
|
|
|
err := compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
expected.FsConfig.Provider = 0
|
|
|
|
expected.FsConfig.S3Config.Bucket = "bucket"
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
expected.FsConfig.S3Config.Bucket = ""
|
|
|
|
expected.FsConfig.S3Config.Region = "region"
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
expected.FsConfig.S3Config.Region = ""
|
|
|
|
expected.FsConfig.S3Config.AccessKey = "access key"
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
expected.FsConfig.S3Config.AccessKey = ""
|
|
|
|
actual.FsConfig.S3Config.AccessSecret = "access secret"
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
secret, _ := utils.EncryptData("access secret")
|
|
|
|
actual.FsConfig.S3Config.AccessSecret = ""
|
|
|
|
expected.FsConfig.S3Config.AccessSecret = secret
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
expected.FsConfig.S3Config.AccessSecret = utils.RemoveDecryptionKey(secret)
|
|
|
|
actual.FsConfig.S3Config.AccessSecret = utils.RemoveDecryptionKey(secret) + "a"
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
expected.FsConfig.S3Config.AccessSecret = "test"
|
|
|
|
actual.FsConfig.S3Config.AccessSecret = ""
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
expected.FsConfig.S3Config.AccessSecret = ""
|
|
|
|
actual.FsConfig.S3Config.AccessSecret = ""
|
|
|
|
expected.FsConfig.S3Config.Endpoint = "http://127.0.0.1:9000/"
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
expected.FsConfig.S3Config.Endpoint = ""
|
|
|
|
expected.FsConfig.S3Config.StorageClass = "Standard"
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 22:23:09 +00:00
|
|
|
expected.FsConfig.S3Config.StorageClass = ""
|
|
|
|
expected.FsConfig.S3Config.KeyPrefix = "somedir/subdir"
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
expected.FsConfig.S3Config.KeyPrefix = ""
|
2020-03-13 16:28:55 +00:00
|
|
|
expected.FsConfig.S3Config.UploadPartSize = 10
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-03-13 16:28:55 +00:00
|
|
|
expected.FsConfig.S3Config.UploadPartSize = 0
|
2020-03-13 18:13:58 +00:00
|
|
|
expected.FsConfig.S3Config.UploadConcurrency = 3
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-19 08:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompareUserGCSConfig(t *testing.T) {
|
|
|
|
expected := &dataprovider.User{}
|
|
|
|
actual := &dataprovider.User{}
|
2020-01-31 18:04:00 +00:00
|
|
|
expected.FsConfig.GCSConfig.KeyPrefix = "somedir/subdir"
|
2020-02-19 08:41:15 +00:00
|
|
|
err := compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
expected.FsConfig.GCSConfig.KeyPrefix = ""
|
|
|
|
expected.FsConfig.GCSConfig.Bucket = "bucket"
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
expected.FsConfig.GCSConfig.Bucket = ""
|
|
|
|
expected.FsConfig.GCSConfig.StorageClass = "Standard"
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
expected.FsConfig.GCSConfig.StorageClass = ""
|
2020-02-19 08:41:15 +00:00
|
|
|
expected.FsConfig.GCSConfig.AutomaticCredentials = 1
|
|
|
|
err = compareUserFsConfig(expected, actual)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-19 08:41:15 +00:00
|
|
|
expected.FsConfig.GCSConfig.AutomaticCredentials = 0
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGCSWebInvalidFormFile(t *testing.T) {
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", "test_username")
|
|
|
|
form.Set("fs_provider", "2")
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, webUserPath, strings.NewReader(form.Encode()))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
2020-05-06 17:36:34 +00:00
|
|
|
err := req.ParseForm()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getFsConfigFromUserPostFields(req)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.EqualError(t, err, http.ErrNotMultipart.Error())
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 11:19:00 +00:00
|
|
|
func TestApiCallsWithBadURL(t *testing.T) {
|
|
|
|
oldBaseURL := httpBaseURL
|
2020-02-03 23:08:00 +00:00
|
|
|
oldAuthUsername := authUsername
|
|
|
|
oldAuthPassword := authPassword
|
|
|
|
SetBaseURLAndCredentials(invalidURL, oldAuthUsername, oldAuthPassword)
|
2020-06-20 10:38:04 +00:00
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: os.TempDir(),
|
|
|
|
}
|
2019-08-03 11:19:00 +00:00
|
|
|
u := dataprovider.User{}
|
2019-08-04 19:37:31 +00:00
|
|
|
_, _, err := UpdateUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-08-04 19:37:31 +00:00
|
|
|
_, err = RemoveUser(u, http.StatusNotFound)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-06-20 10:38:04 +00:00
|
|
|
_, err = RemoveFolder(folder, http.StatusNotFound)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.Error(t, err)
|
2019-08-04 19:37:31 +00:00
|
|
|
_, _, err = GetUsers(1, 0, "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
_, _, err = GetFolders(1, 0, "", http.StatusBadRequest)
|
|
|
|
assert.Error(t, err)
|
2020-06-20 10:38:04 +00:00
|
|
|
_, err = UpdateQuotaUsage(u, "", http.StatusNotFound)
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = UpdateFolderQuotaUsage(folder, "", http.StatusNotFound)
|
|
|
|
assert.Error(t, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
_, err = CloseConnection("non_existent_id", http.StatusNotFound)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
_, _, err = Dumpdata("backup.json", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = Loaddata("/tmp/backup.json", "", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(oldBaseURL, oldAuthUsername, oldAuthPassword)
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestApiCallToNotListeningServer(t *testing.T) {
|
|
|
|
oldBaseURL := httpBaseURL
|
2020-02-03 23:08:00 +00:00
|
|
|
oldAuthUsername := authUsername
|
|
|
|
oldAuthPassword := authPassword
|
|
|
|
SetBaseURLAndCredentials(inactiveURL, oldAuthUsername, oldAuthPassword)
|
2019-08-03 11:19:00 +00:00
|
|
|
u := dataprovider.User{}
|
2019-08-04 19:37:31 +00:00
|
|
|
_, _, err := AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-08-04 19:37:31 +00:00
|
|
|
_, _, err = UpdateUser(u, http.StatusNotFound)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-08-04 19:37:31 +00:00
|
|
|
_, err = RemoveUser(u, http.StatusNotFound)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-08-04 19:37:31 +00:00
|
|
|
_, _, err = GetUserByID(-1, http.StatusNotFound)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-08-04 19:37:31 +00:00
|
|
|
_, _, err = GetUsers(100, 0, "", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-06-20 10:38:04 +00:00
|
|
|
_, err = UpdateQuotaUsage(u, "", http.StatusNotFound)
|
|
|
|
assert.Error(t, err)
|
2019-08-04 19:37:31 +00:00
|
|
|
_, _, err = GetQuotaScans(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-08-04 19:37:31 +00:00
|
|
|
_, err = StartQuotaScan(u, http.StatusNotFound)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: os.TempDir(),
|
|
|
|
}
|
|
|
|
_, err = StartFolderQuotaScan(folder, http.StatusNotFound)
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, _, err = AddFolder(folder, http.StatusOK)
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, _, err = GetFolders(0, 0, "", http.StatusOK)
|
|
|
|
assert.Error(t, err)
|
2020-06-20 10:38:04 +00:00
|
|
|
_, err = UpdateFolderQuotaUsage(folder, "", http.StatusNotFound)
|
|
|
|
assert.Error(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
_, _, err = GetFoldersQuotaScans(http.StatusOK)
|
|
|
|
assert.Error(t, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
_, _, err = GetConnections(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
_, err = CloseConnection("non_existent_id", http.StatusNotFound)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-08-08 08:01:33 +00:00
|
|
|
_, _, err = GetVersion(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-11-14 17:48:01 +00:00
|
|
|
_, _, err = GetProviderStatus(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
_, _, err = Dumpdata("backup.json", "0", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = Loaddata("/tmp/backup.json", "", "", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(oldBaseURL, oldAuthUsername, oldAuthPassword)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicAuth(t *testing.T) {
|
|
|
|
oldAuthUsername := authUsername
|
|
|
|
oldAuthPassword := authPassword
|
|
|
|
authUserFile := filepath.Join(os.TempDir(), "http_users.txt")
|
|
|
|
authUserData := []byte("test1:$2y$05$bcHSED7aO1cfLto6ZdDBOOKzlwftslVhtpIkRhAtSa4GuLmk5mola\n")
|
2020-05-06 17:36:34 +00:00
|
|
|
err := ioutil.WriteFile(authUserFile, authUserData, 0666)
|
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
httpAuth, _ = newBasicAuthProvider(authUserFile)
|
2020-05-06 17:36:34 +00:00
|
|
|
_, _, err = GetVersion(http.StatusUnauthorized)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(httpBaseURL, "test1", "password1")
|
|
|
|
_, _, err = GetVersion(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(httpBaseURL, "test1", "wrong_password")
|
|
|
|
resp, _ := sendHTTPRequest(http.MethodGet, buildURLRelativeToBase(metricsPath), nil, "")
|
|
|
|
defer resp.Body.Close()
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
2020-02-03 23:08:00 +00:00
|
|
|
authUserData = append(authUserData, []byte("test2:$apr1$gLnIkRIf$Xr/6aJfmIrihP4b2N2tcs/\n")...)
|
2020-05-03 13:24:26 +00:00
|
|
|
err = ioutil.WriteFile(authUserFile, authUserData, 0666)
|
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(httpBaseURL, "test2", "password2")
|
|
|
|
_, _, err = GetVersion(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(httpBaseURL, "test2", "wrong_password")
|
|
|
|
_, _, err = GetVersion(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
authUserData = append(authUserData, []byte("test3:$apr1$gLnIkRIf$Xr/6$aJfmIr$ihP4b2N2tcs/\n")...)
|
2020-05-06 17:36:34 +00:00
|
|
|
err = ioutil.WriteFile(authUserFile, authUserData, 0666)
|
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(httpBaseURL, "test3", "wrong_password")
|
|
|
|
_, _, err = GetVersion(http.StatusUnauthorized)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
authUserData = append(authUserData, []byte("test4:$invalid$gLnIkRIf$Xr/6$aJfmIr$ihP4b2N2tcs/\n")...)
|
2020-05-06 17:36:34 +00:00
|
|
|
err = ioutil.WriteFile(authUserFile, authUserData, 0666)
|
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(httpBaseURL, "test3", "password2")
|
|
|
|
_, _, err = GetVersion(http.StatusUnauthorized)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
authUserData = append(authUserData, []byte("test5:$apr1$gLnIkRIf$Xr/6aJfmIrihP4b2N2tcs/\n")...)
|
2020-05-03 13:24:26 +00:00
|
|
|
err = ioutil.WriteFile(authUserFile, authUserData, 0666)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Chmod(authUserFile, 0001)
|
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(httpBaseURL, "test5", "password2")
|
|
|
|
_, _, err = GetVersion(http.StatusUnauthorized)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Chmod(authUserFile, 0666)
|
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
}
|
|
|
|
authUserData = append(authUserData, []byte("\"foo\"bar\"\r\n")...)
|
2020-05-03 13:24:26 +00:00
|
|
|
err = ioutil.WriteFile(authUserFile, authUserData, 0666)
|
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(httpBaseURL, "test2", "password2")
|
|
|
|
_, _, err = GetVersion(http.StatusUnauthorized)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(authUserFile)
|
|
|
|
assert.NoError(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
SetBaseURLAndCredentials(httpBaseURL, oldAuthUsername, oldAuthPassword)
|
|
|
|
httpAuth, _ = newBasicAuthProvider("")
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
2019-08-08 19:42:07 +00:00
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestCloseConnectionHandler(t *testing.T) {
|
2019-08-08 19:42:07 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodDelete, activeConnectionsPath+"/connectionID", nil)
|
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("connectionID", "")
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
rr := httptest.NewRecorder()
|
2019-08-24 12:41:15 +00:00
|
|
|
handleCloseConnection(rr, req)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2019-08-08 19:42:07 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
|
|
|
|
func TestRenderInvalidTemplate(t *testing.T) {
|
|
|
|
tmpl, err := template.New("test").Parse("{{.Count}}")
|
2020-05-03 13:24:26 +00:00
|
|
|
if assert.NoError(t, err) {
|
2019-10-07 16:19:01 +00:00
|
|
|
templates["no_match"] = tmpl
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
renderTemplate(rw, "no_match", map[string]string{})
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rw.Code)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
|
|
|
|
func TestQuotaScanInvalidFs(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
|
|
|
Username: "test",
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
FsConfig: dataprovider.Filesystem{
|
|
|
|
Provider: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sftpd.AddQuotaScan(user.Username)
|
|
|
|
err := doQuotaScan(user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|