Improve APIv4 test lib (#5237)
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
7e9cf13aa3
Коммит
0bc0a467a4
@@ -9,7 +9,9 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
l4g "github.com/alecthomas/log4go"
|
||||||
"github.com/mattermost/platform/app"
|
"github.com/mattermost/platform/app"
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/store"
|
"github.com/mattermost/platform/store"
|
||||||
@@ -19,6 +21,11 @@ import (
|
|||||||
type TestHelper struct {
|
type TestHelper struct {
|
||||||
Client *model.Client4
|
Client *model.Client4
|
||||||
BasicUser *model.User
|
BasicUser *model.User
|
||||||
|
BasicUser2 *model.User
|
||||||
|
TeamAdminUser *model.User
|
||||||
|
BasicTeam *model.Team
|
||||||
|
|
||||||
|
SystemAdminClient *model.Client4
|
||||||
SystemAdminUser *model.User
|
SystemAdminUser *model.User
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,11 +54,18 @@ func Setup() *TestHelper {
|
|||||||
|
|
||||||
th := &TestHelper{}
|
th := &TestHelper{}
|
||||||
th.Client = th.CreateClient()
|
th.Client = th.CreateClient()
|
||||||
|
th.SystemAdminClient = th.CreateClient()
|
||||||
return th
|
return th
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *TestHelper) InitBasic() *TestHelper {
|
func (me *TestHelper) InitBasic() *TestHelper {
|
||||||
|
me.TeamAdminUser = me.CreateUser()
|
||||||
|
me.LoginTeamAdmin()
|
||||||
|
me.BasicTeam = me.CreateTeam()
|
||||||
me.BasicUser = me.CreateUser()
|
me.BasicUser = me.CreateUser()
|
||||||
|
LinkUserToTeam(me.BasicUser, me.BasicTeam)
|
||||||
|
me.BasicUser2 = me.CreateUser()
|
||||||
|
LinkUserToTeam(me.BasicUser2, me.BasicTeam)
|
||||||
app.UpdateUserRoles(me.BasicUser.Id, model.ROLE_SYSTEM_USER.Id)
|
app.UpdateUserRoles(me.BasicUser.Id, model.ROLE_SYSTEM_USER.Id)
|
||||||
me.LoginBasic()
|
me.LoginBasic()
|
||||||
|
|
||||||
@@ -61,6 +75,7 @@ func (me *TestHelper) InitBasic() *TestHelper {
|
|||||||
func (me *TestHelper) InitSystemAdmin() *TestHelper {
|
func (me *TestHelper) InitSystemAdmin() *TestHelper {
|
||||||
me.SystemAdminUser = me.CreateUser()
|
me.SystemAdminUser = me.CreateUser()
|
||||||
app.UpdateUserRoles(me.SystemAdminUser.Id, model.ROLE_SYSTEM_USER.Id+" "+model.ROLE_SYSTEM_ADMIN.Id)
|
app.UpdateUserRoles(me.SystemAdminUser.Id, model.ROLE_SYSTEM_USER.Id+" "+model.ROLE_SYSTEM_ADMIN.Id)
|
||||||
|
me.LoginSystemAdmin()
|
||||||
|
|
||||||
return me
|
return me
|
||||||
}
|
}
|
||||||
@@ -70,6 +85,29 @@ func (me *TestHelper) CreateClient() *model.Client4 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (me *TestHelper) CreateUser() *model.User {
|
func (me *TestHelper) CreateUser() *model.User {
|
||||||
|
return me.CreateUserWithClient(me.Client)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *TestHelper) CreateTeam() *model.Team {
|
||||||
|
return me.CreateTeamWithClient(me.Client)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *TestHelper) CreateTeamWithClient(client *model.Client4) *model.Team {
|
||||||
|
id := model.NewId()
|
||||||
|
team := &model.Team{
|
||||||
|
DisplayName: "dn_" + id,
|
||||||
|
Name: GenerateTestTeamName(),
|
||||||
|
Email: GenerateTestEmail(),
|
||||||
|
Type: model.TEAM_OPEN,
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.DisableDebugLogForTest()
|
||||||
|
rteam, _ := client.CreateTeam(team)
|
||||||
|
utils.EnableDebugLogForTest()
|
||||||
|
return rteam
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *TestHelper) CreateUserWithClient(client *model.Client4) *model.User {
|
||||||
id := model.NewId()
|
id := model.NewId()
|
||||||
|
|
||||||
user := &model.User{
|
user := &model.User{
|
||||||
@@ -82,7 +120,7 @@ func (me *TestHelper) CreateUser() *model.User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
utils.DisableDebugLogForTest()
|
utils.DisableDebugLogForTest()
|
||||||
ruser, _ := me.Client.CreateUser(user)
|
ruser, _ := client.CreateUser(user)
|
||||||
ruser.Password = "Password1"
|
ruser.Password = "Password1"
|
||||||
VerifyUserEmail(ruser.Id)
|
VerifyUserEmail(ruser.Id)
|
||||||
utils.EnableDebugLogForTest()
|
utils.EnableDebugLogForTest()
|
||||||
@@ -90,14 +128,56 @@ func (me *TestHelper) CreateUser() *model.User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (me *TestHelper) LoginBasic() {
|
func (me *TestHelper) LoginBasic() {
|
||||||
utils.DisableDebugLogForTest()
|
me.LoginBasicWithClient(me.Client)
|
||||||
me.Client.Login(me.BasicUser.Email, me.BasicUser.Password)
|
}
|
||||||
utils.EnableDebugLogForTest()
|
|
||||||
|
func (me *TestHelper) LoginBasic2() {
|
||||||
|
me.LoginBasic2WithClient(me.Client)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *TestHelper) LoginTeamAdmin() {
|
||||||
|
me.LoginTeamAdminWithClient(me.Client)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *TestHelper) LoginSystemAdmin() {
|
func (me *TestHelper) LoginSystemAdmin() {
|
||||||
|
me.LoginSystemAdminWithClient(me.SystemAdminClient)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *TestHelper) LoginBasicWithClient(client *model.Client4) {
|
||||||
utils.DisableDebugLogForTest()
|
utils.DisableDebugLogForTest()
|
||||||
me.Client.Login(me.SystemAdminUser.Email, me.SystemAdminUser.Password)
|
client.Login(me.BasicUser.Email, me.BasicUser.Password)
|
||||||
|
utils.EnableDebugLogForTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *TestHelper) LoginBasic2WithClient(client *model.Client4) {
|
||||||
|
utils.DisableDebugLogForTest()
|
||||||
|
client.Login(me.BasicUser.Email, me.BasicUser.Password)
|
||||||
|
utils.EnableDebugLogForTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *TestHelper) LoginTeamAdminWithClient(client *model.Client4) {
|
||||||
|
utils.DisableDebugLogForTest()
|
||||||
|
client.Login(me.TeamAdminUser.Email, me.TeamAdminUser.Password)
|
||||||
|
utils.EnableDebugLogForTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *TestHelper) LoginSystemAdminWithClient(client *model.Client4) {
|
||||||
|
utils.DisableDebugLogForTest()
|
||||||
|
client.Login(me.SystemAdminUser.Email, me.SystemAdminUser.Password)
|
||||||
|
utils.EnableDebugLogForTest()
|
||||||
|
}
|
||||||
|
|
||||||
|
func LinkUserToTeam(user *model.User, team *model.Team) {
|
||||||
|
utils.DisableDebugLogForTest()
|
||||||
|
|
||||||
|
err := app.JoinUserToTeam(team, user)
|
||||||
|
if err != nil {
|
||||||
|
l4g.Error(err.Error())
|
||||||
|
l4g.Close()
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
utils.EnableDebugLogForTest()
|
utils.EnableDebugLogForTest()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +186,11 @@ func GenerateTestEmail() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GenerateTestUsername() string {
|
func GenerateTestUsername() string {
|
||||||
return "n" + model.NewId()
|
return "fakeuser" + model.NewId()
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateTestTeamName() string {
|
||||||
|
return "faketeam" + model.NewId()
|
||||||
}
|
}
|
||||||
|
|
||||||
func VerifyUserEmail(userId string) {
|
func VerifyUserEmail(userId string) {
|
||||||
@@ -197,15 +281,15 @@ func CheckBadRequestStatus(t *testing.T, resp *model.Response) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckErrorMessage(t *testing.T, resp *model.Response, message string) {
|
func CheckErrorMessage(t *testing.T, resp *model.Response, errorId string) {
|
||||||
if resp.Error == nil {
|
if resp.Error == nil {
|
||||||
t.Fatal("should have errored with message:" + message)
|
t.Fatal("should have errored with message:" + errorId)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Error.Message != message {
|
if resp.Error.Id != errorId {
|
||||||
t.Log("actual: " + resp.Error.Message)
|
t.Log("actual: " + resp.Error.Id)
|
||||||
t.Log("expected: " + message)
|
t.Log("expected: " + errorId)
|
||||||
t.Fatal("incorrect error message")
|
t.Fatal("incorrect error message")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,12 +37,12 @@ func TestCreateTeam(t *testing.T) {
|
|||||||
|
|
||||||
rteam.Id = ""
|
rteam.Id = ""
|
||||||
_, resp = Client.CreateTeam(rteam)
|
_, resp = Client.CreateTeam(rteam)
|
||||||
CheckErrorMessage(t, resp, "A team with that name already exists")
|
CheckErrorMessage(t, resp, "store.sql_team.save.domain_exists.app_error")
|
||||||
CheckBadRequestStatus(t, resp)
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
rteam.Name = ""
|
rteam.Name = ""
|
||||||
_, resp = Client.CreateTeam(rteam)
|
_, resp = Client.CreateTeam(rteam)
|
||||||
CheckErrorMessage(t, resp, "Name must be 2 or more lowercase alphanumeric characters")
|
CheckErrorMessage(t, resp, "model.team.is_valid.characters.app_error")
|
||||||
CheckBadRequestStatus(t, resp)
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
if r, err := Client.DoApiPost("/teams", "garbage"); err == nil {
|
if r, err := Client.DoApiPost("/teams", "garbage"); err == nil {
|
||||||
@@ -64,6 +64,7 @@ func TestCreateTeam(t *testing.T) {
|
|||||||
enableTeamCreation := utils.Cfg.TeamSettings.EnableTeamCreation
|
enableTeamCreation := utils.Cfg.TeamSettings.EnableTeamCreation
|
||||||
defer func() {
|
defer func() {
|
||||||
utils.Cfg.TeamSettings.EnableTeamCreation = enableTeamCreation
|
utils.Cfg.TeamSettings.EnableTeamCreation = enableTeamCreation
|
||||||
|
utils.SetDefaultRolesBasedOnConfig()
|
||||||
}()
|
}()
|
||||||
utils.Cfg.TeamSettings.EnableTeamCreation = false
|
utils.Cfg.TeamSettings.EnableTeamCreation = false
|
||||||
utils.SetDefaultRolesBasedOnConfig()
|
utils.SetDefaultRolesBasedOnConfig()
|
||||||
@@ -71,5 +72,4 @@ func TestCreateTeam(t *testing.T) {
|
|||||||
th.LoginBasic()
|
th.LoginBasic()
|
||||||
_, resp = Client.CreateTeam(team)
|
_, resp = Client.CreateTeam(team)
|
||||||
CheckForbiddenStatus(t, resp)
|
CheckForbiddenStatus(t, resp)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,18 +40,18 @@ func TestCreateUser(t *testing.T) {
|
|||||||
ruser.Username = GenerateTestUsername()
|
ruser.Username = GenerateTestUsername()
|
||||||
ruser.Password = "passwd1"
|
ruser.Password = "passwd1"
|
||||||
_, resp = Client.CreateUser(ruser)
|
_, resp = Client.CreateUser(ruser)
|
||||||
CheckErrorMessage(t, resp, "An account with that email already exists.")
|
CheckErrorMessage(t, resp, "store.sql_user.save.email_exists.app_error")
|
||||||
CheckBadRequestStatus(t, resp)
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
ruser.Email = GenerateTestEmail()
|
ruser.Email = GenerateTestEmail()
|
||||||
ruser.Username = user.Username
|
ruser.Username = user.Username
|
||||||
_, resp = Client.CreateUser(ruser)
|
_, resp = Client.CreateUser(ruser)
|
||||||
CheckErrorMessage(t, resp, "An account with that username already exists.")
|
CheckErrorMessage(t, resp, "store.sql_user.save.username_exists.app_error")
|
||||||
CheckBadRequestStatus(t, resp)
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
ruser.Email = ""
|
ruser.Email = ""
|
||||||
_, resp = Client.CreateUser(ruser)
|
_, resp = Client.CreateUser(ruser)
|
||||||
CheckErrorMessage(t, resp, "Invalid email")
|
CheckErrorMessage(t, resp, "model.user.is_valid.email.app_error")
|
||||||
CheckBadRequestStatus(t, resp)
|
CheckBadRequestStatus(t, resp)
|
||||||
|
|
||||||
if r, err := Client.DoApiPost("/users", "garbage"); err == nil {
|
if r, err := Client.DoApiPost("/users", "garbage"); err == nil {
|
||||||
@@ -116,8 +116,7 @@ func TestGetUser(t *testing.T) {
|
|||||||
CheckUnauthorizedStatus(t, resp)
|
CheckUnauthorizedStatus(t, resp)
|
||||||
|
|
||||||
// System admins should ignore privacy settings
|
// System admins should ignore privacy settings
|
||||||
th.LoginSystemAdmin()
|
ruser, resp = th.SystemAdminClient.GetUser(user.Id, resp.Etag)
|
||||||
ruser, resp = Client.GetUser(user.Id, resp.Etag)
|
|
||||||
if ruser.Email == "" {
|
if ruser.Email == "" {
|
||||||
t.Fatal("email should not be blank")
|
t.Fatal("email should not be blank")
|
||||||
}
|
}
|
||||||
@@ -180,7 +179,6 @@ func TestUpdateUser(t *testing.T) {
|
|||||||
_, resp = Client.UpdateUser(user)
|
_, resp = Client.UpdateUser(user)
|
||||||
CheckForbiddenStatus(t, resp)
|
CheckForbiddenStatus(t, resp)
|
||||||
|
|
||||||
th.LoginSystemAdmin()
|
_, resp = th.SystemAdminClient.UpdateUser(user)
|
||||||
_, resp = Client.UpdateUser(user)
|
|
||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user