mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
avoid t.Fatal() in tests (#9189)
I've been burned a few times by tests that simply fatal, requiring me to run another build to learn more about what the mismatch was. Avoid this. This is part of a long running goal of mine to make testing "better".
This commit is contained in:
committed by
Jesús Espino
parent
0bbabd137b
commit
d8c8a19d35
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/mattermost/mattermost-server/mlog"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetPing(t *testing.T) {
|
||||
@@ -47,9 +48,7 @@ func TestGetConfig(t *testing.T) {
|
||||
cfg, resp := th.SystemAdminClient.GetConfig()
|
||||
CheckNoError(t, resp)
|
||||
|
||||
if len(cfg.TeamSettings.SiteName) == 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotEqual(t, "", cfg.TeamSettings.SiteName)
|
||||
|
||||
if *cfg.LdapSettings.BindPassword != model.FAKE_SETTING && len(*cfg.LdapSettings.BindPassword) != 0 {
|
||||
t.Fatal("did not sanitize properly")
|
||||
@@ -121,28 +120,14 @@ func TestUpdateConfig(t *testing.T) {
|
||||
cfg, resp = th.SystemAdminClient.UpdateConfig(cfg)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
if len(cfg.TeamSettings.SiteName) == 0 {
|
||||
t.Fatal()
|
||||
} else {
|
||||
if cfg.TeamSettings.SiteName != "MyFancyName" {
|
||||
t.Log("It should update the SiteName")
|
||||
t.Fatal()
|
||||
}
|
||||
}
|
||||
require.Equal(t, "MyFancyName", cfg.TeamSettings.SiteName, "It should update the SiteName")
|
||||
|
||||
//Revert the change
|
||||
cfg.TeamSettings.SiteName = SiteName
|
||||
cfg, resp = th.SystemAdminClient.UpdateConfig(cfg)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
if len(cfg.TeamSettings.SiteName) == 0 {
|
||||
t.Fatal()
|
||||
} else {
|
||||
if cfg.TeamSettings.SiteName != SiteName {
|
||||
t.Log("It should update the SiteName")
|
||||
t.Fatal()
|
||||
}
|
||||
}
|
||||
require.Equal(t, SiteName, cfg.TeamSettings.SiteName, "It should update the SiteName")
|
||||
|
||||
t.Run("Should not be able to modify PluginSettings.EnableUploads", func(t *testing.T) {
|
||||
oldEnableUploads := *th.App.GetConfig().PluginSettings.EnableUploads
|
||||
|
||||
@@ -17,7 +17,7 @@ func TestAssignRole(t *testing.T) {
|
||||
CheckCommand(t, "roles", "system_admin", th.BasicUser.Email)
|
||||
|
||||
if result := <-th.App.Srv.Store.User().GetByEmail(th.BasicUser.Email); result.Err != nil {
|
||||
t.Fatal()
|
||||
t.Fatal(result.Err)
|
||||
} else {
|
||||
user := result.Data.(*model.User)
|
||||
if user.Roles != "system_user system_admin" {
|
||||
@@ -28,7 +28,7 @@ func TestAssignRole(t *testing.T) {
|
||||
CheckCommand(t, "roles", "member", th.BasicUser.Email)
|
||||
|
||||
if result := <-th.App.Srv.Store.User().GetByEmail(th.BasicUser.Email); result.Err != nil {
|
||||
t.Fatal()
|
||||
t.Fatal(result.Err)
|
||||
} else {
|
||||
user := result.Data.(*model.User)
|
||||
if user.Roles != "system_user" {
|
||||
|
||||
@@ -50,12 +50,10 @@ func TestCreateUserWithoutTeam(t *testing.T) {
|
||||
CheckCommand(t, "user", "create", "--email", email, "--password", "mypassword1", "--username", username)
|
||||
|
||||
if result := <-th.App.Srv.Store.User().GetByEmail(email); result.Err != nil {
|
||||
t.Fatal()
|
||||
t.Fatal(result.Err)
|
||||
} else {
|
||||
user := result.Data.(*model.User)
|
||||
if user.Email != email {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, email, user.Email)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +90,7 @@ func TestChangeUserEmail(t *testing.T) {
|
||||
t.Fatal("should've updated to the new email")
|
||||
}
|
||||
if result := <-th.App.Srv.Store.User().GetByEmail(newEmail); result.Err != nil {
|
||||
t.Fatal()
|
||||
t.Fatal(result.Err)
|
||||
} else {
|
||||
user := result.Data.(*model.User)
|
||||
if user.Email != newEmail {
|
||||
|
||||
@@ -6,6 +6,8 @@ package model
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAccessJson(t *testing.T) {
|
||||
@@ -26,9 +28,7 @@ func TestAccessJson(t *testing.T) {
|
||||
func TestAccessIsValid(t *testing.T) {
|
||||
ad := AccessData{}
|
||||
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.ClientId = NewRandomString(28)
|
||||
if err := ad.IsValid(); err == nil {
|
||||
@@ -41,9 +41,7 @@ func TestAccessIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
ad.ClientId = NewId()
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.UserId = NewRandomString(28)
|
||||
if err := ad.IsValid(); err == nil {
|
||||
@@ -66,9 +64,7 @@ func TestAccessIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
ad.Token = NewId()
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.RefreshToken = NewRandomString(28)
|
||||
if err := ad.IsValid(); err == nil {
|
||||
@@ -76,9 +72,7 @@ func TestAccessIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
ad.RefreshToken = NewId()
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.RedirectUri = ""
|
||||
if err := ad.IsValid(); err == nil {
|
||||
|
||||
@@ -6,6 +6,8 @@ package model
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAuthJson(t *testing.T) {
|
||||
@@ -46,9 +48,7 @@ func TestAuthIsValid(t *testing.T) {
|
||||
|
||||
ad := AuthData{}
|
||||
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.ClientId = NewRandomString(28)
|
||||
if err := ad.IsValid(); err == nil {
|
||||
@@ -56,9 +56,7 @@ func TestAuthIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
ad.ClientId = NewId()
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.UserId = NewRandomString(28)
|
||||
if err := ad.IsValid(); err == nil {
|
||||
@@ -66,9 +64,7 @@ func TestAuthIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
ad.UserId = NewId()
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.Code = NewRandomString(129)
|
||||
if err := ad.IsValid(); err == nil {
|
||||
@@ -81,9 +77,7 @@ func TestAuthIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
ad.Code = NewId()
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.ExpiresIn = 0
|
||||
if err := ad.IsValid(); err == nil {
|
||||
@@ -91,9 +85,7 @@ func TestAuthIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
ad.ExpiresIn = 1
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.CreateAt = 0
|
||||
if err := ad.IsValid(); err == nil {
|
||||
@@ -101,9 +93,7 @@ func TestAuthIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
ad.CreateAt = 1
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.State = NewRandomString(129)
|
||||
if err := ad.IsValid(); err == nil {
|
||||
@@ -121,9 +111,7 @@ func TestAuthIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
ad.Scope = NewRandomString(128)
|
||||
if err := ad.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, ad.IsValid())
|
||||
|
||||
ad.RedirectUri = ""
|
||||
if err := ad.IsValid(); err == nil {
|
||||
|
||||
@@ -6,6 +6,8 @@ package model
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestClusterMessage(t *testing.T) {
|
||||
@@ -17,9 +19,7 @@ func TestClusterMessage(t *testing.T) {
|
||||
json := m.ToJson()
|
||||
result := ClusterMessageFromJson(strings.NewReader(json))
|
||||
|
||||
if result.Data != "hello" {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, "hello", result.Data)
|
||||
|
||||
badresult := ClusterMessageFromJson(strings.NewReader("junk"))
|
||||
if badresult != nil {
|
||||
|
||||
@@ -5,25 +5,20 @@ package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCompliancePostHeader(t *testing.T) {
|
||||
if CompliancePostHeader()[0] != "TeamName" {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, "TeamName", CompliancePostHeader()[0])
|
||||
}
|
||||
|
||||
func TestCompliancePost(t *testing.T) {
|
||||
o := CompliancePost{TeamName: "test", PostFileIds: "files", PostCreateAt: GetMillis()}
|
||||
r := o.Row()
|
||||
|
||||
if r[0] != "test" {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if r[len(r)-1] != "files" {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, "test", r[0])
|
||||
require.Equal(t, "files", r[len(r)-1])
|
||||
}
|
||||
|
||||
var cleanTests = []struct {
|
||||
|
||||
@@ -6,6 +6,8 @@ package model
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEmojiIsValid(t *testing.T) {
|
||||
@@ -23,61 +25,39 @@ func TestEmojiIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
emoji.Id = "1234"
|
||||
if err := emoji.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, emoji.IsValid())
|
||||
|
||||
emoji.Id = NewId()
|
||||
emoji.CreateAt = 0
|
||||
if err := emoji.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, emoji.IsValid())
|
||||
|
||||
emoji.CreateAt = 1234
|
||||
emoji.UpdateAt = 0
|
||||
if err := emoji.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, emoji.IsValid())
|
||||
|
||||
emoji.UpdateAt = 1234
|
||||
emoji.CreatorId = strings.Repeat("1", 27)
|
||||
if err := emoji.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, emoji.IsValid())
|
||||
|
||||
emoji.CreatorId = NewId()
|
||||
emoji.Name = strings.Repeat("1", 65)
|
||||
if err := emoji.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, emoji.IsValid())
|
||||
|
||||
emoji.Name = ""
|
||||
if err := emoji.IsValid(); err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NotNil(t, emoji.IsValid())
|
||||
|
||||
emoji.Name = strings.Repeat("1", 64)
|
||||
if err := emoji.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, emoji.IsValid())
|
||||
|
||||
emoji.Name = "name-"
|
||||
if err := emoji.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, emoji.IsValid())
|
||||
|
||||
emoji.Name = "name_"
|
||||
if err := emoji.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, emoji.IsValid())
|
||||
|
||||
emoji.Name = "name:"
|
||||
if err := emoji.IsValid(); err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NotNil(t, emoji.IsValid())
|
||||
|
||||
emoji.Name = "croissant"
|
||||
if err := emoji.IsValid(); err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NotNil(t, emoji.IsValid())
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ package model
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestOAuthAppJson(t *testing.T) {
|
||||
@@ -52,52 +54,32 @@ func TestOAuthAppPreUpdate(t *testing.T) {
|
||||
func TestOAuthAppIsValid(t *testing.T) {
|
||||
app := OAuthApp{}
|
||||
|
||||
if err := app.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, app.IsValid())
|
||||
|
||||
app.Id = NewId()
|
||||
if err := app.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, app.IsValid())
|
||||
|
||||
app.CreateAt = 1
|
||||
if err := app.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, app.IsValid())
|
||||
|
||||
app.UpdateAt = 1
|
||||
if err := app.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, app.IsValid())
|
||||
|
||||
app.CreatorId = NewId()
|
||||
if err := app.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, app.IsValid())
|
||||
|
||||
app.ClientSecret = NewId()
|
||||
if err := app.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, app.IsValid())
|
||||
|
||||
app.Name = "TestOAuthApp"
|
||||
if err := app.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, app.IsValid())
|
||||
|
||||
app.CallbackUrls = []string{"https://nowhere.com"}
|
||||
if err := app.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, app.IsValid())
|
||||
|
||||
app.Homepage = "https://nowhere.com"
|
||||
if err := app.IsValid(); err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Nil(t, app.IsValid())
|
||||
|
||||
app.IconURL = "https://nowhere.com/icon_image.png"
|
||||
if err := app.IsValid(); err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Nil(t, app.IsValid())
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPreferenceIsValid(t *testing.T) {
|
||||
@@ -16,54 +18,34 @@ func TestPreferenceIsValid(t *testing.T) {
|
||||
Name: NewId(),
|
||||
}
|
||||
|
||||
if err := preference.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, preference.IsValid())
|
||||
|
||||
preference.UserId = NewId()
|
||||
if err := preference.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, preference.IsValid())
|
||||
|
||||
preference.Category = strings.Repeat("01234567890", 20)
|
||||
if err := preference.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, preference.IsValid())
|
||||
|
||||
preference.Category = PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
|
||||
if err := preference.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, preference.IsValid())
|
||||
|
||||
preference.Name = strings.Repeat("01234567890", 20)
|
||||
if err := preference.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, preference.IsValid())
|
||||
|
||||
preference.Name = NewId()
|
||||
if err := preference.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, preference.IsValid())
|
||||
|
||||
preference.Value = strings.Repeat("01234567890", 201)
|
||||
if err := preference.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, preference.IsValid())
|
||||
|
||||
preference.Value = "1234garbage"
|
||||
if err := preference.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, preference.IsValid())
|
||||
|
||||
preference.Category = PREFERENCE_CATEGORY_THEME
|
||||
if err := preference.IsValid(); err == nil {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotNil(t, preference.IsValid())
|
||||
|
||||
preference.Value = `{"color": "#ff0000", "color2": "#faf"}`
|
||||
if err := preference.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, preference.IsValid())
|
||||
}
|
||||
|
||||
func TestPreferencePreUpdate(t *testing.T) {
|
||||
@@ -79,11 +61,9 @@ func TestPreferencePreUpdate(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if props["color"] != "#ff0000" || props["color2"] != "#faf" || props["codeTheme"] != "github" {
|
||||
t.Fatal("shouldn't have changed valid props")
|
||||
}
|
||||
require.Equal(t, "#ff0000", props["color"], "shouldn't have changed valid props")
|
||||
require.Equal(t, "#faf", props["color2"], "shouldn't have changed valid props")
|
||||
require.Equal(t, "github", props["codeTheme"], "shouldn't have changed valid props")
|
||||
|
||||
if props["invalid"] == "invalid" {
|
||||
t.Fatal("should have changed invalid prop")
|
||||
}
|
||||
require.NotEqual(t, "invalid", props["invalid"], "should have changed invalid prop")
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPasswordHash(t *testing.T) {
|
||||
@@ -117,41 +118,34 @@ func TestUserIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
user.Id = NewId()
|
||||
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "create_at", user.Id) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := user.IsValid()
|
||||
require.True(t, HasExpectedUserIsValidError(err, "create_at", user.Id), "expected user is valid error: %s", err.Error())
|
||||
|
||||
user.CreateAt = GetMillis()
|
||||
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "update_at", user.Id) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = user.IsValid()
|
||||
require.True(t, HasExpectedUserIsValidError(err, "update_at", user.Id), "expected user is valid error: %s", err.Error())
|
||||
|
||||
user.UpdateAt = GetMillis()
|
||||
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "username", user.Id) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = user.IsValid()
|
||||
require.True(t, HasExpectedUserIsValidError(err, "username", user.Id), "expected user is valid error: %s", err.Error())
|
||||
|
||||
user.Username = NewId() + "^hello#"
|
||||
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "username", user.Id) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = user.IsValid()
|
||||
require.True(t, HasExpectedUserIsValidError(err, "username", user.Id), "expected user is valid error: %s", err.Error())
|
||||
|
||||
user.Username = NewId()
|
||||
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "email", user.Id) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = user.IsValid()
|
||||
require.True(t, HasExpectedUserIsValidError(err, "email", user.Id), "expected user is valid error: %s", err.Error())
|
||||
|
||||
user.Email = strings.Repeat("01234567890", 20)
|
||||
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "email", user.Id) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = user.IsValid()
|
||||
require.True(t, HasExpectedUserIsValidError(err, "email", user.Id), "expected user is valid error: %s", err.Error())
|
||||
|
||||
user.Email = "user@example.com"
|
||||
|
||||
user.Nickname = strings.Repeat("a", 65)
|
||||
if err := user.IsValid(); !HasExpectedUserIsValidError(err, "nickname", user.Id) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = user.IsValid()
|
||||
require.True(t, HasExpectedUserIsValidError(err, "nickname", user.Id), "expected user is valid error: %s", err.Error())
|
||||
|
||||
user.Nickname = strings.Repeat("a", 64)
|
||||
if err := user.IsValid(); err != nil {
|
||||
@@ -331,28 +325,10 @@ func TestCleanUsername(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRoles(t *testing.T) {
|
||||
|
||||
if !IsValidUserRoles("team_user") {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if IsValidUserRoles("system_admin") {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if !IsValidUserRoles("system_user system_admin") {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if IsInRole("system_admin junk", "admin") {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if !IsInRole("system_admin junk", "system_admin") {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if IsInRole("admin", "system_admin") {
|
||||
t.Fatal()
|
||||
}
|
||||
require.True(t, IsValidUserRoles("team_user"))
|
||||
require.False(t, IsValidUserRoles("system_admin"))
|
||||
require.True(t, IsValidUserRoles("system_user system_admin"))
|
||||
require.False(t, IsInRole("system_admin junk", "admin"))
|
||||
require.True(t, IsInRole("system_admin junk", "system_admin"))
|
||||
require.False(t, IsInRole("admin", "system_admin"))
|
||||
}
|
||||
|
||||
@@ -34,18 +34,14 @@ func TestAppError(t *testing.T) {
|
||||
err := NewAppError("TestAppError", "message", nil, "", http.StatusInternalServerError)
|
||||
json := err.ToJson()
|
||||
rerr := AppErrorFromJson(strings.NewReader(json))
|
||||
if err.Message != rerr.Message {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, err.Message, rerr.Message)
|
||||
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestAppErrorJunk(t *testing.T) {
|
||||
rerr := AppErrorFromJson(strings.NewReader("<html><body>This is a broken test</body></html>"))
|
||||
if "body: <html><body>This is a broken test</body></html>" != rerr.DetailedError {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, "body: <html><body>This is a broken test</body></html>", rerr.DetailedError)
|
||||
}
|
||||
|
||||
func TestCopyStringMap(t *testing.T) {
|
||||
@@ -173,9 +169,7 @@ func TestValidLower(t *testing.T) {
|
||||
|
||||
func TestEtag(t *testing.T) {
|
||||
etag := Etag("hello", 24)
|
||||
if len(etag) <= 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.NotEqual(t, "", etag)
|
||||
}
|
||||
|
||||
var hashtags = map[string]string{
|
||||
|
||||
@@ -6,100 +6,59 @@ package model
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSplitVersion(t *testing.T) {
|
||||
major1, minor1, patch1 := SplitVersion("junk")
|
||||
if major1 != 0 || minor1 != 0 || patch1 != 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.EqualValues(t, 0, major1)
|
||||
require.EqualValues(t, 0, minor1)
|
||||
require.EqualValues(t, 0, patch1)
|
||||
|
||||
major2, minor2, patch2 := SplitVersion("1.2.3")
|
||||
if major2 != 1 || minor2 != 2 || patch2 != 3 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.EqualValues(t, 1, major2)
|
||||
require.EqualValues(t, 2, minor2)
|
||||
require.EqualValues(t, 3, patch2)
|
||||
|
||||
major3, minor3, patch3 := SplitVersion("1.2")
|
||||
if major3 != 1 || minor3 != 2 || patch3 != 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.EqualValues(t, 1, major3)
|
||||
require.EqualValues(t, 2, minor3)
|
||||
require.EqualValues(t, 0, patch3)
|
||||
|
||||
major4, minor4, patch4 := SplitVersion("1")
|
||||
if major4 != 1 || minor4 != 0 || patch4 != 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.EqualValues(t, 1, major4)
|
||||
require.EqualValues(t, 0, minor4)
|
||||
require.EqualValues(t, 0, patch4)
|
||||
|
||||
major5, minor5, patch5 := SplitVersion("1.2.3.junkgoeswhere")
|
||||
if major5 != 1 || minor5 != 2 || patch5 != 3 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.EqualValues(t, 1, major5)
|
||||
require.EqualValues(t, 2, minor5)
|
||||
require.EqualValues(t, 3, patch5)
|
||||
}
|
||||
|
||||
func TestGetPreviousVersion(t *testing.T) {
|
||||
if GetPreviousVersion("1.3.0") != "1.2.0" {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if GetPreviousVersion("1.2.1") != "1.1.0" {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if GetPreviousVersion("1.1.0") != "1.0.0" {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if GetPreviousVersion("1.0.0") != "0.7.0" {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if GetPreviousVersion("0.7.1") != "0.6.0" {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if GetPreviousVersion("0.5.0") != "" {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, "1.2.0", GetPreviousVersion("1.3.0"))
|
||||
require.Equal(t, "1.1.0", GetPreviousVersion("1.2.1"))
|
||||
require.Equal(t, "1.0.0", GetPreviousVersion("1.1.0"))
|
||||
require.Equal(t, "0.7.0", GetPreviousVersion("1.0.0"))
|
||||
require.Equal(t, "0.6.0", GetPreviousVersion("0.7.1"))
|
||||
require.Equal(t, "", GetPreviousVersion("0.5.0"))
|
||||
}
|
||||
|
||||
func TestIsCurrentVersion(t *testing.T) {
|
||||
major, minor, patch := SplitVersion(CurrentVersion)
|
||||
|
||||
if !IsCurrentVersion(CurrentVersion) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if !IsCurrentVersion(fmt.Sprintf("%v.%v.%v", major, minor, patch+100)) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if IsCurrentVersion(fmt.Sprintf("%v.%v.%v", major, minor+1, patch)) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if IsCurrentVersion(fmt.Sprintf("%v.%v.%v", major+1, minor, patch)) {
|
||||
t.Fatal()
|
||||
}
|
||||
require.True(t, IsCurrentVersion(CurrentVersion))
|
||||
require.True(t, IsCurrentVersion(fmt.Sprintf("%v.%v.%v", major, minor, patch+100)))
|
||||
require.False(t, IsCurrentVersion(fmt.Sprintf("%v.%v.%v", major, minor+1, patch)))
|
||||
require.False(t, IsCurrentVersion(fmt.Sprintf("%v.%v.%v", major+1, minor, patch)))
|
||||
}
|
||||
|
||||
func TestIsPreviousVersionsSupported(t *testing.T) {
|
||||
|
||||
if !IsPreviousVersionsSupported(versionsWithoutHotFixes[0]) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if !IsPreviousVersionsSupported(versionsWithoutHotFixes[1]) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if !IsPreviousVersionsSupported(versionsWithoutHotFixes[2]) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if IsPreviousVersionsSupported(versionsWithoutHotFixes[4]) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if IsPreviousVersionsSupported(versionsWithoutHotFixes[5]) {
|
||||
t.Fatal()
|
||||
}
|
||||
require.True(t, IsPreviousVersionsSupported(versionsWithoutHotFixes[0]))
|
||||
require.True(t, IsPreviousVersionsSupported(versionsWithoutHotFixes[1]))
|
||||
require.True(t, IsPreviousVersionsSupported(versionsWithoutHotFixes[2]))
|
||||
require.False(t, IsPreviousVersionsSupported(versionsWithoutHotFixes[4]))
|
||||
require.False(t, IsPreviousVersionsSupported(versionsWithoutHotFixes[5]))
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/store"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestComplianceStore(t *testing.T, ss store.Store) {
|
||||
@@ -35,9 +36,8 @@ func testComplianceStore(t *testing.T, ss store.Store) {
|
||||
result := <-c
|
||||
compliances := result.Data.(model.Compliances)
|
||||
|
||||
if compliances[0].Status != model.COMPLIANCE_STATUS_RUNNING && compliance2.Id != compliances[0].Id {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, model.COMPLIANCE_STATUS_RUNNING, compliances[0].Status)
|
||||
require.Equal(t, compliance2.Id, compliances[0].Id)
|
||||
|
||||
compliance2.Status = model.COMPLIANCE_STATUS_FAILED
|
||||
store.Must(ss.Compliance().Update(compliance2))
|
||||
@@ -46,17 +46,14 @@ func testComplianceStore(t *testing.T, ss store.Store) {
|
||||
result = <-c
|
||||
compliances = result.Data.(model.Compliances)
|
||||
|
||||
if compliances[0].Status != model.COMPLIANCE_STATUS_FAILED && compliance2.Id != compliances[0].Id {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, model.COMPLIANCE_STATUS_FAILED, compliances[0].Status)
|
||||
require.Equal(t, compliance2.Id, compliances[0].Id)
|
||||
|
||||
c = ss.Compliance().GetAll(0, 1)
|
||||
result = <-c
|
||||
compliances = result.Data.(model.Compliances)
|
||||
|
||||
if len(compliances) != 1 {
|
||||
t.Fatal("should only have returned 1")
|
||||
}
|
||||
require.Len(t, compliances, 1)
|
||||
|
||||
c = ss.Compliance().GetAll(1, 1)
|
||||
result = <-c
|
||||
@@ -67,9 +64,7 @@ func testComplianceStore(t *testing.T, ss store.Store) {
|
||||
}
|
||||
|
||||
rc2 := (<-ss.Compliance().Get(compliance2.Id)).Data.(*model.Compliance)
|
||||
if rc2.Status != compliance2.Status {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, compliance2.Status, rc2.Status)
|
||||
}
|
||||
|
||||
func testComplianceExport(t *testing.T, ss store.Store) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/store"
|
||||
@@ -103,9 +104,7 @@ func testActiveUserCount(t *testing.T, ss store.Store) {
|
||||
t.Fatal(result.Err)
|
||||
} else {
|
||||
count := result.Data.(int64)
|
||||
if count <= 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.True(t, count > 0, "expected count > 0, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/store"
|
||||
@@ -25,9 +26,7 @@ func testSystemStore(t *testing.T, ss store.Store) {
|
||||
result := <-ss.System().Get()
|
||||
systems := result.Data.(model.StringMap)
|
||||
|
||||
if systems[system.Name] != system.Value {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, system.Value, systems[system.Name])
|
||||
|
||||
system.Value = "value2"
|
||||
store.Must(ss.System().Update(system))
|
||||
@@ -35,15 +34,11 @@ func testSystemStore(t *testing.T, ss store.Store) {
|
||||
result2 := <-ss.System().Get()
|
||||
systems2 := result2.Data.(model.StringMap)
|
||||
|
||||
if systems2[system.Name] != system.Value {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, system.Value, systems2[system.Name])
|
||||
|
||||
result3 := <-ss.System().GetByName(system.Name)
|
||||
rsystem := result3.Data.(*model.System)
|
||||
if rsystem.Value != system.Value {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Equal(t, system.Value, rsystem.Value)
|
||||
}
|
||||
|
||||
func testSystemStoreSaveOrUpdate(t *testing.T, ss store.Store) {
|
||||
|
||||
@@ -590,10 +590,7 @@ func testTeamMembers(t *testing.T, ss store.Store) {
|
||||
t.Fatal(r1.Err)
|
||||
} else {
|
||||
ms := r1.Data.([]*model.TeamMember)
|
||||
|
||||
if len(ms) != 2 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Len(t, ms, 2)
|
||||
}
|
||||
|
||||
if r1 := <-ss.Team().GetMembers(teamId2, 0, 100); r1.Err != nil {
|
||||
@@ -601,14 +598,8 @@ func testTeamMembers(t *testing.T, ss store.Store) {
|
||||
} else {
|
||||
ms := r1.Data.([]*model.TeamMember)
|
||||
|
||||
if len(ms) != 1 {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if ms[0].UserId != m3.UserId {
|
||||
t.Fatal()
|
||||
|
||||
}
|
||||
require.Len(t, ms, 1)
|
||||
require.Equal(t, m3.UserId, ms[0].UserId)
|
||||
}
|
||||
|
||||
if r1 := <-ss.Team().GetTeamsForUser(m1.UserId); r1.Err != nil {
|
||||
@@ -616,14 +607,8 @@ func testTeamMembers(t *testing.T, ss store.Store) {
|
||||
} else {
|
||||
ms := r1.Data.([]*model.TeamMember)
|
||||
|
||||
if len(ms) != 1 {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if ms[0].TeamId != m1.TeamId {
|
||||
t.Fatal()
|
||||
|
||||
}
|
||||
require.Len(t, ms, 1)
|
||||
require.Equal(t, m1.TeamId, ms[0].TeamId)
|
||||
}
|
||||
|
||||
if r1 := <-ss.Team().RemoveMember(teamId1, m1.UserId); r1.Err != nil {
|
||||
@@ -635,14 +620,8 @@ func testTeamMembers(t *testing.T, ss store.Store) {
|
||||
} else {
|
||||
ms := r1.Data.([]*model.TeamMember)
|
||||
|
||||
if len(ms) != 1 {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
if ms[0].UserId != m2.UserId {
|
||||
t.Fatal()
|
||||
|
||||
}
|
||||
require.Len(t, ms, 1)
|
||||
require.Equal(t, m2.UserId, ms[0].UserId)
|
||||
}
|
||||
|
||||
store.Must(ss.Team().SaveMember(m1, -1))
|
||||
@@ -656,9 +635,7 @@ func testTeamMembers(t *testing.T, ss store.Store) {
|
||||
} else {
|
||||
ms := r1.Data.([]*model.TeamMember)
|
||||
|
||||
if len(ms) != 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Len(t, ms, 0)
|
||||
}
|
||||
|
||||
uid := model.NewId()
|
||||
@@ -672,9 +649,7 @@ func testTeamMembers(t *testing.T, ss store.Store) {
|
||||
} else {
|
||||
ms := r1.Data.([]*model.TeamMember)
|
||||
|
||||
if len(ms) != 2 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Len(t, ms, 2)
|
||||
}
|
||||
|
||||
if r1 := <-ss.Team().RemoveAllMembersByUser(uid); r1.Err != nil {
|
||||
@@ -686,9 +661,7 @@ func testTeamMembers(t *testing.T, ss store.Store) {
|
||||
} else {
|
||||
ms := r1.Data.([]*model.TeamMember)
|
||||
|
||||
if len(ms) != 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.Len(t, ms, 0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -246,9 +246,7 @@ func testUserCount(t *testing.T, ss store.Store) {
|
||||
t.Fatal(result.Err)
|
||||
} else {
|
||||
count := result.Data.(int64)
|
||||
if count <= 0 {
|
||||
t.Fatal()
|
||||
}
|
||||
require.False(t, count <= 0, "expected count > 0, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user