2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-26 09:34:44 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-17 21:29:08 +00:00
|
|
|
"bytes"
|
2019-08-08 19:42:07 +00:00
|
|
|
"context"
|
2021-01-17 21:29:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-07-26 09:34:44 +00:00
|
|
|
"fmt"
|
2019-10-09 09:48:54 +00:00
|
|
|
"html/template"
|
2019-07-26 09:34:44 +00:00
|
|
|
"net/http"
|
2019-08-08 19:42:07 +00:00
|
|
|
"net/http/httptest"
|
2020-01-31 18:04:00 +00:00
|
|
|
"net/url"
|
2020-01-19 06:41:05 +00:00
|
|
|
"os"
|
2021-01-17 21:29:08 +00:00
|
|
|
"path"
|
2020-02-03 23:08:00 +00:00
|
|
|
"runtime"
|
2020-01-31 18:04:00 +00:00
|
|
|
"strings"
|
2019-07-26 09:34:44 +00:00
|
|
|
"testing"
|
2021-01-17 21:29:08 +00:00
|
|
|
"time"
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/go-chi/chi"
|
2021-01-17 21:29:08 +00:00
|
|
|
"github.com/go-chi/jwtauth"
|
|
|
|
"github.com/lestrrat-go/jwx/jwt"
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-29 18:02:56 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-06 17:36:34 +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/dataprovider"
|
2020-01-19 06:41:05 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2019-08-03 11:19:00 +00:00
|
|
|
)
|
|
|
|
|
2020-12-29 18:02:56 +00:00
|
|
|
func TestShouldBind(t *testing.T) {
|
|
|
|
c := Conf{
|
|
|
|
BindPort: 10000,
|
|
|
|
}
|
|
|
|
require.True(t, c.ShouldBind())
|
|
|
|
|
|
|
|
c.BindPort = 0
|
|
|
|
require.False(t, c.ShouldBind())
|
|
|
|
|
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
c.BindAddress = "/absolute/path"
|
|
|
|
require.True(t, c.ShouldBind())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestGetRespStatus(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
err = &dataprovider.MethodDisabledError{}
|
|
|
|
respStatus := getRespStatus(err)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, respStatus)
|
2019-07-26 09:34:44 +00:00
|
|
|
err = fmt.Errorf("generic error")
|
|
|
|
respStatus = getRespStatus(err)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, respStatus)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestGCSWebInvalidFormFile(t *testing.T) {
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", "test_username")
|
|
|
|
form.Set("fs_provider", "2")
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, webUserPath, strings.NewReader(form.Encode()))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
err := req.ParseForm()
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = getFsConfigFromUserPostFields(req)
|
|
|
|
assert.EqualError(t, err, http.ErrNotMultipart.Error())
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestInvalidToken(t *testing.T) {
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: "admin",
|
|
|
|
}
|
|
|
|
errFake := errors.New("fake error")
|
|
|
|
asJSON, err := json.Marshal(admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPut, path.Join(adminPath, admin.Username), bytes.NewBuffer(asJSON))
|
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("username", admin.Username)
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), jwtauth.ErrorCtxKey, errFake))
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
updateAdmin(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteAdmin(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
adminPwd := adminPwd{
|
|
|
|
CurrentPassword: "old",
|
|
|
|
NewPassword: "new",
|
|
|
|
}
|
|
|
|
asJSON, err = json.Marshal(adminPwd)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, "", bytes.NewBuffer(asJSON))
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), jwtauth.ErrorCtxKey, errFake))
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
changeAdminPassword(rr, req)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code)
|
|
|
|
adm := getAdminFromToken(req)
|
|
|
|
assert.Empty(t, adm.Username)
|
2020-02-19 21:39:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestUpdateWebAdminInvalidClaims(t *testing.T) {
|
|
|
|
server := httpdServer{}
|
|
|
|
server.initializeRouter()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: "",
|
|
|
|
Password: "password",
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
c := jwtTokenClaims{
|
|
|
|
Username: admin.Username,
|
|
|
|
Permissions: admin.Permissions,
|
|
|
|
Signature: admin.GetSignature(),
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
token, err := c.createTokenResponse(server.tokenAuth)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("status", "1")
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, path.Join(webAdminPath, "admin"), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("username", "admin")
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
|
|
|
handleWebUpdateAdminPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateTokenError(t *testing.T) {
|
|
|
|
server := httpdServer{
|
|
|
|
tokenAuth: jwtauth.New("PS256", utils.GenerateRandomBytes(32), nil),
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: "admin",
|
|
|
|
Password: "password",
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, tokenPath, nil)
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
server.checkAddrAndSendToken(rr, req, admin)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code)
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", admin.Username)
|
|
|
|
form.Set("password", admin.Password)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.RemoteAddr = "127.0.0.1:1234"
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
server.handleWebLoginPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
|
|
|
// req with no content type
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webLoginPath, nil)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebLoginPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
|
|
|
// req with no POST body
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webLoginPath+"?a=a%C3%AO%GG", nil)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebLoginPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webLoginPath+"?a=a%C3%A1%G2", nil)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
handleWebAdminChangePwdPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webLoginPath+"?a=a%C3%A2%G3", nil)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
_, err := getAdminFromPostFields(req)
|
2020-11-25 21:26:34 +00:00
|
|
|
assert.Error(t, err)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
2019-08-03 11:19:00 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestJWTTokenValidation(t *testing.T) {
|
|
|
|
tokenAuth := jwtauth.New("HS256", utils.GenerateRandomBytes(32), nil)
|
|
|
|
claims := make(map[string]interface{})
|
|
|
|
claims["username"] = "admin"
|
|
|
|
claims[jwt.ExpirationKey] = time.Now().UTC().Add(-1 * time.Hour)
|
|
|
|
token, _, err := tokenAuth.Encode(claims)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-19 08:41:15 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
r := GetHTTPRouter()
|
|
|
|
fn := jwtAuthenticator(r)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, userPath, nil)
|
|
|
|
ctx := jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusUnauthorized, rr.Code)
|
2020-01-31 18:04:00 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
fn = jwtAuthenticatorWeb(r)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
2020-10-25 07:18:48 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
|
|
|
|
|
|
|
errTest := errors.New("test error")
|
|
|
|
permFn := checkPerm(dataprovider.PermAdminAny)
|
|
|
|
fn = permFn(r)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, errTest)
|
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
|
|
|
|
permFn = checkPerm(dataprovider.PermAdminAny)
|
|
|
|
fn = permFn(r)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath, nil)
|
|
|
|
req.RequestURI = webUserPath
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, errTest)
|
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestAdminAllowListConnAddr(t *testing.T) {
|
|
|
|
server := httpdServer{}
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Filters: dataprovider.AdminFilters{
|
|
|
|
AllowList: []string{"192.168.1.0/24"},
|
|
|
|
},
|
2020-06-20 10:38:04 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
ctx := context.WithValue(req.Context(), connAddrKey, "127.0.0.1:4567")
|
|
|
|
req.RemoteAddr = "192.168.1.16:1234"
|
|
|
|
server.checkAddrAndSendToken(rr, req.WithContext(ctx), admin)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code, rr.Body.String())
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestUpdateContextFromCookie(t *testing.T) {
|
|
|
|
server := httpdServer{
|
|
|
|
tokenAuth: jwtauth.New("HS256", utils.GenerateRandomBytes(32), nil),
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
claims := make(map[string]interface{})
|
|
|
|
claims["a"] = "b"
|
|
|
|
token, _, err := server.tokenAuth.Encode(claims)
|
|
|
|
assert.NoError(t, err)
|
2021-01-02 18:33:24 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
ctx := jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.updateContextFromCookie(req.WithContext(ctx))
|
2020-02-03 23:08:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestCookieExpiration(t *testing.T) {
|
|
|
|
server := httpdServer{
|
|
|
|
tokenAuth: jwtauth.New("HS256", utils.GenerateRandomBytes(32), nil),
|
|
|
|
}
|
|
|
|
err := errors.New("test error")
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
ctx := jwtauth.NewContext(req.Context(), nil, err)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie := rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
claims := make(map[string]interface{})
|
|
|
|
claims["a"] = "b"
|
|
|
|
token, _, err := server.tokenAuth.Encode(claims)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: "newtestadmin",
|
|
|
|
Password: "password",
|
|
|
|
Permissions: []string{dataprovider.PermAdminAny},
|
|
|
|
}
|
|
|
|
claims = make(map[string]interface{})
|
|
|
|
claims[claimUsernameKey] = admin.Username
|
|
|
|
claims[claimPermissionsKey] = admin.Permissions
|
|
|
|
claims[jwt.SubjectKey] = admin.GetSignature()
|
|
|
|
claims[jwt.ExpirationKey] = time.Now().Add(1 * time.Minute)
|
|
|
|
token, _, err = server.tokenAuth.Encode(claims)
|
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, tokenPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
admin.Status = 0
|
|
|
|
err = dataprovider.AddAdmin(&admin)
|
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, tokenPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
admin.Status = 1
|
|
|
|
admin.Filters.AllowList = []string{"172.16.1.0/24"}
|
|
|
|
err = dataprovider.UpdateAdmin(&admin)
|
2020-09-04 19:08:09 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
admin, err = dataprovider.AdminExists(admin.Username)
|
2020-09-04 19:08:09 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
claims = make(map[string]interface{})
|
|
|
|
claims[claimUsernameKey] = admin.Username
|
|
|
|
claims[claimPermissionsKey] = admin.Permissions
|
|
|
|
claims[jwt.SubjectKey] = admin.GetSignature()
|
|
|
|
claims[jwt.ExpirationKey] = time.Now().Add(1 * time.Minute)
|
|
|
|
token, _, err = server.tokenAuth.Encode(claims)
|
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, tokenPath, nil)
|
|
|
|
req.RemoteAddr = "192.168.8.1:1234"
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
req.RemoteAddr = "172.16.1.2:1234"
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
ctx = context.WithValue(ctx, connAddrKey, "10.9.9.9")
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
req.RemoteAddr = "172.16.1.12:4567"
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.True(t, strings.HasPrefix(cookie, "jwt="))
|
|
|
|
|
|
|
|
err = dataprovider.DeleteAdmin(admin.Username)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetURLParam(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, adminPwdPath, nil)
|
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("val", "testuser%C3%A0")
|
|
|
|
rctx.URLParams.Add("inval", "testuser%C3%AO%GG")
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
escaped := getURLParam(req, "val")
|
|
|
|
assert.Equal(t, "testuserà", escaped)
|
|
|
|
escaped = getURLParam(req, "inval")
|
|
|
|
assert.Equal(t, "testuser%C3%AO%GG", escaped)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangePwdValidationErrors(t *testing.T) {
|
|
|
|
err := doChangeAdminPassword(nil, "", "", "")
|
|
|
|
require.Error(t, err)
|
|
|
|
err = doChangeAdminPassword(nil, "a", "b", "c")
|
|
|
|
require.Error(t, err)
|
|
|
|
err = doChangeAdminPassword(nil, "a", "a", "a")
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest(http.MethodPut, adminPwdPath, nil)
|
|
|
|
err = doChangeAdminPassword(req, "currentpwd", "newpwd", "newpwd")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderUnexistingFolder(t *testing.T) {
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, nil)
|
|
|
|
renderFolder(rr, req, "path not mapped")
|
|
|
|
assert.Equal(t, http.StatusNotFound, rr.Code)
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
2019-08-08 19:42:07 +00:00
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestCloseConnectionHandler(t *testing.T) {
|
2019-08-08 19:42:07 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodDelete, activeConnectionsPath+"/connectionID", nil)
|
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("connectionID", "")
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
rr := httptest.NewRecorder()
|
2019-08-24 12:41:15 +00:00
|
|
|
handleCloseConnection(rr, req)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2019-08-08 19:42:07 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
|
|
|
|
func TestRenderInvalidTemplate(t *testing.T) {
|
|
|
|
tmpl, err := template.New("test").Parse("{{.Count}}")
|
2020-05-03 13:24:26 +00:00
|
|
|
if assert.NoError(t, err) {
|
2019-10-07 16:19:01 +00:00
|
|
|
templates["no_match"] = tmpl
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
renderTemplate(rw, "no_match", map[string]string{})
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rw.Code)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
|
|
|
|
func TestQuotaScanInvalidFs(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
|
|
|
Username: "test",
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
FsConfig: dataprovider.Filesystem{
|
2020-10-05 18:58:41 +00:00
|
|
|
Provider: dataprovider.S3FilesystemProvider,
|
2020-01-19 06:41:05 +00:00
|
|
|
},
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
common.QuotaScans.AddUserQuotaScan(user.Username)
|
2020-01-19 06:41:05 +00:00
|
|
|
err := doQuotaScan(user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|