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"
|
2021-02-02 08:14:10 +00:00
|
|
|
"errors"
|
2019-07-26 09:34:44 +00:00
|
|
|
"fmt"
|
2020-01-31 18:04:00 +00:00
|
|
|
"io"
|
|
|
|
"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"
|
2021-01-17 21:29:08 +00:00
|
|
|
"path"
|
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"
|
2021-02-03 07:55:28 +00:00
|
|
|
"golang.org/x/net/html"
|
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"
|
2021-01-17 21:29:08 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpclient"
|
2019-10-07 16:19:01 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpd"
|
2021-01-17 21:29:08 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpdtest"
|
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"
|
2021-01-17 21:29:08 +00:00
|
|
|
defaultTokenAuthUser = "admin"
|
|
|
|
defaultTokenAuthPass = "password"
|
|
|
|
altAdminUsername = "newTestAdmin"
|
|
|
|
altAdminPassword = "password1"
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfFormToken = "_form_token"
|
2021-01-17 21:29:08 +00:00
|
|
|
userPath = "/api/v2/users"
|
|
|
|
adminPath = "/api/v2/admins"
|
|
|
|
adminPwdPath = "/api/v2/changepwd/admin"
|
|
|
|
folderPath = "/api/v2/folders"
|
|
|
|
activeConnectionsPath = "/api/v2/connections"
|
|
|
|
serverStatusPath = "/api/v2/status"
|
|
|
|
quotaScanPath = "/api/v2/quota-scans"
|
|
|
|
quotaScanVFolderPath = "/api/v2/folder-quota-scans"
|
|
|
|
updateUsedQuotaPath = "/api/v2/quota-update"
|
|
|
|
updateFolderUsedQuotaPath = "/api/v2/folder-quota-update"
|
|
|
|
defenderUnban = "/api/v2/defender/unban"
|
|
|
|
versionPath = "/api/v2/version"
|
2021-01-26 21:35:36 +00:00
|
|
|
logoutPath = "/api/v2/logout"
|
2021-01-17 21:29:08 +00:00
|
|
|
healthzPath = "/healthz"
|
2020-06-20 10:38:04 +00:00
|
|
|
webBasePath = "/web"
|
2021-01-17 21:29:08 +00:00
|
|
|
webLoginPath = "/web/login"
|
|
|
|
webLogoutPath = "/web/logout"
|
2020-06-20 10:38:04 +00:00
|
|
|
webUsersPath = "/web/users"
|
|
|
|
webUserPath = "/web/user"
|
|
|
|
webFoldersPath = "/web/folders"
|
|
|
|
webFolderPath = "/web/folder"
|
|
|
|
webConnectionsPath = "/web/connections"
|
2020-12-08 10:18:34 +00:00
|
|
|
webStatusPath = "/web/status"
|
2021-01-17 21:29:08 +00:00
|
|
|
webAdminsPath = "/web/admins"
|
|
|
|
webAdminPath = "/web/admin"
|
2021-01-22 18:42:18 +00:00
|
|
|
webMaintenancePath = "/web/maintenance"
|
|
|
|
webRestorePath = "/web/restore"
|
2021-01-17 21:29:08 +00:00
|
|
|
webChangeAdminPwdPath = "/web/changepwd/admin"
|
2021-01-25 20:31:33 +00:00
|
|
|
webTemplateUser = "/web/template/user"
|
2021-02-04 18:09:43 +00:00
|
|
|
webTemplateFolder = "/web/template/folder"
|
2021-01-17 21:29:08 +00:00
|
|
|
httpBaseURL = "http://127.0.0.1:8081"
|
2020-06-20 10:38:04 +00:00
|
|
|
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-----`
|
2020-12-12 09:31:09 +00:00
|
|
|
sftpPrivateKey = `-----BEGIN OPENSSH PRIVATE KEY-----
|
|
|
|
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
|
|
|
QyNTUxOQAAACB+RB4yNTZz9mHOkawwUibNdemijVV3ErMeLxWUBlCN/gAAAJA7DjpfOw46
|
|
|
|
XwAAAAtzc2gtZWQyNTUxOQAAACB+RB4yNTZz9mHOkawwUibNdemijVV3ErMeLxWUBlCN/g
|
|
|
|
AAAEA0E24gi8ab/XRSvJ85TGZJMe6HVmwxSG4ExPfTMwwe2n5EHjI1NnP2Yc6RrDBSJs11
|
|
|
|
6aKNVXcSsx4vFZQGUI3+AAAACW5pY29sYUBwMQECAwQ=
|
|
|
|
-----END OPENSSH PRIVATE KEY-----`
|
|
|
|
sftpPkeyFingerprint = "SHA256:QVQ06XHZZbYZzqfrsZcf3Yozy2WTnqQPeLOkcJCdbP0"
|
2021-02-15 18:38:53 +00:00
|
|
|
redactedSecret = "[**redacted**]"
|
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
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
err = common.Initialize(config.GetCommonConfig())
|
|
|
|
if err != nil {
|
|
|
|
logger.WarnToConsole("error initializing common: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
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()
|
2021-02-13 13:41:37 +00:00
|
|
|
httpConfig.Initialize(configDir) //nolint:errcheck
|
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()
|
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
httpdConf.Bindings[0].Port = 8081
|
2021-01-17 21:29:08 +00:00
|
|
|
httpdtest.SetBaseURL(httpBaseURL)
|
2020-02-03 23:08:00 +00:00
|
|
|
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-12-18 08:47:22 +00:00
|
|
|
if err := httpdConf.Initialize(configDir); 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
|
|
|
}()
|
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
waitTCPListening(httpdConf.Bindings[0].GetAddress())
|
|
|
|
httpd.ReloadCertificateMgr() //nolint:errcheck
|
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")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.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)
|
|
|
|
}
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.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)
|
|
|
|
}
|
2021-01-19 17:59:41 +00:00
|
|
|
httpdConf.Bindings[0].Port = 8443
|
|
|
|
httpdConf.Bindings[0].EnableHTTPS = true
|
2020-02-04 22:21:33 +00:00
|
|
|
httpdConf.CertificateFile = certPath
|
|
|
|
httpdConf.CertificateKeyFile = keyPath
|
2021-01-19 17:59:41 +00:00
|
|
|
httpdConf.Bindings = append(httpdConf.Bindings, httpd.Binding{})
|
2020-02-04 22:21:33 +00:00
|
|
|
|
|
|
|
go func() {
|
2020-12-18 08:47:22 +00:00
|
|
|
if err := httpdConf.Initialize(configDir); 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
|
|
|
}
|
|
|
|
}()
|
2021-01-19 17:59:41 +00:00
|
|
|
waitTCPListening(httpdConf.Bindings[0].GetAddress())
|
2021-01-03 16:03:04 +00:00
|
|
|
httpd.ReloadCertificateMgr() //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 = backupsPath
|
2020-06-18 21:53:38 +00:00
|
|
|
httpdConf.CertificateFile = invalidFile
|
|
|
|
httpdConf.CertificateKeyFile = invalidFile
|
2020-12-18 08:47:22 +00:00
|
|
|
err = httpdConf.Initialize(configDir)
|
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-12-18 08:47:22 +00:00
|
|
|
err = httpdConf.Initialize(configDir)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-06-18 21:53:38 +00:00
|
|
|
httpdConf = config.GetHTTPDConfig()
|
|
|
|
httpdConf.BackupsPath = ".."
|
2020-12-18 08:47:22 +00:00
|
|
|
err = httpdConf.Initialize(configDir)
|
2020-06-18 21:53:38 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
httpdConf.BackupsPath = backupsPath
|
|
|
|
httpdConf.CertificateFile = invalidFile
|
|
|
|
httpdConf.CertificateKeyFile = invalidFile
|
|
|
|
httpdConf.StaticFilesPath = ""
|
|
|
|
httpdConf.TemplatesPath = ""
|
2020-12-18 08:47:22 +00:00
|
|
|
err = httpdConf.Initialize(configDir)
|
2020-06-18 21:53:38 +00:00
|
|
|
assert.Error(t, err)
|
2021-01-19 17:59:41 +00:00
|
|
|
httpdConf.CertificateFile = filepath.Join(os.TempDir(), "test.crt")
|
|
|
|
httpdConf.CertificateKeyFile = filepath.Join(os.TempDir(), "test.key")
|
|
|
|
httpdConf.CACertificates = append(httpdConf.CACertificates, invalidFile)
|
|
|
|
err = httpdConf.Initialize(configDir)
|
|
|
|
assert.Error(t, err)
|
|
|
|
httpdConf.CACertificates = nil
|
|
|
|
httpdConf.CARevocationLists = append(httpdConf.CARevocationLists, invalidFile)
|
|
|
|
err = httpdConf.Initialize(configDir)
|
|
|
|
assert.Error(t, err)
|
|
|
|
httpdConf.CARevocationLists = nil
|
|
|
|
httpdConf.Bindings[0].Port = 8081
|
|
|
|
httpdConf.Bindings[0].EnableHTTPS = true
|
|
|
|
httpdConf.Bindings[0].ClientAuthType = 1
|
|
|
|
err = httpdConf.Initialize(configDir)
|
|
|
|
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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
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"
|
2021-02-28 11:10:40 +00:00
|
|
|
user.Filters.TLSUsername = dataprovider.TLSUsernameCN
|
2020-10-22 08:42:40 +00:00
|
|
|
originalUser := user
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.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)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(defaultUsername, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicAdminHandling(t *testing.T) {
|
|
|
|
// we have one admin by default
|
|
|
|
admins, _, err := httpdtest.GetAdmins(0, 0, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(admins), 1)
|
|
|
|
admin := getTestAdmin()
|
|
|
|
// the default admin already exists
|
|
|
|
_, _, err = httpdtest.AddAdmin(admin, http.StatusInternalServerError)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
admin.Username = altAdminUsername
|
|
|
|
admin, _, err = httpdtest.AddAdmin(admin, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
admin, _, err = httpdtest.GetAdminByUsername(admin.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
admin.AdditionalInfo = "test info"
|
|
|
|
admin, _, err = httpdtest.UpdateAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test info", admin.AdditionalInfo)
|
|
|
|
|
|
|
|
admins, _, err = httpdtest.GetAdmins(1, 0, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, admins, 1)
|
|
|
|
assert.NotEqual(t, admin.Username, admins[0].Username)
|
|
|
|
|
|
|
|
admins, _, err = httpdtest.GetAdmins(1, 1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, admins, 1)
|
|
|
|
assert.Equal(t, admin.Username, admins[0].Username)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusNotFound)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
admin, _, err = httpdtest.GetAdminByUsername(admin.Username+"123", http.StatusNotFound)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
admin.Username = defaultTokenAuthUser
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangeAdminPassword(t *testing.T) {
|
|
|
|
_, err := httpdtest.ChangeAdminPassword("wrong", defaultTokenAuthPass, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.ChangeAdminPassword(defaultTokenAuthPass, defaultTokenAuthPass, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.ChangeAdminPassword(defaultTokenAuthPass, defaultTokenAuthPass+"1", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.ChangeAdminPassword(defaultTokenAuthPass+"1", defaultTokenAuthPass, http.StatusUnauthorized)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
admin, err := dataprovider.AdminExists(defaultTokenAuthUser)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
admin.Password = defaultTokenAuthPass
|
|
|
|
err = dataprovider.UpdateAdmin(&admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdminAllowList(t *testing.T) {
|
|
|
|
a := getTestAdmin()
|
|
|
|
a.Username = altAdminUsername
|
|
|
|
a.Password = altAdminPassword
|
|
|
|
|
|
|
|
admin, _, err := httpdtest.AddAdmin(a, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
token, _, err := httpdtest.GetToken(altAdminUsername, altAdminPassword)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
httpdtest.SetJWTToken(token)
|
|
|
|
_, _, err = httpdtest.GetStatus(http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
httpdtest.SetJWTToken("")
|
|
|
|
|
|
|
|
admin.Password = altAdminPassword
|
|
|
|
admin.Filters.AllowList = []string{"10.6.6.0/32"}
|
|
|
|
admin, _, err = httpdtest.UpdateAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, _, err = httpdtest.GetToken(altAdminUsername, altAdminPassword)
|
|
|
|
assert.EqualError(t, err, "wrong status code: got 401 want 200")
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, 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
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-11-13 10:36:21 +00:00
|
|
|
user.Status = 2
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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{}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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 = ""
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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 = ""
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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{}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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"}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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"}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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"}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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"}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
u.Filters.DeniedLoginMethods = dataprovider.ValidLoginMethods
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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{},
|
|
|
|
},
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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{},
|
|
|
|
},
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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"},
|
|
|
|
},
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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{},
|
|
|
|
},
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-11-15 21:04:48 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.Filters.FilePatterns = []dataprovider.PatternsFilter{
|
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
AllowedPatterns: []string{},
|
|
|
|
DeniedPatterns: []string{},
|
|
|
|
},
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-11-15 21:04:48 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.Filters.FilePatterns = []dataprovider.PatternsFilter{
|
|
|
|
{
|
|
|
|
Path: "/subdir",
|
|
|
|
AllowedPatterns: []string{"*.zip"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "/subdir",
|
|
|
|
AllowedPatterns: []string{"*.rar"},
|
|
|
|
DeniedPatterns: []string{"*.jpg"},
|
|
|
|
},
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-11-15 21:04:48 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.Filters.FilePatterns = []dataprovider.PatternsFilter{
|
|
|
|
{
|
|
|
|
Path: "/subdir",
|
|
|
|
AllowedPatterns: []string{"a\\"},
|
|
|
|
},
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-11-15 21:04:48 +00:00
|
|
|
assert.NoError(t, err)
|
2020-08-17 10:49:20 +00:00
|
|
|
u.Filters.DeniedProtocols = []string{"invalid"}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-08-17 12:08:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
u.Filters.DeniedProtocols = nil
|
|
|
|
u.Filters.TLSUsername = "not a supported attribute"
|
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
|
|
|
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 = ""
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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/"
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-13 16:28:55 +00:00
|
|
|
u.FsConfig.S3Config.KeyPrefix = ""
|
|
|
|
u.FsConfig.S3Config.UploadPartSize = 3
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-10-25 11:10:11 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-13 18:13:58 +00:00
|
|
|
u.FsConfig.S3Config.UploadPartSize = 0
|
|
|
|
u.FsConfig.S3Config.UploadConcurrency = -1
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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 = ""
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.GCSConfig.Credentials.SetStatus(kms.SecretStatusPlain)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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", "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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/"
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.FsConfig.AzBlobConfig.SASURL = ""
|
|
|
|
u.FsConfig.AzBlobConfig.AccountName = "name"
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.FsConfig.AzBlobConfig.Container = "container"
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-10-25 07:18:48 +00:00
|
|
|
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/"
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.AzBlobConfig.AccountKey.SetStatus(kms.SecretStatusPlain)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
2020-10-25 11:10:11 +00:00
|
|
|
u.FsConfig.AzBlobConfig.KeyPrefix = "amedir/subdir/"
|
|
|
|
u.FsConfig.AzBlobConfig.UploadPartSize = -1
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-10-25 11:10:11 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.FsConfig.AzBlobConfig.UploadPartSize = 101
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-10-25 11:10:11 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-05 12:48:13 +00:00
|
|
|
|
|
|
|
u = getTestUser()
|
|
|
|
u.FsConfig.Provider = dataprovider.CryptedFilesystemProvider
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.FsConfig.CryptConfig.Passphrase = kms.NewSecret(kms.SecretStatusRedacted, "akey", "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
u = getTestUser()
|
|
|
|
u.FsConfig.Provider = dataprovider.SFTPFilesystemProvider
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.FsConfig.SFTPConfig.Password = kms.NewSecret(kms.SecretStatusRedacted, "randompkey", "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.FsConfig.SFTPConfig.Password = kms.NewEmptySecret()
|
|
|
|
u.FsConfig.SFTPConfig.PrivateKey = kms.NewSecret(kms.SecretStatusRedacted, "keyforpkey", "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-12-12 09:31:09 +00:00
|
|
|
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()
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := "fname"
|
2020-02-23 10:30:26 +00:00
|
|
|
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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2021-02-01 18:04:15 +00:00
|
|
|
VirtualPath: "vdir", // invalid
|
2020-02-23 10:30:26 +00:00
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2021-02-01 18:04:15 +00:00
|
|
|
VirtualPath: "/", // invalid
|
2020-02-23 10:30:26 +00:00
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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{
|
2021-02-01 18:04:15 +00:00
|
|
|
MappedPath: filepath.Join(u.GetHomeDir(), "mapped_dir"), // invalid, inside home dir
|
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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{
|
2021-02-01 18:04:15 +00:00
|
|
|
MappedPath: u.GetHomeDir(), // invalid
|
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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{
|
2021-02-01 18:04:15 +00:00
|
|
|
MappedPath: filepath.Join(u.GetHomeDir(), ".."), // invalid, contains home dir
|
|
|
|
Name: "tmp",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName + "1",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2021-02-01 18:04:15 +00:00
|
|
|
VirtualPath: "/vdir", // invalid, already defined
|
2020-02-23 10:30:26 +00:00
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
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{
|
2021-02-01 18:04:15 +00:00
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir"), // invalid, already defined
|
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir2",
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName + "2",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
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{
|
2021-02-01 18:04:15 +00:00
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir"), // invalid, contains mapped_dir/subdir
|
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir2",
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
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{
|
2021-02-01 18:04:15 +00:00
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir", "subdir"), // invalid, contained in mapped_dir
|
|
|
|
Name: folderName + "3",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualPath: "/vdir2",
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName + "1",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName + "2",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2021-02-01 18:04:15 +00:00
|
|
|
VirtualPath: "/vdir1/../vdir1", // invalid, overlaps with /vdir1/subdir
|
2020-02-23 10:30:26 +00:00
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName + "1",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
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"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName + "2",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
2021-02-01 18:04:15 +00:00
|
|
|
VirtualPath: "/vdir1/subdir", // invalid, contained inside /vdir1
|
2020-06-07 21:30:18 +00:00
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName + "1",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1/",
|
|
|
|
QuotaSize: -1,
|
2021-02-01 18:04:15 +00:00
|
|
|
QuotaFiles: 1, // invvalid, we cannot have -1 and > 0
|
2020-06-07 21:30:18 +00:00
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName + "1",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1/",
|
|
|
|
QuotaSize: 1,
|
|
|
|
QuotaFiles: -1,
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName + "1",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1/",
|
2021-02-01 18:04:15 +00:00
|
|
|
QuotaSize: -2, // invalid
|
2020-06-07 21:30:18 +00:00
|
|
|
QuotaFiles: 0,
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir1"),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName + "1",
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1/",
|
|
|
|
QuotaSize: 0,
|
2021-02-01 18:04:15 +00:00
|
|
|
QuotaFiles: -2, // invalid
|
|
|
|
})
|
|
|
|
_, _, err = httpdtest.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_dir"),
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
})
|
|
|
|
// folder name is mandatory
|
|
|
|
_, _, err = httpdtest.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.VirtualFolders = nil
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
Name: "aa=a", // char not allowed
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped_dir"),
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1",
|
2020-02-23 10:30:26 +00:00
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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()
|
2021-02-15 18:38:53 +00:00
|
|
|
u.Password = ""
|
2019-07-31 15:06:12 +00:00
|
|
|
invalidPubKey := "invalid"
|
2019-08-07 21:41:10 +00:00
|
|
|
u.PublicKeys = []string{invalidPubKey}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.AddUser(u, http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-15 18:38:53 +00:00
|
|
|
u.PublicKeys = []string{testPubKey}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-15 18:38:53 +00:00
|
|
|
|
|
|
|
dbUser, err := dataprovider.UserExists(u.Username)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, dbUser.Password)
|
|
|
|
assert.False(t, dbUser.IsPasswordHashed())
|
|
|
|
|
|
|
|
user.PublicKeys = []string{testPubKey, invalidPubKey}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.UpdateUser(user, http.StatusBadRequest, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-15 18:38:53 +00:00
|
|
|
user.PublicKeys = []string{testPubKey, testPubKey, testPubKey}
|
|
|
|
user.Password = defaultPassword
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-15 18:38:53 +00:00
|
|
|
|
|
|
|
dbUser, err = dataprovider.UserExists(u.Username)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, dbUser.Password)
|
|
|
|
assert.True(t, dbUser.IsPasswordHashed())
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateUserEmptyPassword(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.PublicKeys = []string{testPubKey}
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// the password is not empty
|
|
|
|
dbUser, err := dataprovider.UserExists(u.Username)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, dbUser.Password)
|
|
|
|
assert.True(t, dbUser.IsPasswordHashed())
|
|
|
|
// now update the user and set an empty password
|
|
|
|
customUser := make(map[string]interface{})
|
|
|
|
customUser["password"] = ""
|
|
|
|
asJSON, err := json.Marshal(customUser)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
userNoPwd, _, err := httpdtest.UpdateUserWithJSON(user, http.StatusOK, "", asJSON)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, user, userNoPwd) // the password is hidden so the user must be equal
|
|
|
|
// check the password within the data provider
|
|
|
|
dbUser, err = dataprovider.UserExists(u.Username)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, dbUser.Password)
|
|
|
|
assert.False(t, dbUser.IsPasswordHashed())
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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
|
2021-02-28 11:10:40 +00:00
|
|
|
u.Filters.TLSUsername = dataprovider.TLSUsernameCN
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
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}
|
2021-02-28 11:10:40 +00:00
|
|
|
user.Filters.TLSUsername = dataprovider.TLSUsernameNone
|
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")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName1 := filepath.Base(mappedPath1)
|
|
|
|
folderName2 := filepath.Base(mappedPath2)
|
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{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName1,
|
2020-06-07 21:30:18 +00:00
|
|
|
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{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName2,
|
2020-06-07 21:30:18 +00:00
|
|
|
MappedPath: mappedPath2,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir12/subdir",
|
|
|
|
QuotaSize: 123,
|
|
|
|
QuotaFiles: 2,
|
2020-02-23 10:30:26 +00:00
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-09-01 14:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.UpdateUser(user, http.StatusBadRequest, "invalid")
|
2020-09-01 14:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "0")
|
2020-09-01 14:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.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{}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.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
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err := httpdtest.GetFolderByName(folderName1, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName1, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder.Users, 0)
|
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, folder.Users, 0)
|
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateQuotaUsage(u, "invalid_mode", http.StatusBadRequest)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateQuotaUsage(u, "", http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, usedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, usedQuotaSize, user.UsedQuotaSize)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateQuotaUsage(u, "add", http.StatusBadRequest)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err, "user has no quota restrictions add mode should fail")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, usedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, usedQuotaSize, user.UsedQuotaSize)
|
|
|
|
user.QuotaFiles = 100
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateQuotaUsage(u, "add", http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 2*usedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, 2*usedQuotaSize, user.UsedQuotaSize)
|
|
|
|
u.UsedQuotaFiles = -1
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateQuotaUsage(u, "", http.StatusBadRequest)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u.UsedQuotaFiles = usedQuotaFiles
|
|
|
|
u.Username = u.Username + "1"
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateQuotaUsage(u, "", http.StatusNotFound)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
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")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName1 := filepath.Base(mappedPath1)
|
|
|
|
folderName2 := filepath.Base(mappedPath2)
|
2020-06-07 21:30:18 +00:00
|
|
|
u1 := getTestUser()
|
|
|
|
u1.VirtualFolders = append(u1.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName1,
|
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,
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
user1, _, err := httpdtest.AddUser(u1, http.StatusCreated)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// virtual folder must be auto created
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err := httpdtest.GetFolderByName(folderName1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user1.Username)
|
|
|
|
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{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName1,
|
2020-06-07 21:30:18 +00:00
|
|
|
MappedPath: mappedPath1,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
QuotaSize: 0,
|
|
|
|
QuotaFiles: 0,
|
|
|
|
})
|
|
|
|
u2.VirtualFolders = append(u2.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName2,
|
2020-06-07 21:30:18 +00:00
|
|
|
MappedPath: mappedPath2,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir2",
|
|
|
|
QuotaSize: -1,
|
|
|
|
QuotaFiles: -1,
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
user2, _, err := httpdtest.AddUser(u2, http.StatusCreated)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName2, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user2.Username)
|
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName1, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder.Users, 2)
|
|
|
|
assert.Contains(t, folder.Users, user1.Username)
|
|
|
|
assert.Contains(t, folder.Users, user2.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
// now update user2 removing mappedPath1
|
|
|
|
user2.VirtualFolders = nil
|
|
|
|
user2.VirtualFolders = append(user2.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName2,
|
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,
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
user2, _, err = httpdtest.UpdateUser(user2, http.StatusOK, "")
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName2, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user2.Username)
|
|
|
|
assert.Equal(t, 0, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(0), folder.UsedQuotaSize)
|
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName1, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user1.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
// add mappedPath1 again to user2
|
|
|
|
user2.VirtualFolders = append(user2.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName1,
|
2020-06-07 21:30:18 +00:00
|
|
|
MappedPath: mappedPath1,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
})
|
2021-01-17 21:29:08 +00:00
|
|
|
user2, _, err = httpdtest.UpdateUser(user2, http.StatusOK, "")
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName2, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user2.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
// removing virtual folders should clear relations on both side
|
2021-02-01 18:04:15 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(vfs.BaseVirtualFolder{Name: folderName2}, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
user2, _, err = httpdtest.GetUserByUsername(user2.Username, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, user2.VirtualFolders, 1) {
|
|
|
|
folder := user2.VirtualFolders[0]
|
|
|
|
assert.Equal(t, mappedPath1, folder.MappedPath)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, folderName1, folder.Name)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
user1, _, err = httpdtest.GetUserByUsername(user1.Username, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, user2.VirtualFolders, 1) {
|
|
|
|
folder := user2.VirtualFolders[0]
|
|
|
|
assert.Equal(t, mappedPath1, folder.MappedPath)
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName1, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder.Users, 2)
|
2020-06-07 21:30:18 +00:00
|
|
|
// removing a user should clear virtual folder mapping
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user1, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName1, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder.Users, 1)
|
|
|
|
assert.Contains(t, folder.Users, user2.Username)
|
2020-06-07 21:30:18 +00:00
|
|
|
// removing a folder should clear mapping on the user side too
|
2021-02-01 18:04:15 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(vfs.BaseVirtualFolder{Name: folderName1}, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
user2, _, err = httpdtest.GetUserByUsername(user2.Username, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, user2.VirtualFolders, 0)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user2, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, body, err := httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-11-22 20:53:04 +00:00
|
|
|
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())
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.Error(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.S3Config.AccessSecret.SetStatus(kms.SecretStatusPlain)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, bb, err := httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err, string(bb))
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, body, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err, string(body))
|
|
|
|
assert.True(t, user.FsConfig.S3Config.AccessSecret.IsEmpty())
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
// shared credential test for add instead of update
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
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())
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, bb, err := httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err, string(bb))
|
2020-11-22 20:53:04 +00:00
|
|
|
credentialFile := filepath.Join(credentialsPath, fmt.Sprintf("%v_gcs_credentials.json", user.Username))
|
|
|
|
assert.FileExists(t, credentialFile)
|
2021-02-25 20:53:04 +00:00
|
|
|
creds, err := os.ReadFile(credentialFile)
|
2020-11-22 20:53:04 +00:00
|
|
|
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", "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.FileExists(t, credentialFile)
|
2021-02-25 20:53:04 +00:00
|
|
|
creds, err = os.ReadFile(credentialFile)
|
2020-11-22 20:53:04 +00:00
|
|
|
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())
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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", "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.Error(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.GCSConfig.Credentials.SetStatus(kms.SecretStatusPlain)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, body, err := httpdtest.AddUser(user, http.StatusCreated)
|
2020-10-22 08:42:40 +00:00
|
|
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.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"
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.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")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-10-25 07:18:48 +00:00
|
|
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-10-25 07:18:48 +00:00
|
|
|
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")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-11-22 20:53:04 +00:00
|
|
|
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
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-10-25 07:18:48 +00:00
|
|
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.Error(t, err)
|
2020-11-30 20:46:34 +00:00
|
|
|
user.FsConfig.AzBlobConfig.AccountKey = kms.NewPlainSecret("Server-Account-Key-Test")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
2020-10-25 07:18:48 +00:00
|
|
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-10-25 07:18:48 +00:00
|
|
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.NoError(t, err)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.True(t, user.FsConfig.AzBlobConfig.AccountKey.IsEmpty())
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
// sas test for add instead of update
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, user.FsConfig.AzBlobConfig.AccountKey.IsEmpty())
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
func TestUserCryptFs(t *testing.T) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.FsConfig.Provider = dataprovider.CryptedFilesystemProvider
|
|
|
|
user.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret("crypt passphrase")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-12-05 12:48:13 +00:00
|
|
|
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")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, bb, err := httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-12-05 12:48:13 +00:00
|
|
|
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())
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
secret := kms.NewSecret(kms.SecretStatusSecretBox, "invalid encrypted payload", "", "")
|
|
|
|
user.FsConfig.CryptConfig.Passphrase = secret
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
user.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret("passphrase test")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
2020-12-05 12:48:13 +00:00
|
|
|
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")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, bb, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-12-05 12:48:13 +00:00
|
|
|
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())
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
func TestUserSFTPFs(t *testing.T) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.FsConfig.Provider = dataprovider.SFTPFilesystemProvider
|
2021-02-03 10:29:04 +00:00
|
|
|
user.FsConfig.SFTPConfig.Endpoint = "127.0.0.1" // missing port
|
2020-12-12 09:31:09 +00:00
|
|
|
user.FsConfig.SFTPConfig.Username = "sftp_user"
|
|
|
|
user.FsConfig.SFTPConfig.Password = kms.NewPlainSecret("sftp_pwd")
|
|
|
|
user.FsConfig.SFTPConfig.PrivateKey = kms.NewPlainSecret(sftpPrivateKey)
|
|
|
|
user.FsConfig.SFTPConfig.Fingerprints = []string{sftpPkeyFingerprint}
|
2021-02-03 10:29:04 +00:00
|
|
|
_, resp, err := httpdtest.UpdateUser(user, http.StatusBadRequest, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, string(resp), "invalid endpoint")
|
|
|
|
|
|
|
|
user.FsConfig.SFTPConfig.Endpoint = "127.0.0.1:2022"
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/", user.FsConfig.SFTPConfig.Prefix)
|
|
|
|
initialPwdPayload := user.FsConfig.SFTPConfig.Password.GetPayload()
|
|
|
|
initialPkeyPayload := user.FsConfig.SFTPConfig.PrivateKey.GetPayload()
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.SFTPConfig.Password.GetStatus())
|
|
|
|
assert.NotEmpty(t, initialPwdPayload)
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.Password.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.Password.GetKey())
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.SFTPConfig.PrivateKey.GetStatus())
|
|
|
|
assert.NotEmpty(t, initialPkeyPayload)
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.PrivateKey.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.PrivateKey.GetKey())
|
|
|
|
user.FsConfig.SFTPConfig.Password.SetStatus(kms.SecretStatusSecretBox)
|
|
|
|
user.FsConfig.SFTPConfig.Password.SetAdditionalData("adata")
|
|
|
|
user.FsConfig.SFTPConfig.Password.SetKey("fake pwd key")
|
|
|
|
user.FsConfig.SFTPConfig.PrivateKey.SetStatus(kms.SecretStatusSecretBox)
|
|
|
|
user.FsConfig.SFTPConfig.PrivateKey.SetAdditionalData("adata")
|
|
|
|
user.FsConfig.SFTPConfig.PrivateKey.SetKey("fake key")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, bb, err := httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err, string(bb))
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.SFTPConfig.Password.GetStatus())
|
|
|
|
assert.Equal(t, initialPwdPayload, user.FsConfig.SFTPConfig.Password.GetPayload())
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.Password.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.Password.GetKey())
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.SFTPConfig.PrivateKey.GetStatus())
|
|
|
|
assert.Equal(t, initialPkeyPayload, user.FsConfig.SFTPConfig.PrivateKey.GetPayload())
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.PrivateKey.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.PrivateKey.GetKey())
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
secret := kms.NewSecret(kms.SecretStatusSecretBox, "invalid encrypted payload", "", "")
|
|
|
|
user.FsConfig.SFTPConfig.Password = secret
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
user.FsConfig.SFTPConfig.Password = kms.NewEmptySecret()
|
|
|
|
user.FsConfig.SFTPConfig.PrivateKey = secret
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
user.FsConfig.SFTPConfig.PrivateKey = kms.NewPlainSecret(sftpPrivateKey)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.AddUser(user, http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
initialPkeyPayload = user.FsConfig.SFTPConfig.PrivateKey.GetPayload()
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.Password.GetStatus())
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.SFTPConfig.PrivateKey.GetStatus())
|
|
|
|
assert.NotEmpty(t, initialPkeyPayload)
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.PrivateKey.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.PrivateKey.GetKey())
|
|
|
|
user.FsConfig.Provider = dataprovider.SFTPFilesystemProvider
|
|
|
|
user.FsConfig.SFTPConfig.PrivateKey.SetKey("k")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, bb, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err, string(bb))
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, user.FsConfig.SFTPConfig.PrivateKey.GetStatus())
|
|
|
|
assert.NotEmpty(t, initialPkeyPayload)
|
|
|
|
assert.Equal(t, initialPkeyPayload, user.FsConfig.SFTPConfig.PrivateKey.GetPayload())
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.PrivateKey.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.SFTPConfig.PrivateKey.GetKey())
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// sensitive data must be hidden but not deleted from the dataprovider
|
2020-12-12 09:31:09 +00:00
|
|
|
usernames := []string{"user1", "user2", "user3", "user4", "user5"}
|
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")
|
2021-01-17 21:29:08 +00:00
|
|
|
user1, _, err := httpdtest.AddUser(u1, http.StatusCreated)
|
2020-11-22 20:53:04 +00:00
|
|
|
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")
|
2021-01-17 21:29:08 +00:00
|
|
|
user2, _, err := httpdtest.AddUser(u2, http.StatusCreated)
|
2020-11-22 20:53:04 +00:00
|
|
|
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")
|
2021-01-17 21:29:08 +00:00
|
|
|
user3, _, err := httpdtest.AddUser(u3, http.StatusCreated)
|
2020-11-22 20:53:04 +00:00
|
|
|
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")
|
2021-01-17 21:29:08 +00:00
|
|
|
user4, _, err := httpdtest.AddUser(u4, http.StatusCreated)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
u5 := getTestUser()
|
|
|
|
u5.Username = usernames[4]
|
|
|
|
u5.FsConfig.Provider = dataprovider.SFTPFilesystemProvider
|
|
|
|
u5.FsConfig.SFTPConfig.Endpoint = "127.0.0.1:2022"
|
|
|
|
u5.FsConfig.SFTPConfig.Username = "sftp_user"
|
|
|
|
u5.FsConfig.SFTPConfig.Password = kms.NewPlainSecret("apassword")
|
|
|
|
u5.FsConfig.SFTPConfig.PrivateKey = kms.NewPlainSecret(sftpPrivateKey)
|
|
|
|
u5.FsConfig.SFTPConfig.Fingerprints = []string{sftpPkeyFingerprint}
|
|
|
|
u5.FsConfig.SFTPConfig.Prefix = "/prefix"
|
2021-01-17 21:29:08 +00:00
|
|
|
user5, _, err := httpdtest.AddUser(u5, http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
users, _, err := httpdtest.GetUsers(0, 0, http.StatusOK)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.GreaterOrEqual(t, len(users), 5)
|
2020-11-22 20:53:04 +00:00
|
|
|
for _, username := range usernames {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.GetUserByUsername(username, http.StatusOK)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Empty(t, user.Password)
|
2020-11-22 20:53:04 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
user1, _, err = httpdtest.GetUserByUsername(user1.Username, http.StatusOK)
|
2020-11-22 20:53:04 +00:00
|
|
|
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
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user2, _, err = httpdtest.GetUserByUsername(user2.Username, http.StatusOK)
|
2020-11-22 20:53:04 +00:00
|
|
|
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
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user3, _, err = httpdtest.GetUserByUsername(user3.Username, http.StatusOK)
|
2020-11-22 20:53:04 +00:00
|
|
|
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
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user4, _, err = httpdtest.GetUserByUsername(user4.Username, http.StatusOK)
|
2020-12-05 12:48:13 +00:00
|
|
|
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())
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user5, _, err = httpdtest.GetUserByUsername(user5.Username, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, user5.Password)
|
|
|
|
assert.Empty(t, user5.FsConfig.SFTPConfig.Password.GetKey())
|
|
|
|
assert.Empty(t, user5.FsConfig.SFTPConfig.Password.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.Password.GetStatus())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.Password.GetPayload())
|
|
|
|
assert.Empty(t, user5.FsConfig.SFTPConfig.PrivateKey.GetKey())
|
|
|
|
assert.Empty(t, user5.FsConfig.SFTPConfig.PrivateKey.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.PrivateKey.GetStatus())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.PrivateKey.GetPayload())
|
|
|
|
assert.Equal(t, "/prefix", user5.FsConfig.SFTPConfig.Prefix)
|
|
|
|
|
2020-11-22 20:53:04 +00:00
|
|
|
// finally check that we have all the data inside the data provider
|
2021-01-17 21:29:08 +00:00
|
|
|
user1, err = dataprovider.UserExists(user1.Username)
|
2020-11-22 20:53:04 +00:00
|
|
|
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
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user2, err = dataprovider.UserExists(user2.Username)
|
2020-11-22 20:53:04 +00:00
|
|
|
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
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user3, err = dataprovider.UserExists(user3.Username)
|
2020-11-22 20:53:04 +00:00
|
|
|
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
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user4, err = dataprovider.UserExists(user4.Username)
|
2020-12-05 12:48:13 +00:00
|
|
|
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())
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user5, err = dataprovider.UserExists(user5.Username)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, user5.Password)
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.Password.GetKey())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.Password.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.Password.GetStatus())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.Password.GetPayload())
|
|
|
|
err = user5.FsConfig.SFTPConfig.Password.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, kms.SecretStatusPlain, user5.FsConfig.SFTPConfig.Password.GetStatus())
|
|
|
|
assert.Equal(t, u5.FsConfig.SFTPConfig.Password.GetPayload(), user5.FsConfig.SFTPConfig.Password.GetPayload())
|
|
|
|
assert.Empty(t, user5.FsConfig.SFTPConfig.Password.GetKey())
|
|
|
|
assert.Empty(t, user5.FsConfig.SFTPConfig.Password.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.PrivateKey.GetKey())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.PrivateKey.GetAdditionalData())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.PrivateKey.GetStatus())
|
|
|
|
assert.NotEmpty(t, user5.FsConfig.SFTPConfig.PrivateKey.GetPayload())
|
|
|
|
err = user5.FsConfig.SFTPConfig.PrivateKey.Decrypt()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, kms.SecretStatusPlain, user5.FsConfig.SFTPConfig.PrivateKey.GetStatus())
|
|
|
|
assert.Equal(t, u5.FsConfig.SFTPConfig.PrivateKey.GetPayload(), user5.FsConfig.SFTPConfig.PrivateKey.GetPayload())
|
|
|
|
assert.Empty(t, user5.FsConfig.SFTPConfig.PrivateKey.GetKey())
|
|
|
|
assert.Empty(t, user5.FsConfig.SFTPConfig.PrivateKey.GetAdditionalData())
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user1, http.StatusOK)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user2, http.StatusOK)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user3, http.StatusOK)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user4, http.StatusOK)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user5, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-11-22 20:53:04 +00:00
|
|
|
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")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(masterKeyPath, []byte("test key"), os.ModePerm)
|
2020-12-01 21:18:16 +00:00
|
|
|
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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
user.HomeDir = ""
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.UpdateUser(user, http.StatusBadRequest, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
user.HomeDir = "relative_path"
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.UpdateUser(user, http.StatusBadRequest, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.GetUserByUsername("na", 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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err := httpdtest.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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(getTestUser(), http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err, "adding a duplicate user must fail")
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user1, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
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"
|
2021-01-17 21:29:08 +00:00
|
|
|
user2, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
users, _, err := httpdtest.GetUsers(0, 0, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(users), 2)
|
2021-01-17 21:29:08 +00:00
|
|
|
users, _, err = httpdtest.GetUsers(1, 0, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
2021-01-17 21:29:08 +00:00
|
|
|
users, _, err = httpdtest.GetUsers(1, 1, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(users))
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.GetUsers(1, 1, http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user1, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.GetQuotaScans(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.GetQuotaScans(http.StatusInternalServerError)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.Error(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.GetFoldersQuotaScans(http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.GetFoldersQuotaScans(http.StatusInternalServerError)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartQuotaScan(t *testing.T) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.StartQuotaScan(user, http.StatusAccepted)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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{
|
2021-02-24 18:40:29 +00:00
|
|
|
Name: "vfolder",
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "folder"),
|
|
|
|
Description: "virtual folder",
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddFolder(folder, http.StatusCreated)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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 {
|
2021-01-17 21:29:08 +00:00
|
|
|
quotaScan, _, err := httpdtest.GetFoldersQuotaScans(http.StatusOK)
|
2020-06-22 18:02:51 +00:00
|
|
|
if !assert.NoError(t, err, "Error getting active scans") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(quotaScan) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func TestEmbeddedFolders(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "mapped_path")
|
|
|
|
name := filepath.Base(mappedPath)
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
Name: name,
|
|
|
|
UsedQuotaFiles: 1000,
|
|
|
|
UsedQuotaSize: 8192,
|
|
|
|
LastQuotaUpdate: 123,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir",
|
|
|
|
QuotaSize: 4096,
|
|
|
|
QuotaFiles: 1,
|
|
|
|
})
|
|
|
|
_, _, err := httpdtest.AddUser(u, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u.VirtualFolders[0].MappedPath = mappedPath
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// check that the folder was created
|
|
|
|
folder, _, err := httpdtest.GetFolderByName(name, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, mappedPath, folder.MappedPath)
|
|
|
|
assert.Equal(t, 0, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(0), folder.UsedQuotaSize)
|
|
|
|
assert.Equal(t, int64(0), folder.LastQuotaUpdate)
|
|
|
|
if assert.Len(t, user.VirtualFolders, 1) {
|
|
|
|
assert.Equal(t, mappedPath, user.VirtualFolders[0].MappedPath)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].VirtualPath, user.VirtualFolders[0].VirtualPath)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].QuotaFiles, user.VirtualFolders[0].QuotaFiles)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].QuotaSize, user.VirtualFolders[0].QuotaSize)
|
|
|
|
}
|
|
|
|
// if the folder already exists we can just reference it by name while adding/updating a user
|
|
|
|
u.Username = u.Username + "1"
|
|
|
|
u.VirtualFolders[0].MappedPath = ""
|
|
|
|
user1, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.EqualError(t, err, "Virtual folders mismatch")
|
|
|
|
if assert.Len(t, user1.VirtualFolders, 1) {
|
|
|
|
assert.Equal(t, mappedPath, user1.VirtualFolders[0].MappedPath)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].VirtualPath, user1.VirtualFolders[0].VirtualPath)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].QuotaFiles, user1.VirtualFolders[0].QuotaFiles)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].QuotaSize, user1.VirtualFolders[0].QuotaSize)
|
|
|
|
}
|
|
|
|
user1.VirtualFolders = u.VirtualFolders
|
|
|
|
user1, _, err = httpdtest.UpdateUser(user1, http.StatusOK, "")
|
|
|
|
assert.EqualError(t, err, "Virtual folders mismatch")
|
|
|
|
if assert.Len(t, user1.VirtualFolders, 1) {
|
|
|
|
assert.Equal(t, mappedPath, user1.VirtualFolders[0].MappedPath)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].VirtualPath, user1.VirtualFolders[0].VirtualPath)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].QuotaFiles, user1.VirtualFolders[0].QuotaFiles)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].QuotaSize, user1.VirtualFolders[0].QuotaSize)
|
|
|
|
}
|
|
|
|
// now the virtual folder contains all the required paths
|
|
|
|
user1, _, err = httpdtest.UpdateUser(user1, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, user1.VirtualFolders, 1) {
|
|
|
|
assert.Equal(t, mappedPath, user1.VirtualFolders[0].MappedPath)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].VirtualPath, user1.VirtualFolders[0].VirtualPath)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].QuotaFiles, user1.VirtualFolders[0].QuotaFiles)
|
|
|
|
assert.Equal(t, u.VirtualFolders[0].QuotaSize, user1.VirtualFolders[0].QuotaSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveUser(user1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveFolder(vfs.BaseVirtualFolder{Name: name}, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
func TestUpdateFolderQuotaUsage(t *testing.T) {
|
|
|
|
f := vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: "vdir",
|
2020-06-20 10:38:04 +00:00
|
|
|
MappedPath: filepath.Join(os.TempDir(), "folder"),
|
|
|
|
}
|
|
|
|
usedQuotaFiles := 1
|
|
|
|
usedQuotaSize := int64(65535)
|
|
|
|
f.UsedQuotaFiles = usedQuotaFiles
|
|
|
|
f.UsedQuotaSize = usedQuotaSize
|
2021-01-17 21:29:08 +00:00
|
|
|
folder, _, err := httpdtest.AddFolder(f, http.StatusCreated)
|
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)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateFolderQuotaUsage(folder, "invalid mode", http.StatusBadRequest)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateFolderQuotaUsage(f, "reset", http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err = httpdtest.GetFolderByName(f.Name, http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, usedQuotaFiles, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, usedQuotaSize, folder.UsedQuotaSize)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateFolderQuotaUsage(f, "add", http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err = httpdtest.GetFolderByName(f.Name, http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, 2*usedQuotaFiles, folder.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, 2*usedQuotaSize, folder.UsedQuotaSize)
|
2020-06-20 10:38:04 +00:00
|
|
|
f.UsedQuotaSize = -1
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateFolderQuotaUsage(f, "", http.StatusBadRequest)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
f.UsedQuotaSize = usedQuotaSize
|
2021-02-01 18:04:15 +00:00
|
|
|
f.Name = f.Name + "1"
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateFolderQuotaUsage(f, "", http.StatusNotFound)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-08-08 08:01:33 +00:00
|
|
|
func TestGetVersion(t *testing.T) {
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.GetVersion(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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
|
|
|
}
|
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
func TestGetStatus(t *testing.T) {
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.GetStatus(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.GetStatus(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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.GetConnections(http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err := httpdtest.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)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.CloseConnection(c.GetID(), http.StatusOK)
|
2020-09-01 14:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseConnectionAfterUserUpdateDelete(t *testing.T) {
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-09-01 14:10:26 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "0")
|
2020-09-01 14:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 2)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "1")
|
2020-09-01 14:10:26 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-09-01 14:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 0)
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:48:53 +00:00
|
|
|
func TestSkipNaturalKeysValidation(t *testing.T) {
|
|
|
|
err := dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
providerConf.SkipNaturalKeysValidation = true
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
u := getTestUser()
|
|
|
|
u.Username = "user@example.com"
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.AdditionalInfo = "info"
|
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
a := getTestAdmin()
|
|
|
|
a.Username = "admin@example.com"
|
|
|
|
admin, _, err := httpdtest.AddAdmin(a, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
admin.Email = admin.Username
|
|
|
|
admin, _, err = httpdtest.UpdateAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
admin, _, err = httpdtest.GetAdminByUsername(admin.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
f := vfs.BaseVirtualFolder{
|
|
|
|
Name: "文件夹",
|
|
|
|
MappedPath: filepath.Clean(os.TempDir()),
|
|
|
|
}
|
|
|
|
folder, resp, err := httpdtest.AddFolder(f, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
|
|
|
folder.Description = folder.Name
|
|
|
|
folder, resp, err = httpdtest.UpdateFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err, string(resp))
|
|
|
|
folder, resp, err = httpdtest.GetFolderByName(folder.Name, http.StatusOK)
|
|
|
|
assert.NoError(t, err, string(resp))
|
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
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, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-09-28 20:48:52 +00:00
|
|
|
u := getTestUser()
|
|
|
|
u.HomeDir = ""
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-06-07 21:30:18 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualError(t, err, "HomeDir mismatch")
|
|
|
|
}
|
|
|
|
assert.Equal(t, filepath.Join(providerConf.UsersBaseDir, u.Username), user.HomeDir)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-06-07 21:30:18 +00:00
|
|
|
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
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// user quota scan must fail
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.StartQuotaScan(user, http.StatusForbidden)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateQuotaUsage(user, "", http.StatusForbidden)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.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{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: "folder_quota_test",
|
2020-06-07 22:15:14 +00:00
|
|
|
MappedPath: filepath.Clean(os.TempDir()),
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
folder, resp, err := httpdtest.AddFolder(folder, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
|
|
|
_, err = httpdtest.StartFolderQuotaScan(folder, http.StatusForbidden)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.UpdateFolderQuotaUsage(folder, "", http.StatusForbidden)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
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) {
|
2021-01-17 21:29:08 +00:00
|
|
|
token, _, err := httpdtest.GetToken(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
testServerToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
httpdtest.SetJWTToken(token)
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpdtest.GetUserByUsername("na", http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.GetUsers(1, 0, http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.GetAdmins(1, 0, http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.UpdateUser(dataprovider.User{Username: "auser"}, http.StatusInternalServerError, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(dataprovider.User{Username: "auser"}, http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(vfs.BaseVirtualFolder{Name: "aname"}, http.StatusInternalServerError)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
status, _, err := httpdtest.GetStatus(http.StatusOK)
|
2020-12-08 10:18:34 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.False(t, status.DataProvider.IsActive)
|
|
|
|
}
|
2021-01-22 18:42:18 +00:00
|
|
|
_, _, err = httpdtest.Dumpdata("backup.json", "", "", http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
_, _, err = httpdtest.GetFolders(0, 0, http.StatusInternalServerError)
|
2020-06-07 21:30:18 +00:00
|
|
|
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")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(backupFilePath, backupContent, os.ModePerm)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.Loaddata(backupFilePath, "", "", http.StatusInternalServerError)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
backupData.Folders = append(backupData.Folders, vfs.BaseVirtualFolder{Name: "testFolder", MappedPath: filepath.Clean(os.TempDir())})
|
2020-06-07 21:30:18 +00:00
|
|
|
backupContent, err = json.Marshal(backupData)
|
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(backupFilePath, backupContent, os.ModePerm)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.Loaddata(backupFilePath, "", "", http.StatusInternalServerError)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
backupData.Users = nil
|
|
|
|
backupData.Folders = nil
|
|
|
|
backupData.Admins = append(backupData.Admins, getTestAdmin())
|
|
|
|
backupContent, err = json.Marshal(backupData)
|
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(backupFilePath, backupContent, os.ModePerm)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpdtest.Loaddata(backupFilePath, "", "", http.StatusInternalServerError)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-03 13:24:26 +00:00
|
|
|
err = os.Remove(backupFilePath)
|
|
|
|
assert.NoError(t, err)
|
2021-01-25 20:31:33 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, webUserPath+"?clone-from=user", nil)
|
2020-12-26 14:11:38 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
setJWTCookieForReq(req, testServerToken)
|
2020-12-26 14:11:38 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
2021-01-25 20:31:33 +00:00
|
|
|
req, err = http.NewRequest(http.MethodGet, webTemplateUser+"?from=auser", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, testServerToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
2021-02-04 18:09:43 +00:00
|
|
|
req, err = http.NewRequest(http.MethodGet, webTemplateFolder+"?from=afolder", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, testServerToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
2020-05-03 13:24:26 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
httpdtest.SetJWTToken("")
|
2019-09-28 20:48:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestFolders(t *testing.T) {
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: "name",
|
2020-06-07 21:30:18 +00:00
|
|
|
MappedPath: "relative path",
|
2021-02-01 18:04:15 +00:00
|
|
|
Users: []string{"1", "2", "3"},
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.AddFolder(folder, http.StatusBadRequest)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 22:15:14 +00:00
|
|
|
folder.MappedPath = filepath.Clean(os.TempDir())
|
2021-01-17 21:29:08 +00:00
|
|
|
folder1, resp, err := httpdtest.AddFolder(folder, http.StatusCreated)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.EqualError(t, err, "folder users mismatch", string(resp))
|
|
|
|
assert.Equal(t, folder.Name, folder1.Name)
|
2020-06-07 21:30:18 +00:00
|
|
|
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)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder1.Users, 0)
|
2020-06-07 21:30:18 +00:00
|
|
|
// adding a duplicate folder must fail
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.AddFolder(folder, http.StatusCreated)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
folder.MappedPath = filepath.Join(os.TempDir(), "vfolder")
|
2021-02-01 18:04:15 +00:00
|
|
|
folder.Name = filepath.Base(folder.MappedPath)
|
2020-06-07 21:30:18 +00:00
|
|
|
folder.UsedQuotaFiles = 1
|
|
|
|
folder.UsedQuotaSize = 345
|
|
|
|
folder.LastQuotaUpdate = 10
|
2021-01-17 21:29:08 +00:00
|
|
|
folder2, _, err := httpdtest.AddFolder(folder, http.StatusCreated)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.EqualError(t, err, "folder users mismatch", string(resp))
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.Equal(t, 1, folder2.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, int64(345), folder2.UsedQuotaSize)
|
|
|
|
assert.Equal(t, int64(10), folder2.LastQuotaUpdate)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder2.Users, 0)
|
|
|
|
folders, _, err := httpdtest.GetFolders(0, 0, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
numResults := len(folders)
|
|
|
|
assert.GreaterOrEqual(t, numResults, 2)
|
2021-02-01 18:04:15 +00:00
|
|
|
folders, _, err = httpdtest.GetFolders(0, 1, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, folders, numResults-1)
|
2021-02-01 18:04:15 +00:00
|
|
|
folders, _, err = httpdtest.GetFolders(1, 0, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, folders, 1)
|
2021-02-01 18:04:15 +00:00
|
|
|
f, _, err := httpdtest.GetFolderByName(folder1.Name, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, folder1.Name, f.Name)
|
|
|
|
assert.Equal(t, folder1.MappedPath, f.MappedPath)
|
|
|
|
f, _, err = httpdtest.GetFolderByName(folder2.Name, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, folder2.Name, f.Name)
|
|
|
|
assert.Equal(t, folder2.MappedPath, f.MappedPath)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: "invalid",
|
2020-06-07 21:30:18 +00:00
|
|
|
}, http.StatusNotFound)
|
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
_, _, err = httpdtest.UpdateFolder(vfs.BaseVirtualFolder{Name: "notfound"}, http.StatusNotFound)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
folder1.MappedPath = "a/relative/path"
|
|
|
|
_, _, err = httpdtest.UpdateFolder(folder1, http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
folder1.MappedPath = filepath.Join(os.TempDir(), "updated")
|
2021-02-24 18:40:29 +00:00
|
|
|
folder1.Description = "updated folder description"
|
2021-02-01 18:04:15 +00:00
|
|
|
f, _, err = httpdtest.UpdateFolder(folder1, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, folder1.MappedPath, f.MappedPath)
|
2021-02-24 18:40:29 +00:00
|
|
|
assert.Equal(t, folder1.Description, f.Description)
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(folder1, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(folder2, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
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()
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-22 18:42:18 +00:00
|
|
|
_, _, err = httpdtest.Dumpdata("", "", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-22 18:42:18 +00:00
|
|
|
_, _, err = httpdtest.Dumpdata(filepath.Join(backupsPath, "backup.json"), "", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-22 18:42:18 +00:00
|
|
|
_, _, err = httpdtest.Dumpdata("../backup.json", "", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-22 18:42:18 +00:00
|
|
|
_, _, err = httpdtest.Dumpdata("backup.json", "", "0", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-22 18:42:18 +00:00
|
|
|
response, _, err := httpdtest.Dumpdata("", "1", "0", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, ok := response["admins"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
_, ok = response["users"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
_, ok = response["folders"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
_, ok = response["version"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
_, _, err = httpdtest.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)
|
2021-01-22 18:42:18 +00:00
|
|
|
_, _, err = httpdtest.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
|
2021-01-22 18:42:18 +00:00
|
|
|
_, _, err = httpdtest.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)
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 18:33:24 +00:00
|
|
|
func TestDefenderAPI(t *testing.T) {
|
|
|
|
oldConfig := config.GetCommonConfig()
|
|
|
|
|
|
|
|
cfg := config.GetCommonConfig()
|
|
|
|
cfg.DefenderConfig.Enabled = true
|
|
|
|
cfg.DefenderConfig.Threshold = 3
|
|
|
|
|
|
|
|
err := common.Initialize(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ip := "::1"
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
response, _, err := httpdtest.GetBanTime(ip, http.StatusOK)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
banTime, ok := response["date_time"]
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Nil(t, banTime)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
response, _, err = httpdtest.GetScore(ip, http.StatusOK)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
score, ok := response["score"]
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, float64(0), score)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
err = httpdtest.UnbanIP(ip, http.StatusNotFound)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
common.AddDefenderEvent(ip, common.HostEventNoLoginTried)
|
2021-01-17 21:29:08 +00:00
|
|
|
response, _, err = httpdtest.GetScore(ip, http.StatusOK)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
score, ok = response["score"]
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, float64(2), score)
|
|
|
|
|
|
|
|
common.AddDefenderEvent(ip, common.HostEventNoLoginTried)
|
2021-01-17 21:29:08 +00:00
|
|
|
response, _, err = httpdtest.GetBanTime(ip, http.StatusOK)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
banTime, ok = response["date_time"]
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.NotNil(t, banTime)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
err = httpdtest.UnbanIP(ip, http.StatusOK)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
err = httpdtest.UnbanIP(ip, http.StatusNotFound)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = common.Initialize(oldConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefenderAPIErrors(t *testing.T) {
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err := httpdtest.GetBanTime("", http.StatusBadRequest)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.GetBanTime("invalid", http.StatusBadRequest)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.GetScore("", http.StatusBadRequest)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
err = httpdtest.UnbanIP("", http.StatusBadRequest)
|
2021-01-02 18:33:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:42:18 +00:00
|
|
|
func TestLoaddataFromPostBody(t *testing.T) {
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "restored_folder")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := filepath.Base(mappedPath)
|
2021-01-22 18:42:18 +00:00
|
|
|
user := getTestUser()
|
|
|
|
user.ID = 1
|
|
|
|
user.Username = "test_user_restored"
|
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.ID = 1
|
|
|
|
admin.Username = "test_admin_restored"
|
|
|
|
backupData := dataprovider.BackupData{}
|
|
|
|
backupData.Users = append(backupData.Users, user)
|
|
|
|
backupData.Admins = append(backupData.Admins, admin)
|
|
|
|
backupData.Folders = []vfs.BaseVirtualFolder{
|
|
|
|
{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
2021-01-22 18:42:18 +00:00
|
|
|
MappedPath: mappedPath,
|
|
|
|
UsedQuotaSize: 123,
|
|
|
|
UsedQuotaFiles: 456,
|
|
|
|
LastQuotaUpdate: 789,
|
|
|
|
Users: []string{"user"},
|
|
|
|
},
|
|
|
|
{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
|
|
|
MappedPath: mappedPath + "1",
|
2021-01-22 18:42:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
backupContent, err := json.Marshal(backupData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpdtest.LoaddataFromPostBody(nil, "0", "0", http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpdtest.LoaddataFromPostBody(backupContent, "a", "0", http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpdtest.LoaddataFromPostBody([]byte("invalid content"), "0", "0", http.StatusBadRequest)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpdtest.LoaddataFromPostBody(backupContent, "0", "0", http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
admin, _, err = httpdtest.GetAdminByUsername(admin.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err := httpdtest.GetFolderByName(folderName, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, mappedPath+"1", 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 = httpdtest.RemoveFolder(folder, http.StatusOK)
|
2021-01-22 18:42:18 +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")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := filepath.Base(mappedPath)
|
2021-02-24 18:40:29 +00:00
|
|
|
foldeDesc := "restored folder desc"
|
2019-12-27 22:12:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
user.ID = 1
|
|
|
|
user.Username = "test_user_restore"
|
2021-01-17 21:29:08 +00:00
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.ID = 1
|
|
|
|
admin.Username = "test_admin_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)
|
2021-01-17 21:29:08 +00:00
|
|
|
backupData.Admins = append(backupData.Admins, admin)
|
2020-06-07 21:30:18 +00:00
|
|
|
backupData.Folders = []vfs.BaseVirtualFolder{
|
|
|
|
{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
|
|
|
MappedPath: mappedPath + "1",
|
2020-06-07 21:30:18 +00:00
|
|
|
UsedQuotaSize: 123,
|
|
|
|
UsedQuotaFiles: 456,
|
|
|
|
LastQuotaUpdate: 789,
|
|
|
|
Users: []string{"user"},
|
|
|
|
},
|
|
|
|
{
|
2021-02-24 18:40:29 +00:00
|
|
|
MappedPath: mappedPath,
|
|
|
|
Name: folderName,
|
|
|
|
Description: foldeDesc,
|
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")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(backupFilePath, backupContent, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.Loaddata(backupFilePath, "a", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.Loaddata(backupFilePath, "", "a", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.Loaddata("backup.json", "1", "", http.StatusBadRequest)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
// add user, folder, admin from backup
|
|
|
|
_, _, err = httpdtest.Loaddata(backupFilePath, "1", "", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
// update from backup
|
|
|
|
_, _, err = httpdtest.Loaddata(backupFilePath, "2", "", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
admin, _, err = httpdtest.GetAdminByUsername(admin.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err := httpdtest.GetFolderByName(folderName, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
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)
|
2021-02-24 18:40:29 +00:00
|
|
|
assert.Equal(t, foldeDesc, folder.Description)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, folder.Users, 0)
|
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.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) {
|
2021-02-01 18:04:15 +00:00
|
|
|
mappedPath := filepath.Join(os.TempDir(), "restored_fold")
|
|
|
|
folderName := filepath.Base(mappedPath)
|
2020-01-31 18:10:45 +00:00
|
|
|
user := getTestUser()
|
|
|
|
user.ID = 1
|
|
|
|
user.Username = "test_user_restore"
|
2021-01-17 21:29:08 +00:00
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.ID = 1
|
|
|
|
admin.Username = "test_admin_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)
|
2021-01-17 21:29:08 +00:00
|
|
|
backupData.Admins = append(backupData.Admins, admin)
|
2021-02-01 18:04:15 +00:00
|
|
|
backupData.Folders = []vfs.BaseVirtualFolder{
|
|
|
|
{
|
|
|
|
Name: folderName,
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
UsedQuotaSize: 123,
|
|
|
|
UsedQuotaFiles: 456,
|
|
|
|
LastQuotaUpdate: 789,
|
|
|
|
Users: []string{"user"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MappedPath: mappedPath + "1",
|
|
|
|
Name: folderName,
|
|
|
|
},
|
|
|
|
}
|
2020-01-31 18:10:45 +00:00
|
|
|
backupContent, _ := json.Marshal(backupData)
|
|
|
|
backupFilePath := filepath.Join(backupsPath, "backup.json")
|
2021-02-25 20:53:04 +00:00
|
|
|
err := os.WriteFile(backupFilePath, backupContent, os.ModePerm)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.Loaddata(backupFilePath, "0", "0", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err := httpdtest.GetFolderByName(folderName, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, mappedPath+"1", 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)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:10:45 +00:00
|
|
|
oldUploadBandwidth := user.UploadBandwidth
|
|
|
|
user.UploadBandwidth = oldUploadBandwidth + 128
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
admin, _, err = httpdtest.GetAdminByUsername(admin.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
oldInfo := admin.AdditionalInfo
|
2021-02-24 18:40:29 +00:00
|
|
|
oldDesc := admin.Description
|
2021-01-17 21:29:08 +00:00
|
|
|
admin.AdditionalInfo = "newInfo"
|
2021-02-24 18:40:29 +00:00
|
|
|
admin.Description = "newDesc"
|
2021-01-17 21:29:08 +00:00
|
|
|
admin, _, err = httpdtest.UpdateAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
|
|
|
|
backupData.Folders = []vfs.BaseVirtualFolder{
|
|
|
|
{
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
Name: folderName,
|
|
|
|
},
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.Loaddata(backupFilePath, "0", "1", http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, mappedPath+"1", 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)
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, oldUploadBandwidth, user.UploadBandwidth)
|
2021-01-17 21:29:08 +00:00
|
|
|
admin, _, err = httpdtest.GetAdminByUsername(admin.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, oldInfo, admin.AdditionalInfo)
|
2021-02-24 18:40:29 +00:00
|
|
|
assert.NotEqual(t, oldDesc, admin.Description)
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
_, _, err = httpdtest.Loaddata(backupFilePath, "0", "2", http.StatusOK)
|
2020-09-01 14:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// mode 2 will update the user and close the previous connection
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 0)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
2020-09-01 14:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, oldUploadBandwidth, user.UploadBandwidth)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusOK)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2020-05-03 13:24:26 +00:00
|
|
|
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,
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
resp, err := client.Get("https://localhost:8443" + healthzPath)
|
2020-05-06 17:36:34 +00:00
|
|
|
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) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
2020-05-03 13:24:26 +00:00
|
|
|
err = render.DecodeJSON(rr.Body, &user)
|
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, err = http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+user.Username, bytes.NewBuffer(userAsJSON))
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"/"+user.Username, nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
|
|
|
|
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["/"]))
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, userPath+"/"+user.Username, nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserNoUsernameMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
user.Username = ""
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserInvalidHomeDirMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
user.HomeDir = "relative_path"
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddUserInvalidPermsMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestAddFolderInvalidJsonMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, bytes.NewBuffer([]byte("invalid json")))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func TestUpdateFolderInvalidJsonMock(t *testing.T) {
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
Name: "name",
|
|
|
|
MappedPath: filepath.Clean(os.TempDir()),
|
|
|
|
}
|
|
|
|
folder, resp, err := httpdtest.AddFolder(folder, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPut, path.Join(folderPath, folder.Name), bytes.NewBuffer([]byte("not a json")))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-02 18:33:24 +00:00
|
|
|
func TestUnbanInvalidJsonMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-02 18:33:24 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, defenderUnban, bytes.NewBuffer([]byte("invalid json")))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2021-01-02 18:33:24 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2021-01-02 18:33:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestAddUserInvalidJsonMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer([]byte("invalid json")))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddAdminInvalidJsonMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, adminPath, bytes.NewBuffer([]byte("...")))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddAdminNoPasswordMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.Password = ""
|
|
|
|
asJSON, err := json.Marshal(admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, adminPath, bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "please set a password")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangeAdminPwdInvalidJsonMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPut, adminPwdPath, bytes.NewBuffer([]byte("{")))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoginInvalidPasswordMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
_, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass+"1")
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
// now a login with no credentials
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/api/v2/token", nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
assert.Equal(t, http.StatusUnauthorized, rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangeAdminPwdMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.Username = altAdminUsername
|
|
|
|
admin.Password = altAdminPassword
|
|
|
|
admin.Permissions = []string{dataprovider.PermAdminAddUsers, dataprovider.PermAdminDeleteUsers}
|
|
|
|
asJSON, err := json.Marshal(admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, adminPath, bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
altToken, err := getJWTAPITokenFromTestServer(altAdminUsername, altAdminPassword)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
|
|
|
setBearerForReq(req, altToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(userPath, user.Username), bytes.NewBuffer(userAsJSON))
|
|
|
|
setBearerForReq(req, altToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
|
|
|
|
pwd := make(map[string]string)
|
|
|
|
pwd["current_password"] = altAdminPassword
|
|
|
|
pwd["new_password"] = defaultTokenAuthPass
|
|
|
|
asJSON, err = json.Marshal(pwd)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, adminPwdPath, bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, altToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
_, err = getJWTAPITokenFromTestServer(altAdminUsername, altAdminPassword)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
altToken, err = getJWTAPITokenFromTestServer(altAdminUsername, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, adminPwdPath, bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, altToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr) // current password does not match
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
|
|
|
setBearerForReq(req, altToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(adminPath, altAdminUsername), nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateAdminMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.Username = altAdminUsername
|
|
|
|
admin.Permissions = []string{dataprovider.PermAdminManageAdmins}
|
|
|
|
asJSON, err := json.Marshal(admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, adminPath, bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(adminPath, "abc"), bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(adminPath, altAdminUsername), bytes.NewBuffer([]byte("no json")))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
admin.Permissions = nil
|
|
|
|
asJSON, err = json.Marshal(admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(adminPath, altAdminUsername), bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
admin = getTestAdmin()
|
|
|
|
admin.Status = 0
|
|
|
|
asJSON, err = json.Marshal(admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(adminPath, defaultTokenAuthUser), bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
admin.Status = 1
|
|
|
|
admin.Permissions = []string{dataprovider.PermAdminAddUsers}
|
|
|
|
asJSON, err = json.Marshal(admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(adminPath, defaultTokenAuthUser), bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
altToken, err := getJWTAPITokenFromTestServer(altAdminUsername, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
admin.Permissions = []string{dataprovider.PermAdminManageAdmins, dataprovider.PermAdminCloseConnections}
|
|
|
|
asJSON, err = json.Marshal(admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(adminPath, altAdminUsername), bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, altToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(adminPath, altAdminUsername), nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
func TestUpdateUserMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/"+user.Username, bytes.NewBuffer(userAsJSON))
|
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"/"+user.Username, nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-12-25 17:20:19 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 10:38:04 +00:00
|
|
|
func TestUpdateUserQuotaUsageMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-20 10:38:04 +00:00
|
|
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
err = render.DecodeJSON(rr.Body, &user)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, updateUsedQuotaPath, bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-20 10:38:04 +00:00
|
|
|
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")))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusConflict, rr)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.True(t, common.QuotaScans.RemoveUserQuotaScan(user.Username))
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-20 10:38:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
func TestUserPermissionsMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-12-25 17:20:19 +00:00
|
|
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-12-25 17:20:19 +00:00
|
|
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(userPath, user.Username), bytes.NewBuffer(userAsJSON))
|
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-12-25 17:20:19 +00:00
|
|
|
delete(user.Permissions, "/somedir")
|
2020-02-10 18:28:35 +00:00
|
|
|
user.Permissions["/somedir/.."] = []string{dataprovider.PermAny}
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(userPath, user.Username), bytes.NewBuffer(userAsJSON))
|
|
|
|
setBearerForReq(req, token)
|
2020-02-10 18:28:35 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2020-02-10 18:28:35 +00:00
|
|
|
delete(user.Permissions, "/somedir/..")
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions["not_abs_path"] = []string{dataprovider.PermAny}
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(userPath, user.Username), bytes.NewBuffer(userAsJSON))
|
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-12-25 17:20:19 +00:00
|
|
|
delete(user.Permissions, "not_abs_path")
|
|
|
|
user.Permissions["/somedir/../otherdir/"] = []string{dataprovider.PermListItems}
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(userPath, user.Username), bytes.NewBuffer(userAsJSON))
|
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-12-25 17:20:19 +00:00
|
|
|
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
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-12-25 17:20:19 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestUpdateUserInvalidJsonMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
err = render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(userPath, user.Username), bytes.NewBuffer([]byte("Invalid json")))
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateUserInvalidParamsMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(userPath, user.Username), bytes.NewBuffer(userAsJSON))
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
userID := user.ID
|
|
|
|
user.ID = 0
|
|
|
|
userAsJSON = getUserAsJSON(t, user)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, path.Join(userPath, user.Username), bytes.NewBuffer(userAsJSON))
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
user.ID = userID
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, userPath+"/0", bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAdminsMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.Username = altAdminUsername
|
|
|
|
asJSON, err := json.Marshal(admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, adminPath, bytes.NewBuffer(asJSON))
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, adminPath+"?limit=510&offset=0&order=ASC", nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var admins []dataprovider.Admin
|
|
|
|
err = render.DecodeJSON(rr.Body, &admins)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(admins), 1)
|
|
|
|
firtAdmin := admins[0].Username
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, adminPath+"?limit=510&offset=0&order=DESC", nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
admins = nil
|
|
|
|
err = render.DecodeJSON(rr.Body, &admins)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(admins), 1)
|
|
|
|
assert.NotEqual(t, firtAdmin, admins[0].Username)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, adminPath+"?limit=510&offset=1&order=ASC", nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
admins = nil
|
|
|
|
err = render.DecodeJSON(rr.Body, &admins)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(admins), 1)
|
|
|
|
assert.NotEqual(t, firtAdmin, admins[0].Username)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, adminPath+"?limit=a&offset=0&order=ASC", nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, adminPath+"?limit=1&offset=a&order=ASC", nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, adminPath+"?limit=1&offset=0&order=ASCa", nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(adminPath, admin.Username), nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUsersMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
err = render.DecodeJSON(rr.Body, &user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=510&offset=0&order=ASC", nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
var users []dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &users)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.GreaterOrEqual(t, len(users), 1)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=a&offset=0&order=ASC", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=a&order=ASC", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath+"?limit=1&offset=0&order=ASCa", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteUserInvalidParamsMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodDelete, userPath+"/0", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetQuotaScansMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, err := http.NewRequest("GET", quotaScanPath, nil)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartQuotaScanMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-08-04 19:37:31 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusConflict, rr)
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusAccepted, rr)
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusAccepted, rr)
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-12-30 17:37:50 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-12-30 17:37:50 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
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) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-20 10:38:04 +00:00
|
|
|
mappedPath := filepath.Join(os.TempDir(), "vfolder")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := filepath.Base(mappedPath)
|
2020-06-20 10:38:04 +00:00
|
|
|
f := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath,
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
2020-06-20 10:38:04 +00:00
|
|
|
}
|
|
|
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
2020-06-20 10:38:04 +00:00
|
|
|
err = render.DecodeJSON(rr.Body, &folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, updateFolderUsedQuotaPath, bytes.NewBuffer(folderAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-20 10:38:04 +00:00
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
var folderGet vfs.BaseVirtualFolder
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(folderPath, folderName), nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2021-02-01 18:04:15 +00:00
|
|
|
err = render.DecodeJSON(rr.Body, &folderGet)
|
2020-06-20 10:38:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, usedQuotaFiles, folderGet.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, usedQuotaSize, folderGet.UsedQuotaSize)
|
2020-06-20 10:38:04 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, updateFolderUsedQuotaPath, bytes.NewBuffer([]byte("string")))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2020-06-20 10:38:04 +00:00
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.True(t, common.QuotaScans.AddVFolderQuotaScan(folderName))
|
2020-06-20 10:38:04 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPut, updateFolderUsedQuotaPath, bytes.NewBuffer(folderAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusConflict, rr)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.True(t, common.QuotaScans.RemoveVFolderQuotaScan(folderName))
|
2020-06-20 10:38:04 +00:00
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(folderPath, folderName), nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-20 10:38:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-20 10:38:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestStartFolderQuotaScanMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
mappedPath := filepath.Join(os.TempDir(), "vfolder")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := filepath.Base(mappedPath)
|
2020-06-07 21:30:18 +00:00
|
|
|
folder := vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
MappedPath: mappedPath,
|
|
|
|
}
|
|
|
|
folderAsJSON, err := json.Marshal(folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, bytes.NewBuffer(folderAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
_, err = os.Stat(mappedPath)
|
|
|
|
if err == nil {
|
|
|
|
err = os.Remove(mappedPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
// simulate a duplicate quota scan
|
2021-02-01 18:04:15 +00:00
|
|
|
common.QuotaScans.AddVFolderQuotaScan(folderName)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, quotaScanVFolderPath, bytes.NewBuffer(folderAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusConflict, rr)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.True(t, common.QuotaScans.RemoveVFolderQuotaScan(folderName))
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusAccepted, rr)
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
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
|
2021-02-01 18:04:15 +00:00
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(folderPath, folderName), nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
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) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, quotaScanPath, bytes.NewBuffer(userAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestStartQuotaScanBadUserMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, quotaScanPath, bytes.NewBuffer([]byte("invalid json")))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestStartQuotaScanBadFolderMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, quotaScanVFolderPath, bytes.NewBuffer([]byte("invalid json")))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartQuotaScanNonExistentFolderMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: os.TempDir(),
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: "afolder",
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
folderAsJSON, err := json.Marshal(folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, quotaScanVFolderPath, bytes.NewBuffer(folderAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFoldersMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
mappedPath := filepath.Join(os.TempDir(), "vfolder")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := filepath.Base(mappedPath)
|
2020-06-07 21:30:18 +00:00
|
|
|
folder := vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: folderName,
|
2020-06-07 21:30:18 +00:00
|
|
|
MappedPath: mappedPath,
|
|
|
|
}
|
|
|
|
folderAsJSON, err := json.Marshal(folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, bytes.NewBuffer(folderAsJSON))
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
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)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, url.String(), nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
err = render.DecodeJSON(rr.Body, &folders)
|
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.GreaterOrEqual(t, len(folders), 1)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, folderPath+"?limit=a&offset=0&order=ASC", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, folderPath+"?limit=1&offset=a&order=ASC", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, folderPath+"?limit=1&offset=0&order=ASCa", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(folderPath, folderName), nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 08:01:33 +00:00
|
|
|
func TestGetVersionMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, versionPath, nil)
|
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusUnauthorized, rr)
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, versionPath, nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, versionPath, nil)
|
|
|
|
setBearerForReq(req, "abcde")
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusUnauthorized, rr)
|
2019-08-08 08:01:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestGetConnectionsMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, activeConnectionsPath, nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
func TestGetStatusMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-08 10:18:34 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, serverStatusPath, nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2020-12-08 10:18:34 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-12-08 10:18:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestDeleteActiveConnectionMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodDelete, activeConnectionsPath+"/connectionID", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotFoundMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/non/existing/path", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-07-26 09:34:44 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMethodNotAllowedMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, activeConnectionsPath, nil)
|
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusMethodNotAllowed, rr)
|
2019-09-13 16:45:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 20:50:21 +00:00
|
|
|
func TestHealthCheck(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/healthz", nil)
|
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-10-29 20:50:21 +00:00
|
|
|
assert.Equal(t, "ok", rr.Body.String())
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func TestGetWebRootMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusMovedPermanently, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webBasePath, nil)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusMovedPermanently, rr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebNotFoundURI(t *testing.T) {
|
|
|
|
urlString := httpBaseURL + webBasePath + "/a"
|
|
|
|
req, err := http.NewRequest(http.MethodGet, urlString, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
resp, err := httpclient.GetHTTPClient().Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, urlString, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, "invalid token")
|
|
|
|
resp, err = httpclient.GetHTTPClient().Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:35:36 +00:00
|
|
|
func TestLogout(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-26 21:35:36 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, serverStatusPath, nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, logoutPath, nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, serverStatusPath, nil)
|
|
|
|
setBearerForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusUnauthorized, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Your token is no longer valid")
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:11:47 +00:00
|
|
|
func TestTokenHeaderCookie(t *testing.T) {
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, serverStatusPath, nil)
|
|
|
|
setJWTCookieForReq(req, apiToken)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusUnauthorized, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "no token found")
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, serverStatusPath, nil)
|
|
|
|
setBearerForReq(req, apiToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webStatusPath, nil)
|
|
|
|
setBearerForReq(req, webToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusFound, rr)
|
|
|
|
assert.Equal(t, webLoginPath, rr.Header().Get("Location"))
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webStatusPath, nil)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
}
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
func TestTokenAudience(t *testing.T) {
|
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, serverStatusPath, nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr := executeRequest(req)
|
2021-02-02 08:14:10 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, serverStatusPath, nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, webToken)
|
2021-02-02 08:14:10 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusUnauthorized, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Your token audience is not valid")
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webStatusPath, nil)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webStatusPath, nil)
|
|
|
|
setJWTCookieForReq(req, apiToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusFound, rr)
|
|
|
|
assert.Equal(t, webLoginPath, rr.Header().Get("Location"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebLoginMock(t *testing.T) {
|
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, serverStatusPath, nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2021-02-02 08:14:10 +00:00
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webStatusPath+"notfound", nil)
|
|
|
|
req.RequestURI = webStatusPath + "notfound"
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
|
|
|
|
2021-01-26 21:35:36 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webStatusPath, nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-01-26 21:35:36 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webLogoutPath, nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusFound, rr)
|
2021-02-02 08:14:10 +00:00
|
|
|
cookie := rr.Header().Get("Cookie")
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Empty(t, cookie)
|
2021-01-26 21:35:36 +00:00
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, logoutPath, nil)
|
|
|
|
setBearerForReq(req, apiToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
2021-01-26 21:35:36 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, serverStatusPath, nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2021-01-26 21:35:36 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusUnauthorized, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Your token is no longer valid")
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webStatusPath, nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-01-26 21:35:36 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusFound, rr)
|
|
|
|
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
// now try using wrong credentials
|
2021-02-03 07:55:28 +00:00
|
|
|
form := getAdminLoginForm(defaultTokenAuthUser, "wrong pwd", csrfToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
// try from an ip not allowed
|
|
|
|
a := getTestAdmin()
|
|
|
|
a.Username = altAdminUsername
|
|
|
|
a.Password = altAdminPassword
|
|
|
|
a.Filters.AllowList = []string{"10.0.0.0/8"}
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
_, _, err = httpdtest.AddAdmin(a, http.StatusCreated)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-02-03 07:55:28 +00:00
|
|
|
form = getAdminLoginForm(altAdminUsername, altAdminPassword, csrfToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.RemoteAddr = "127.1.1.1:1234"
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "login from IP 127.1.1.1 not allowed")
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.RemoteAddr = "10.9.9.9:1234"
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusFound, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.RemoteAddr = "127.0.1.1:4567"
|
|
|
|
req.Header.Set("X-Forwarded-For", "10.9.9.9")
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Login from IP 127.0.1.1:4567 is not allowed")
|
|
|
|
|
2021-02-03 07:55:28 +00:00
|
|
|
// invalid csrf token
|
|
|
|
form = getAdminLoginForm(altAdminUsername, altAdminPassword, "invalid csrf")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.RemoteAddr = "10.9.9.8:1234"
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webLoginPath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveAdmin(a, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdminNoToken(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, webChangeAdminPwdPath, nil)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusFound, rr)
|
|
|
|
assert.Equal(t, webLoginPath, rr.Header().Get("Location"))
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusFound, rr)
|
|
|
|
assert.Equal(t, webLoginPath, rr.Header().Get("Location"))
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusUnauthorized, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, activeConnectionsPath, nil)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusUnauthorized, rr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebAdminPwdChange(t *testing.T) {
|
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.Username = altAdminUsername
|
|
|
|
admin.Password = altAdminPassword
|
|
|
|
admin, _, err := httpdtest.AddAdmin(admin, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(admin.Username, altAdminPassword)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, webChangeAdminPwdPath, nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("current_password", altAdminPassword)
|
|
|
|
form.Set("new_password1", altAdminPassword)
|
|
|
|
form.Set("new_password2", altAdminPassword)
|
2021-02-03 07:55:28 +00:00
|
|
|
// no csrf token
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webChangeAdminPwdPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webChangeAdminPwdPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2021-02-02 08:14:10 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), "the new password must be different from the current one")
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
form.Set("new_password1", altAdminPassword+"1")
|
|
|
|
form.Set("new_password2", altAdminPassword+"1")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webChangeAdminPwdPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusFound, rr)
|
|
|
|
assert.Equal(t, webLoginPath, rr.Header().Get("Location"))
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicWebUsersMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
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))
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, token)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
err = render.DecodeJSON(rr.Body, &user1)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUsersPath, nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUsersPath+"?qlimit=a", nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUsersPath+"?qlimit=1", nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath, nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(webUserPath, user.Username), nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath+"/0", nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", user.Username)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/0", &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/aaa", &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webUserPath, user.Username), nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr = executeRequest(req)
|
2021-02-03 07:55:28 +00:00
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token")
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webUserPath, user.Username), nil)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
setCSRFHeaderForReq(req, csrfToken)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webUserPath, user1.Username), nil)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-02-03 07:55:28 +00:00
|
|
|
setCSRFHeaderForReq(req, csrfToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebAdminBasicMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.Username = altAdminUsername
|
|
|
|
admin.Password = altAdminPassword
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", admin.Username)
|
|
|
|
form.Set("password", "")
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set("status", "1")
|
2021-01-17 21:29:08 +00:00
|
|
|
form.Set("permissions", "*")
|
2021-02-24 18:40:29 +00:00
|
|
|
form.Set("description", admin.Description)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, webAdminPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
2021-02-03 07:55:28 +00:00
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
|
|
|
form.Set(csrfFormToken, csrfToken)
|
|
|
|
form.Set("status", "a")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
form.Set("status", "1")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
form.Set("password", admin.Password)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
|
|
|
|
_, _, err = httpdtest.GetAdminByUsername(altAdminUsername, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webAdminsPath+"?qlimit=a", nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webAdminsPath+"?qlimit=1", nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webAdminPath, nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
form.Set("password", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webAdminPath, altAdminUsername), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, "invalid csrf")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webAdminPath, altAdminUsername), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
form.Set("email", "not-an-email")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webAdminPath, altAdminUsername), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
form.Set("email", "")
|
|
|
|
form.Set("status", "b")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webAdminPath, altAdminUsername), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
form.Set("email", "admin@example.com")
|
|
|
|
form.Set("status", "0")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webAdminPath, altAdminUsername), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webAdminPath, altAdminUsername+"1"), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(webAdminPath, altAdminUsername), nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(webAdminPath, altAdminUsername+"1"), nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webAdminPath, altAdminUsername), nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
2021-02-03 07:55:28 +00:00
|
|
|
setCSRFHeaderForReq(req, csrfToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusNotFound)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webAdminPath, defaultTokenAuthUser), nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
2021-02-03 07:55:28 +00:00
|
|
|
setCSRFHeaderForReq(req, csrfToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "You cannot delete yourself")
|
2021-02-03 07:55:28 +00:00
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webAdminPath, defaultTokenAuthUser), nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebAdminPermissions(t *testing.T) {
|
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.Username = altAdminUsername
|
|
|
|
admin.Password = altAdminPassword
|
|
|
|
admin.Permissions = []string{dataprovider.PermAdminAddUsers}
|
|
|
|
_, _, err := httpdtest.AddAdmin(admin, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebToken(altAdminUsername, altAdminPassword)
|
|
|
|
require.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, httpBaseURL+webUserPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
resp, err := httpclient.GetHTTPClient().Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, httpBaseURL+path.Join(webUserPath, "auser"), nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
resp, err = httpclient.GetHTTPClient().Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, httpBaseURL+webFolderPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
resp, err = httpclient.GetHTTPClient().Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, httpBaseURL+webStatusPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
resp, err = httpclient.GetHTTPClient().Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, httpBaseURL+webConnectionsPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
resp, err = httpclient.GetHTTPClient().Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, httpBaseURL+webAdminPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
resp, err = httpclient.GetHTTPClient().Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, httpBaseURL+path.Join(webAdminPath, "a"), nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
resp, err = httpclient.GetHTTPClient().Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdminUpdateSelfMock(t *testing.T) {
|
|
|
|
admin, _, err := httpdtest.GetAdminByUsername(defaultTokenAuthUser, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", admin.Username)
|
|
|
|
form.Set("password", admin.Password)
|
|
|
|
form.Set("status", "0")
|
|
|
|
form.Set("permissions", dataprovider.PermAdminAddUsers)
|
|
|
|
form.Set("permissions", dataprovider.PermAdminCloseConnections)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, path.Join(webAdminPath, defaultTokenAuthUser), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "You cannot remove these permissions to yourself")
|
|
|
|
|
|
|
|
form.Set("permissions", dataprovider.PermAdminAny)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webAdminPath, defaultTokenAuthUser), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "You cannot disable yourself")
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 18:42:18 +00:00
|
|
|
func TestWebMaintenanceMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-22 18:42:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, webMaintenancePath, nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2021-01-22 18:42:18 +00:00
|
|
|
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("mode", "a")
|
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webRestorePath, &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-02-03 07:55:28 +00:00
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
|
|
|
form.Set(csrfFormToken, csrfToken)
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webRestorePath, &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-22 18:42:18 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
form.Set("mode", "0")
|
|
|
|
form.Set("quota", "a")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webRestorePath, &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
form.Set("quota", "0")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webRestorePath, &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webRestorePath+"?a=%3", &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
backupFilePath := filepath.Join(os.TempDir(), "backup.json")
|
|
|
|
err = createTestFile(backupFilePath, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "backup_file", backupFilePath)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webRestorePath, &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
err = createTestFile(backupFilePath, 10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "backup_file", backupFilePath)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webRestorePath, &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
user := getTestUser()
|
|
|
|
user.ID = 1
|
|
|
|
user.Username = "test_user_web_restore"
|
|
|
|
admin := getTestAdmin()
|
|
|
|
admin.ID = 1
|
|
|
|
admin.Username = "test_admin_web_restore"
|
|
|
|
backupData := dataprovider.BackupData{}
|
|
|
|
backupData.Users = append(backupData.Users, user)
|
|
|
|
backupData.Admins = append(backupData.Admins, admin)
|
|
|
|
backupContent, err := json.Marshal(backupData)
|
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(backupFilePath, backupContent, os.ModePerm)
|
2021-01-22 18:42:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "backup_file", backupFilePath)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webRestorePath, &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Your backup was successfully restored")
|
|
|
|
|
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
admin, _, err = httpdtest.GetAdminByUsername(admin.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveAdmin(admin, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func TestWebUserAddMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
user := getTestUser()
|
|
|
|
user.UploadBandwidth = 32
|
|
|
|
user.DownloadBandwidth = 64
|
|
|
|
user.UID = 1000
|
2020-11-25 21:26:34 +00:00
|
|
|
user.AdditionalInfo = "info"
|
2021-02-24 18:40:29 +00:00
|
|
|
user.Description = "user dsc"
|
2020-02-23 10:30:26 +00:00
|
|
|
mappedDir := filepath.Join(os.TempDir(), "mapped")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := filepath.Base(mappedDir)
|
|
|
|
f := vfs.BaseVirtualFolder{
|
|
|
|
Name: folderName,
|
|
|
|
MappedPath: mappedDir,
|
|
|
|
}
|
|
|
|
folderAsJSON, err := json.Marshal(f)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, bytes.NewBuffer(folderAsJSON))
|
2021-02-02 08:14:10 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2021-02-01 18:04:15 +00:00
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
form := make(url.Values)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
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 ")
|
2021-02-01 18:04:15 +00:00
|
|
|
form.Set("virtual_folders", fmt.Sprintf(" /vdir:: %v :: 2 :: 1024", folderName))
|
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)
|
2021-02-24 18:40:29 +00:00
|
|
|
form.Set("description", user.Description)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
2019-10-07 16:19:01 +00:00
|
|
|
// test invalid url escape
|
2021-02-01 18:04:15 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"?a=%2", &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2021-02-01 18:04:15 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-11-13 10:36:21 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-11-13 10:36:21 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-11-13 10:36:21 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-11-13 10:36:21 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-12-30 17:37:50 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-12-30 17:37:50 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-12-30 17:37:50 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-12-30 17:37:50 +00:00
|
|
|
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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-08-16 18:17:02 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-08-16 18:17:02 +00:00
|
|
|
form.Set("max_upload_file_size", "1000")
|
2021-02-28 11:10:40 +00:00
|
|
|
// test invalid tls username
|
|
|
|
form.Set("tls_username", "username")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Validation error: invalid TLS username")
|
|
|
|
form.Set("tls_username", string(dataprovider.TLSUsernameNone))
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, "invalid form token")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath, &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
2021-02-15 18:38:53 +00:00
|
|
|
|
|
|
|
dbUser, err := dataprovider.UserExists(user.Username)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, dbUser.Password)
|
|
|
|
assert.True(t, dbUser.IsPasswordHashed())
|
2019-10-07 16:19:01 +00:00
|
|
|
// 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)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
newUser := dataprovider.User{}
|
|
|
|
err = render.DecodeJSON(rr.Body, &newUser)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
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)
|
2021-02-24 18:40:29 +00:00
|
|
|
assert.Equal(t, user.Description, newUser.Description)
|
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")
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, v.Name, folderName)
|
2020-06-07 21:30:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-02-28 11:10:40 +00:00
|
|
|
assert.Equal(t, dataprovider.TLSUsernameNone, newUser.Filters.TLSUsername)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, newUser.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2021-02-01 18:04:15 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(folderPath, folderName), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebUserUpdateMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
2021-02-15 18:38:53 +00:00
|
|
|
dbUser, err := dataprovider.UserExists(user.Username)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, dbUser.Password)
|
|
|
|
assert.True(t, dbUser.IsPasswordHashed())
|
2021-01-17 21:29:08 +00:00
|
|
|
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)
|
2021-02-15 18:38:53 +00:00
|
|
|
form.Set("password", "")
|
|
|
|
form.Set("public_keys", testPubKey)
|
2019-10-07 16:19:01 +00:00
|
|
|
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)
|
2021-02-24 18:40:29 +00:00
|
|
|
form.Set("description", user.Description)
|
2021-02-28 11:10:40 +00:00
|
|
|
form.Set("tls_username", string(dataprovider.TLSUsernameCN))
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-02-03 07:55:28 +00:00
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
|
|
|
form.Set(csrfFormToken, csrfToken)
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
2021-02-15 18:38:53 +00:00
|
|
|
dbUser, err = dataprovider.UserExists(user.Username)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, dbUser.Password)
|
|
|
|
assert.False(t, dbUser.IsPasswordHashed())
|
|
|
|
|
|
|
|
form.Set("password", defaultPassword)
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
dbUser, err = dataprovider.UserExists(user.Username)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, dbUser.Password)
|
|
|
|
assert.True(t, dbUser.IsPasswordHashed())
|
|
|
|
prevPwd := dbUser.Password
|
|
|
|
|
|
|
|
form.Set("password", redactedSecret)
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
dbUser, err = dataprovider.UserExists(user.Username)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, dbUser.Password)
|
|
|
|
assert.True(t, dbUser.IsPasswordHashed())
|
|
|
|
assert.Equal(t, prevPwd, dbUser.Password)
|
2021-02-03 07:55:28 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var updateUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &updateUser)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
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)
|
2021-02-24 18:40:29 +00:00
|
|
|
assert.Equal(t, user.Description, updateUser.Description)
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.Equal(t, int64(100), updateUser.Filters.MaxUploadFileSize)
|
2021-02-28 11:10:40 +00:00
|
|
|
assert.Equal(t, dataprovider.TLSUsernameCN, updateUser.Filters.TLSUsername)
|
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))
|
2021-01-17 21:29:08 +00:00
|
|
|
req, err = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-01-19 06:41:05 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 18:09:43 +00:00
|
|
|
func TestRenderFolderTemplateMock(t *testing.T) {
|
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, webTemplateFolder, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
2021-02-24 18:40:29 +00:00
|
|
|
Name: "templatefolder",
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped"),
|
|
|
|
Description: "template folder desc",
|
2021-02-04 18:09:43 +00:00
|
|
|
}
|
|
|
|
folder, _, err = httpdtest.AddFolder(folder, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, webTemplateFolder+fmt.Sprintf("?from=%v", folder.Name), nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, webTemplateFolder+"?from=unknown-folder", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
func TestRenderUserTemplateMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2020-12-26 14:11:38 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-25 20:31:33 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, webTemplateUser, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-12-26 14:11:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
req, err = http.NewRequest(http.MethodGet, webTemplateUser+fmt.Sprintf("?from=%v", user.Username), nil)
|
2020-12-26 14:11:38 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
2021-01-25 20:31:33 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-12-26 14:11:38 +00:00
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
req, err = http.NewRequest(http.MethodGet, webTemplateUser+"?from=unknown", nil)
|
2020-12-26 14:11:38 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
2020-12-26 14:11:38 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
2020-12-26 14:11:38 +00:00
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-12-26 14:11:38 +00:00
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
func TestRenderWebCloneUserMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-25 20:31:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, webUserPath+fmt.Sprintf("?clone-from=%v", user.Username), nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, webUserPath+fmt.Sprintf("?clone-from=%v", altAdminPassword), nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
2020-12-26 14:11:38 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-12-26 14:11:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
func TestUserTemplateWithFoldersMock(t *testing.T) {
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
2021-02-24 18:40:29 +00:00
|
|
|
Name: "vfolder",
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped"),
|
|
|
|
Description: "vfolder desc with spéciàl ch@rs",
|
2021-02-01 18:04:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
user := getTestUser()
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", user.Username)
|
|
|
|
form.Set("home_dir", filepath.Join(os.TempDir(), "%username%"))
|
|
|
|
form.Set("uid", strconv.FormatInt(int64(user.UID), 10))
|
|
|
|
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("fs_provider", "0")
|
|
|
|
form.Set("max_upload_file_size", "0")
|
2021-02-24 18:40:29 +00:00
|
|
|
form.Set("description", "desc %username% %password%")
|
2021-02-01 18:04:15 +00:00
|
|
|
form.Set("virtual_folders", "/vdir%username%::"+folder.Name+"::-1::-1")
|
|
|
|
form.Set("users", "auser1::password1\nauser2::password2::"+testPubKey+"\nauser1::password")
|
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, path.Join(webTemplateUser), &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr := executeRequest(req)
|
2021-02-03 07:55:28 +00:00
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
require.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
|
|
|
form.Set(csrfFormToken, csrfToken)
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webTemplateUser), &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-02-01 18:04:15 +00:00
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
require.Contains(t, rr.Body.String(), "invalid folder mapped path")
|
|
|
|
|
|
|
|
folder, resp, err := httpdtest.AddFolder(folder, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
|
|
|
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webTemplateUser), &b)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
var dump dataprovider.BackupData
|
|
|
|
err = json.Unmarshal(rr.Body.Bytes(), &dump)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, dump.Users, 2)
|
|
|
|
assert.Len(t, dump.Folders, 1)
|
|
|
|
user1 := dump.Users[0]
|
|
|
|
user2 := dump.Users[1]
|
|
|
|
folder1 := dump.Folders[0]
|
|
|
|
assert.Equal(t, "auser1", user1.Username)
|
|
|
|
assert.Equal(t, "auser2", user2.Username)
|
2021-02-24 18:40:29 +00:00
|
|
|
assert.Equal(t, "desc auser1 password1", user1.Description)
|
|
|
|
assert.Equal(t, "desc auser2 password2", user2.Description)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, filepath.Join(os.TempDir(), user1.Username), user1.HomeDir)
|
|
|
|
assert.Equal(t, filepath.Join(os.TempDir(), user2.Username), user2.HomeDir)
|
|
|
|
assert.Equal(t, folder.Name, folder1.Name)
|
|
|
|
assert.Equal(t, folder.MappedPath, folder1.MappedPath)
|
2021-02-24 18:40:29 +00:00
|
|
|
assert.Equal(t, folder.Description, folder1.Description)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Len(t, user1.PublicKeys, 0)
|
|
|
|
assert.Len(t, user2.PublicKeys, 1)
|
|
|
|
assert.Len(t, user1.VirtualFolders, 1)
|
|
|
|
assert.Len(t, user2.VirtualFolders, 1)
|
|
|
|
assert.Equal(t, "/vdirauser1", user1.VirtualFolders[0].VirtualPath)
|
|
|
|
assert.Equal(t, "/vdirauser2", user2.VirtualFolders[0].VirtualPath)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveFolder(folder, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
func TestUserTemplateMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-25 20:31:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user := getTestUser()
|
|
|
|
user.FsConfig.Provider = dataprovider.S3FilesystemProvider
|
|
|
|
user.FsConfig.S3Config.Bucket = "test"
|
|
|
|
user.FsConfig.S3Config.Region = "eu-central-1"
|
|
|
|
user.FsConfig.S3Config.AccessKey = "%username%"
|
|
|
|
user.FsConfig.S3Config.KeyPrefix = "somedir/subdir/"
|
|
|
|
user.FsConfig.S3Config.UploadPartSize = 5
|
|
|
|
user.FsConfig.S3Config.UploadConcurrency = 4
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2021-01-25 20:31:33 +00:00
|
|
|
form := make(url.Values)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2021-01-25 20:31:33 +00:00
|
|
|
form.Set("username", user.Username)
|
|
|
|
form.Set("home_dir", filepath.Join(os.TempDir(), "%username%"))
|
|
|
|
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", "%username%")
|
|
|
|
form.Set("s3_access_secret", "%password%")
|
|
|
|
form.Set("s3_key_prefix", "base/%username%")
|
|
|
|
form.Set("allowed_extensions", "/dir1::.jpg,.png")
|
|
|
|
form.Set("denied_extensions", "/dir2::.zip")
|
|
|
|
form.Set("max_upload_file_size", "0")
|
|
|
|
// test invalid s3_upload_part_size
|
|
|
|
form.Set("s3_upload_part_size", "a")
|
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
2021-02-04 18:09:43 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, webTemplateUser, &b)
|
2021-01-25 20:31:33 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
form.Set("s3_upload_part_size", strconv.FormatInt(user.FsConfig.S3Config.UploadPartSize, 10))
|
|
|
|
form.Set("s3_upload_concurrency", strconv.Itoa(user.FsConfig.S3Config.UploadConcurrency))
|
|
|
|
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-02-04 18:09:43 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webTemplateUser, &b)
|
2021-01-25 20:31:33 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
|
|
|
|
form.Set("users", "user1::password1::invalid-pkey")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-02-04 18:09:43 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webTemplateUser, &b)
|
2021-01-25 20:31:33 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
require.Contains(t, rr.Body.String(), "Error validating user")
|
|
|
|
|
|
|
|
form.Set("users", "user1:password1")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-02-04 18:09:43 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webTemplateUser, &b)
|
2021-01-25 20:31:33 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
require.Contains(t, rr.Body.String(), "No valid users found, export is not possible")
|
|
|
|
|
|
|
|
form.Set("users", "user1::password1\nuser2::password2::"+testPubKey+"\nuser3::::")
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-02-04 18:09:43 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webTemplateUser, &b)
|
2021-01-25 20:31:33 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
var dump dataprovider.BackupData
|
|
|
|
err = json.Unmarshal(rr.Body.Bytes(), &dump)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, dump.Users, 2)
|
2021-02-04 18:09:43 +00:00
|
|
|
require.Len(t, dump.Admins, 0)
|
|
|
|
require.Len(t, dump.Folders, 0)
|
2021-01-25 20:31:33 +00:00
|
|
|
user1 := dump.Users[0]
|
|
|
|
user2 := dump.Users[1]
|
|
|
|
require.Equal(t, "user1", user1.Username)
|
|
|
|
require.Equal(t, dataprovider.S3FilesystemProvider, user1.FsConfig.Provider)
|
|
|
|
require.Equal(t, "user2", user2.Username)
|
|
|
|
require.Equal(t, dataprovider.S3FilesystemProvider, user2.FsConfig.Provider)
|
|
|
|
require.Len(t, user2.PublicKeys, 1)
|
|
|
|
require.Equal(t, filepath.Join(os.TempDir(), user1.Username), user1.HomeDir)
|
|
|
|
require.Equal(t, filepath.Join(os.TempDir(), user2.Username), user2.HomeDir)
|
|
|
|
require.Equal(t, user1.Username, user1.FsConfig.S3Config.AccessKey)
|
|
|
|
require.Equal(t, user2.Username, user2.FsConfig.S3Config.AccessKey)
|
|
|
|
require.Equal(t, path.Join("base", user1.Username)+"/", user1.FsConfig.S3Config.KeyPrefix)
|
|
|
|
require.Equal(t, path.Join("base", user2.Username)+"/", user2.FsConfig.S3Config.KeyPrefix)
|
|
|
|
require.True(t, user1.FsConfig.S3Config.AccessSecret.IsEncrypted())
|
|
|
|
err = user1.FsConfig.S3Config.AccessSecret.Decrypt()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "password1", user1.FsConfig.S3Config.AccessSecret.GetPayload())
|
|
|
|
require.True(t, user2.FsConfig.S3Config.AccessSecret.IsEncrypted())
|
|
|
|
err = user2.FsConfig.S3Config.AccessSecret.Decrypt()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "password2", user2.FsConfig.S3Config.AccessSecret.GetPayload())
|
|
|
|
}
|
|
|
|
|
2021-02-04 18:09:43 +00:00
|
|
|
func TestFolderTemplateMock(t *testing.T) {
|
|
|
|
folderName := "vfolder-template"
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "%name%mapped%name%path")
|
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("name", folderName)
|
|
|
|
form.Set("mapped_path", mappedPath)
|
2021-02-24 18:40:29 +00:00
|
|
|
form.Set("description", "desc folder %name%")
|
2021-02-04 18:09:43 +00:00
|
|
|
form.Set("folders", "folder1\nfolder2\nfolder3\nfolder1\n\n\n")
|
|
|
|
contentType := "application/x-www-form-urlencoded"
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, webTemplateFolder, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
|
|
|
form.Set(csrfFormToken, csrfToken)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webTemplateFolder+"?param=p%C3%AO%GG", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Error parsing folders fields")
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webTemplateFolder, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
var dump dataprovider.BackupData
|
|
|
|
err = json.Unmarshal(rr.Body.Bytes(), &dump)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, dump.Users, 0)
|
|
|
|
require.Len(t, dump.Admins, 0)
|
|
|
|
require.Len(t, dump.Folders, 3)
|
|
|
|
require.Equal(t, "folder1", dump.Folders[0].Name)
|
2021-02-24 18:40:29 +00:00
|
|
|
require.Equal(t, "desc folder folder1", dump.Folders[0].Description)
|
2021-02-04 18:09:43 +00:00
|
|
|
require.True(t, strings.HasSuffix(dump.Folders[0].MappedPath, "folder1mappedfolder1path"))
|
|
|
|
require.Equal(t, "folder2", dump.Folders[1].Name)
|
2021-02-24 18:40:29 +00:00
|
|
|
require.Equal(t, "desc folder folder2", dump.Folders[1].Description)
|
2021-02-04 18:09:43 +00:00
|
|
|
require.True(t, strings.HasSuffix(dump.Folders[1].MappedPath, "folder2mappedfolder2path"))
|
|
|
|
require.Equal(t, "folder3", dump.Folders[2].Name)
|
2021-02-24 18:40:29 +00:00
|
|
|
require.Equal(t, "desc folder folder3", dump.Folders[2].Description)
|
2021-02-04 18:09:43 +00:00
|
|
|
require.True(t, strings.HasSuffix(dump.Folders[2].MappedPath, "folder3mappedfolder3path"))
|
|
|
|
|
|
|
|
form.Set("folders", "\n\n\n")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webTemplateFolder, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "No folders to export")
|
|
|
|
|
|
|
|
form.Set("folders", "name")
|
|
|
|
form.Set("mapped_path", "relative-path")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webTemplateFolder, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Error validating folder")
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
func TestWebUserS3Mock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-01-19 06:41:05 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
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
|
2021-02-24 18:40:29 +00:00
|
|
|
user.Description = "s3 tèst user"
|
2020-01-19 06:41:05 +00:00
|
|
|
form := make(url.Values)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2020-01-19 06:41:05 +00:00
|
|
|
form.Set("username", user.Username)
|
2021-02-15 18:38:53 +00:00
|
|
|
form.Set("password", redactedSecret)
|
2020-01-19 06:41:05 +00:00
|
|
|
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")
|
2021-02-24 18:40:29 +00:00
|
|
|
form.Set("description", user.Description)
|
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, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
2020-01-19 06:41:05 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
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, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-03-13 18:13:58 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-03-13 18:13:58 +00:00
|
|
|
// 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, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-03-13 16:28:55 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-01-19 06:41:05 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var updateUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &updateUser)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
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())
|
2021-02-24 18:40:29 +00:00
|
|
|
assert.Equal(t, user.Description, updateUser.Description)
|
2020-11-22 20:53:04 +00:00
|
|
|
// now check that a redacted password is not saved
|
2021-02-15 18:38:53 +00:00
|
|
|
form.Set("s3_access_secret", redactedSecret)
|
2020-11-22 20:53:04 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-11-22 20:53:04 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-11-22 20:53:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
var lastUpdatedUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &lastUpdatedUser)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
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, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-11-22 20:53:04 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-11-22 20:53:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var userGet dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &userGet)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.True(t, userGet.FsConfig.S3Config.AccessSecret.IsEmpty())
|
2020-11-22 20:53:04 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
func TestWebUserGCSMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2020-01-31 18:04:00 +00:00
|
|
|
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)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
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)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
form.Set("username", user.Username)
|
2021-02-15 18:38:53 +00:00
|
|
|
form.Set("password", redactedSecret)
|
2020-01-31 18:04:00 +00:00
|
|
|
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, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-01-31 18:04:00 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "gcs_credential_file", credentialsFilePath)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-01-31 18:04:00 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var updateUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &updateUser)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
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, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-02-19 08:41:15 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-02-19 08:41:15 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
updateUser = dataprovider.User{}
|
|
|
|
err = render.DecodeJSON(rr.Body, &updateUser)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, updateUser.FsConfig.GCSConfig.AutomaticCredentials)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-01-31 18:04:00 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
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) {
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2020-10-25 07:18:48 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-10-25 07:18:48 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
err = render.DecodeJSON(rr.Body, &user)
|
2020-10-25 07:18:48 +00:00
|
|
|
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)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2020-10-25 07:18:48 +00:00
|
|
|
form.Set("username", user.Username)
|
2021-02-15 18:38:53 +00:00
|
|
|
form.Set("password", redactedSecret)
|
2020-10-25 07:18:48 +00:00
|
|
|
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, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-10-25 07:18:48 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-10-25 07:18:48 +00:00
|
|
|
// 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, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-10-25 07:18:48 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-10-25 07:18:48 +00:00
|
|
|
// now add the user
|
|
|
|
form.Set("az_upload_concurrency", strconv.Itoa(user.FsConfig.AzBlobConfig.UploadConcurrency))
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-10-25 07:18:48 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-10-25 07:18:48 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var updateUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &updateUser)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
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
|
2021-02-15 18:38:53 +00:00
|
|
|
form.Set("az_account_key", redactedSecret+" ")
|
2020-11-22 20:53:04 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-11-22 20:53:04 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-11-22 20:53:04 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var lastUpdatedUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &lastUpdatedUser)
|
2020-11-22 20:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
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())
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-10-25 07:18:48 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-12-05 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebUserCryptMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2020-12-05 12:48:13 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-12-05 12:48:13 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
err = render.DecodeJSON(rr.Body, &user)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.FsConfig.Provider = dataprovider.CryptedFilesystemProvider
|
|
|
|
user.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret("crypted passphrase")
|
|
|
|
form := make(url.Values)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2020-12-05 12:48:13 +00:00
|
|
|
form.Set("username", user.Username)
|
2021-02-15 18:38:53 +00:00
|
|
|
form.Set("password", redactedSecret)
|
2020-12-05 12:48:13 +00:00
|
|
|
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, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-12-05 12:48:13 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-12-05 12:48:13 +00:00
|
|
|
form.Set("crypt_passphrase", user.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-12-05 12:48:13 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-12-05 12:48:13 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var updateUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &updateUser)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
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
|
2021-02-15 18:38:53 +00:00
|
|
|
form.Set("crypt_passphrase", redactedSecret+" ")
|
2020-12-05 12:48:13 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-12-05 12:48:13 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-12-05 12:48:13 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var lastUpdatedUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &lastUpdatedUser)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
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())
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-12-05 12:48:13 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
func TestWebUserSFTPFsMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
user := getTestUser()
|
|
|
|
userAsJSON := getUserAsJSON(t, user)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, userPath, bytes.NewBuffer(userAsJSON))
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-12-12 09:31:09 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
|
|
|
err = render.DecodeJSON(rr.Body, &user)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.FsConfig.Provider = dataprovider.SFTPFilesystemProvider
|
2021-02-03 10:29:04 +00:00
|
|
|
user.FsConfig.SFTPConfig.Endpoint = "127.0.0.1:22"
|
2020-12-12 09:31:09 +00:00
|
|
|
user.FsConfig.SFTPConfig.Username = "sftpuser"
|
|
|
|
user.FsConfig.SFTPConfig.Password = kms.NewPlainSecret("pwd")
|
|
|
|
user.FsConfig.SFTPConfig.PrivateKey = kms.NewPlainSecret(sftpPrivateKey)
|
|
|
|
user.FsConfig.SFTPConfig.Fingerprints = []string{sftpPkeyFingerprint}
|
|
|
|
user.FsConfig.SFTPConfig.Prefix = "/home/sftpuser"
|
|
|
|
form := make(url.Values)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2020-12-12 09:31:09 +00:00
|
|
|
form.Set("username", user.Username)
|
2021-02-15 18:38:53 +00:00
|
|
|
form.Set("password", redactedSecret)
|
2020-12-12 09:31:09 +00:00
|
|
|
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", "5")
|
|
|
|
form.Set("crypt_passphrase", "")
|
|
|
|
form.Set("allowed_extensions", "/dir1::.jpg,.png")
|
|
|
|
form.Set("denied_extensions", "/dir2::.zip")
|
|
|
|
form.Set("max_upload_file_size", "0")
|
|
|
|
// empty sftpconfig
|
|
|
|
b, contentType, _ := getMultipartFormData(form, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-12-12 09:31:09 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-12-12 09:31:09 +00:00
|
|
|
form.Set("sftp_endpoint", user.FsConfig.SFTPConfig.Endpoint)
|
|
|
|
form.Set("sftp_username", user.FsConfig.SFTPConfig.Username)
|
|
|
|
form.Set("sftp_password", user.FsConfig.SFTPConfig.Password.GetPayload())
|
|
|
|
form.Set("sftp_private_key", user.FsConfig.SFTPConfig.PrivateKey.GetPayload())
|
|
|
|
form.Set("sftp_fingerprints", user.FsConfig.SFTPConfig.Fingerprints[0])
|
|
|
|
form.Set("sftp_prefix", user.FsConfig.SFTPConfig.Prefix)
|
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-12-12 09:31:09 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-12-12 09:31:09 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var updateUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &updateUser)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1577836800000), updateUser.ExpirationDate)
|
|
|
|
assert.Equal(t, 2, len(updateUser.Filters.FileExtensions))
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, updateUser.FsConfig.SFTPConfig.Password.GetStatus())
|
|
|
|
assert.NotEmpty(t, updateUser.FsConfig.SFTPConfig.Password.GetPayload())
|
|
|
|
assert.Empty(t, updateUser.FsConfig.SFTPConfig.Password.GetKey())
|
|
|
|
assert.Empty(t, updateUser.FsConfig.SFTPConfig.Password.GetAdditionalData())
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, updateUser.FsConfig.SFTPConfig.PrivateKey.GetStatus())
|
|
|
|
assert.NotEmpty(t, updateUser.FsConfig.SFTPConfig.PrivateKey.GetPayload())
|
|
|
|
assert.Empty(t, updateUser.FsConfig.SFTPConfig.PrivateKey.GetKey())
|
|
|
|
assert.Empty(t, updateUser.FsConfig.SFTPConfig.PrivateKey.GetAdditionalData())
|
|
|
|
assert.Equal(t, updateUser.FsConfig.SFTPConfig.Prefix, user.FsConfig.SFTPConfig.Prefix)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.SFTPConfig.Username, user.FsConfig.SFTPConfig.Username)
|
|
|
|
assert.Equal(t, updateUser.FsConfig.SFTPConfig.Endpoint, user.FsConfig.SFTPConfig.Endpoint)
|
|
|
|
assert.Len(t, updateUser.FsConfig.SFTPConfig.Fingerprints, 1)
|
|
|
|
assert.Contains(t, updateUser.FsConfig.SFTPConfig.Fingerprints, sftpPkeyFingerprint)
|
|
|
|
// now check that a redacted credentials are not saved
|
2021-02-15 18:38:53 +00:00
|
|
|
form.Set("sftp_password", redactedSecret+" ")
|
|
|
|
form.Set("sftp_private_key", redactedSecret)
|
2020-12-12 09:31:09 +00:00
|
|
|
b, contentType, _ = getMultipartFormData(form, "", "")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webUserPath, user.Username), &b)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-12-12 09:31:09 +00:00
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-12-12 09:31:09 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
var lastUpdatedUser dataprovider.User
|
|
|
|
err = render.DecodeJSON(rr.Body, &lastUpdatedUser)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, lastUpdatedUser.FsConfig.SFTPConfig.Password.GetStatus())
|
|
|
|
assert.Equal(t, updateUser.FsConfig.SFTPConfig.Password.GetPayload(), lastUpdatedUser.FsConfig.SFTPConfig.Password.GetPayload())
|
|
|
|
assert.Empty(t, lastUpdatedUser.FsConfig.SFTPConfig.Password.GetKey())
|
|
|
|
assert.Empty(t, lastUpdatedUser.FsConfig.SFTPConfig.Password.GetAdditionalData())
|
|
|
|
assert.Equal(t, kms.SecretStatusSecretBox, lastUpdatedUser.FsConfig.SFTPConfig.PrivateKey.GetStatus())
|
|
|
|
assert.Equal(t, updateUser.FsConfig.SFTPConfig.PrivateKey.GetPayload(), lastUpdatedUser.FsConfig.SFTPConfig.PrivateKey.GetPayload())
|
|
|
|
assert.Empty(t, lastUpdatedUser.FsConfig.SFTPConfig.PrivateKey.GetKey())
|
|
|
|
assert.Empty(t, lastUpdatedUser.FsConfig.SFTPConfig.PrivateKey.GetAdditionalData())
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(userPath, user.Username), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-12-12 09:31:09 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
func TestAddWebFoldersMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2020-06-07 22:15:14 +00:00
|
|
|
mappedPath := filepath.Clean(os.TempDir())
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := filepath.Base(mappedPath)
|
2021-02-24 18:40:29 +00:00
|
|
|
folderDesc := "a simple desc"
|
2020-06-07 21:30:18 +00:00
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("mapped_path", mappedPath)
|
2021-02-01 18:04:15 +00:00
|
|
|
form.Set("name", folderName)
|
2021-02-24 18:40:29 +00:00
|
|
|
form.Set("description", folderDesc)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, webFolderPath, strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr := executeRequest(req)
|
2021-02-03 07:55:28 +00:00
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
|
|
|
form.Set(csrfFormToken, csrfToken)
|
|
|
|
req, err = http.NewRequest(http.MethodPost, webFolderPath, strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
2021-02-01 18:04:15 +00:00
|
|
|
// adding the same folder will fail since the name must be unique
|
2020-06-07 21:30:18 +00:00
|
|
|
req, err = http.NewRequest(http.MethodPost, webFolderPath, strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
// invalid form
|
|
|
|
req, err = http.NewRequest(http.MethodPost, webFolderPath, strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
req.Header.Set("Content-Type", "text/plain; boundary=")
|
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
|
|
|
|
// now render the add folder page
|
|
|
|
req, err = http.NewRequest(http.MethodGet, webFolderPath, nil)
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
var folder vfs.BaseVirtualFolder
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(folderPath, folderName), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2021-02-01 18:04:15 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
err = render.DecodeJSON(rr.Body, &folder)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, mappedPath, folder.MappedPath)
|
|
|
|
assert.Equal(t, folderName, folder.Name)
|
2021-02-24 18:40:29 +00:00
|
|
|
assert.Equal(t, folderDesc, folder.Description)
|
2021-02-01 18:04:15 +00:00
|
|
|
// cleanup
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(folderPath, folderName), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2021-02-01 18:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateWebFolderMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := "vfolderupdate"
|
2021-02-24 18:40:29 +00:00
|
|
|
folderDesc := "updated desc"
|
2021-02-01 18:04:15 +00:00
|
|
|
folder := vfs.BaseVirtualFolder{
|
2021-02-24 18:40:29 +00:00
|
|
|
Name: folderName,
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "folderupdate"),
|
|
|
|
Description: "dsc",
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
_, _, err = httpdtest.AddFolder(folder, http.StatusCreated)
|
|
|
|
newMappedPath := folder.MappedPath + "1"
|
|
|
|
assert.NoError(t, err)
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("mapped_path", newMappedPath)
|
|
|
|
form.Set("name", folderName)
|
2021-02-24 18:40:29 +00:00
|
|
|
form.Set("description", folderDesc)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, "")
|
2021-02-01 18:04:15 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, path.Join(webFolderPath, folderName), strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-02-01 18:04:15 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr := executeRequest(req)
|
2021-02-03 07:55:28 +00:00
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Unable to verify form token")
|
|
|
|
|
|
|
|
form.Set(csrfFormToken, csrfToken)
|
|
|
|
req, err = http.NewRequest(http.MethodPost, path.Join(webFolderPath, folderName), strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = executeRequest(req)
|
2021-02-01 18:04:15 +00:00
|
|
|
checkResponseCode(t, http.StatusSeeOther, rr)
|
|
|
|
|
2021-02-24 18:40:29 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(folderPath, folderName), nil)
|
|
|
|
setBearerForReq(req, apiToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
err = render.DecodeJSON(rr.Body, &folder)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, newMappedPath, folder.MappedPath)
|
|
|
|
assert.Equal(t, folderName, folder.Name)
|
|
|
|
assert.Equal(t, folderDesc, folder.Description)
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
// parse form error
|
|
|
|
req, err = http.NewRequest(http.MethodPost, path.Join(webFolderPath, folderName)+"??a=a%B3%A2%G3", strings.NewReader(form.Encode()))
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-02-01 18:04:15 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "invalid URL escape")
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodPost, path.Join(webFolderPath, folderName+"1"), strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-02-01 18:04:15 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
|
|
|
|
|
|
|
form.Set("mapped_path", "arelative/path")
|
|
|
|
req, err = http.NewRequest(http.MethodPost, path.Join(webFolderPath, folderName), strings.NewReader(form.Encode()))
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-02-01 18:04:15 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
// render update folder page
|
|
|
|
req, err = http.NewRequest(http.MethodGet, path.Join(webFolderPath, folderName), nil)
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-02-01 18:04:15 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, path.Join(webFolderPath, folderName+"1"), nil)
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2021-02-01 18:04:15 +00:00
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
|
|
|
|
2021-02-03 07:55:28 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webFolderPath, folderName), nil)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token")
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webFolderPath, folderName), nil)
|
|
|
|
setJWTCookieForReq(req, apiToken) // api token is not accepted
|
|
|
|
setCSRFHeaderForReq(req, csrfToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusFound, rr)
|
|
|
|
assert.Equal(t, webLoginPath, rr.Header().Get("Location"))
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webFolderPath, folderName), nil)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
setCSRFHeaderForReq(req, csrfToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebFoldersMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
webToken, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
apiToken, err := getJWTAPITokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-06-07 21:30:18 +00:00
|
|
|
mappedPath1 := filepath.Join(os.TempDir(), "vfolder1")
|
|
|
|
mappedPath2 := filepath.Join(os.TempDir(), "vfolder2")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName1 := filepath.Base(mappedPath1)
|
|
|
|
folderName2 := filepath.Base(mappedPath2)
|
2021-02-24 18:40:29 +00:00
|
|
|
folderDesc1 := "vfolder1 desc"
|
|
|
|
folderDesc2 := "vfolder2 desc"
|
2020-06-07 21:30:18 +00:00
|
|
|
folders := []vfs.BaseVirtualFolder{
|
|
|
|
{
|
2021-02-24 18:40:29 +00:00
|
|
|
Name: folderName1,
|
|
|
|
MappedPath: mappedPath1,
|
|
|
|
Description: folderDesc1,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
|
|
|
{
|
2021-02-24 18:40:29 +00:00
|
|
|
Name: folderName2,
|
|
|
|
MappedPath: mappedPath2,
|
|
|
|
Description: folderDesc2,
|
2020-06-07 21:30:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, folder := range folders {
|
|
|
|
folderAsJSON, err := json.Marshal(folder)
|
|
|
|
assert.NoError(t, err)
|
2021-02-24 18:40:29 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, folderPath, bytes.NewBuffer(folderAsJSON))
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusCreated, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 18:40:29 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, folderPath, nil)
|
2020-06-07 21:30:18 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-24 18:40:29 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2021-02-24 18:40:29 +00:00
|
|
|
var foldersGet []vfs.BaseVirtualFolder
|
|
|
|
err = render.DecodeJSON(rr.Body, &foldersGet)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
numFound := 0
|
|
|
|
for _, f := range foldersGet {
|
|
|
|
if f.Name == folderName1 {
|
|
|
|
assert.Equal(t, mappedPath1, f.MappedPath)
|
|
|
|
assert.Equal(t, folderDesc1, f.Description)
|
|
|
|
numFound++
|
|
|
|
}
|
|
|
|
if f.Name == folderName2 {
|
|
|
|
assert.Equal(t, mappedPath2, f.MappedPath)
|
|
|
|
assert.Equal(t, folderDesc2, f.Description)
|
|
|
|
numFound++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, 2, numFound)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, webFoldersPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
setJWTCookieForReq(req, webToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, err = http.NewRequest(http.MethodGet, webFoldersPath+"?qlimit=a", nil)
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, err = http.NewRequest(http.MethodGet, webFoldersPath+"?qlimit=1", nil)
|
|
|
|
assert.NoError(t, err)
|
2021-02-02 08:14:10 +00:00
|
|
|
setJWTCookieForReq(req, webToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
|
|
|
|
for _, folder := range folders {
|
2021-02-01 18:04:15 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodDelete, path.Join(folderPath, folder.Name), nil)
|
2021-02-02 12:11:47 +00:00
|
|
|
setBearerForReq(req, apiToken)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func TestProviderClosedMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
2020-06-07 21:30:18 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUsersPath, nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
2020-06-07 21:30:18 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath+"/0", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
form := make(url.Values)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2019-10-07 16:19:01 +00:00
|
|
|
form.Set("username", "test")
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webUserPath+"/0", strings.NewReader(form.Encode()))
|
2021-01-17 21:29:08 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr = executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(webAdminPath, defaultTokenAuthUser), nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webAdminPath, defaultTokenAuthUser), strings.NewReader(form.Encode()))
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webAdminsPath, nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
|
|
|
|
2021-02-01 18:04:15 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, path.Join(webFolderPath, defaultTokenAuthUser), nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webFolderPath, defaultTokenAuthUser), strings.NewReader(form.Encode()))
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusInternalServerError, rr)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
err = config.LoadConfig(configDir, "")
|
2020-05-06 17:36:34 +00:00
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 07:55:28 +00:00
|
|
|
func TestWebConnectionsMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 16:19:01 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, webConnectionsPath, nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
2019-10-07 16:19:01 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2021-02-03 07:55:28 +00:00
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webConnectionsPath, "id"), nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token")
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webConnectionsPath, "id"), nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
setCSRFHeaderForReq(req, "csrfToken")
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusForbidden, rr)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token")
|
|
|
|
|
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodDelete, path.Join(webConnectionsPath, "id"), nil)
|
|
|
|
setJWTCookieForReq(req, token)
|
|
|
|
setCSRFHeaderForReq(req, csrfToken)
|
|
|
|
rr = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
func TestGetWebStatusMock(t *testing.T) {
|
2021-02-02 08:14:10 +00:00
|
|
|
token, err := getJWTWebTokenFromTestServer(defaultTokenAuthUser, defaultTokenAuthPass)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-08 10:18:34 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, webStatusPath, nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
setJWTCookieForReq(req, token)
|
2020-12-08 10:18:34 +00:00
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2020-12-08 10:18:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func TestStaticFilesMock(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/static/favicon.ico", nil)
|
|
|
|
rr := executeRequest(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
checkResponseCode(t, http.StatusOK, rr)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 13:08:08 +00:00
|
|
|
func waitTCPListening(address string) {
|
|
|
|
for {
|
|
|
|
conn, err := net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
2021-02-14 21:08:08 +00:00
|
|
|
logger.WarnToConsole("tcp server %v not listening: %v", address, err)
|
2019-07-26 13:08:08 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2021-02-14 21:08:08 +00:00
|
|
|
logger.InfoToConsole("tcp server %v now listening", address)
|
2020-07-24 21:39:38 +00:00
|
|
|
conn.Close()
|
2019-07-26 13:08:08 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func getTestAdmin() dataprovider.Admin {
|
|
|
|
return dataprovider.Admin{
|
|
|
|
Username: defaultTokenAuthUser,
|
|
|
|
Password: defaultTokenAuthPass,
|
|
|
|
Status: 1,
|
|
|
|
Permissions: []string{dataprovider.PermAdminAny},
|
|
|
|
Email: "admin@example.com",
|
2021-02-24 18:40:29 +00:00
|
|
|
Description: "test admin",
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 13:08:08 +00:00
|
|
|
func getTestUser() dataprovider.User {
|
2019-12-25 17:20:19 +00:00
|
|
|
user := dataprovider.User{
|
2021-02-24 18:40:29 +00:00
|
|
|
Username: defaultUsername,
|
|
|
|
Password: defaultPassword,
|
|
|
|
HomeDir: filepath.Join(homeBasePath, defaultUsername),
|
|
|
|
Status: 1,
|
|
|
|
Description: "test user",
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-03 07:55:28 +00:00
|
|
|
func getCSRFToken() (string, error) {
|
|
|
|
req, err := http.NewRequest(http.MethodGet, httpBaseURL+webLoginPath, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
resp, err := httpclient.GetHTTPClient().Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
doc, err := html.Parse(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var csrfToken string
|
|
|
|
var f func(*html.Node)
|
|
|
|
|
|
|
|
f = func(n *html.Node) {
|
|
|
|
if n.Type == html.ElementNode && n.Data == "input" {
|
|
|
|
var name, value string
|
|
|
|
for _, attr := range n.Attr {
|
|
|
|
if attr.Key == "value" {
|
|
|
|
value = attr.Val
|
|
|
|
}
|
|
|
|
if attr.Key == "name" {
|
|
|
|
name = attr.Val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if name == csrfFormToken {
|
|
|
|
csrfToken = value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
|
|
f(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f(doc)
|
|
|
|
|
|
|
|
return csrfToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAdminLoginForm(username, password, csrfToken string) url.Values {
|
2021-01-17 21:29:08 +00:00
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", username)
|
|
|
|
form.Set("password", password)
|
2021-02-03 07:55:28 +00:00
|
|
|
form.Set(csrfFormToken, csrfToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
return form
|
|
|
|
}
|
|
|
|
|
2021-02-03 07:55:28 +00:00
|
|
|
func setCSRFHeaderForReq(req *http.Request, csrfToken string) {
|
|
|
|
req.Header.Set("X-CSRF-TOKEN", csrfToken)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func setBearerForReq(req *http.Request, jwtToken string) {
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", jwtToken))
|
|
|
|
}
|
|
|
|
|
|
|
|
func setJWTCookieForReq(req *http.Request, jwtToken string) {
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", jwtToken))
|
|
|
|
}
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
func getJWTAPITokenFromTestServer(username, password string) (string, error) {
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/api/v2/token", nil)
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
rr := executeRequest(req)
|
|
|
|
if rr.Code != http.StatusOK {
|
|
|
|
return "", fmt.Errorf("unexpected status code %v", rr)
|
|
|
|
}
|
|
|
|
responseHolder := make(map[string]interface{})
|
|
|
|
err := render.DecodeJSON(rr.Body, &responseHolder)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return responseHolder["access_token"].(string), nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 08:14:10 +00:00
|
|
|
func getJWTWebToken(username, password string) (string, error) {
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
form := getAdminLoginForm(username, password, csrfToken)
|
2021-02-02 08:14:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, httpBaseURL+webLoginPath,
|
|
|
|
bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusFound {
|
|
|
|
return "", fmt.Errorf("unexpected status code %v", resp.StatusCode)
|
|
|
|
}
|
|
|
|
cookie := resp.Header.Get("Set-Cookie")
|
|
|
|
if strings.HasPrefix(cookie, "jwt=") {
|
|
|
|
return cookie[4:], nil
|
|
|
|
}
|
|
|
|
return "", errors.New("no cookie found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getJWTWebTokenFromTestServer(username, password string) (string, error) {
|
2021-02-03 07:55:28 +00:00
|
|
|
csrfToken, err := getCSRFToken()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
form := getAdminLoginForm(username, password, csrfToken)
|
2021-02-02 08:14:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, webLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr := executeRequest(req)
|
|
|
|
if rr.Code != http.StatusFound {
|
|
|
|
return "", fmt.Errorf("unexpected status code %v", rr)
|
|
|
|
}
|
2021-02-02 12:11:47 +00:00
|
|
|
cookie := strings.Split(rr.Header().Get("Set-Cookie"), ";")
|
|
|
|
if strings.HasPrefix(cookie[0], "jwt=") {
|
|
|
|
return cookie[0][4:], nil
|
2021-02-02 08:14:10 +00:00
|
|
|
}
|
|
|
|
return "", errors.New("no cookie found")
|
|
|
|
}
|
|
|
|
|
2019-07-26 13:08:08 +00:00
|
|
|
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
return rr
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func checkResponseCode(t *testing.T, expected int, rr *httptest.ResponseRecorder) {
|
|
|
|
assert.Equal(t, expected, rr.Code, rr.Body.String())
|
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
|
|
|
}
|
2021-02-25 20:53:04 +00:00
|
|
|
return os.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)
|
|
|
|
}
|
|
|
|
}
|