test(users): add e2e tests for preferences apis

This commit is contained in:
hexxa 2021-10-17 20:11:35 +08:00 committed by Hexxa
parent fbe8708718
commit 2497968b82
5 changed files with 68 additions and 3 deletions

View file

@ -5,9 +5,9 @@ import (
"fmt"
"net/http"
"github.com/ihexxa/quickshare/src/db/userstore"
"github.com/ihexxa/quickshare/src/handlers"
"github.com/ihexxa/quickshare/src/handlers/multiusers"
"github.com/ihexxa/quickshare/src/db/userstore"
"github.com/parnurzeal/gorequest"
)
@ -162,3 +162,12 @@ func (cl *SingleUserClient) Self(token *http.Cookie) (*http.Response, *multiuser
}
return resp, selfResp, errs
}
func (cl *SingleUserClient) SetPreferences(prefers *userstore.Preferences, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/preferences")).
Send(multiusers.SetPreferencesReq{
Preferences: prefers,
}).
AddCookie(token).
End()
}

View file

@ -46,7 +46,6 @@ type Quota struct {
type Preferences struct {
Bg *sitestore.BgConfig `json:"bg"`
BgURL string `json:"bgURL"`
CSSURL string `json:"cssURL"`
LanPackURL string `json:"lanPackURL"`
}

View file

@ -46,6 +46,7 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.AdminRole, "DELETE", "/v1/users/"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/users/list"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/users/self"): true,
apiRuleCname(userstore.AdminRole, "PATCH", "/v1/users/preferences"): true,
apiRuleCname(userstore.AdminRole, "POST", "/v1/roles/"): true,
apiRuleCname(userstore.AdminRole, "DELETE", "/v1/roles/"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/roles/list"): true,
@ -80,6 +81,7 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.UserRole, "PATCH", "/v1/users/pwd"): true,
apiRuleCname(userstore.UserRole, "PATCH", "/v1/users/"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/users/self"): true,
apiRuleCname(userstore.UserRole, "PATCH", "/v1/users/preferences"): true,
apiRuleCname(userstore.UserRole, "POST", "/v1/fs/files"): true,
apiRuleCname(userstore.UserRole, "DELETE", "/v1/fs/files"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/fs/files"): true,

View file

@ -242,6 +242,7 @@ func initHandlers(router *gin.Engine, cfg gocfg.ICfg, deps *depidx.Deps) (*gin.E
usersAPI.GET("/list", userHdrs.ListUsers)
usersAPI.GET("/self", userHdrs.Self)
usersAPI.PATCH("/", userHdrs.SetUser)
usersAPI.PATCH("/preferences", userHdrs.SetPreferences)
rolesAPI := v1.Group("/roles")
rolesAPI.POST("/", userHdrs.AddRole)

View file

@ -3,13 +3,15 @@ package server
import (
"fmt"
"os"
"reflect"
"strconv"
"testing"
"github.com/ihexxa/quickshare/src/client"
"github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/db/userstore"
q "github.com/ihexxa/quickshare/src/handlers"
su "github.com/ihexxa/quickshare/src/handlers/singleuserhdr"
"github.com/ihexxa/quickshare/src/db/userstore"
)
func TestUsersHandlers(t *testing.T) {
@ -377,4 +379,56 @@ func TestUsersHandlers(t *testing.T) {
t.Fatal(resp.StatusCode)
}
})
t.Run("Login,SetPreferences, Self, Logout", func(t *testing.T) {
usersCl := client.NewSingleUserClient(addr)
resp, _, errs := usersCl.Login(adminName, adminNewPwd)
if len(errs) > 0 {
t.Fatal(errs)
} else if resp.StatusCode != 200 {
t.Fatal(resp.StatusCode)
}
token := client.GetCookie(resp.Cookies(), su.TokenCookie)
prefers := []*userstore.Preferences{
&userstore.Preferences{
Bg: &sitestore.BgConfig{
Url: "/bgurl",
Repeat: "no-repeat",
Position: "center",
Align: "fixed",
},
CSSURL: "/cssurl",
LanPackURL: "/lanpack",
},
&userstore.Preferences{
Bg: &sitestore.BgConfig{
Url: "/bgurl2",
Repeat: "no-repeat2",
Position: "center2",
Align: "fixed2",
},
CSSURL: "/cssurl2",
LanPackURL: "/lanpack2",
},
}
for _, prefer := range prefers {
resp, _, errs := usersCl.SetPreferences(prefer, token)
if len(errs) > 0 {
t.Fatal(errs)
} else if resp.StatusCode != 200 {
t.Fatal(resp.StatusCode)
}
resp, selfResp, errs := usersCl.Self(token)
if len(errs) > 0 {
t.Fatal(errs)
} else if resp.StatusCode != 200 {
t.Fatal(resp.StatusCode)
} else if !reflect.DeepEqual(selfResp.Preferences, prefer) {
t.Fatal("preference not equal")
}
}
})
}