2019-10-07 16:19:01 +00:00
|
|
|
package httpd_test
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-26 09:34:44 +00:00
|
|
|
"bytes"
|
2019-12-27 22:12:44 +00:00
|
|
|
"crypto/rand"
|
2019-07-26 09:34:44 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-01-31 18:04:00 +00:00
|
|
|
"io"
|
2019-12-27 22:12:44 +00:00
|
|
|
"io/ioutil"
|
2020-01-31 18:04:00 +00:00
|
|
|
"mime/multipart"
|
2019-07-26 09:34:44 +00:00
|
|
|
"net"
|
2019-07-20 10:26:52 +00:00
|
|
|
"net/http"
|
2019-07-26 09:34:44 +00:00
|
|
|
"net/http/httptest"
|
2019-10-07 16:19:01 +00:00
|
|
|
"net/url"
|
2019-07-26 09:34:44 +00:00
|
|
|
"os"
|
2019-07-20 10:26:52 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-07-26 09:34:44 +00:00
|
|
|
"strconv"
|
2019-10-07 16:19:01 +00:00
|
|
|
"strings"
|
2019-07-20 10:26:52 +00:00
|
|
|
"testing"
|
2019-07-26 09:34:44 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/rs/zerolog"
|
2020-05-03 13:24:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-11-22 20:53:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2019-07-26 09:34:44 +00:00
|
|
|
"github.com/drakkan/sftpgo/config"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2019-10-07 16:19:01 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpd"
|
2020-11-30 20:46:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/kms"
|
2019-07-26 09:34:44 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2019-10-07 16:19:01 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-02-23 10:30:26 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-06-20 10:38:04 +00:00
|
|
|
defaultUsername = "test_user"
|
|
|
|
defaultPassword = "test_password"
|
|
|
|
testPubKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC03jj0D+djk7pxIf/0OhrxrchJTRZklofJ1NoIu4752Sq02mdXmarMVsqJ1cAjV5LBVy3D1F5U6XW4rppkXeVtd04Pxb09ehtH0pRRPaoHHlALiJt8CoMpbKYMA8b3KXPPriGxgGomvtU2T2RMURSwOZbMtpsugfjYSWenyYX+VORYhylWnSXL961LTyC21ehd6d6QnW9G7E5hYMITMY9TuQZz3bROYzXiTsgN0+g6Hn7exFQp50p45StUMfV/SftCMdCxlxuyGny2CrN/vfjO7xxOo2uv7q1qm10Q46KPWJQv+pgZ/OfL+EDjy07n5QVSKHlbx+2nT4Q0EgOSQaCTYwn3YjtABfIxWwgAFdyj6YlPulCL22qU4MYhDcA6PSBwDdf8hvxBfvsiHdM+JcSHvv8/VeJhk6CmnZxGY0fxBupov27z3yEO8nAg8k+6PaUiW1MSUfuGMF/ktB8LOstXsEPXSszuyXiOv4DaryOXUiSn7bmRqKcEFlJusO6aZP0= nicola@p1"
|
|
|
|
userPath = "/api/v1/user"
|
|
|
|
folderPath = "/api/v1/folder"
|
|
|
|
activeConnectionsPath = "/api/v1/connection"
|
|
|
|
quotaScanPath = "/api/v1/quota_scan"
|
|
|
|
quotaScanVFolderPath = "/api/v1/folder_quota_scan"
|
|
|
|
updateUsedQuotaPath = "/api/v1/quota_update"
|
|
|
|
updateFolderUsedQuotaPath = "/api/v1/folder_quota_update"
|
|
|
|
versionPath = "/api/v1/version"
|
|
|
|
metricsPath = "/metrics"
|
|
|
|
pprofPath = "/debug/pprof/"
|
|
|
|
webBasePath = "/web"
|
|
|
|
webUsersPath = "/web/users"
|
|
|
|
webUserPath = "/web/user"
|
|
|
|
webFoldersPath = "/web/folders"
|
|
|
|
webFolderPath = "/web/folder"
|
|
|
|
webConnectionsPath = "/web/connections"
|
|
|
|
configDir = ".."
|
|
|
|
httpsCert = `-----BEGIN CERTIFICATE-----
|
2020-02-04 22:21:33 +00:00
|
|
|
MIICHTCCAaKgAwIBAgIUHnqw7QnB1Bj9oUsNpdb+ZkFPOxMwCgYIKoZIzj0EAwIw
|
|
|
|
RTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGElu
|
|
|
|
dGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDAyMDQwOTUzMDRaFw0zMDAyMDEw
|
|
|
|
OTUzMDRaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYD
|
|
|
|
VQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwdjAQBgcqhkjOPQIBBgUrgQQA
|
|
|
|
IgNiAARCjRMqJ85rzMC998X5z761nJ+xL3bkmGVqWvrJ51t5OxV0v25NsOgR82CA
|
|
|
|
NXUgvhVYs7vNFN+jxtb2aj6Xg+/2G/BNxkaFspIVCzgWkxiz7XE4lgUwX44FCXZM
|
|
|
|
3+JeUbKjUzBRMB0GA1UdDgQWBBRhLw+/o3+Z02MI/d4tmaMui9W16jAfBgNVHSME
|
|
|
|
GDAWgBRhLw+/o3+Z02MI/d4tmaMui9W16jAPBgNVHRMBAf8EBTADAQH/MAoGCCqG
|
|
|
|
SM49BAMCA2kAMGYCMQDqLt2lm8mE+tGgtjDmtFgdOcI72HSbRQ74D5rYTzgST1rY
|
|
|
|
/8wTi5xl8TiFUyLMUsICMQC5ViVxdXbhuG7gX6yEqSkMKZICHpO8hqFwOD/uaFVI
|
|
|
|
dV4vKmHUzwK/eIx+8Ay3neE=
|
|
|
|
-----END CERTIFICATE-----`
|
|
|
|
httpsKey = `-----BEGIN EC PARAMETERS-----
|
|
|
|
BgUrgQQAIg==
|
|
|
|
-----END EC PARAMETERS-----
|
|
|
|
-----BEGIN EC PRIVATE KEY-----
|
|
|
|
MIGkAgEBBDCfMNsN6miEE3rVyUPwElfiJSWaR5huPCzUenZOfJT04GAcQdWvEju3
|
|
|
|
UM2lmBLIXpGgBwYFK4EEACKhZANiAARCjRMqJ85rzMC998X5z761nJ+xL3bkmGVq
|
|
|
|
WvrJ51t5OxV0v25NsOgR82CANXUgvhVYs7vNFN+jxtb2aj6Xg+/2G/BNxkaFspIV
|
|
|
|
CzgWkxiz7XE4lgUwX44FCXZM3+JeUbI=
|
|
|
|
-----END EC PRIVATE KEY-----`
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-10-07 16:19:01 +00:00
|
|
|
defaultPerms = []string{dataprovider.PermAny}
|
|
|
|
homeBasePath string
|
2019-12-27 22:12:44 +00:00
|
|
|
backupsPath string
|
2020-01-31 18:04:00 +00:00
|
|
|
credentialsPath string
|
2019-10-07 16:19:01 +00:00
|
|
|
testServer *httptest.Server
|
|
|
|
providerDriverName string
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2020-09-01 14:10:26 +00:00
|
|
|
type fakeConnection struct {
|
|
|
|
*common.BaseConnection
|
|
|
|
command string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeConnection) Disconnect() error {
|
|
|
|
common.Connections.Remove(c.GetID())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeConnection) GetClientVersion() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeConnection) GetCommand() string {
|
|
|
|
return c.command
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeConnection) GetRemoteAddress() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2019-12-27 22:12:44 +00:00
|
|
|
homeBasePath = os.TempDir()
|
2019-07-26 09:34:44 +00:00
|
|
|
logfilePath := filepath.Join(configDir, "sftpgo_api_test.log")
|
2019-07-31 12:11:44 +00:00
|
|
|
logger.InitLogger(logfilePath, 5, 1, 28, false, zerolog.DebugLevel)
|
2020-05-06 17:36:34 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
if err != nil {
|
|
|
|
logger.WarnToConsole("error loading configuration: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-07-26 09:34:44 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
2020-01-31 18:04:00 +00:00
|
|
|
credentialsPath = filepath.Join(os.TempDir(), "test_credentials")
|
|
|
|
providerConf.CredentialsPath = credentialsPath
|
2019-10-07 16:19:01 +00:00
|
|
|
providerDriverName = providerConf.Driver
|
2020-05-06 17:36:34 +00:00
|
|
|
os.RemoveAll(credentialsPath) //nolint:errcheck
|
2020-06-26 21:38:29 +00:00
|
|
|
logger.InfoToConsole("Starting HTTPD tests, provider: %v", providerConf.Driver)
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
common.Initialize(config.GetCommonConfig())
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
2019-07-26 09:34:44 +00:00
|
|
|
if err != nil {
|
2020-05-06 17:36:34 +00:00
|
|
|
logger.WarnToConsole("error initializing data provider: %v", err)
|
2019-07-26 09:34:44 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-04-26 21:29:09 +00:00
|
|
|
|
|
|
|
httpConfig := config.GetHTTPConfig()
|
|
|
|
httpConfig.Initialize(configDir)
|
2020-11-30 20:46:34 +00:00
|
|
|
kmsConfig := config.GetKMSConfig()
|
|
|
|
err = kmsConfig.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error initializing kms: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-04-26 21:29:09 +00:00
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
httpdConf := config.GetHTTPDConfig()
|
|
|
|
|
|
|
|
httpdConf.BindPort = 8081
|
2020-02-03 23:08:00 +00:00
|
|
|
httpd.SetBaseURLAndCredentials("http://127.0.0.1:8081", "", "")
|
|
|
|
backupsPath = filepath.Join(os.TempDir(), "test_backups")
|
|
|
|
httpdConf.BackupsPath = backupsPath
|
2020-06-08 17:40:17 +00:00
|
|
|
err = os.MkdirAll(backupsPath, os.ModePerm)
|
2020-05-06 17:36:34 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
logger.ErrorToConsole("error creating backups path: %v", err)
|
2020-05-06 17:36:34 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-07-26 09:34:44 +00:00
|
|
|
|
|
|
|
go func() {
|
2020-03-15 14:16:35 +00:00
|
|
|
if err := httpdConf.Initialize(configDir, true); err != nil {
|
2020-05-06 17:36:34 +00:00
|
|
|
logger.ErrorToConsole("could not start HTTP server: %v", err)
|
2020-07-24 21:39:38 +00:00
|
|
|
os.Exit(1)
|
2020-02-04 22:21:33 +00:00
|
|
|
}
|
2019-07-26 09:34:44 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
waitTCPListening(fmt.Sprintf("%s:%d", httpdConf.BindAddress, httpdConf.BindPort))
|
2020-02-04 22:21:33 +00:00
|
|
|
// now start an https server
|
|
|
|
certPath := filepath.Join(os.TempDir(), "test.crt")
|
|
|
|
keyPath := filepath.Join(os.TempDir(), "test.key")
|
2020-07-24 21:39:38 +00:00
|
|
|
err = ioutil.WriteFile(certPath, []byte(httpsCert), os.ModePerm)
|
2020-05-06 17:36:34 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
logger.ErrorToConsole("error writing HTTPS certificate: %v", err)
|
2020-05-06 17:36:34 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
err = ioutil.WriteFile(keyPath, []byte(httpsKey), os.ModePerm)
|
2020-05-06 17:36:34 +00:00
|
|
|
if err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
logger.ErrorToConsole("error writing HTTPS private key: %v", err)
|
2020-05-06 17:36:34 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-02-04 22:21:33 +00:00
|
|
|
httpdConf.BindPort = 8443
|
|
|
|
httpdConf.CertificateFile = certPath
|
|
|
|
httpdConf.CertificateKeyFile = keyPath
|
|
|
|
|
|
|
|
go func() {
|
2020-03-15 14:16:35 +00:00
|
|
|
if err := httpdConf.Initialize(configDir, true); err != nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
logger.ErrorToConsole("could not start HTTPS server: %v", err)
|
|
|
|
os.Exit(1)
|
2020-02-04 22:21:33 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
waitTCPListening(fmt.Sprintf("%s:%d", httpdConf.BindAddress, httpdConf.BindPort))
|
2020-05-06 17:36:34 +00:00
|
|
|
httpd.ReloadTLSCertificate() //nolint:errcheck
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
testServer = httptest.NewServer(httpd.GetHTTPRouter())
|
2020-09-11 07:30:25 +00:00
|
|
|
defer testServer.Close()
|
2019-10-07 16:19:01 +00:00
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
exitCode := m.Run()
|
2020-11-30 20:46:34 +00:00
|
|
|
os.Remove(logfilePath) //nolint:errcheck
|
2020-05-06 17:36:34 +00:00
|
|
|
os.RemoveAll(backupsPath) //nolint:errcheck
|
|
|
|
os.RemoveAll(credentialsPath) //nolint:errcheck
|
|
|
|
os.Remove(certPath) //nolint:errcheck
|
|
|
|
os.Remove(keyPath) //nolint:errcheck
|
|
|
|
os.Exit(exitCode) //nolint:errcheck
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:08:00 +00:00
|
|
|
func TestInitialization(t *testing.T) {
|
2020-05-03 13:24:26 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
2020-06-18 21:53:38 +00:00
|
|
|
invalidFile := "invalid file"
|
2020-02-03 23:08:00 +00:00
|
|
|
httpdConf := config.GetHTTPDConfig()
|
|
|
|
httpdConf.BackupsPath = "test_backups"
|
2020-06-18 21:53:38 +00:00
|
|
|
httpdConf.AuthUserFile = invalidFile
|
2020-05-03 13:24:26 +00:00
|
|
|
err = httpdConf.Initialize(configDir, true)
|
|
|
|
assert.Error(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
httpdConf.BackupsPath = backupsPath
|
|
|
|
httpdConf.AuthUserFile = ""
|
2020-06-18 21:53:38 +00:00
|
|
|
httpdConf.CertificateFile = invalidFile
|
|
|
|
httpdConf.CertificateKeyFile = invalidFile
|
2020-03-15 14:16:35 +00:00
|
|
|
err = httpdConf.Initialize(configDir, true)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-03-03 08:09:58 +00:00
|
|
|
httpdConf.CertificateFile = ""
|
|
|
|
httpdConf.CertificateKeyFile = ""
|
|
|
|
httpdConf.TemplatesPath = "."
|
2020-03-15 14:16:35 +00:00
|
|
|
err = httpdConf.Initialize(configDir, true)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-04-30 12:23:55 +00:00
|
|
|
err = httpd.ReloadTLSCertificate()
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err, "reloading TLS Certificate must return nil error if no certificate is configured")
|
2020-06-18 21:53:38 +00:00
|
|
|
httpdConf = config.GetHTTPDConfig()
|
|
|
|
httpdConf.BackupsPath = ".."
|
|
|
|
err = httpdConf.Initialize(configDir, true)
|
|
|
|
assert.Error(t, err)
|
|
|
|
httpdConf.BackupsPath = backupsPath
|
|
|
|
httpdConf.CertificateFile = invalidFile
|
|
|
|
httpdConf.CertificateKeyFile = invalidFile
|
|
|
|
httpdConf.StaticFilesPath = ""
|
|
|
|
httpdConf.TemplatesPath = ""
|
|
|
|
err = httpdConf.Initialize(configDir, true)
|
|
|
|
assert.Error(t, err)
|
2020-02-03 23:08:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func TestBasicUserHandling(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
user.MaxSessions = 10
|
|
|
|
user.QuotaSize = 4096
|
|
|
|
user.QuotaFiles = 2
|
|
|
|
user.UploadBandwidth = 128
|
|
|
|
user.DownloadBandwidth = 64
|
2019-11-13 10:36:21 +00:00
|
|
|
user.ExpirationDate = utils.GetTimeAsMsSinceEpoch(time.Now())
|
2020-11-25 21:26:34 +00:00
|
|
|
user.AdditionalInfo = "some free text"
|
2020-10-22 08:42:40 +00:00
|
|
|
originalUser := user
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-10-22 08:42:40 +00:00
|
|
|
assert.Equal(t, originalUser.ID, user.ID)
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
users, _, err := httpd.GetUsers(0, 0, defaultUsername, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 10:36:21 +00:00
|
|
|
func TestUserStatus(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Status = 3
|
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-11-13 10:36:21 +00:00
|
|
|
u.Status = 0
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-11-13 10:36:21 +00:00
|
|
|
user.Status = 2
|
2020-09-01 14:10:26 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusBadRequest, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-11-13 10:36:21 +00:00
|
|
|
user.Status = 1
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-11-13 10:36:21 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func TestAddUserNoCredentials(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Password = ""
|
2019-08-07 21:41:10 +00:00
|
|
|
u.PublicKeys = []string{}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserNoUsername(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Username = ""
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserNoHomeDir(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.HomeDir = ""
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserInvalidHomeDir(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2020-05-06 17:36:34 +00:00
|
|
|
u.HomeDir = "relative_path" //nolint:goconst
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserNoPerms(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions = make(map[string][]string)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserInvalidPerms(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/"] = []string{"invalidPerm"}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
// permissions for root dir are mandatory
|
2020-02-10 18:28:35 +00:00
|
|
|
u.Permissions["/"] = []string{}
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions["/somedir"] = []string{dataprovider.PermAny}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-10 18:28:35 +00:00
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
u.Permissions["/subdir/.."] = []string{dataprovider.PermAny}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-30 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserInvalidFilters(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Filters.AllowedIP = []string{"192.168.1.0/24", "192.168.2.0"}
|
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-30 17:37:50 +00:00
|
|
|
u.Filters.AllowedIP = []string{}
|
|
|
|
u.Filters.DeniedIP = []string{"192.168.3.0/16", "invalid"}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-19 21:39:30 +00:00
|
|
|
u.Filters.DeniedIP = []string{}
|
|
|
|
u.Filters.DeniedLoginMethods = []string{"invalid"}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-04-09 21:32:42 +00:00
|
|
|
u.Filters.DeniedLoginMethods = dataprovider.ValidSSHLoginMethods
|
2020-02-19 21:39:30 +00:00
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-01 21:10:29 +00:00
|
|
|
u.Filters.DeniedLoginMethods = []string{}
|
|
|
|
u.Filters.FileExtensions = []dataprovider.ExtensionsFilter{
|
2020-03-02 09:13:49 +00:00
|
|
|
{
|
2020-03-01 21:10:29 +00:00
|
|
|
Path: "relative",
|
|
|
|
AllowedExtensions: []string{},
|
|
|
|
DeniedExtensions: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-01 21:10:29 +00:00
|
|
|
u.Filters.FileExtensions = []dataprovider.ExtensionsFilter{
|
2020-03-02 09:13:49 +00:00
|
|
|
{
|
2020-03-01 21:10:29 +00:00
|
|
|
Path: "/",
|
|
|
|
AllowedExtensions: []string{},
|
|
|
|
DeniedExtensions: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-01 21:10:29 +00:00
|
|
|
u.Filters.FileExtensions = []dataprovider.ExtensionsFilter{
|
2020-03-02 09:13:49 +00:00
|
|
|
{
|
2020-03-01 21:10:29 +00:00
|
|
|
Path: "/subdir",
|
|
|
|
AllowedExtensions: []string{".zip"},
|
|
|
|
DeniedExtensions: []string{},
|
|
|
|
},
|
2020-03-02 09:13:49 +00:00
|
|
|
{
|
2020-03-01 21:10:29 +00:00
|
|
|
Path: "/subdir",
|
|
|
|
AllowedExtensions: []string{".rar"},
|
|
|
|
DeniedExtensions: []string{".jpg"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-08-17 10:49:20 +00:00
|
|
|
u.Filters.FileExtensions = nil
|
2020-11-15 21:04:48 +00:00
|
|
|
u.Filters.FilePatterns = []dataprovider.PatternsFilter{
|
|
|
|
{
|
|
|
|
Path: "relative",
|
|
|
|
AllowedPatterns: []string{},
|
|
|
|
DeniedPatterns: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.Filters.FilePatterns = []dataprovider.PatternsFilter{
|
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
AllowedPatterns: []string{},
|
|
|
|
DeniedPatterns: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.Filters.FilePatterns = []dataprovider.PatternsFilter{
|
|
|
|
{
|
|
|
|
Path: "/subdir",
|
|
|
|
AllowedPatterns: []string{"*.zip"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/subdir",
|
|
|
|
AllowedPatterns: []string{"*.rar"},
|
|
|
|
DeniedPatterns: []string{"*.jpg"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.Filters.FilePatterns = []dataprovider.PatternsFilter{
|
|
|
|
{
|
|
|
|
Path: "/subdir",
|
|
|
|
AllowedPatterns: []string{"a\\"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-08-17 10:49:20 +00:00
|
|
|
u.Filters.DeniedProtocols = []string{"invalid"}
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-08-17 12:08:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-08-17 10:49:20 +00:00
|
|
|
u.Filters.DeniedProtocols = dataprovider.ValidProtocols
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-08-17 12:08:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
func TestAddUserInvalidFsConfig(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2020-10-05 18:58:41 +00:00
|
|
|
u.FsConfig.Provider = dataprovider.S3FilesystemProvider
|
2020-01-19 06:41:05 +00:00
|
|
|
u.FsConfig.S3Config.Bucket = ""
|
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(credentialsPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.MkdirAll(credentialsPath, 0700)
|
|
|
|
assert.NoError(t, err)
|
2020-05-06 17:36:34 +00:00
|
|
|
u.FsConfig.S3Config.Bucket = "testbucket"
|
2020-01-19 22:23:09 +00:00
|
|
|
u.FsConfig.S3Config.Region = "eu-west-1"
|
|
|
|
u.FsConfig.S3Config.AccessKey = "access-key"
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.S3Config.AccessSecret = kms.NewSecret(kms.SecretStatusRedacted, "access-secret", "", "")
|
2020-01-19 22:23:09 +00:00
|
|
|
u.FsConfig.S3Config.Endpoint = "http://127.0.0.1:9000/path?a=b"
|
2020-05-06 17:36:34 +00:00
|
|
|
u.FsConfig.S3Config.StorageClass = "Standard" //nolint:goconst
|
|
|
|
u.FsConfig.S3Config.KeyPrefix = "/adir/subdir/"
|
2020-01-19 22:23:09 +00:00
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.S3Config.AccessSecret.SetStatus(kms.SecretStatusPlain)
|
2020-11-22 20:53:04 +00:00
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-03-13 16:28:55 +00:00
|
|
|
u.FsConfig.S3Config.KeyPrefix = ""
|
|
|
|
u.FsConfig.S3Config.UploadPartSize = 3
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-10-25 11:10:11 +00:00
|
|
|
u.FsConfig.S3Config.UploadPartSize = 5001
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-03-13 18:13:58 +00:00
|
|
|
u.FsConfig.S3Config.UploadPartSize = 0
|
|
|
|
u.FsConfig.S3Config.UploadConcurrency = -1
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
u = getTestUser()
|
2020-10-05 18:58:41 +00:00
|
|
|
u.FsConfig.Provider = dataprovider.GCSFilesystemProvider
|
2020-01-31 18:04:00 +00:00
|
|
|
u.FsConfig.GCSConfig.Bucket = ""
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-06 17:36:34 +00:00
|
|
|
u.FsConfig.GCSConfig.Bucket = "abucket"
|
2020-01-31 18:04:00 +00:00
|
|
|
u.FsConfig.GCSConfig.StorageClass = "Standard"
|
|
|
|
u.FsConfig.GCSConfig.KeyPrefix = "/somedir/subdir/"
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.GCSConfig.Credentials = kms.NewSecret(kms.SecretStatusRedacted, "test", "", "") //nolint:goconst
|
2020-11-22 20:53:04 +00:00
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.GCSConfig.Credentials.SetStatus(kms.SecretStatusPlain)
|
2020-01-31 18:04:00 +00:00
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-06 17:36:34 +00:00
|
|
|
u.FsConfig.GCSConfig.KeyPrefix = "somedir/subdir/" //nolint:goconst
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.GCSConfig.Credentials = kms.NewEmptySecret()
|
2020-02-19 08:41:15 +00:00
|
|
|
u.FsConfig.GCSConfig.AutomaticCredentials = 0
|
2020-01-31 18:04:00 +00:00
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.GCSConfig.Credentials = kms.NewSecret(kms.SecretStatusSecretBox, "invalid", "", "")
|
2020-01-31 18:04:00 +00:00
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-10-25 07:18:48 +00:00
|
|
|
|
|
|
|
u = getTestUser()
|
|
|
|
u.FsConfig.Provider = dataprovider.AzureBlobFilesystemProvider
|
|
|
|
u.FsConfig.AzBlobConfig.SASURL = "http://foo\x7f.com/"
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.FsConfig.AzBlobConfig.SASURL = ""
|
|
|
|
u.FsConfig.AzBlobConfig.AccountName = "name"
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.FsConfig.AzBlobConfig.Container = "container"
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.AzBlobConfig.AccountKey = kms.NewSecret(kms.SecretStatusRedacted, "key", "", "")
|
2020-10-25 07:18:48 +00:00
|
|
|
u.FsConfig.AzBlobConfig.KeyPrefix = "/amedir/subdir/"
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.AzBlobConfig.AccountKey.SetStatus(kms.SecretStatusPlain)
|
2020-11-22 20:53:04 +00:00
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-10-25 11:10:11 +00:00
|
|
|
u.FsConfig.AzBlobConfig.KeyPrefix = "amedir/subdir/"
|
|
|
|
u.FsConfig.AzBlobConfig.UploadPartSize = -1
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.FsConfig.AzBlobConfig.UploadPartSize = 101
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-12-05 12:48:13 +00:00
|
|
|
|
|
|
|
u = getTestUser()
|
|
|
|
u.FsConfig.Provider = dataprovider.CryptedFilesystemProvider
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.FsConfig.CryptConfig.Passphrase = kms.NewSecret(kms.SecretStatusRedacted, "akey", "", "")
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 10:30:26 +00:00
|
|
|
func TestAddUserInvalidVirtualFolders(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "vdir",
|
|
|
|
})
|
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/",
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(u.GetHomeDir(), "mapped_dir"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: u.GetHomeDir(),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(u.GetHomeDir(), ".."),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
})
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir2",
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir", "subdir"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
})
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir2",
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
})
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir", "subdir"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir2",
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir1/subdir",
|
|
|
|
})
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir2"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir1/../vdir1",
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir1/",
|
|
|
|
})
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir2"),
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir1/subdir",
|
2020-06-07 21:30:18 +00:00
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1/",
|
|
|
|
QuotaSize: -1,
|
|
|
|
QuotaFiles: 1,
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1/",
|
|
|
|
QuotaSize: 1,
|
|
|
|
QuotaFiles: -1,
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1/",
|
|
|
|
QuotaSize: -2,
|
|
|
|
QuotaFiles: 0,
|
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1/",
|
|
|
|
QuotaSize: 0,
|
|
|
|
QuotaFiles: -2,
|
2020-02-23 10:30:26 +00:00
|
|
|
})
|
|
|
|
_, _, err = httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-23 10:30:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 15:06:12 +00:00
|
|
|
func TestUserPublicKey(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
invalidPubKey := "invalid"
|
|
|
|
validPubKey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC03jj0D+djk7pxIf/0OhrxrchJTRZklofJ1NoIu4752Sq02mdXmarMVsqJ1cAjV5LBVy3D1F5U6XW4rppkXeVtd04Pxb09ehtH0pRRPaoHHlALiJt8CoMpbKYMA8b3KXPPriGxgGomvtU2T2RMURSwOZbMtpsugfjYSWenyYX+VORYhylWnSXL961LTyC21ehd6d6QnW9G7E5hYMITMY9TuQZz3bROYzXiTsgN0+g6Hn7exFQp50p45StUMfV/SftCMdCxlxuyGny2CrN/vfjO7xxOo2uv7q1qm10Q46KPWJQv+pgZ/OfL+EDjy07n5QVSKHlbx+2nT4Q0EgOSQaCTYwn3YjtABfIxWwgAFdyj6YlPulCL22qU4MYhDcA6PSBwDdf8hvxBfvsiHdM+JcSHvv8/VeJhk6CmnZxGY0fxBupov27z3yEO8nAg8k+6PaUiW1MSUfuGMF/ktB8LOstXsEPXSszuyXiOv4DaryOXUiSn7bmRqKcEFlJusO6aZP0= nicola@p1"
|
2019-08-07 21:41:10 +00:00
|
|
|
u.PublicKeys = []string{invalidPubKey}
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-07 21:41:10 +00:00
|
|
|
u.PublicKeys = []string{validPubKey}
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-07 21:41:10 +00:00
|
|
|
user.PublicKeys = []string{validPubKey, invalidPubKey}
|
2020-09-01 14:10:26 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusBadRequest, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-07 21:41:10 +00:00
|
|
|
user.PublicKeys = []string{validPubKey, validPubKey, validPubKey}
|
2020-09-01 14:10:26 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-31 15:06:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func TestUpdateUser(t *testing.T) {
|
2020-06-20 10:38:04 +00:00
|
|
|
u := getTestUser()
|
|
|
|
u.UsedQuotaFiles = 1
|
|
|
|
u.UsedQuotaSize = 2
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.Equal(t, 0, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(0), user.UsedQuotaSize)
|
2019-07-20 10:26:52 +00:00
|
|
|
user.HomeDir = filepath.Join(homeBasePath, "testmod")
|
|
|
|
user.UID = 33
|
|
|
|
user.GID = 101
|
|
|
|
user.MaxSessions = 10
|
|
|
|
user.QuotaSize = 4096
|
|
|
|
user.QuotaFiles = 2
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions["/"] = []string{dataprovider.PermCreateDirs, dataprovider.PermDelete, dataprovider.PermDownload}
|
|
|
|
user.Permissions["/subdir"] = []string{dataprovider.PermListItems, dataprovider.PermUpload}
|
2019-12-30 17:37:50 +00:00
|
|
|
user.Filters.AllowedIP = []string{"192.168.1.0/24", "192.168.2.0/24"}
|
|
|
|
user.Filters.DeniedIP = []string{"192.168.3.0/24", "192.168.4.0/24"}
|
2020-08-12 14:15:12 +00:00
|
|
|
user.Filters.DeniedLoginMethods = []string{dataprovider.LoginMethodPassword}
|
2020-08-17 10:49:20 +00:00
|
|
|
user.Filters.DeniedProtocols = []string{common.ProtocolWebDAV}
|
2020-03-01 21:10:29 +00:00
|
|
|
user.Filters.FileExtensions = append(user.Filters.FileExtensions, dataprovider.ExtensionsFilter{
|
|
|
|
Path: "/subdir",
|
|
|
|
AllowedExtensions: []string{".zip", ".rar"},
|
|
|
|
DeniedExtensions: []string{".jpg", ".png"},
|
|
|
|
})
|
2020-11-15 21:04:48 +00:00
|
|
|
user.Filters.FilePatterns = append(user.Filters.FilePatterns, dataprovider.PatternsFilter{
|
|
|
|
Path: "/subdir",
|
|
|
|
AllowedPatterns: []string{"*.zip", "*.rar"},
|
|
|
|
DeniedPatterns: []string{"*.jpg", "*.png"},
|
|
|
|
})
|
2020-08-16 18:17:02 +00:00
|
|
|
user.Filters.MaxUploadFileSize = 4096
|
2019-07-20 10:26:52 +00:00
|
|
|
user.UploadBandwidth = 1024
|
|
|
|
user.DownloadBandwidth = 512
|
2020-02-23 10:30:26 +00:00
|
|
|
user.VirtualFolders = nil
|
2020-06-07 21:30:18 +00:00
|
|
|
mappedPath1 := filepath.Join(os.TempDir(), "mapped_dir1")
|
|
|
|
mappedPath2 := filepath.Join(os.TempDir(), "mapped_dir2")
|
2020-02-23 10:30:26 +00:00
|
|
|
user.VirtualFolders = append(user.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath1,
|
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
})
|
|
|
|
user.VirtualFolders = append(user.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath2,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir12/subdir",
|
|
|
|
QuotaSize: 123,
|
|
|
|
QuotaFiles: 2,
|
2020-02-23 10:30:26 +00:00
|
|
|
})
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusBadRequest, "invalid")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "0")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "1")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-10 18:28:35 +00:00
|
|
|
user.Permissions["/subdir"] = []string{}
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.Len(t, user.Permissions["/subdir"], 0)
|
|
|
|
assert.Len(t, user.VirtualFolders, 2)
|
|
|
|
for _, folder := range user.VirtualFolders {
|
|
|
|
assert.Greater(t, folder.ID, int64(0))
|
|
|
|
if folder.VirtualPath == "/vdir12/subdir" {
|
|
|
|
assert.Equal(t, int64(123), folder.QuotaSize)
|
|
|
|
assert.Equal(t, 2, folder.QuotaFiles)
|
|
|
|
}
|
2020-02-10 18:28:35 +00:00
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
folder, _, err := httpd.GetFolders(0, 0, mappedPath1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folder, 1) {
|
|
|
|
f := folder[0]
|
|
|
|
assert.Len(t, f.Users, 1)
|
|
|
|
assert.Contains(t, f.Users, user.Username)
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
// removing the user must remove folder mapping
|
|
|
|
folder, _, err = httpd.GetFolders(0, 0, mappedPath1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folder, 1) {
|
|
|
|
f := folder[0]
|
|
|
|
assert.Len(t, f.Users, 0)
|
|
|
|
_, err = httpd.RemoveFolder(f, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
folder, _, err = httpd.GetFolders(0, 0, mappedPath2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folder, 1) {
|
|
|
|
f := folder[0]
|
|
|
|
assert.Len(t, f.Users, 0)
|
|
|
|
_, err = httpd.RemoveFolder(f, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
func TestUpdateUserQuotaUsage(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
usedQuotaFiles := 1
|
|
|
|
usedQuotaSize := int64(65535)
|
|
|
|
u.UsedQuotaFiles = usedQuotaFiles
|
|
|
|
u.UsedQuotaSize = usedQuotaSize
|
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.UpdateQuotaUsage(u, "invalid_mode", http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.UpdateQuotaUsage(u, "", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, usedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, usedQuotaSize, user.UsedQuotaSize)
|
|
|
|
_, err = httpd.UpdateQuotaUsage(u, "add", http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err, "user has no quota restrictions add mode should fail")
|
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, usedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, usedQuotaSize, user.UsedQuotaSize)
|
|
|
|
user.QuotaFiles = 100
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.UpdateQuotaUsage(u, "add", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 2*usedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, 2*usedQuotaSize, user.UsedQuotaSize)
|
|
|
|
u.UsedQuotaFiles = -1
|
|
|
|
_, err = httpd.UpdateQuotaUsage(u, "", http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.UsedQuotaFiles = usedQuotaFiles
|
|
|
|
u.Username = u.Username + "1"
|
|
|
|
_, err = httpd.UpdateQuotaUsage(u, "", http.StatusNotFound)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestUserFolderMapping(t *testing.T) {
|
|
|
|
mappedPath1 := filepath.Join(os.TempDir(), "mapped_dir1")
|
|
|
|
mappedPath2 := filepath.Join(os.TempDir(), "mapped_dir2")
|
|
|
|
u1 := getTestUser()
|
|
|
|
u1.VirtualFolders = append(u1.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2020-06-20 12:30:46 +00:00
|
|
|
MappedPath: mappedPath1,
|
|
|
|
UsedQuotaFiles: 2,
|
|
|
|
UsedQuotaSize: 123,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
|
|
|
VirtualPath: "/vdir",
|
|
|
|
QuotaSize: -1,
|
|
|
|
QuotaFiles: -1,
|
|
|
|
})
|
|
|
|
user1, _, err := httpd.AddUser(u1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// virtual folder must be auto created
|
|
|
|
folders, _, err := httpd.GetFolders(0, 0, mappedPath1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder := folders[0]
|
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user1.Username)
|
2020-06-20 12:30:46 +00:00
|
|
|
assert.Equal(t, 0, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(0), folder.UsedQuotaSize)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
u2 := getTestUser()
|
|
|
|
u2.Username = defaultUsername + "2"
|
|
|
|
u2.VirtualFolders = append(u2.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath1,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
QuotaSize: 0,
|
|
|
|
QuotaFiles: 0,
|
|
|
|
})
|
|
|
|
u2.VirtualFolders = append(u2.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath2,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir2",
|
|
|
|
QuotaSize: -1,
|
|
|
|
QuotaFiles: -1,
|
|
|
|
})
|
|
|
|
user2, _, err := httpd.AddUser(u2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
folders, _, err = httpd.GetFolders(0, 0, mappedPath2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder := folders[0]
|
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user2.Username)
|
|
|
|
}
|
|
|
|
folders, _, err = httpd.GetFolders(0, 0, mappedPath1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder := folders[0]
|
|
|
|
assert.Len(t, folder.Users, 2)
|
|
|
|
assert.Contains(t, folder.Users, user1.Username)
|
|
|
|
assert.Contains(t, folder.Users, user2.Username)
|
|
|
|
}
|
|
|
|
// now update user2 removing mappedPath1
|
|
|
|
user2.VirtualFolders = nil
|
|
|
|
user2.VirtualFolders = append(user2.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2020-06-20 12:30:46 +00:00
|
|
|
MappedPath: mappedPath2,
|
|
|
|
UsedQuotaFiles: 2,
|
|
|
|
UsedQuotaSize: 123,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
|
|
|
VirtualPath: "/vdir",
|
|
|
|
QuotaSize: 0,
|
|
|
|
QuotaFiles: 0,
|
|
|
|
})
|
2020-09-01 14:10:26 +00:00
|
|
|
user2, _, err = httpd.UpdateUser(user2, http.StatusOK, "")
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
folders, _, err = httpd.GetFolders(0, 0, mappedPath2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder := folders[0]
|
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user2.Username)
|
2020-06-20 12:30:46 +00:00
|
|
|
assert.Equal(t, 0, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(0), folder.UsedQuotaSize)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
folders, _, err = httpd.GetFolders(0, 0, mappedPath1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder := folders[0]
|
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user1.Username)
|
|
|
|
}
|
|
|
|
// add mappedPath1 again to user2
|
|
|
|
user2.VirtualFolders = append(user2.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath1,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
})
|
2020-09-01 14:10:26 +00:00
|
|
|
user2, _, err = httpd.UpdateUser(user2, http.StatusOK, "")
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
folders, _, err = httpd.GetFolders(0, 0, mappedPath2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder := folders[0]
|
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user2.Username)
|
|
|
|
}
|
|
|
|
// removing virtual folders should clear relations on both side
|
|
|
|
_, err = httpd.RemoveFolder(vfs.BaseVirtualFolder{MappedPath: mappedPath2}, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user2, _, err = httpd.GetUserByID(user2.ID, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, user2.VirtualFolders, 1) {
|
|
|
|
folder := user2.VirtualFolders[0]
|
|
|
|
assert.Equal(t, mappedPath1, folder.MappedPath)
|
|
|
|
}
|
|
|
|
user1, _, err = httpd.GetUserByID(user1.ID, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, user2.VirtualFolders, 1) {
|
|
|
|
folder := user2.VirtualFolders[0]
|
|
|
|
assert.Equal(t, mappedPath1, folder.MappedPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
folders, _, err = httpd.GetFolders(0, 0, mappedPath1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder := folders[0]
|
|
|
|
assert.Len(t, folder.Users, 2)
|
|
|
|
}
|
|
|
|
// removing a user should clear virtual folder mapping
|
|
|
|
_, err = httpd.RemoveUser(user1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
folders, _, err = httpd.GetFolders(0, 0, mappedPath1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder := folders[0]
|
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user2.Username)
|
|
|
|
}
|
|
|
|
// removing a folder should clear mapping on the user side too
|
|
|
|
_, err = httpd.RemoveFolder(vfs.BaseVirtualFolder{MappedPath: mappedPath1}, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user2, _, err = httpd.GetUserByID(user2.ID, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, user2.VirtualFolders, 0)
|
|
|
|
_, err = httpd.RemoveUser(user2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
func TestUserS3Config(t *testing.T) {
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-10-05 18:58:41 +00:00
|
|
|
user.FsConfig.Provider = dataprovider.S3FilesystemProvider
|
2020-05-06 17:36:34 +00:00
|
|
|
user.FsConfig.S3Config.Bucket = "test" //nolint:goconst
|
|
|
|
user.FsConfig.S3Config.Region = "us-east-1" //nolint:goconst
|
2020-01-19 06:41:05 +00:00
|
|
|
user.FsConfig.S3Config.AccessKey = "Server-Access-Key"
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.S3Config.AccessSecret = kms.NewPlainSecret("Server-Access-Secret")
|
2020-01-19 06:41:05 +00:00
|
|
|
user.FsConfig.S3Config.Endpoint = "http://127.0.0.1:9000"
|
2020-03-13 18:13:58 +00:00
|
|
|
user.FsConfig.S3Config.UploadPartSize = 8
|
2020-11-22 20:53:04 +00:00
|
|
|
user, body, err := httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err, string(body))
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.S3Config.AccessSecret.GetStatus())
|
|
|
|
assert.NotEmpty(t, user.FsConfig.S3Config.AccessSecret.GetPayload())
|
|
|
|
assert.Empty(t, user.FsConfig.S3Config.AccessSecret.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.S3Config.AccessSecret.GetKey())
|
2020-01-19 06:41:05 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
2020-11-30 20:46:34 +00:00
|
|
|
secret := kms.NewSecret(kms.SecretStatusSecretBox, "Server-Access-Secret", "", "")
|
2020-01-19 06:41:05 +00:00
|
|
|
user.FsConfig.S3Config.AccessSecret = secret
|
2020-11-22 20:53:04 +00:00
|
|
|
_, _, err = httpd.AddUser(user, http.StatusOK)
|
|
|
|
assert.Error(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.S3Config.AccessSecret.SetStatus(kms.SecretStatusPlain)
|
2020-01-19 06:41:05 +00:00
|
|
|
user, _, err = httpd.AddUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
initialSecretPayload := user.FsConfig.S3Config.AccessSecret.GetPayload()
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.S3Config.AccessSecret.GetStatus())
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NotEmpty(t, initialSecretPayload)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Empty(t, user.FsConfig.S3Config.AccessSecret.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.S3Config.AccessSecret.GetKey())
|
2020-10-05 18:58:41 +00:00
|
|
|
user.FsConfig.Provider = dataprovider.S3FilesystemProvider
|
2020-05-06 17:36:34 +00:00
|
|
|
user.FsConfig.S3Config.Bucket = "test-bucket"
|
|
|
|
user.FsConfig.S3Config.Region = "us-east-1" //nolint:goconst
|
2020-01-19 06:41:05 +00:00
|
|
|
user.FsConfig.S3Config.AccessKey = "Server-Access-Key1"
|
|
|
|
user.FsConfig.S3Config.Endpoint = "http://localhost:9000"
|
2020-05-06 17:36:34 +00:00
|
|
|
user.FsConfig.S3Config.KeyPrefix = "somedir/subdir" //nolint:goconst
|
2020-03-13 18:13:58 +00:00
|
|
|
user.FsConfig.S3Config.UploadConcurrency = 5
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.S3Config.AccessSecret.GetStatus())
|
|
|
|
assert.Equal(t, initialSecretPayload, user.FsConfig.S3Config.AccessSecret.GetPayload())
|
|
|
|
assert.Empty(t, user.FsConfig.S3Config.AccessSecret.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.S3Config.AccessSecret.GetKey())
|
2020-02-16 09:14:44 +00:00
|
|
|
// test user without access key and access secret (shared config state)
|
2020-10-05 18:58:41 +00:00
|
|
|
user.FsConfig.Provider = dataprovider.S3FilesystemProvider
|
2020-05-06 17:36:34 +00:00
|
|
|
user.FsConfig.S3Config.Bucket = "testbucket"
|
2020-02-16 09:14:44 +00:00
|
|
|
user.FsConfig.S3Config.Region = "us-east-1"
|
|
|
|
user.FsConfig.S3Config.AccessKey = ""
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.S3Config.AccessSecret = kms.NewEmptySecret()
|
2020-02-16 09:14:44 +00:00
|
|
|
user.FsConfig.S3Config.Endpoint = ""
|
|
|
|
user.FsConfig.S3Config.KeyPrefix = "somedir/subdir"
|
2020-03-13 18:13:58 +00:00
|
|
|
user.FsConfig.S3Config.UploadPartSize = 6
|
|
|
|
user.FsConfig.S3Config.UploadConcurrency = 4
|
2020-11-22 20:53:04 +00:00
|
|
|
user, body, err = httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err, string(body))
|
|
|
|
assert.True(t, user.FsConfig.S3Config.AccessSecret.IsEmpty())
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
// shared credential test for add instead of update
|
|
|
|
user, _, err = httpd.AddUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.True(t, user.FsConfig.S3Config.AccessSecret.IsEmpty())
|
2020-01-19 06:41:05 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
func TestUserGCSConfig(t *testing.T) {
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(credentialsPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.MkdirAll(credentialsPath, 0700)
|
|
|
|
assert.NoError(t, err)
|
2020-10-05 18:58:41 +00:00
|
|
|
user.FsConfig.Provider = dataprovider.GCSFilesystemProvider
|
2020-01-31 18:04:00 +00:00
|
|
|
user.FsConfig.GCSConfig.Bucket = "test"
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.GCSConfig.Credentials = kms.NewPlainSecret("fake credentials") //nolint:goconst
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-22 20:53:04 +00:00
|
|
|
credentialFile := filepath.Join(credentialsPath, fmt.Sprintf("%v_gcs_credentials.json", user.Username))
|
|
|
|
assert.FileExists(t, credentialFile)
|
|
|
|
creds, err := ioutil.ReadFile(credentialFile)
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
secret := kms.NewEmptySecret()
|
2020-11-22 20:53:04 +00:00
|
|
|
err = json.Unmarshal(creds, secret)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = secret.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, "fake credentials", secret.GetPayload())
|
|
|
|
user.FsConfig.GCSConfig.Credentials = kms.NewSecret(kms.SecretStatusSecretBox, "fake encrypted credentials", "", "")
|
2020-11-22 20:53:04 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.FileExists(t, credentialFile)
|
|
|
|
creds, err = ioutil.ReadFile(credentialFile)
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
secret = kms.NewEmptySecret()
|
2020-11-22 20:53:04 +00:00
|
|
|
err = json.Unmarshal(creds, secret)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = secret.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, "fake credentials", secret.GetPayload())
|
2020-01-31 18:04:00 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.GCSConfig.Credentials = kms.NewSecret(kms.SecretStatusSecretBox, "fake credentials", "", "")
|
2020-11-22 20:53:04 +00:00
|
|
|
_, _, err = httpd.AddUser(user, http.StatusOK)
|
|
|
|
assert.Error(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.GCSConfig.Credentials.SetStatus(kms.SecretStatusPlain)
|
2020-10-22 08:42:40 +00:00
|
|
|
user, body, err := httpd.AddUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err, string(body))
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.RemoveAll(credentialsPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.MkdirAll(credentialsPath, 0700)
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.GCSConfig.Credentials = kms.NewEmptySecret()
|
2020-02-19 08:41:15 +00:00
|
|
|
user.FsConfig.GCSConfig.AutomaticCredentials = 1
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoFileExists(t, credentialFile)
|
|
|
|
user.FsConfig.GCSConfig = vfs.GCSFsConfig{}
|
2020-10-05 18:58:41 +00:00
|
|
|
user.FsConfig.Provider = dataprovider.S3FilesystemProvider
|
2020-01-31 18:04:00 +00:00
|
|
|
user.FsConfig.S3Config.Bucket = "test1"
|
|
|
|
user.FsConfig.S3Config.Region = "us-east-1"
|
|
|
|
user.FsConfig.S3Config.AccessKey = "Server-Access-Key1"
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.S3Config.AccessSecret = kms.NewPlainSecret("secret")
|
2020-01-31 18:04:00 +00:00
|
|
|
user.FsConfig.S3Config.Endpoint = "http://localhost:9000"
|
|
|
|
user.FsConfig.S3Config.KeyPrefix = "somedir/subdir"
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-22 20:53:04 +00:00
|
|
|
user.FsConfig.S3Config = vfs.S3FsConfig{}
|
2020-10-05 18:58:41 +00:00
|
|
|
user.FsConfig.Provider = dataprovider.GCSFilesystemProvider
|
2020-01-31 18:04:00 +00:00
|
|
|
user.FsConfig.GCSConfig.Bucket = "test1"
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.GCSConfig.Credentials = kms.NewPlainSecret("fake credentials")
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
func TestUserAzureBlobConfig(t *testing.T) {
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.FsConfig.Provider = dataprovider.AzureBlobFilesystemProvider
|
|
|
|
user.FsConfig.AzBlobConfig.Container = "test"
|
|
|
|
user.FsConfig.AzBlobConfig.AccountName = "Server-Account-Name"
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.AzBlobConfig.AccountKey = kms.NewPlainSecret("Server-Account-Key")
|
2020-10-25 07:18:48 +00:00
|
|
|
user.FsConfig.AzBlobConfig.Endpoint = "http://127.0.0.1:9000"
|
|
|
|
user.FsConfig.AzBlobConfig.UploadPartSize = 8
|
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
initialPayload := user.FsConfig.AzBlobConfig.AccountKey.GetPayload()
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.AzBlobConfig.AccountKey.GetStatus())
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NotEmpty(t, initialPayload)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Empty(t, user.FsConfig.AzBlobConfig.AccountKey.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.AzBlobConfig.AccountKey.GetKey())
|
|
|
|
user.FsConfig.AzBlobConfig.AccountKey.SetStatus(kms.SecretStatusSecretBox)
|
|
|
|
user.FsConfig.AzBlobConfig.AccountKey.SetAdditionalData("data")
|
|
|
|
user.FsConfig.AzBlobConfig.AccountKey.SetKey("fake key")
|
2020-11-22 20:53:04 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.AzBlobConfig.AccountKey.GetStatus())
|
|
|
|
assert.Equal(t, initialPayload, user.FsConfig.AzBlobConfig.AccountKey.GetPayload())
|
|
|
|
assert.Empty(t, user.FsConfig.AzBlobConfig.AccountKey.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.AzBlobConfig.AccountKey.GetKey())
|
2020-11-22 20:53:04 +00:00
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
2020-11-30 20:46:34 +00:00
|
|
|
secret := kms.NewSecret(kms.SecretStatusSecretBox, "Server-Account-Key", "", "")
|
2020-10-25 07:18:48 +00:00
|
|
|
user.FsConfig.AzBlobConfig.AccountKey = secret
|
2020-11-22 20:53:04 +00:00
|
|
|
_, _, err = httpd.AddUser(user, http.StatusOK)
|
|
|
|
assert.Error(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.AzBlobConfig.AccountKey = kms.NewPlainSecret("Server-Account-Key-Test")
|
2020-10-25 07:18:48 +00:00
|
|
|
user, _, err = httpd.AddUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
initialPayload = user.FsConfig.AzBlobConfig.AccountKey.GetPayload()
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.AzBlobConfig.AccountKey.GetStatus())
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NotEmpty(t, initialPayload)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Empty(t, user.FsConfig.AzBlobConfig.AccountKey.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.AzBlobConfig.AccountKey.GetKey())
|
2020-10-25 07:18:48 +00:00
|
|
|
user.FsConfig.Provider = dataprovider.AzureBlobFilesystemProvider
|
|
|
|
user.FsConfig.AzBlobConfig.Container = "test-container"
|
|
|
|
user.FsConfig.AzBlobConfig.Endpoint = "http://localhost:9001"
|
|
|
|
user.FsConfig.AzBlobConfig.KeyPrefix = "somedir/subdir"
|
|
|
|
user.FsConfig.AzBlobConfig.UploadConcurrency = 5
|
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.AzBlobConfig.AccountKey.GetStatus())
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NotEmpty(t, initialPayload)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.Equal(t, initialPayload, user.FsConfig.AzBlobConfig.AccountKey.GetPayload())
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Empty(t, user.FsConfig.AzBlobConfig.AccountKey.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.AzBlobConfig.AccountKey.GetKey())
|
2020-10-25 07:18:48 +00:00
|
|
|
// test user without access key and access secret (sas)
|
|
|
|
user.FsConfig.Provider = dataprovider.AzureBlobFilesystemProvider
|
|
|
|
user.FsConfig.AzBlobConfig.SASURL = "https://myaccount.blob.core.windows.net/pictures/profile.jpg?sv=2012-02-12&st=2009-02-09&se=2009-02-10&sr=c&sp=r&si=YWJjZGVmZw%3d%3d&sig=dD80ihBh5jfNpymO5Hg1IdiJIEvHcJpCMiCMnN%2fRnbI%3d"
|
|
|
|
user.FsConfig.AzBlobConfig.KeyPrefix = "somedir/subdir"
|
2020-11-22 20:53:04 +00:00
|
|
|
user.FsConfig.AzBlobConfig.AccountName = ""
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.AzBlobConfig.AccountKey = kms.NewEmptySecret()
|
2020-10-25 07:18:48 +00:00
|
|
|
user.FsConfig.AzBlobConfig.UploadPartSize = 6
|
|
|
|
user.FsConfig.AzBlobConfig.UploadConcurrency = 4
|
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.True(t, user.FsConfig.AzBlobConfig.AccountKey.IsEmpty())
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
// sas test for add instead of update
|
|
|
|
user, _, err = httpd.AddUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, user.FsConfig.AzBlobConfig.AccountKey.IsEmpty())
|
2020-10-25 07:18:48 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
func TestUserCryptFs(t *testing.T) {
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.FsConfig.Provider = dataprovider.CryptedFilesystemProvider
|
|
|
|
user.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret("crypt passphrase")
|
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
initialPayload := user.FsConfig.CryptConfig.Passphrase.GetPayload()
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.CryptConfig.Passphrase.GetStatus())
|
|
|
|
assert.NotEmpty(t, initialPayload)
|
|
|
|
assert.Empty(t, user.FsConfig.CryptConfig.Passphrase.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.CryptConfig.Passphrase.GetKey())
|
|
|
|
user.FsConfig.CryptConfig.Passphrase.SetStatus(kms.SecretStatusSecretBox)
|
|
|
|
user.FsConfig.CryptConfig.Passphrase.SetAdditionalData("data")
|
|
|
|
user.FsConfig.CryptConfig.Passphrase.SetKey("fake pass key")
|
|
|
|
user, bb, err := httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err, string(bb))
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.CryptConfig.Passphrase.GetStatus())
|
|
|
|
assert.Equal(t, initialPayload, user.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
assert.Empty(t, user.FsConfig.CryptConfig.Passphrase.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.CryptConfig.Passphrase.GetKey())
|
|
|
|
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
secret := kms.NewSecret(kms.SecretStatusSecretBox, "invalid encrypted payload", "", "")
|
|
|
|
user.FsConfig.CryptConfig.Passphrase = secret
|
|
|
|
_, _, err = httpd.AddUser(user, http.StatusOK)
|
|
|
|
assert.Error(t, err)
|
|
|
|
user.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret("passphrase test")
|
|
|
|
user, _, err = httpd.AddUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
initialPayload = user.FsConfig.CryptConfig.Passphrase.GetPayload()
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.CryptConfig.Passphrase.GetStatus())
|
|
|
|
assert.NotEmpty(t, initialPayload)
|
|
|
|
assert.Empty(t, user.FsConfig.CryptConfig.Passphrase.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.CryptConfig.Passphrase.GetKey())
|
|
|
|
user.FsConfig.Provider = dataprovider.CryptedFilesystemProvider
|
|
|
|
user.FsConfig.CryptConfig.Passphrase.SetKey("pass")
|
|
|
|
user, bb, err = httpd.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err, string(bb))
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.CryptConfig.Passphrase.GetStatus())
|
|
|
|
assert.NotEmpty(t, initialPayload)
|
|
|
|
assert.Equal(t, initialPayload, user.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
assert.Empty(t, user.FsConfig.CryptConfig.Passphrase.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.CryptConfig.Passphrase.GetKey())
|
|
|
|
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:53:04 +00:00
|
|
|
func TestUserHiddenFields(t *testing.T) {
|
|
|
|
err := dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.PreferDatabaseCredentials = true
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// sensitive data must be hidden but not deleted from the dataprovider
|
2020-12-05 12:48:13 +00:00
|
|
|
usernames := []string{"user1", "user2", "user3", "user4"}
|
2020-11-22 20:53:04 +00:00
|
|
|
u1 := getTestUser()
|
|
|
|
u1.Username = usernames[0]
|
|
|
|
u1.FsConfig.Provider = dataprovider.S3FilesystemProvider
|
|
|
|
u1.FsConfig.S3Config.Bucket = "test"
|
|
|
|
u1.FsConfig.S3Config.Region = "us-east-1"
|
|
|
|
u1.FsConfig.S3Config.AccessKey = "S3-Access-Key"
|
2020-11-30 20:46:34 +00:00
|
|
|
u1.FsConfig.S3Config.AccessSecret = kms.NewPlainSecret("S3-Access-Secret")
|
2020-11-22 20:53:04 +00:00
|
|
|
user1, _, err := httpd.AddUser(u1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
u2 := getTestUser()
|
|
|
|
u2.Username = usernames[1]
|
|
|
|
u2.FsConfig.Provider = dataprovider.GCSFilesystemProvider
|
|
|
|
u2.FsConfig.GCSConfig.Bucket = "test"
|
2020-11-30 20:46:34 +00:00
|
|
|
u2.FsConfig.GCSConfig.Credentials = kms.NewPlainSecret("fake credentials")
|
2020-11-22 20:53:04 +00:00
|
|
|
user2, _, err := httpd.AddUser(u2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
u3 := getTestUser()
|
|
|
|
u3.Username = usernames[2]
|
|
|
|
u3.FsConfig.Provider = dataprovider.AzureBlobFilesystemProvider
|
|
|
|
u3.FsConfig.AzBlobConfig.Container = "test"
|
|
|
|
u3.FsConfig.AzBlobConfig.AccountName = "Server-Account-Name"
|
2020-11-30 20:46:34 +00:00
|
|
|
u3.FsConfig.AzBlobConfig.AccountKey = kms.NewPlainSecret("Server-Account-Key")
|
2020-11-22 20:53:04 +00:00
|
|
|
user3, _, err := httpd.AddUser(u3, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
u4 := getTestUser()
|
|
|
|
u4.Username = usernames[3]
|
|
|
|
u4.FsConfig.Provider = dataprovider.CryptedFilesystemProvider
|
|
|
|
u4.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret("test passphrase")
|
|
|
|
user4, _, err := httpd.AddUser(u4, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-11-22 20:53:04 +00:00
|
|
|
users, _, err := httpd.GetUsers(0, 0, "", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.GreaterOrEqual(t, len(users), 4)
|
2020-11-22 20:53:04 +00:00
|
|
|
for _, username := range usernames {
|
|
|
|
users, _, err = httpd.GetUsers(0, 0, username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, users, 1) {
|
|
|
|
user := users[0]
|
|
|
|
assert.Empty(t, user.Password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user1, _, err = httpd.GetUserByID(user1.ID, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, user1.Password)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Empty(t, user1.FsConfig.S3Config.AccessSecret.GetKey())
|
|
|
|
assert.Empty(t, user1.FsConfig.S3Config.AccessSecret.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user1.FsConfig.S3Config.AccessSecret.GetStatus())
|
|
|
|
assert.NotEmpty(t, user1.FsConfig.S3Config.AccessSecret.GetPayload())
|
2020-11-22 20:53:04 +00:00
|
|
|
|
|
|
|
user2, _, err = httpd.GetUserByID(user2.ID, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, user2.Password)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Empty(t, user2.FsConfig.GCSConfig.Credentials.GetKey())
|
|
|
|
assert.Empty(t, user2.FsConfig.GCSConfig.Credentials.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user2.FsConfig.GCSConfig.Credentials.GetStatus())
|
|
|
|
assert.NotEmpty(t, user2.FsConfig.GCSConfig.Credentials.GetPayload())
|
2020-11-22 20:53:04 +00:00
|
|
|
|
|
|
|
user3, _, err = httpd.GetUserByID(user3.ID, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, user3.Password)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Empty(t, user3.FsConfig.AzBlobConfig.AccountKey.GetKey())
|
|
|
|
assert.Empty(t, user3.FsConfig.AzBlobConfig.AccountKey.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user3.FsConfig.AzBlobConfig.AccountKey.GetStatus())
|
|
|
|
assert.NotEmpty(t, user3.FsConfig.AzBlobConfig.AccountKey.GetPayload())
|
2020-11-22 20:53:04 +00:00
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
user4, _, err = httpd.GetUserByID(user4.ID, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, user4.Password)
|
|
|
|
assert.Empty(t, user4.FsConfig.CryptConfig.Passphrase.GetKey())
|
|
|
|
assert.Empty(t, user4.FsConfig.CryptConfig.Passphrase.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user4.FsConfig.CryptConfig.Passphrase.GetStatus())
|
|
|
|
assert.NotEmpty(t, user4.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
|
2020-11-22 20:53:04 +00:00
|
|
|
// finally check that we have all the data inside the data provider
|
|
|
|
user1, err = dataprovider.GetUserByID(user1.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, user1.Password)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.NotEmpty(t, user1.FsConfig.S3Config.AccessSecret.GetKey())
|
|
|
|
assert.NotEmpty(t, user1.FsConfig.S3Config.AccessSecret.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user1.FsConfig.S3Config.AccessSecret.GetStatus())
|
|
|
|
assert.NotEmpty(t, user1.FsConfig.S3Config.AccessSecret.GetPayload())
|
2020-11-22 20:53:04 +00:00
|
|
|
err = user1.FsConfig.S3Config.AccessSecret.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusPlain, user1.FsConfig.S3Config.AccessSecret.GetStatus())
|
|
|
|
assert.Equal(t, u1.FsConfig.S3Config.AccessSecret.GetPayload(), user1.FsConfig.S3Config.AccessSecret.GetPayload())
|
|
|
|
assert.Empty(t, user1.FsConfig.S3Config.AccessSecret.GetKey())
|
|
|
|
assert.Empty(t, user1.FsConfig.S3Config.AccessSecret.GetAdditionalData())
|
2020-11-22 20:53:04 +00:00
|
|
|
|
|
|
|
user2, err = dataprovider.GetUserByID(user2.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, user2.Password)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.NotEmpty(t, user2.FsConfig.GCSConfig.Credentials.GetKey())
|
|
|
|
assert.NotEmpty(t, user2.FsConfig.GCSConfig.Credentials.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user2.FsConfig.GCSConfig.Credentials.GetStatus())
|
|
|
|
assert.NotEmpty(t, user2.FsConfig.GCSConfig.Credentials.GetPayload())
|
2020-11-22 20:53:04 +00:00
|
|
|
err = user2.FsConfig.GCSConfig.Credentials.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusPlain, user2.FsConfig.GCSConfig.Credentials.GetStatus())
|
|
|
|
assert.Equal(t, u2.FsConfig.GCSConfig.Credentials.GetPayload(), user2.FsConfig.GCSConfig.Credentials.GetPayload())
|
|
|
|
assert.Empty(t, user2.FsConfig.GCSConfig.Credentials.GetKey())
|
|
|
|
assert.Empty(t, user2.FsConfig.GCSConfig.Credentials.GetAdditionalData())
|
2020-11-22 20:53:04 +00:00
|
|
|
|
|
|
|
user3, err = dataprovider.GetUserByID(user3.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, user3.Password)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.NotEmpty(t, user3.FsConfig.AzBlobConfig.AccountKey.GetKey())
|
|
|
|
assert.NotEmpty(t, user3.FsConfig.AzBlobConfig.AccountKey.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user3.FsConfig.AzBlobConfig.AccountKey.GetStatus())
|
|
|
|
assert.NotEmpty(t, user3.FsConfig.AzBlobConfig.AccountKey.GetPayload())
|
2020-11-22 20:53:04 +00:00
|
|
|
err = user3.FsConfig.AzBlobConfig.AccountKey.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusPlain, user3.FsConfig.AzBlobConfig.AccountKey.GetStatus())
|
|
|
|
assert.Equal(t, u3.FsConfig.AzBlobConfig.AccountKey.GetPayload(), user3.FsConfig.AzBlobConfig.AccountKey.GetPayload())
|
|
|
|
assert.Empty(t, user3.FsConfig.AzBlobConfig.AccountKey.GetKey())
|
|
|
|
assert.Empty(t, user3.FsConfig.AzBlobConfig.AccountKey.GetAdditionalData())
|
2020-11-22 20:53:04 +00:00
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
user4, err = dataprovider.GetUserByID(user4.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, user4.Password)
|
|
|
|
assert.NotEmpty(t, user4.FsConfig.CryptConfig.Passphrase.GetKey())
|
|
|
|
assert.NotEmpty(t, user4.FsConfig.CryptConfig.Passphrase.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user4.FsConfig.CryptConfig.Passphrase.GetStatus())
|
|
|
|
assert.NotEmpty(t, user4.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
err = user4.FsConfig.CryptConfig.Passphrase.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, kms.SecretStatusPlain, user4.FsConfig.CryptConfig.Passphrase.GetStatus())
|
|
|
|
assert.Equal(t, u4.FsConfig.CryptConfig.Passphrase.GetPayload(), user4.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
assert.Empty(t, user4.FsConfig.CryptConfig.Passphrase.GetKey())
|
|
|
|
assert.Empty(t, user4.FsConfig.CryptConfig.Passphrase.GetAdditionalData())
|
|
|
|
|
2020-11-22 20:53:04 +00:00
|
|
|
_, err = httpd.RemoveUser(user1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.RemoveUser(user2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.RemoveUser(user3, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2020-12-05 12:48:13 +00:00
|
|
|
_, err = httpd.RemoveUser(user4, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2020-11-22 20:53:04 +00:00
|
|
|
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
providerConf.CredentialsPath = credentialsPath
|
|
|
|
err = os.RemoveAll(credentialsPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretObject(t *testing.T) {
|
2020-11-30 20:46:34 +00:00
|
|
|
s := kms.NewPlainSecret("test data")
|
|
|
|
s.SetAdditionalData("username")
|
2020-11-22 20:53:04 +00:00
|
|
|
require.True(t, s.IsValid())
|
|
|
|
err := s.Encrypt()
|
|
|
|
require.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
require.Equal(t, kms.SecretStatusSecretBox, s.GetStatus())
|
|
|
|
require.NotEmpty(t, s.GetPayload())
|
|
|
|
require.NotEmpty(t, s.GetKey())
|
2020-11-22 20:53:04 +00:00
|
|
|
require.True(t, s.IsValid())
|
|
|
|
err = s.Decrypt()
|
|
|
|
require.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
require.Equal(t, kms.SecretStatusPlain, s.GetStatus())
|
|
|
|
require.Equal(t, "test data", s.GetPayload())
|
|
|
|
require.Empty(t, s.GetKey())
|
2020-11-22 20:53:04 +00:00
|
|
|
|
|
|
|
oldFormat := "$aes$5b97e3a3324a2f53e2357483383367c0$0ed3132b584742ab217866219da633266782b69b13e50ebc6ddfb7c4fbf2f2a414c6d5f813"
|
2020-11-30 20:46:34 +00:00
|
|
|
s, err = kms.GetSecretFromCompatString(oldFormat)
|
2020-11-22 20:53:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, s.IsValid())
|
2020-11-30 20:46:34 +00:00
|
|
|
require.Equal(t, kms.SecretStatusPlain, s.GetStatus())
|
|
|
|
require.Equal(t, "test data", s.GetPayload())
|
|
|
|
require.Empty(t, s.GetKey())
|
2020-11-22 20:53:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 21:18:16 +00:00
|
|
|
func TestSecretObjectCompatibility(t *testing.T) {
|
|
|
|
// this is manually tested against vault too
|
|
|
|
testPayload := "test payload"
|
|
|
|
s := kms.NewPlainSecret(testPayload)
|
|
|
|
require.True(t, s.IsValid())
|
|
|
|
err := s.Encrypt()
|
|
|
|
require.NoError(t, err)
|
|
|
|
localAsJSON, err := json.Marshal(s)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-02 08:44:18 +00:00
|
|
|
for _, secretStatus := range []string{kms.SecretStatusSecretBox} {
|
2020-12-01 21:18:16 +00:00
|
|
|
kmsConfig := config.GetKMSConfig()
|
|
|
|
assert.Empty(t, kmsConfig.Secrets.MasterKeyPath)
|
2020-12-02 08:44:18 +00:00
|
|
|
if secretStatus == kms.SecretStatusVaultTransit {
|
2020-12-01 21:18:16 +00:00
|
|
|
os.Setenv("VAULT_SERVER_URL", "http://127.0.0.1:8200")
|
|
|
|
os.Setenv("VAULT_SERVER_TOKEN", "s.9lYGq83MbgG5KR5kfebXVyhJ")
|
|
|
|
kmsConfig.Secrets.URL = "hashivault://mykey"
|
|
|
|
}
|
|
|
|
err := kmsConfig.Initialize()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// encrypt without a master key
|
|
|
|
secret := kms.NewPlainSecret(testPayload)
|
|
|
|
secret.SetAdditionalData("add data")
|
|
|
|
err = secret.Encrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, secret.GetMode())
|
|
|
|
secretClone := secret.Clone()
|
|
|
|
err = secretClone.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testPayload, secretClone.GetPayload())
|
2020-12-02 08:44:18 +00:00
|
|
|
if secretStatus == kms.SecretStatusVaultTransit {
|
2020-12-01 21:18:16 +00:00
|
|
|
// decrypt the local secret now that the provider is vault
|
|
|
|
secretLocal := kms.NewEmptySecret()
|
|
|
|
err = json.Unmarshal(localAsJSON, secretLocal)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, secretLocal.GetStatus())
|
|
|
|
assert.Equal(t, 0, secretLocal.GetMode())
|
|
|
|
err = secretLocal.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testPayload, secretLocal.GetPayload())
|
|
|
|
assert.Equal(t, kms.SecretStatusPlain, secretLocal.GetStatus())
|
|
|
|
err = secretLocal.Encrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, secretLocal.GetStatus())
|
|
|
|
assert.Equal(t, 0, secretLocal.GetMode())
|
|
|
|
}
|
|
|
|
|
|
|
|
asJSON, err := json.Marshal(secret)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
masterKeyPath := filepath.Join(os.TempDir(), "mkey")
|
|
|
|
err = ioutil.WriteFile(masterKeyPath, []byte("test key"), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
config := kms.Configuration{
|
|
|
|
Secrets: kms.Secrets{
|
|
|
|
MasterKeyPath: masterKeyPath,
|
|
|
|
},
|
|
|
|
}
|
2020-12-02 08:44:18 +00:00
|
|
|
if secretStatus == kms.SecretStatusVaultTransit {
|
2020-12-01 21:18:16 +00:00
|
|
|
config.Secrets.URL = "hashivault://mykey"
|
|
|
|
}
|
|
|
|
err = config.Initialize()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// now build the secret from JSON
|
|
|
|
secret = kms.NewEmptySecret()
|
|
|
|
err = json.Unmarshal(asJSON, secret)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, secret.GetMode())
|
|
|
|
err = secret.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testPayload, secret.GetPayload())
|
|
|
|
err = secret.Encrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, secret.GetMode())
|
|
|
|
err = secret.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testPayload, secret.GetPayload())
|
2020-12-02 08:44:18 +00:00
|
|
|
if secretStatus == kms.SecretStatusVaultTransit {
|
2020-12-01 21:18:16 +00:00
|
|
|
// decrypt the local secret encryped without a master key now that
|
|
|
|
// the provider is vault and a master key is set.
|
|
|
|
// The provider will not change, the master key will be used
|
|
|
|
secretLocal := kms.NewEmptySecret()
|
|
|
|
err = json.Unmarshal(localAsJSON, secretLocal)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, secretLocal.GetStatus())
|
|
|
|
assert.Equal(t, 0, secretLocal.GetMode())
|
|
|
|
err = secretLocal.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testPayload, secretLocal.GetPayload())
|
|
|
|
assert.Equal(t, kms.SecretStatusPlain, secretLocal.GetStatus())
|
|
|
|
err = secretLocal.Encrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, secretLocal.GetStatus())
|
|
|
|
assert.Equal(t, 1, secretLocal.GetMode())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kmsConfig.Initialize()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(masterKeyPath)
|
|
|
|
assert.NoError(t, err)
|
2020-12-02 08:44:18 +00:00
|
|
|
if secretStatus == kms.SecretStatusVaultTransit {
|
2020-12-01 21:18:16 +00:00
|
|
|
os.Unsetenv("VAULT_SERVER_URL")
|
|
|
|
os.Unsetenv("VAULT_SERVER_TOKEN")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func TestUpdateUserNoCredentials(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
user.Password = ""
|
2019-08-07 21:41:10 +00:00
|
|
|
user.PublicKeys = []string{}
|
2019-07-20 19:17:53 +00:00
|
|
|
// password and public key will be omitted from json serialization if empty and so they will remain unchanged
|
2019-07-20 10:26:52 +00:00
|
|
|
// and no validation error will be raised
|
2020-09-01 14:10:26 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateUserEmptyHomeDir(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
user.HomeDir = ""
|
2020-09-01 14:10:26 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusBadRequest, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateUserInvalidHomeDir(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
user.HomeDir = "relative_path"
|
2020-09-01 14:10:26 +00:00
|
|
|
_, _, err = httpd.UpdateUser(user, http.StatusBadRequest, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateNonExistentUser(t *testing.T) {
|
2020-09-01 14:10:26 +00:00
|
|
|
_, _, err := httpd.UpdateUser(getTestUser(), http.StatusNotFound, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetNonExistentUser(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.GetUserByID(0, http.StatusNotFound)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteNonExistentUser(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err := httpd.RemoveUser(getTestUser(), http.StatusNotFound)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddDuplicateUser(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.AddUser(getTestUser(), http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err, "adding a duplicate user must fail")
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUsers(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
user1, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
u := getTestUser()
|
|
|
|
u.Username = defaultUsername + "1"
|
2019-10-07 16:19:01 +00:00
|
|
|
user2, _, err := httpd.AddUser(u, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
users, _, err := httpd.GetUsers(0, 0, "", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(users), 2)
|
2019-10-07 16:19:01 +00:00
|
|
|
users, _, err = httpd.GetUsers(1, 0, "", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
2019-10-07 16:19:01 +00:00
|
|
|
users, _, err = httpd.GetUsers(1, 1, "", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.GetUsers(1, 1, "", http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user1, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user2, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetQuotaScans(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.GetQuotaScans(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.GetQuotaScans(http.StatusInternalServerError)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
_, _, err = httpd.GetFoldersQuotaScans(http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpd.GetFoldersQuotaScans(http.StatusInternalServerError)
|
|
|
|
assert.Error(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartQuotaScan(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-13 17:43:56 +00:00
|
|
|
_, err = httpd.StartQuotaScan(user, http.StatusAccepted)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "folder"),
|
|
|
|
}
|
|
|
|
_, _, err = httpd.AddFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2020-09-13 17:43:56 +00:00
|
|
|
_, err = httpd.StartFolderQuotaScan(folder, http.StatusAccepted)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-22 18:02:51 +00:00
|
|
|
for {
|
|
|
|
quotaScan, _, err := httpd.GetFoldersQuotaScans(http.StatusOK)
|
|
|
|
if !assert.NoError(t, err, "Error getting active scans") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(quotaScan) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
_, err = httpd.RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
func TestUpdateFolderQuotaUsage(t *testing.T) {
|
|
|
|
f := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "folder"),
|
|
|
|
}
|
|
|
|
usedQuotaFiles := 1
|
|
|
|
usedQuotaSize := int64(65535)
|
|
|
|
f.UsedQuotaFiles = usedQuotaFiles
|
|
|
|
f.UsedQuotaSize = usedQuotaSize
|
|
|
|
folder, _, err := httpd.AddFolder(f, http.StatusOK)
|
2020-06-27 10:36:42 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, usedQuotaFiles, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, usedQuotaSize, folder.UsedQuotaSize)
|
|
|
|
}
|
2020-06-20 10:38:04 +00:00
|
|
|
_, err = httpd.UpdateFolderQuotaUsage(folder, "invalid mode", http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.UpdateFolderQuotaUsage(f, "reset", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
folders, _, err := httpd.GetFolders(0, 0, f.MappedPath, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder = folders[0]
|
|
|
|
assert.Equal(t, usedQuotaFiles, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, usedQuotaSize, folder.UsedQuotaSize)
|
|
|
|
}
|
|
|
|
_, err = httpd.UpdateFolderQuotaUsage(f, "add", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
folders, _, err = httpd.GetFolders(0, 0, f.MappedPath, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder = folders[0]
|
|
|
|
assert.Equal(t, 2*usedQuotaFiles, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, 2*usedQuotaSize, folder.UsedQuotaSize)
|
|
|
|
}
|
|
|
|
f.UsedQuotaSize = -1
|
|
|
|
_, err = httpd.UpdateFolderQuotaUsage(f, "", http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
f.UsedQuotaSize = usedQuotaSize
|
|
|
|
f.MappedPath = f.MappedPath + "1"
|
|
|
|
_, err = httpd.UpdateFolderQuotaUsage(f, "", http.StatusNotFound)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-08-08 08:01:33 +00:00
|
|
|
func TestGetVersion(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.GetVersion(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.GetVersion(http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err, "get version request must succeed, we requested to check a wrong status code")
|
2019-08-08 08:01:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 17:48:01 +00:00
|
|
|
func TestGetProviderStatus(t *testing.T) {
|
|
|
|
_, _, err := httpd.GetProviderStatus(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-11-14 17:48:01 +00:00
|
|
|
_, _, err = httpd.GetProviderStatus(http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err, "get provider status request must succeed, we requested to check a wrong status code")
|
2019-11-14 17:48:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestGetConnections(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err := httpd.GetConnections(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.GetConnections(http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err, "get sftp connections request must succeed, we requested to check a wrong status code")
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestCloseActiveConnection(t *testing.T) {
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err := httpd.CloseConnection("non_existent_id", http.StatusNotFound)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-01 14:10:26 +00:00
|
|
|
user := getTestUser()
|
|
|
|
c := common.NewBaseConnection("connID", common.ProtocolSFTP, user, nil)
|
|
|
|
fakeConn := &fakeConnection{
|
|
|
|
BaseConnection: c,
|
|
|
|
}
|
|
|
|
common.Connections.Add(fakeConn)
|
|
|
|
_, err = httpd.CloseConnection(c.GetID(), http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseConnectionAfterUserUpdateDelete(t *testing.T) {
|
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
c := common.NewBaseConnection("connID", common.ProtocolFTP, user, nil)
|
|
|
|
fakeConn := &fakeConnection{
|
|
|
|
BaseConnection: c,
|
|
|
|
}
|
|
|
|
common.Connections.Add(fakeConn)
|
|
|
|
c1 := common.NewBaseConnection("connID1", common.ProtocolSFTP, user, nil)
|
|
|
|
fakeConn1 := &fakeConnection{
|
|
|
|
BaseConnection: c1,
|
|
|
|
}
|
|
|
|
common.Connections.Add(fakeConn1)
|
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "0")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 2)
|
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 0)
|
|
|
|
|
|
|
|
common.Connections.Add(fakeConn)
|
|
|
|
common.Connections.Add(fakeConn1)
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 2)
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 0)
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 20:48:52 +00:00
|
|
|
func TestUserBaseDir(t *testing.T) {
|
2020-07-08 17:59:31 +00:00
|
|
|
err := dataprovider.Close()
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
2019-09-28 20:48:52 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.UsersBaseDir = homeBasePath
|
2020-05-03 13:24:26 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
|
|
|
assert.NoError(t, err)
|
2019-09-28 20:48:52 +00:00
|
|
|
u := getTestUser()
|
|
|
|
u.HomeDir = ""
|
2020-06-07 21:30:18 +00:00
|
|
|
user, _, err := httpd.AddUser(u, http.StatusOK)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualError(t, err, "HomeDir mismatch")
|
|
|
|
}
|
|
|
|
assert.Equal(t, filepath.Join(providerConf.UsersBaseDir, u.Username), user.HomeDir)
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2020-07-08 17:59:31 +00:00
|
|
|
err = dataprovider.Close()
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
providerConf.CredentialsPath = credentialsPath
|
|
|
|
err = os.RemoveAll(credentialsPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotaTrackingDisabled(t *testing.T) {
|
2020-07-08 17:59:31 +00:00
|
|
|
err := dataprovider.Close()
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.TrackQuota = 0
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// user quota scan must fail
|
2019-10-07 16:19:01 +00:00
|
|
|
user, _, err := httpd.AddUser(getTestUser(), http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
_, err = httpd.StartQuotaScan(user, http.StatusForbidden)
|
|
|
|
assert.NoError(t, err)
|
2020-06-20 10:38:04 +00:00
|
|
|
_, err = httpd.UpdateQuotaUsage(user, "", http.StatusForbidden)
|
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
// folder quota scan must fail
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
2020-06-07 22:15:14 +00:00
|
|
|
MappedPath: filepath.Clean(os.TempDir()),
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
folder, _, err = httpd.AddFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.StartFolderQuotaScan(folder, http.StatusForbidden)
|
|
|
|
assert.NoError(t, err)
|
2020-06-20 10:38:04 +00:00
|
|
|
_, err = httpd.UpdateFolderQuotaUsage(folder, "", http.StatusForbidden)
|
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
_, err = httpd.RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-07-08 17:59:31 +00:00
|
|
|
err = dataprovider.Close()
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
2019-09-28 20:48:52 +00:00
|
|
|
providerConf = config.GetProviderConf()
|
2020-01-31 18:04:00 +00:00
|
|
|
providerConf.CredentialsPath = credentialsPath
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.RemoveAll(credentialsPath)
|
|
|
|
assert.NoError(t, err)
|
2019-09-28 20:48:52 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-09-28 20:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProviderErrors(t *testing.T) {
|
2020-07-08 17:59:31 +00:00
|
|
|
err := dataprovider.Close()
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpd.GetUserByID(0, http.StatusInternalServerError)
|
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, _, err = httpd.GetUsers(1, 0, defaultUsername, http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-01 14:10:26 +00:00
|
|
|
_, _, err = httpd.UpdateUser(dataprovider.User{}, http.StatusInternalServerError, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
_, err = httpd.RemoveUser(dataprovider.User{}, http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
_, err = httpd.RemoveFolder(vfs.BaseVirtualFolder{MappedPath: "apath"}, http.StatusInternalServerError)
|
|
|
|
assert.NoError(t, err)
|
2019-11-14 17:48:01 +00:00
|
|
|
_, _, err = httpd.GetProviderStatus(http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
_, _, err = httpd.Dumpdata("backup.json", "", http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
_, _, err = httpd.GetFolders(0, 0, "", http.StatusInternalServerError)
|
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
user.ID = 1
|
2020-02-02 21:20:39 +00:00
|
|
|
backupData := dataprovider.BackupData{}
|
2019-12-27 22:12:44 +00:00
|
|
|
backupData.Users = append(backupData.Users, user)
|
2020-06-07 21:30:18 +00:00
|
|
|
backupContent, err := json.Marshal(backupData)
|
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
backupFilePath := filepath.Join(backupsPath, "backup.json")
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(backupFilePath, backupContent, os.ModePerm)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "", "", http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
backupData.Folders = append(backupData.Folders, vfs.BaseVirtualFolder{MappedPath: os.TempDir()})
|
|
|
|
backupContent, err = json.Marshal(backupData)
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(backupFilePath, backupContent, os.ModePerm)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "", "", http.StatusInternalServerError)
|
|
|
|
assert.NoError(t, err)
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.Remove(backupFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
2019-09-28 20:48:52 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
2020-01-31 18:04:00 +00:00
|
|
|
providerConf.CredentialsPath = credentialsPath
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.RemoveAll(credentialsPath)
|
|
|
|
assert.NoError(t, err)
|
2019-09-28 20:48:52 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-09-28 20:48:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestFolders(t *testing.T) {
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: "relative path",
|
|
|
|
}
|
|
|
|
_, _, err := httpd.AddFolder(folder, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-06-07 22:15:14 +00:00
|
|
|
folder.MappedPath = filepath.Clean(os.TempDir())
|
2020-06-07 21:30:18 +00:00
|
|
|
folder1, _, err := httpd.AddFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, folder.MappedPath, folder1.MappedPath)
|
|
|
|
assert.Equal(t, 0, folder1.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(0), folder1.UsedQuotaSize)
|
|
|
|
assert.Equal(t, int64(0), folder1.LastQuotaUpdate)
|
|
|
|
// adding a duplicate folder must fail
|
|
|
|
_, _, err = httpd.AddFolder(folder, http.StatusOK)
|
|
|
|
assert.Error(t, err)
|
|
|
|
folder.MappedPath = filepath.Join(os.TempDir(), "vfolder")
|
|
|
|
folder.UsedQuotaFiles = 1
|
|
|
|
folder.UsedQuotaSize = 345
|
|
|
|
folder.LastQuotaUpdate = 10
|
|
|
|
folder2, _, err := httpd.AddFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, folder2.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(345), folder2.UsedQuotaSize)
|
|
|
|
assert.Equal(t, int64(10), folder2.LastQuotaUpdate)
|
|
|
|
folders, _, err := httpd.GetFolders(0, 0, "", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
numResults := len(folders)
|
|
|
|
assert.GreaterOrEqual(t, numResults, 2)
|
|
|
|
folders, _, err = httpd.GetFolders(0, 1, "", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, folders, numResults-1)
|
|
|
|
folders, _, err = httpd.GetFolders(1, 0, "", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, folders, 1)
|
|
|
|
folders, _, err = httpd.GetFolders(0, 0, folder1.MappedPath, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
f := folders[0]
|
|
|
|
assert.Equal(t, folder1.MappedPath, f.MappedPath)
|
|
|
|
}
|
|
|
|
folders, _, err = httpd.GetFolders(0, 0, folder2.MappedPath, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
f := folders[0]
|
|
|
|
assert.Equal(t, folder2.MappedPath, f.MappedPath)
|
|
|
|
}
|
|
|
|
_, err = httpd.RemoveFolder(vfs.BaseVirtualFolder{}, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.RemoveFolder(vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: "invalid",
|
|
|
|
}, http.StatusNotFound)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = httpd.RemoveFolder(folder1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpd.RemoveFolder(folder2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-12-27 22:12:44 +00:00
|
|
|
func TestDumpdata(t *testing.T) {
|
2020-07-08 17:59:31 +00:00
|
|
|
err := dataprovider.Close()
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
2020-05-03 13:24:26 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
|
|
|
assert.NoError(t, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
_, _, err = httpd.Dumpdata("", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
_, _, err = httpd.Dumpdata(filepath.Join(backupsPath, "backup.json"), "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
_, _, err = httpd.Dumpdata("../backup.json", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
_, _, err = httpd.Dumpdata("backup.json", "0", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
_, _, err = httpd.Dumpdata("backup.json", "1", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(filepath.Join(backupsPath, "backup.json"))
|
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.Chmod(backupsPath, 0001)
|
|
|
|
assert.NoError(t, err)
|
2020-02-02 21:20:39 +00:00
|
|
|
_, _, err = httpd.Dumpdata("bck.json", "", http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-04-30 12:23:55 +00:00
|
|
|
// subdir cannot be created
|
|
|
|
_, _, err = httpd.Dumpdata(filepath.Join("subdir", "bck.json"), "", http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Chmod(backupsPath, 0755)
|
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
2020-07-08 17:59:31 +00:00
|
|
|
err = dataprovider.Close()
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
providerConf.CredentialsPath = credentialsPath
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.RemoveAll(credentialsPath)
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoaddata(t *testing.T) {
|
2020-06-07 21:30:18 +00:00
|
|
|
mappedPath := filepath.Join(os.TempDir(), "restored_folder")
|
2019-12-27 22:12:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
user.ID = 1
|
|
|
|
user.Username = "test_user_restore"
|
2020-02-02 21:20:39 +00:00
|
|
|
backupData := dataprovider.BackupData{}
|
2019-12-27 22:12:44 +00:00
|
|
|
backupData.Users = append(backupData.Users, user)
|
2020-06-07 21:30:18 +00:00
|
|
|
backupData.Folders = []vfs.BaseVirtualFolder{
|
|
|
|
{
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
UsedQuotaSize: 123,
|
|
|
|
UsedQuotaFiles: 456,
|
|
|
|
LastQuotaUpdate: 789,
|
|
|
|
Users: []string{"user"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
backupContent, err := json.Marshal(backupData)
|
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
backupFilePath := filepath.Join(backupsPath, "backup.json")
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ioutil.WriteFile(backupFilePath, backupContent, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "a", "", http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "", "a", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = httpd.Loaddata("backup.json", "1", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = httpd.Loaddata(backupFilePath+"a", "1", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.Chmod(backupFilePath, 0111)
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "1", "", http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Chmod(backupFilePath, 0644)
|
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
// add user and folder from backup
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "1", "", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
// update user from backup
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "2", "", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
users, _, err := httpd.GetUsers(1, 0, user.Username, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
if assert.Len(t, users, 1) {
|
2020-05-03 13:24:26 +00:00
|
|
|
user = users[0]
|
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
folders, _, err := httpd.GetFolders(1, 0, mappedPath, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder := folders[0]
|
|
|
|
assert.Equal(t, mappedPath, folder.MappedPath)
|
|
|
|
assert.Equal(t, int64(123), folder.UsedQuotaSize)
|
|
|
|
assert.Equal(t, 456, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(789), folder.LastQuotaUpdate)
|
|
|
|
assert.Len(t, folder.Users, 0)
|
|
|
|
_, err = httpd.RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.Remove(backupFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = createTestFile(backupFilePath, 10485761)
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "1", "0", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(backupFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = createTestFile(backupFilePath, 65535)
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "1", "0", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(backupFilePath)
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoaddataMode(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
user.ID = 1
|
|
|
|
user.Username = "test_user_restore"
|
2020-02-02 21:20:39 +00:00
|
|
|
backupData := dataprovider.BackupData{}
|
2020-01-31 18:10:45 +00:00
|
|
|
backupData.Users = append(backupData.Users, user)
|
|
|
|
backupContent, _ := json.Marshal(backupData)
|
|
|
|
backupFilePath := filepath.Join(backupsPath, "backup.json")
|
2020-07-29 19:56:56 +00:00
|
|
|
err := ioutil.WriteFile(backupFilePath, backupContent, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "0", "0", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
users, _, err := httpd.GetUsers(1, 0, user.Username, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
2020-01-31 18:10:45 +00:00
|
|
|
user = users[0]
|
|
|
|
oldUploadBandwidth := user.UploadBandwidth
|
|
|
|
user.UploadBandwidth = oldUploadBandwidth + 128
|
2020-09-01 14:10:26 +00:00
|
|
|
user, _, err = httpd.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "0", "1", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-01 14:10:26 +00:00
|
|
|
|
|
|
|
c := common.NewBaseConnection("connID", common.ProtocolFTP, user, nil)
|
|
|
|
fakeConn := &fakeConnection{
|
|
|
|
BaseConnection: c,
|
|
|
|
}
|
|
|
|
common.Connections.Add(fakeConn)
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 1)
|
2020-01-31 18:10:45 +00:00
|
|
|
users, _, err = httpd.GetUsers(1, 0, user.Username, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
2020-01-31 18:10:45 +00:00
|
|
|
user = users[0]
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NotEqual(t, oldUploadBandwidth, user.UploadBandwidth)
|
2020-09-01 14:10:26 +00:00
|
|
|
_, _, err = httpd.Loaddata(backupFilePath, "0", "2", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// mode 2 will update the user and close the previous connection
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 0)
|
|
|
|
users, _, err = httpd.GetUsers(1, 0, user.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
|
|
|
user = users[0]
|
|
|
|
assert.Equal(t, oldUploadBandwidth, user.UploadBandwidth)
|
2020-01-31 18:10:45 +00:00
|
|
|
_, err = httpd.RemoveUser(user, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(backupFilePath)
|
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 22:21:33 +00:00
|
|
|
func TestHTTPSConnection(t *testing.T) {
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
resp, err := client.Get("https://localhost:8443" + metricsPath)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
if !strings.Contains(err.Error(), "certificate is not valid") &&
|
|
|
|
!strings.Contains(err.Error(), "certificate signed by unknown authority") {
|
|
|
|
assert.Fail(t, err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resp.Body.Close()
|
2020-02-04 22:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
// test using mock http server
|
|
|
|
|
|
|
|
func TestBasicUserHandlingMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
2020-05-03 13:24:26 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2020-05-03 13:24:26 +00:00
|
|
|
err = render.DecodeJSON(rr.Body, &user)
|
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr.Code)
|
|
|
|
user.MaxSessions = 10
|
|
|
|
user.UploadBandwidth = 128
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny, dataprovider.PermDelete, dataprovider.PermDownload}
|
2019-07-26 09:34:44 +00:00
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+strconv.FormatInt(user.ID, 10), bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
var updatedUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &updatedUser)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, user.MaxSessions, updatedUser.MaxSessions)
|
|
|
|
assert.Equal(t, user.UploadBandwidth, updatedUser.UploadBandwidth)
|
|
|
|
assert.Equal(t, 1, len(updatedUser.Permissions["/"]))
|
|
|
|
assert.True(t, utils.IsStringInSlice(dataprovider.PermAny, updatedUser.Permissions["/"]))
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUserByIdInvalidParamsMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, userPath+"/0", nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"/a", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserNoUsernameMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
user.Username = ""
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserInvalidHomeDirMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
user.HomeDir = "relative_path"
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserInvalidPermsMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions["/"] = []string{}
|
2019-07-26 09:34:44 +00:00
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestAddFolderInvalidJsonMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, bytes.NewBuffer([]byte("invalid json")))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestAddUserInvalidJsonMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer([]byte("invalid json")))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
func TestUpdateUserMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
// permissions should not change if empty or nil
|
|
|
|
permissions := user.Permissions
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+strconv.FormatInt(user.ID, 10), bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
var updatedUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &updatedUser)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
for dir, perms := range permissions {
|
|
|
|
if actualPerms, ok := updatedUser.Permissions[dir]; ok {
|
|
|
|
for _, v := range actualPerms {
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(v, perms))
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Fail(t, "Permissions directories mismatch")
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
func TestUpdateUserQuotaUsageMock(t *testing.T) {
|
|
|
|
var user dataprovider.User
|
|
|
|
u := getTestUser()
|
|
|
|
usedQuotaFiles := 1
|
|
|
|
usedQuotaSize := int64(65535)
|
|
|
|
u.UsedQuotaFiles = usedQuotaFiles
|
|
|
|
u.UsedQuotaSize = usedQuotaSize
|
|
|
|
userAsJSON := getUserAsJSON(t, u)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, updateUsedQuotaPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &user)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, usedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, usedQuotaSize, user.UsedQuotaSize)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, updateUsedQuotaPath, bytes.NewBuffer([]byte("string")))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.True(t, common.QuotaScans.AddUserQuotaScan(user.Username))
|
2020-06-20 10:38:04 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, updateUsedQuotaPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusConflict, rr.Code)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.True(t, common.QuotaScans.RemoveUserQuotaScan(user.Username))
|
2020-06-20 10:38:04 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
func TestUserPermissionsMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/somedir"] = []string{dataprovider.PermAny}
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
user.Permissions[".."] = []string{dataprovider.PermAny}
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-10 18:28:35 +00:00
|
|
|
user.Permissions["/somedir"] = []string{"invalid"}
|
2019-12-25 17:20:19 +00:00
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+strconv.FormatInt(user.ID, 10), bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
delete(user.Permissions, "/somedir")
|
2020-02-10 18:28:35 +00:00
|
|
|
user.Permissions["/somedir/.."] = []string{dataprovider.PermAny}
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+strconv.FormatInt(user.ID, 10), bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
delete(user.Permissions, "/somedir/..")
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions["not_abs_path"] = []string{dataprovider.PermAny}
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+strconv.FormatInt(user.ID, 10), bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
delete(user.Permissions, "not_abs_path")
|
|
|
|
user.Permissions["/somedir/../otherdir/"] = []string{dataprovider.PermListItems}
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+strconv.FormatInt(user.ID, 10), bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
var updatedUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &updatedUser)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
if val, ok := updatedUser.Permissions["/otherdir"]; ok {
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(dataprovider.PermListItems, val))
|
|
|
|
assert.Equal(t, 1, len(val))
|
2019-12-25 17:20:19 +00:00
|
|
|
} else {
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Fail(t, "expected dir not found in permissions")
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestUpdateUserInvalidJsonMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+strconv.FormatInt(user.ID, 10), bytes.NewBuffer([]byte("Invalid json")))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateUserInvalidParamsMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
user.HomeDir = ""
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+strconv.FormatInt(user.ID, 10), bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
userID := user.ID
|
|
|
|
user.ID = 0
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+strconv.FormatInt(userID, 10), bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
user.ID = userID
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/0", bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/a", bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUsersMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=510&offset=0&order=ASC&username="+defaultUsername, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
var users []dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=a&offset=0&order=ASC", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=a&order=ASC", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASCa", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteUserInvalidParamsMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodDelete, userPath+"/0", nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/a", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetQuotaScansMock(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", quotaScanPath, nil)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartQuotaScanMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
_, err = os.Stat(user.HomeDir)
|
|
|
|
if err == nil {
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.Remove(user.HomeDir)
|
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
2019-08-04 19:37:31 +00:00
|
|
|
// simulate a duplicate quota scan
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
2020-07-24 21:39:38 +00:00
|
|
|
common.QuotaScans.AddUserQuotaScan(user.Username)
|
2019-08-04 19:37:31 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, quotaScanPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusConflict, rr.Code)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.True(t, common.QuotaScans.RemoveUserQuotaScan(user.Username))
|
2019-08-04 19:37:31 +00:00
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, quotaScanPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
2020-09-13 17:43:56 +00:00
|
|
|
checkResponseCode(t, http.StatusAccepted, rr.Code)
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
for {
|
2020-07-24 21:39:38 +00:00
|
|
|
var scans []common.ActiveQuotaScan
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, quotaScanPath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &scans)
|
2020-06-22 18:02:51 +00:00
|
|
|
if !assert.NoError(t, err, "Error getting active scans") {
|
2019-07-26 09:34:44 +00:00
|
|
|
break
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
if len(scans) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
_, err = os.Stat(user.HomeDir)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
2020-06-08 17:40:17 +00:00
|
|
|
err = os.MkdirAll(user.HomeDir, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, quotaScanPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr = executeRequest(req)
|
2020-09-13 17:43:56 +00:00
|
|
|
checkResponseCode(t, http.StatusAccepted, rr.Code)
|
2019-12-30 17:37:50 +00:00
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
for {
|
2020-07-24 21:39:38 +00:00
|
|
|
var scans []common.ActiveQuotaScan
|
2019-12-30 17:37:50 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, quotaScanPath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &scans)
|
2020-06-07 21:30:18 +00:00
|
|
|
if !assert.NoError(t, err) {
|
2020-06-22 18:02:51 +00:00
|
|
|
assert.Fail(t, err.Error(), "Error getting active scans")
|
2019-12-30 17:37:50 +00:00
|
|
|
break
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
if len(scans) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
func TestUpdateFolderQuotaUsageMock(t *testing.T) {
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "vfolder")
|
|
|
|
f := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
}
|
|
|
|
usedQuotaFiles := 1
|
|
|
|
usedQuotaSize := int64(65535)
|
|
|
|
f.UsedQuotaFiles = usedQuotaFiles
|
|
|
|
f.UsedQuotaSize = usedQuotaSize
|
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
folderAsJSON, err := json.Marshal(f)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, bytes.NewBuffer(folderAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, updateFolderUsedQuotaPath, bytes.NewBuffer(folderAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
var folders []vfs.BaseVirtualFolder
|
|
|
|
url, err := url.Parse(folderPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("folder_path", mappedPath)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, url.String(), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &folders)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder = folders[0]
|
|
|
|
assert.Equal(t, usedQuotaFiles, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, usedQuotaSize, folder.UsedQuotaSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, updateFolderUsedQuotaPath, bytes.NewBuffer([]byte("string")))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.True(t, common.QuotaScans.AddVFolderQuotaScan(mappedPath))
|
2020-06-20 10:38:04 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, updateFolderUsedQuotaPath, bytes.NewBuffer(folderAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusConflict, rr.Code)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.True(t, common.QuotaScans.RemoveVFolderQuotaScan(mappedPath))
|
2020-06-20 10:38:04 +00:00
|
|
|
|
|
|
|
url, err = url.Parse(folderPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
q = url.Query()
|
|
|
|
q.Add("folder_path", mappedPath)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, url.String(), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestStartFolderQuotaScanMock(t *testing.T) {
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "vfolder")
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
}
|
|
|
|
folderAsJSON, err := json.Marshal(folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, bytes.NewBuffer(folderAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
_, err = os.Stat(mappedPath)
|
|
|
|
if err == nil {
|
|
|
|
err = os.Remove(mappedPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
// simulate a duplicate quota scan
|
2020-07-24 21:39:38 +00:00
|
|
|
common.QuotaScans.AddVFolderQuotaScan(mappedPath)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, quotaScanVFolderPath, bytes.NewBuffer(folderAsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusConflict, rr.Code)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.True(t, common.QuotaScans.RemoveVFolderQuotaScan(mappedPath))
|
2020-06-07 21:30:18 +00:00
|
|
|
// and now a real quota scan
|
|
|
|
_, err = os.Stat(mappedPath)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
2020-06-08 17:40:17 +00:00
|
|
|
err = os.MkdirAll(mappedPath, os.ModePerm)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, quotaScanVFolderPath, bytes.NewBuffer(folderAsJSON))
|
|
|
|
rr = executeRequest(req)
|
2020-09-13 17:43:56 +00:00
|
|
|
checkResponseCode(t, http.StatusAccepted, rr.Code)
|
2020-07-24 21:39:38 +00:00
|
|
|
var scans []common.ActiveVirtualFolderQuotaScan
|
2020-06-07 21:30:18 +00:00
|
|
|
for {
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, quotaScanVFolderPath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &scans)
|
2020-06-22 18:02:51 +00:00
|
|
|
if !assert.NoError(t, err, "Error getting active folders scans") {
|
2020-06-07 21:30:18 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(scans) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
// cleanup
|
|
|
|
url, err := url.Parse(folderPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("folder_path", mappedPath)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, url.String(), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = os.RemoveAll(folderPath)
|
|
|
|
assert.NoError(t, err)
|
2020-07-10 17:20:37 +00:00
|
|
|
err = os.RemoveAll(mappedPath)
|
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartQuotaScanNonExistentUserMock(t *testing.T) {
|
2019-07-26 09:34:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, quotaScanPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr.Code)
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestStartQuotaScanBadUserMock(t *testing.T) {
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, quotaScanPath, bytes.NewBuffer([]byte("invalid json")))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestStartQuotaScanBadFolderMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, quotaScanVFolderPath, bytes.NewBuffer([]byte("invalid json")))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartQuotaScanNonExistentFolderMock(t *testing.T) {
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: os.TempDir(),
|
|
|
|
}
|
|
|
|
folderAsJSON, err := json.Marshal(folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, quotaScanVFolderPath, bytes.NewBuffer(folderAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFoldersMock(t *testing.T) {
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "vfolder")
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
}
|
|
|
|
folderAsJSON, err := json.Marshal(folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, bytes.NewBuffer(folderAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var folders []vfs.BaseVirtualFolder
|
|
|
|
url, err := url.Parse(folderPath + "?limit=510&offset=0&order=DESC")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("folder_path", mappedPath)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, url.String(), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &folders)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, folders, 1)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, folderPath+"?limit=a&offset=0&order=ASC", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, folderPath+"?limit=1&offset=a&order=ASC", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, folderPath+"?limit=1&offset=0&order=ASCa", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
|
|
|
|
url, err = url.Parse(folderPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
q = url.Query()
|
|
|
|
q.Add("folder_path", mappedPath)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, url.String(), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
2019-08-08 08:01:33 +00:00
|
|
|
func TestGetVersionMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, versionPath, nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestGetConnectionsMock(t *testing.T) {
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, activeConnectionsPath, nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteActiveConnectionMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodDelete, activeConnectionsPath+"/connectionID", nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotFoundMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/non/existing/path", nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMethodNotAllowedMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, activeConnectionsPath, nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusMethodNotAllowed, rr.Code)
|
|
|
|
}
|
2019-07-26 13:08:08 +00:00
|
|
|
|
2019-09-13 16:45:36 +00:00
|
|
|
func TestMetricsMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, metricsPath, nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
2020-10-29 20:50:21 +00:00
|
|
|
func TestHealthCheck(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/healthz", nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
assert.Equal(t, "ok", rr.Body.String())
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:16:35 +00:00
|
|
|
func TestPProfEndPointMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, pprofPath, nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func TestGetWebRootMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusMovedPermanently, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webBasePath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusMovedPermanently, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicWebUsersMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
user1 := getTestUser()
|
|
|
|
user1.Username += "1"
|
|
|
|
user1AsJSON := getUserAsJSON(t, user1)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(user1AsJSON))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &user1)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUsersPath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUsersPath+"?qlimit=a", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUsersPath+"?qlimit=1", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath+"/0", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath+"/a", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", user.Username)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/0", &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr.Code)
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/a", &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user1.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebUserAddMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
user.UploadBandwidth = 32
|
|
|
|
user.DownloadBandwidth = 64
|
|
|
|
user.UID = 1000
|
2020-11-25 21:26:34 +00:00
|
|
|
user.AdditionalInfo = "info"
|
2020-02-23 10:30:26 +00:00
|
|
|
mappedDir := filepath.Join(os.TempDir(), "mapped")
|
2019-10-07 16:19:01 +00:00
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", user.Username)
|
|
|
|
form.Set("home_dir", user.HomeDir)
|
|
|
|
form.Set("password", user.Password)
|
2019-11-13 10:36:21 +00:00
|
|
|
form.Set("status", strconv.Itoa(user.Status))
|
|
|
|
form.Set("expiration_date", "")
|
2019-10-07 16:19:01 +00:00
|
|
|
form.Set("permissions", "*")
|
2020-02-23 10:30:26 +00:00
|
|
|
form.Set("sub_dirs_permissions", " /subdir::list ,download ")
|
2020-06-07 21:30:18 +00:00
|
|
|
form.Set("virtual_folders", fmt.Sprintf(" /vdir:: %v :: 2 :: 1024", mappedDir))
|
2020-11-16 18:21:50 +00:00
|
|
|
form.Set("allowed_extensions", "/dir2::.jpg,.png\n/dir2::.ico\n/dir1::.rar")
|
|
|
|
form.Set("denied_extensions", "/dir2::.webp,.webp\n/dir2::.tiff\n/dir1::.zip")
|
|
|
|
form.Set("allowed_patterns", "/dir2::*.jpg,*.png\n/dir1::*.png")
|
|
|
|
form.Set("denied_patterns", "/dir1::*.zip\n/dir3::*.rar\n/dir2::*.mkv")
|
2020-11-25 21:26:34 +00:00
|
|
|
form.Set("additional_info", user.AdditionalInfo)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
2019-10-07 16:19:01 +00:00
|
|
|
// test invalid url escape
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, webUserPath+"?a=%2", &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("public_keys", testPubKey)
|
|
|
|
form.Set("uid", strconv.FormatInt(int64(user.UID), 10))
|
|
|
|
form.Set("gid", "a")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2019-10-07 16:19:01 +00:00
|
|
|
// test invalid gid
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("gid", "0")
|
|
|
|
form.Set("max_sessions", "a")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2019-10-07 16:19:01 +00:00
|
|
|
// test invalid max sessions
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("max_sessions", "0")
|
|
|
|
form.Set("quota_size", "a")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2019-10-07 16:19:01 +00:00
|
|
|
// test invalid quota size
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("quota_size", "0")
|
|
|
|
form.Set("quota_files", "a")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2019-10-07 16:19:01 +00:00
|
|
|
// test invalid quota files
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("quota_files", "0")
|
|
|
|
form.Set("upload_bandwidth", "a")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2019-10-07 16:19:01 +00:00
|
|
|
// test invalid upload bandwidth
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("upload_bandwidth", strconv.FormatInt(user.UploadBandwidth, 10))
|
|
|
|
form.Set("download_bandwidth", "a")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2019-10-07 16:19:01 +00:00
|
|
|
// test invalid download bandwidth
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("download_bandwidth", strconv.FormatInt(user.DownloadBandwidth, 10))
|
2019-11-13 10:36:21 +00:00
|
|
|
form.Set("status", "a")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2019-11-13 10:36:21 +00:00
|
|
|
// test invalid status
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-11-13 10:36:21 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("status", strconv.Itoa(user.Status))
|
|
|
|
form.Set("expiration_date", "123")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2019-11-13 10:36:21 +00:00
|
|
|
// test invalid expiration date
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-11-13 10:36:21 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("expiration_date", "")
|
2019-12-30 17:37:50 +00:00
|
|
|
form.Set("allowed_ip", "invalid,ip")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2019-12-30 17:37:50 +00:00
|
|
|
// test invalid allowed_ip
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-12-30 17:37:50 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("allowed_ip", "")
|
|
|
|
form.Set("denied_ip", "192.168.1.2") // it should be 192.168.1.2/32
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2019-12-30 17:37:50 +00:00
|
|
|
// test invalid denied_ip
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-12-30 17:37:50 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("denied_ip", "")
|
2020-08-16 18:17:02 +00:00
|
|
|
// test invalid max file upload size
|
|
|
|
form.Set("max_upload_file_size", "a")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("max_upload_file_size", "1000")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
// the user already exists, was created with the above request
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
var users []dataprovider.User
|
2020-05-06 17:36:34 +00:00
|
|
|
err := render.DecodeJSON(rr.Body, &users)
|
|
|
|
assert.NoError(t, err)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, 1, len(users))
|
2019-10-07 16:19:01 +00:00
|
|
|
newUser := users[0]
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, user.UID, newUser.UID)
|
|
|
|
assert.Equal(t, user.UploadBandwidth, newUser.UploadBandwidth)
|
|
|
|
assert.Equal(t, user.DownloadBandwidth, newUser.DownloadBandwidth)
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.Equal(t, int64(1000), newUser.Filters.MaxUploadFileSize)
|
2020-11-25 21:26:34 +00:00
|
|
|
assert.Equal(t, user.AdditionalInfo, newUser.AdditionalInfo)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(testPubKey, newUser.PublicKeys))
|
2019-12-30 17:37:50 +00:00
|
|
|
if val, ok := newUser.Permissions["/subdir"]; ok {
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(dataprovider.PermListItems, val))
|
|
|
|
assert.True(t, utils.IsStringInSlice(dataprovider.PermDownload, val))
|
2019-12-30 17:37:50 +00:00
|
|
|
} else {
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Fail(t, "user permissions must contain /somedir", "actual: %v", newUser.Permissions)
|
2020-02-23 10:30:26 +00:00
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.Len(t, newUser.VirtualFolders, 1)
|
2020-02-23 10:30:26 +00:00
|
|
|
for _, v := range newUser.VirtualFolders {
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.Equal(t, v.VirtualPath, "/vdir")
|
|
|
|
assert.Equal(t, v.MappedPath, mappedDir)
|
|
|
|
assert.Equal(t, v.QuotaFiles, 2)
|
|
|
|
assert.Equal(t, v.QuotaSize, int64(1024))
|
2020-02-23 10:30:26 +00:00
|
|
|
}
|
2020-11-15 21:04:48 +00:00
|
|
|
assert.Len(t, newUser.Filters.FileExtensions, 2)
|
|
|
|
for _, filter := range newUser.Filters.FileExtensions {
|
|
|
|
if filter.Path == "/dir1" {
|
2020-11-16 18:21:50 +00:00
|
|
|
assert.Len(t, filter.DeniedExtensions, 1)
|
|
|
|
assert.Len(t, filter.AllowedExtensions, 1)
|
2020-11-15 21:04:48 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(".zip", filter.DeniedExtensions))
|
2020-11-16 18:21:50 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(".rar", filter.AllowedExtensions))
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
|
|
|
if filter.Path == "/dir2" {
|
2020-11-16 18:21:50 +00:00
|
|
|
assert.Len(t, filter.DeniedExtensions, 2)
|
|
|
|
assert.Len(t, filter.AllowedExtensions, 3)
|
2020-11-15 21:04:48 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(".jpg", filter.AllowedExtensions))
|
|
|
|
assert.True(t, utils.IsStringInSlice(".png", filter.AllowedExtensions))
|
|
|
|
assert.True(t, utils.IsStringInSlice(".ico", filter.AllowedExtensions))
|
2020-11-16 18:21:50 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(".webp", filter.DeniedExtensions))
|
|
|
|
assert.True(t, utils.IsStringInSlice(".tiff", filter.DeniedExtensions))
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 18:21:50 +00:00
|
|
|
assert.Len(t, newUser.Filters.FilePatterns, 3)
|
2020-11-15 21:04:48 +00:00
|
|
|
for _, filter := range newUser.Filters.FilePatterns {
|
|
|
|
if filter.Path == "/dir1" {
|
2020-11-16 18:21:50 +00:00
|
|
|
assert.Len(t, filter.DeniedPatterns, 1)
|
|
|
|
assert.Len(t, filter.AllowedPatterns, 1)
|
|
|
|
assert.True(t, utils.IsStringInSlice("*.png", filter.AllowedPatterns))
|
2020-11-15 21:04:48 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice("*.zip", filter.DeniedPatterns))
|
|
|
|
}
|
|
|
|
if filter.Path == "/dir2" {
|
2020-11-16 18:21:50 +00:00
|
|
|
assert.Len(t, filter.DeniedPatterns, 1)
|
|
|
|
assert.Len(t, filter.AllowedPatterns, 2)
|
2020-11-15 21:04:48 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice("*.jpg", filter.AllowedPatterns))
|
|
|
|
assert.True(t, utils.IsStringInSlice("*.png", filter.AllowedPatterns))
|
2020-11-16 18:21:50 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice("*.mkv", filter.DeniedPatterns))
|
|
|
|
}
|
|
|
|
if filter.Path == "/dir3" {
|
|
|
|
assert.Len(t, filter.DeniedPatterns, 1)
|
|
|
|
assert.Len(t, filter.AllowedPatterns, 0)
|
|
|
|
assert.True(t, utils.IsStringInSlice("*.rar", filter.DeniedPatterns))
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(newUser.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2020-06-07 21:30:18 +00:00
|
|
|
url, err := url.Parse(folderPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("folder_path", mappedDir)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, url.String(), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebUserUpdateMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
user.MaxSessions = 1
|
|
|
|
user.QuotaFiles = 2
|
|
|
|
user.QuotaSize = 3
|
|
|
|
user.GID = 1000
|
2020-11-25 21:26:34 +00:00
|
|
|
user.AdditionalInfo = "new additional info"
|
2019-10-07 16:19:01 +00:00
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", user.Username)
|
|
|
|
form.Set("home_dir", user.HomeDir)
|
|
|
|
form.Set("uid", "0")
|
|
|
|
form.Set("gid", strconv.FormatInt(int64(user.GID), 10))
|
|
|
|
form.Set("max_sessions", strconv.FormatInt(int64(user.MaxSessions), 10))
|
|
|
|
form.Set("quota_size", strconv.FormatInt(user.QuotaSize, 10))
|
|
|
|
form.Set("quota_files", strconv.FormatInt(int64(user.QuotaFiles), 10))
|
|
|
|
form.Set("upload_bandwidth", "0")
|
|
|
|
form.Set("download_bandwidth", "0")
|
|
|
|
form.Set("permissions", "*")
|
2020-02-23 10:30:26 +00:00
|
|
|
form.Set("sub_dirs_permissions", "/otherdir :: list ,upload ")
|
2019-11-13 10:36:21 +00:00
|
|
|
form.Set("status", strconv.Itoa(user.Status))
|
|
|
|
form.Set("expiration_date", "2020-01-01 00:00:00")
|
2019-12-30 17:37:50 +00:00
|
|
|
form.Set("allowed_ip", " 192.168.1.3/32, 192.168.2.0/24 ")
|
|
|
|
form.Set("denied_ip", " 10.0.0.2/32 ")
|
2020-03-01 21:10:29 +00:00
|
|
|
form.Set("denied_extensions", "/dir1::.zip")
|
2020-02-19 21:39:30 +00:00
|
|
|
form.Set("ssh_login_methods", dataprovider.SSHLoginMethodKeyboardInteractive)
|
2020-08-17 10:49:20 +00:00
|
|
|
form.Set("denied_protocols", common.ProtocolFTP)
|
2020-08-16 18:17:02 +00:00
|
|
|
form.Set("max_upload_file_size", "100")
|
2020-09-01 14:10:26 +00:00
|
|
|
form.Set("disconnect", "1")
|
2020-11-25 21:26:34 +00:00
|
|
|
form.Set("additional_info", user.AdditionalInfo)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
var users []dataprovider.User
|
2020-05-06 17:36:34 +00:00
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
|
|
|
assert.NoError(t, err)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, 1, len(users))
|
2019-10-07 16:19:01 +00:00
|
|
|
updateUser := users[0]
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, user.HomeDir, updateUser.HomeDir)
|
|
|
|
assert.Equal(t, user.MaxSessions, updateUser.MaxSessions)
|
|
|
|
assert.Equal(t, user.QuotaFiles, updateUser.QuotaFiles)
|
|
|
|
assert.Equal(t, user.QuotaSize, updateUser.QuotaSize)
|
|
|
|
assert.Equal(t, user.UID, updateUser.UID)
|
|
|
|
assert.Equal(t, user.GID, updateUser.GID)
|
2020-11-25 21:26:34 +00:00
|
|
|
assert.Equal(t, user.AdditionalInfo, updateUser.AdditionalInfo)
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.Equal(t, int64(100), updateUser.Filters.MaxUploadFileSize)
|
2020-05-03 13:24:26 +00:00
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
if val, ok := updateUser.Permissions["/otherdir"]; ok {
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(dataprovider.PermListItems, val))
|
|
|
|
assert.True(t, utils.IsStringInSlice(dataprovider.PermUpload, val))
|
2019-12-30 17:37:50 +00:00
|
|
|
} else {
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Fail(t, "user permissions must contains /otherdir", "actual: %v", updateUser.Permissions)
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice("192.168.1.3/32", updateUser.Filters.AllowedIP))
|
|
|
|
assert.True(t, utils.IsStringInSlice("10.0.0.2/32", updateUser.Filters.DeniedIP))
|
|
|
|
assert.True(t, utils.IsStringInSlice(dataprovider.SSHLoginMethodKeyboardInteractive, updateUser.Filters.DeniedLoginMethods))
|
2020-08-17 10:49:20 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(common.ProtocolFTP, updateUser.Filters.DeniedProtocols))
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.True(t, utils.IsStringInSlice(".zip", updateUser.Filters.FileExtensions[0].DeniedExtensions))
|
2020-05-06 17:36:34 +00:00
|
|
|
req, err = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebUserS3Mock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-10-05 18:58:41 +00:00
|
|
|
user.FsConfig.Provider = dataprovider.S3FilesystemProvider
|
2020-01-19 06:41:05 +00:00
|
|
|
user.FsConfig.S3Config.Bucket = "test"
|
|
|
|
user.FsConfig.S3Config.Region = "eu-west-1"
|
|
|
|
user.FsConfig.S3Config.AccessKey = "access-key"
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.S3Config.AccessSecret = kms.NewPlainSecret("access-secret")
|
2020-01-19 06:41:05 +00:00
|
|
|
user.FsConfig.S3Config.Endpoint = "http://127.0.0.1:9000/path?a=b"
|
|
|
|
user.FsConfig.S3Config.StorageClass = "Standard"
|
2020-01-19 22:23:09 +00:00
|
|
|
user.FsConfig.S3Config.KeyPrefix = "somedir/subdir/"
|
2020-03-13 16:28:55 +00:00
|
|
|
user.FsConfig.S3Config.UploadPartSize = 5
|
2020-03-13 18:13:58 +00:00
|
|
|
user.FsConfig.S3Config.UploadConcurrency = 4
|
2020-01-19 06:41:05 +00:00
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", user.Username)
|
|
|
|
form.Set("home_dir", user.HomeDir)
|
|
|
|
form.Set("uid", "0")
|
|
|
|
form.Set("gid", strconv.FormatInt(int64(user.GID), 10))
|
|
|
|
form.Set("max_sessions", strconv.FormatInt(int64(user.MaxSessions), 10))
|
|
|
|
form.Set("quota_size", strconv.FormatInt(user.QuotaSize, 10))
|
|
|
|
form.Set("quota_files", strconv.FormatInt(int64(user.QuotaFiles), 10))
|
|
|
|
form.Set("upload_bandwidth", "0")
|
|
|
|
form.Set("download_bandwidth", "0")
|
|
|
|
form.Set("permissions", "*")
|
|
|
|
form.Set("sub_dirs_permissions", "")
|
|
|
|
form.Set("status", strconv.Itoa(user.Status))
|
|
|
|
form.Set("expiration_date", "2020-01-01 00:00:00")
|
|
|
|
form.Set("allowed_ip", "")
|
|
|
|
form.Set("denied_ip", "")
|
|
|
|
form.Set("fs_provider", "1")
|
|
|
|
form.Set("s3_bucket", user.FsConfig.S3Config.Bucket)
|
|
|
|
form.Set("s3_region", user.FsConfig.S3Config.Region)
|
|
|
|
form.Set("s3_access_key", user.FsConfig.S3Config.AccessKey)
|
2020-11-30 20:46:34 +00:00
|
|
|
form.Set("s3_access_secret", user.FsConfig.S3Config.AccessSecret.GetPayload())
|
2020-01-19 06:41:05 +00:00
|
|
|
form.Set("s3_storage_class", user.FsConfig.S3Config.StorageClass)
|
|
|
|
form.Set("s3_endpoint", user.FsConfig.S3Config.Endpoint)
|
2020-01-19 22:23:09 +00:00
|
|
|
form.Set("s3_key_prefix", user.FsConfig.S3Config.KeyPrefix)
|
2020-03-01 21:10:29 +00:00
|
|
|
form.Set("allowed_extensions", "/dir1::.jpg,.png")
|
|
|
|
form.Set("denied_extensions", "/dir2::.zip")
|
2020-08-16 18:17:02 +00:00
|
|
|
form.Set("max_upload_file_size", "0")
|
2020-03-13 16:28:55 +00:00
|
|
|
// test invalid s3_upload_part_size
|
|
|
|
form.Set("s3_upload_part_size", "a")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2020-01-19 06:41:05 +00:00
|
|
|
rr = executeRequest(req)
|
2020-03-13 16:28:55 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2020-03-13 18:13:58 +00:00
|
|
|
// test invalid s3_concurrency
|
2020-03-13 16:28:55 +00:00
|
|
|
form.Set("s3_upload_part_size", strconv.FormatInt(user.FsConfig.S3Config.UploadPartSize, 10))
|
2020-03-13 18:13:58 +00:00
|
|
|
form.Set("s3_upload_concurrency", "a")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
// now add the user
|
|
|
|
form.Set("s3_upload_concurrency", strconv.Itoa(user.FsConfig.S3Config.UploadConcurrency))
|
2020-03-13 16:28:55 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2020-01-19 06:41:05 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
var users []dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
2020-01-19 06:41:05 +00:00
|
|
|
updateUser := users[0]
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, int64(1577836800000), updateUser.ExpirationDate)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.S3Config.Bucket, user.FsConfig.S3Config.Bucket)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.S3Config.Region, user.FsConfig.S3Config.Region)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.S3Config.AccessKey, user.FsConfig.S3Config.AccessKey)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.S3Config.StorageClass, user.FsConfig.S3Config.StorageClass)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.S3Config.Endpoint, user.FsConfig.S3Config.Endpoint)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.S3Config.KeyPrefix, user.FsConfig.S3Config.KeyPrefix)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.S3Config.UploadPartSize, user.FsConfig.S3Config.UploadPartSize)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.S3Config.UploadConcurrency, user.FsConfig.S3Config.UploadConcurrency)
|
|
|
|
assert.Equal(t, 2, len(updateUser.Filters.FileExtensions))
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, updateUser.FsConfig.S3Config.AccessSecret.GetStatus())
|
|
|
|
assert.NotEmpty(t, updateUser.FsConfig.S3Config.AccessSecret.GetPayload())
|
|
|
|
assert.Empty(t, updateUser.FsConfig.S3Config.AccessSecret.GetKey())
|
|
|
|
assert.Empty(t, updateUser.FsConfig.S3Config.AccessSecret.GetAdditionalData())
|
2020-11-22 20:53:04 +00:00
|
|
|
// now check that a redacted password is not saved
|
|
|
|
form.Set("s3_access_secret", "[**redacted**] ")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
users = nil
|
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
|
|
|
lastUpdatedUser := users[0]
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, lastUpdatedUser.FsConfig.S3Config.AccessSecret.GetStatus())
|
|
|
|
assert.Equal(t, updateUser.FsConfig.S3Config.AccessSecret.GetPayload(), lastUpdatedUser.FsConfig.S3Config.AccessSecret.GetPayload())
|
|
|
|
assert.Empty(t, lastUpdatedUser.FsConfig.S3Config.AccessSecret.GetKey())
|
|
|
|
assert.Empty(t, lastUpdatedUser.FsConfig.S3Config.AccessSecret.GetAdditionalData())
|
2020-11-22 20:53:04 +00:00
|
|
|
// now clear credentials
|
|
|
|
form.Set("s3_access_key", "")
|
|
|
|
form.Set("s3_access_secret", "")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
users = nil
|
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
|
|
|
assert.True(t, users[0].FsConfig.S3Config.AccessSecret.IsEmpty())
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
func TestWebUserGCSMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
2020-05-03 13:24:26 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2020-05-03 13:24:26 +00:00
|
|
|
err = render.DecodeJSON(rr.Body, &user)
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
credentialsFilePath := filepath.Join(os.TempDir(), "gcs.json")
|
|
|
|
err = createTestFile(credentialsFilePath, 0)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-10-05 18:58:41 +00:00
|
|
|
user.FsConfig.Provider = dataprovider.GCSFilesystemProvider
|
2020-01-31 18:04:00 +00:00
|
|
|
user.FsConfig.GCSConfig.Bucket = "test"
|
|
|
|
user.FsConfig.GCSConfig.KeyPrefix = "somedir/subdir/"
|
|
|
|
user.FsConfig.GCSConfig.StorageClass = "standard"
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", user.Username)
|
|
|
|
form.Set("home_dir", user.HomeDir)
|
|
|
|
form.Set("uid", "0")
|
|
|
|
form.Set("gid", strconv.FormatInt(int64(user.GID), 10))
|
|
|
|
form.Set("max_sessions", strconv.FormatInt(int64(user.MaxSessions), 10))
|
|
|
|
form.Set("quota_size", strconv.FormatInt(user.QuotaSize, 10))
|
|
|
|
form.Set("quota_files", strconv.FormatInt(int64(user.QuotaFiles), 10))
|
|
|
|
form.Set("upload_bandwidth", "0")
|
|
|
|
form.Set("download_bandwidth", "0")
|
|
|
|
form.Set("permissions", "*")
|
|
|
|
form.Set("sub_dirs_permissions", "")
|
|
|
|
form.Set("status", strconv.Itoa(user.Status))
|
|
|
|
form.Set("expiration_date", "2020-01-01 00:00:00")
|
|
|
|
form.Set("allowed_ip", "")
|
|
|
|
form.Set("denied_ip", "")
|
|
|
|
form.Set("fs_provider", "2")
|
|
|
|
form.Set("gcs_bucket", user.FsConfig.GCSConfig.Bucket)
|
|
|
|
form.Set("gcs_storage_class", user.FsConfig.GCSConfig.StorageClass)
|
|
|
|
form.Set("gcs_key_prefix", user.FsConfig.GCSConfig.KeyPrefix)
|
2020-03-01 21:10:29 +00:00
|
|
|
form.Set("allowed_extensions", "/dir1::.jpg,.png")
|
2020-08-16 18:17:02 +00:00
|
|
|
form.Set("max_upload_file_size", "0")
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "gcs_credential_file", credentialsFilePath)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = createTestFile(credentialsFilePath, 4096)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "gcs_credential_file", credentialsFilePath)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
var users []dataprovider.User
|
2020-05-06 17:36:34 +00:00
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
|
|
|
assert.NoError(t, err)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, 1, len(users))
|
2020-01-31 18:04:00 +00:00
|
|
|
updateUser := users[0]
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, int64(1577836800000), updateUser.ExpirationDate)
|
|
|
|
assert.Equal(t, user.FsConfig.Provider, updateUser.FsConfig.Provider)
|
|
|
|
assert.Equal(t, user.FsConfig.GCSConfig.Bucket, updateUser.FsConfig.GCSConfig.Bucket)
|
|
|
|
assert.Equal(t, user.FsConfig.GCSConfig.StorageClass, updateUser.FsConfig.GCSConfig.StorageClass)
|
|
|
|
assert.Equal(t, user.FsConfig.GCSConfig.KeyPrefix, updateUser.FsConfig.GCSConfig.KeyPrefix)
|
|
|
|
assert.Equal(t, "/dir1", updateUser.Filters.FileExtensions[0].Path)
|
2020-02-19 08:41:15 +00:00
|
|
|
form.Set("gcs_auto_credentials", "on")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
2020-02-19 08:41:15 +00:00
|
|
|
updateUser = users[0]
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, 1, updateUser.FsConfig.GCSConfig.AutomaticCredentials)
|
2020-01-31 18:04:00 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.Remove(credentialsFilePath)
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
func TestWebUserAzureBlobMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.FsConfig.Provider = dataprovider.AzureBlobFilesystemProvider
|
|
|
|
user.FsConfig.AzBlobConfig.Container = "container"
|
|
|
|
user.FsConfig.AzBlobConfig.AccountName = "aname"
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.AzBlobConfig.AccountKey = kms.NewPlainSecret("access-skey")
|
2020-10-25 07:18:48 +00:00
|
|
|
user.FsConfig.AzBlobConfig.Endpoint = "http://127.0.0.1:9000/path?b=c"
|
|
|
|
user.FsConfig.AzBlobConfig.KeyPrefix = "somedir/subdir/"
|
|
|
|
user.FsConfig.AzBlobConfig.UploadPartSize = 5
|
|
|
|
user.FsConfig.AzBlobConfig.UploadConcurrency = 4
|
|
|
|
user.FsConfig.AzBlobConfig.UseEmulator = true
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", user.Username)
|
|
|
|
form.Set("home_dir", user.HomeDir)
|
|
|
|
form.Set("uid", "0")
|
|
|
|
form.Set("gid", strconv.FormatInt(int64(user.GID), 10))
|
|
|
|
form.Set("max_sessions", strconv.FormatInt(int64(user.MaxSessions), 10))
|
|
|
|
form.Set("quota_size", strconv.FormatInt(user.QuotaSize, 10))
|
|
|
|
form.Set("quota_files", strconv.FormatInt(int64(user.QuotaFiles), 10))
|
|
|
|
form.Set("upload_bandwidth", "0")
|
|
|
|
form.Set("download_bandwidth", "0")
|
|
|
|
form.Set("permissions", "*")
|
|
|
|
form.Set("sub_dirs_permissions", "")
|
|
|
|
form.Set("status", strconv.Itoa(user.Status))
|
|
|
|
form.Set("expiration_date", "2020-01-01 00:00:00")
|
|
|
|
form.Set("allowed_ip", "")
|
|
|
|
form.Set("denied_ip", "")
|
|
|
|
form.Set("fs_provider", "3")
|
|
|
|
form.Set("az_container", user.FsConfig.AzBlobConfig.Container)
|
|
|
|
form.Set("az_account_name", user.FsConfig.AzBlobConfig.AccountName)
|
2020-11-30 20:46:34 +00:00
|
|
|
form.Set("az_account_key", user.FsConfig.AzBlobConfig.AccountKey.GetPayload())
|
2020-10-25 07:18:48 +00:00
|
|
|
form.Set("az_sas_url", user.FsConfig.AzBlobConfig.SASURL)
|
|
|
|
form.Set("az_endpoint", user.FsConfig.AzBlobConfig.Endpoint)
|
|
|
|
form.Set("az_key_prefix", user.FsConfig.AzBlobConfig.KeyPrefix)
|
|
|
|
form.Set("az_use_emulator", "checked")
|
|
|
|
form.Set("allowed_extensions", "/dir1::.jpg,.png")
|
|
|
|
form.Set("denied_extensions", "/dir2::.zip")
|
|
|
|
form.Set("max_upload_file_size", "0")
|
|
|
|
// test invalid az_upload_part_size
|
|
|
|
form.Set("az_upload_part_size", "a")
|
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
// test invalid az_upload_concurrency
|
|
|
|
form.Set("az_upload_part_size", strconv.FormatInt(user.FsConfig.AzBlobConfig.UploadPartSize, 10))
|
|
|
|
form.Set("az_upload_concurrency", "a")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
// now add the user
|
|
|
|
form.Set("az_upload_concurrency", strconv.Itoa(user.FsConfig.AzBlobConfig.UploadConcurrency))
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
var users []dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
|
|
|
updateUser := users[0]
|
|
|
|
assert.Equal(t, int64(1577836800000), updateUser.ExpirationDate)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.AzBlobConfig.Container, user.FsConfig.AzBlobConfig.Container)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.AzBlobConfig.AccountName, user.FsConfig.AzBlobConfig.AccountName)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.AzBlobConfig.Endpoint, user.FsConfig.AzBlobConfig.Endpoint)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.AzBlobConfig.SASURL, user.FsConfig.AzBlobConfig.SASURL)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.AzBlobConfig.KeyPrefix, user.FsConfig.AzBlobConfig.KeyPrefix)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.AzBlobConfig.UploadPartSize, user.FsConfig.AzBlobConfig.UploadPartSize)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.AzBlobConfig.UploadConcurrency, user.FsConfig.AzBlobConfig.UploadConcurrency)
|
|
|
|
assert.Equal(t, 2, len(updateUser.Filters.FileExtensions))
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, updateUser.FsConfig.AzBlobConfig.AccountKey.GetStatus())
|
|
|
|
assert.NotEmpty(t, updateUser.FsConfig.AzBlobConfig.AccountKey.GetPayload())
|
|
|
|
assert.Empty(t, updateUser.FsConfig.AzBlobConfig.AccountKey.GetKey())
|
|
|
|
assert.Empty(t, updateUser.FsConfig.AzBlobConfig.AccountKey.GetAdditionalData())
|
2020-11-22 20:53:04 +00:00
|
|
|
// now check that a redacted password is not saved
|
|
|
|
form.Set("az_account_key", "[**redacted**] ")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
users = nil
|
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
|
|
|
lastUpdatedUser := users[0]
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, lastUpdatedUser.FsConfig.AzBlobConfig.AccountKey.GetStatus())
|
|
|
|
assert.Equal(t, updateUser.FsConfig.AzBlobConfig.AccountKey.GetPayload(), lastUpdatedUser.FsConfig.AzBlobConfig.AccountKey.GetPayload())
|
|
|
|
assert.Empty(t, lastUpdatedUser.FsConfig.AzBlobConfig.AccountKey.GetKey())
|
|
|
|
assert.Empty(t, lastUpdatedUser.FsConfig.AzBlobConfig.AccountKey.GetAdditionalData())
|
2020-10-25 07:18:48 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2020-12-05 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebUserCryptMock(t *testing.T) {
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err := render.DecodeJSON(rr.Body, &user)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.FsConfig.Provider = dataprovider.CryptedFilesystemProvider
|
|
|
|
user.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret("crypted passphrase")
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", user.Username)
|
|
|
|
form.Set("home_dir", user.HomeDir)
|
|
|
|
form.Set("uid", "0")
|
|
|
|
form.Set("gid", strconv.FormatInt(int64(user.GID), 10))
|
|
|
|
form.Set("max_sessions", strconv.FormatInt(int64(user.MaxSessions), 10))
|
|
|
|
form.Set("quota_size", strconv.FormatInt(user.QuotaSize, 10))
|
|
|
|
form.Set("quota_files", strconv.FormatInt(int64(user.QuotaFiles), 10))
|
|
|
|
form.Set("upload_bandwidth", "0")
|
|
|
|
form.Set("download_bandwidth", "0")
|
|
|
|
form.Set("permissions", "*")
|
|
|
|
form.Set("sub_dirs_permissions", "")
|
|
|
|
form.Set("status", strconv.Itoa(user.Status))
|
|
|
|
form.Set("expiration_date", "2020-01-01 00:00:00")
|
|
|
|
form.Set("allowed_ip", "")
|
|
|
|
form.Set("denied_ip", "")
|
|
|
|
form.Set("fs_provider", "4")
|
|
|
|
form.Set("crypt_passphrase", "")
|
|
|
|
form.Set("allowed_extensions", "/dir1::.jpg,.png")
|
|
|
|
form.Set("denied_extensions", "/dir2::.zip")
|
|
|
|
form.Set("max_upload_file_size", "0")
|
|
|
|
// passphrase cannot be empty
|
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
form.Set("crypt_passphrase", user.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
var users []dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
|
|
|
updateUser := users[0]
|
|
|
|
assert.Equal(t, int64(1577836800000), updateUser.ExpirationDate)
|
|
|
|
assert.Equal(t, 2, len(updateUser.Filters.FileExtensions))
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, updateUser.FsConfig.CryptConfig.Passphrase.GetStatus())
|
|
|
|
assert.NotEmpty(t, updateUser.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
assert.Empty(t, updateUser.FsConfig.CryptConfig.Passphrase.GetKey())
|
|
|
|
assert.Empty(t, updateUser.FsConfig.CryptConfig.Passphrase.GetAdditionalData())
|
|
|
|
// now check that a redacted password is not saved
|
|
|
|
form.Set("crypt_passphrase", "[**redacted**] ")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/"+strconv.FormatInt(user.ID, 10), &b)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASC&username="+user.Username, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
users = nil
|
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
|
|
|
lastUpdatedUser := users[0]
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, lastUpdatedUser.FsConfig.CryptConfig.Passphrase.GetStatus())
|
|
|
|
assert.Equal(t, updateUser.FsConfig.CryptConfig.Passphrase.GetPayload(), lastUpdatedUser.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
assert.Empty(t, lastUpdatedUser.FsConfig.CryptConfig.Passphrase.GetKey())
|
|
|
|
assert.Empty(t, lastUpdatedUser.FsConfig.CryptConfig.Passphrase.GetAdditionalData())
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+strconv.FormatInt(user.ID, 10), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestAddWebFoldersMock(t *testing.T) {
|
2020-06-07 22:15:14 +00:00
|
|
|
mappedPath := filepath.Clean(os.TempDir())
|
2020-06-07 21:30:18 +00:00
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("mapped_path", mappedPath)
|
|
|
|
req, err := http.NewRequest(http.MethodPost, webFolderPath, strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr.Code)
|
|
|
|
// adding the same folder will fail since the path must be unique
|
|
|
|
req, err = http.NewRequest(http.MethodPost, webFolderPath, strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
// invalid form
|
|
|
|
req, err = http.NewRequest(http.MethodPost, webFolderPath, strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.Header.Set("Content-Type", "text/plain; boundary=")
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
// now render the add folder page
|
|
|
|
req, err = http.NewRequest(http.MethodGet, webFolderPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
var folders []vfs.BaseVirtualFolder
|
|
|
|
url, err := url.Parse(folderPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("folder_path", mappedPath)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, url.String(), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
err = render.DecodeJSON(rr.Body, &folders)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, folders, 1) {
|
|
|
|
folder := folders[0]
|
|
|
|
assert.Equal(t, mappedPath, folder.MappedPath)
|
|
|
|
}
|
|
|
|
// cleanup
|
|
|
|
url, err = url.Parse(folderPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
q = url.Query()
|
|
|
|
q.Add("folder_path", mappedPath)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, url.String(), nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebFoldersMock(t *testing.T) {
|
|
|
|
mappedPath1 := filepath.Join(os.TempDir(), "vfolder1")
|
|
|
|
mappedPath2 := filepath.Join(os.TempDir(), "vfolder2")
|
|
|
|
folders := []vfs.BaseVirtualFolder{
|
|
|
|
{
|
|
|
|
MappedPath: mappedPath1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MappedPath: mappedPath2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, folder := range folders {
|
|
|
|
folderAsJSON, err := json.Marshal(folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, bytes.NewBuffer(folderAsJSON))
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, webFoldersPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, err = http.NewRequest(http.MethodGet, webFoldersPath+"?qlimit=a", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
req, err = http.NewRequest(http.MethodGet, webFoldersPath+"?qlimit=1", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
for _, folder := range folders {
|
|
|
|
url, err := url.Parse(folderPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
q := url.Query()
|
|
|
|
q.Add("folder_path", folder.MappedPath)
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
req, _ := http.NewRequest(http.MethodDelete, url.String(), nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func TestProviderClosedMock(t *testing.T) {
|
2020-07-08 17:59:31 +00:00
|
|
|
dataprovider.Close()
|
2020-06-07 21:30:18 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, webFoldersPath, nil)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr.Code)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUsersPath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr.Code)
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath+"/0", nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr.Code)
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", "test")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/0", strings.NewReader(form.Encode()))
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr.Code)
|
2020-05-06 17:36:34 +00:00
|
|
|
err := config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
2020-01-31 18:04:00 +00:00
|
|
|
providerConf.CredentialsPath = credentialsPath
|
2020-05-06 17:36:34 +00:00
|
|
|
err = os.RemoveAll(credentialsPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWebConnectionsMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, webConnectionsPath, nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStaticFilesMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/static/favicon.ico", nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr.Code)
|
|
|
|
}
|
|
|
|
|
2019-07-26 13:08:08 +00:00
|
|
|
func waitTCPListening(address string) {
|
|
|
|
for {
|
|
|
|
conn, err := net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
2019-07-31 12:11:44 +00:00
|
|
|
logger.WarnToConsole("tcp server %v not listening: %v\n", address, err)
|
2019-07-26 13:08:08 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2019-07-31 12:11:44 +00:00
|
|
|
logger.InfoToConsole("tcp server %v now listening\n", address)
|
2020-07-24 21:39:38 +00:00
|
|
|
conn.Close()
|
2019-07-26 13:08:08 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTestUser() dataprovider.User {
|
2019-12-25 17:20:19 +00:00
|
|
|
user := dataprovider.User{
|
|
|
|
Username: defaultUsername,
|
|
|
|
Password: defaultPassword,
|
|
|
|
HomeDir: filepath.Join(homeBasePath, defaultUsername),
|
|
|
|
Status: 1,
|
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = defaultPerms
|
|
|
|
return user
|
2019-07-26 13:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getUserAsJSON(t *testing.T, user dataprovider.User) []byte {
|
|
|
|
json, err := json.Marshal(user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 13:08:08 +00:00
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
return rr
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkResponseCode(t *testing.T, expected, actual int) {
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, expected, actual)
|
2019-07-26 13:08:08 +00:00
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
|
|
|
|
func createTestFile(path string, size int64) error {
|
|
|
|
baseDir := filepath.Dir(path)
|
|
|
|
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
|
2020-06-08 17:40:17 +00:00
|
|
|
err = os.MkdirAll(baseDir, os.ModePerm)
|
2020-05-06 17:36:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
content := make([]byte, size)
|
2020-01-31 18:04:00 +00:00
|
|
|
if size > 0 {
|
|
|
|
_, err := rand.Read(content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
return ioutil.WriteFile(path, content, os.ModePerm)
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
|
|
|
|
func getMultipartFormData(values url.Values, fileFieldName, filePath string) (bytes.Buffer, string, error) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
w := multipart.NewWriter(&b)
|
|
|
|
for k, v := range values {
|
|
|
|
for _, s := range v {
|
|
|
|
if err := w.WriteField(k, s); err != nil {
|
|
|
|
return b, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(fileFieldName) > 0 && len(filePath) > 0 {
|
|
|
|
fw, err := w.CreateFormFile(fileFieldName, filepath.Base(filePath))
|
|
|
|
if err != nil {
|
|
|
|
return b, "", err
|
|
|
|
}
|
|
|
|
f, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return b, "", err
|
|
|
|
}
|
2020-05-03 20:46:39 +00:00
|
|
|
defer f.Close()
|
2020-01-31 18:04:00 +00:00
|
|
|
if _, err = io.Copy(fw, f); err != nil {
|
|
|
|
return b, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := w.Close()
|
|
|
|
return b, w.FormDataContentType(), err
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
|
|
|
|
func BenchmarkSecretDecryption(b *testing.B) {
|
|
|
|
s := kms.NewPlainSecret("test data")
|
|
|
|
s.SetAdditionalData("username")
|
|
|
|
err := s.Encrypt()
|
|
|
|
require.NoError(b, err)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
err = s.Clone().Decrypt()
|
|
|
|
require.NoError(b, err)
|
|
|
|
}
|
|
|
|
}
|