MM-18384 - Migrate tests from "app/notification_test.go" to use testify (#13526)

* MM-18384 - Migrate tests from "app/notification_test.go" to use testify

* fix golangcibot

* fix golangcibot

* break complex comparisons into multiple simple statements

* make test cleaner

* add missing changes
Этот коммит содержится в:
Vladimir Lebedev
2020-01-06 22:15:12 +03:00
коммит произвёл Joram Wilander
родитель 88fecdb1a3
Коммит 5c6bdc357a
2 изменённых файлов: 144 добавлений и 120 удалений

Просмотреть файл

@@ -861,22 +861,18 @@ func TestGetExplicitMentionsAtHere(t *testing.T) {
for message, shouldMention := range cases { for message, shouldMention := range cases {
post := &model.Post{Message: message} post := &model.Post{Message: message}
if m := getExplicitMentions(post, nil); m.HereMentioned && !shouldMention { m := getExplicitMentions(post, nil)
t.Fatalf("shouldn't have mentioned @here with \"%v\"", message) require.False(t, m.HereMentioned && !shouldMention, "shouldn't have mentioned @here with \"%v\"")
} else if !m.HereMentioned && shouldMention { require.False(t, !m.HereMentioned && shouldMention, "should've mentioned @here with \"%v\"")
t.Fatalf("should've mentioned @here with \"%v\"", message)
}
} }
// mentioning @here and someone // mentioning @here and someone
id := model.NewId() id := model.NewId()
if m := getExplicitMentions(&model.Post{Message: "@here @user @potential"}, map[string][]string{"@user": {id}}); !m.HereMentioned { m := getExplicitMentions(&model.Post{Message: "@here @user @potential"}, map[string][]string{"@user": {id}})
t.Fatal("should've mentioned @here with \"@here @user\"") require.True(t, m.HereMentioned, "should've mentioned @here with \"@here @user\"")
} else if len(m.Mentions) != 1 || m.Mentions[id] != KeywordMention { require.Len(t, m.Mentions, 1)
t.Fatal("should've mentioned @user with \"@here @user\"") require.Equal(t, KeywordMention, m.Mentions[id], "should've mentioned @user with \"@here @user\"")
} else if len(m.OtherPotentialMentions) > 1 { require.LessOrEqual(t, len(m.OtherPotentialMentions), 1, "should've potential mentions for @potential")
t.Fatal("should've potential mentions for @potential")
}
} }
func TestAllowChannelMentions(t *testing.T) { func TestAllowChannelMentions(t *testing.T) {
@@ -947,15 +943,17 @@ func TestGetMentionKeywords(t *testing.T) {
profiles := map[string]*model.User{user1.Id: user1} profiles := map[string]*model.User{user1.Id: user1}
mentions := th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap1Off) mentions := th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap1Off)
if len(mentions) != 3 { require.Len(t, mentions, 3, "should've returned three mention keywords")
t.Fatal("should've returned three mention keywords")
} else if ids, ok := mentions["user"]; !ok || ids[0] != user1.Id { ids, ok := mentions["user"]
t.Fatal("should've returned mention key of user") require.True(t, ok)
} else if ids, ok := mentions["@user"]; !ok || ids[0] != user1.Id { require.Equal(t, user1.Id, ids[0], "should've returned mention key of user")
t.Fatal("should've returned mention key of @user") ids, ok = mentions["@user"]
} else if ids, ok := mentions["mention"]; !ok || ids[0] != user1.Id { require.True(t, ok)
t.Fatal("should've returned mention key of mention") require.Equal(t, user1.Id, ids[0], "should've returned mention key of @user")
} ids, ok = mentions["mention"]
require.True(t, ok)
require.Equal(t, user1.Id, ids[0], "should've returned mention key of mention")
// user with first name mention enabled // user with first name mention enabled
user2 := &model.User{ user2 := &model.User{
@@ -975,11 +973,11 @@ func TestGetMentionKeywords(t *testing.T) {
profiles = map[string]*model.User{user2.Id: user2} profiles = map[string]*model.User{user2.Id: user2}
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap2Off) mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap2Off)
if len(mentions) != 2 { require.Len(t, mentions, 2, "should've returned two mention keyword")
t.Fatal("should've returned two mention keyword")
} else if ids, ok := mentions["First"]; !ok || ids[0] != user2.Id { ids, ok = mentions["First"]
t.Fatal("should've returned mention key of First") require.True(t, ok)
} require.Equal(t, user2.Id, ids[0], "should've returned mention key of First")
// user with @channel/@all mentions enabled // user with @channel/@all mentions enabled
user3 := &model.User{ user3 := &model.User{
@@ -999,13 +997,13 @@ func TestGetMentionKeywords(t *testing.T) {
} }
profiles = map[string]*model.User{user3.Id: user3} profiles = map[string]*model.User{user3.Id: user3}
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap3Off) mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap3Off)
if len(mentions) != 3 { require.Len(t, mentions, 3, "should've returned three mention keywords")
t.Fatal("should've returned three mention keywords") ids, ok = mentions["@channel"]
} else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id { require.True(t, ok)
t.Fatal("should've returned mention key of @channel") require.Equal(t, user3.Id, ids[0], "should've returned mention key of @channel")
} else if ids, ok := mentions["@all"]; !ok || ids[0] != user3.Id { ids, ok = mentions["@all"]
t.Fatal("should've returned mention key of @all") require.True(t, ok)
} require.Equal(t, user3.Id, ids[0], "should've returned mention key of @all")
// Channel member notify props is set to default // Channel member notify props is set to default
channelMemberNotifyPropsMapDefault := map[string]model.StringMap{ channelMemberNotifyPropsMapDefault := map[string]model.StringMap{
@@ -1015,25 +1013,25 @@ func TestGetMentionKeywords(t *testing.T) {
} }
profiles = map[string]*model.User{user3.Id: user3} profiles = map[string]*model.User{user3.Id: user3}
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMapDefault) mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMapDefault)
if len(mentions) != 3 { require.Len(t, mentions, 3, "should've returned three mention keywords")
t.Fatal("should've returned three mention keywords") ids, ok = mentions["@channel"]
} else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id { require.True(t, ok)
t.Fatal("should've returned mention key of @channel") require.Equal(t, user3.Id, ids[0], "should've returned mention key of @channel")
} else if ids, ok := mentions["@all"]; !ok || ids[0] != user3.Id { ids, ok = mentions["@all"]
t.Fatal("should've returned mention key of @all") require.True(t, ok)
} require.Equal(t, user3.Id, ids[0], "should've returned mention key of @all")
// Channel member notify props is empty // Channel member notify props is empty
channelMemberNotifyPropsMapEmpty := map[string]model.StringMap{} channelMemberNotifyPropsMapEmpty := map[string]model.StringMap{}
profiles = map[string]*model.User{user3.Id: user3} profiles = map[string]*model.User{user3.Id: user3}
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMapEmpty) mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMapEmpty)
if len(mentions) != 3 { require.Len(t, mentions, 3, "should've returned three mention keywords")
t.Fatal("should've returned three mention keywords") ids, ok = mentions["@channel"]
} else if ids, ok := mentions["@channel"]; !ok || ids[0] != user3.Id { require.True(t, ok)
t.Fatal("should've returned mention key of @channel") require.Equal(t, user3.Id, ids[0], "should've returned mention key of @channel")
} else if ids, ok := mentions["@all"]; !ok || ids[0] != user3.Id { ids, ok = mentions["@all"]
t.Fatal("should've returned mention key of @all") require.True(t, ok)
} require.Equal(t, user3.Id, ids[0], "should've returned mention key of @all")
// Channel-wide mentions are ignored channel level // Channel-wide mentions are ignored channel level
channelMemberNotifyPropsMap3On := map[string]model.StringMap{ channelMemberNotifyPropsMap3On := map[string]model.StringMap{
@@ -1042,9 +1040,7 @@ func TestGetMentionKeywords(t *testing.T) {
}, },
} }
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap3On) mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap3On)
if len(mentions) == 0 { require.NotEmpty(t, mentions, "should've not returned any keywords")
t.Fatal("should've not returned any keywords")
}
// user with all types of mentions enabled // user with all types of mentions enabled
user4 := &model.User{ user4 := &model.User{
@@ -1067,21 +1063,25 @@ func TestGetMentionKeywords(t *testing.T) {
profiles = map[string]*model.User{user4.Id: user4} profiles = map[string]*model.User{user4.Id: user4}
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap4Off) mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap4Off)
if len(mentions) != 6 { require.Len(t, mentions, 6, "should've returned six mention keywords")
t.Fatal("should've returned six mention keywords") ids, ok = mentions["user"]
} else if ids, ok := mentions["user"]; !ok || ids[0] != user4.Id { require.True(t, ok)
t.Fatal("should've returned mention key of user") require.Equal(t, user4.Id, ids[0], "should've returned mention key of user")
} else if ids, ok := mentions["@user"]; !ok || ids[0] != user4.Id { ids, ok = mentions["@user"]
t.Fatal("should've returned mention key of @user") require.True(t, ok)
} else if ids, ok := mentions["mention"]; !ok || ids[0] != user4.Id { require.Equal(t, user4.Id, ids[0], "should've returned mention key of @user")
t.Fatal("should've returned mention key of mention") ids, ok = mentions["mention"]
} else if ids, ok := mentions["First"]; !ok || ids[0] != user4.Id { require.True(t, ok)
t.Fatal("should've returned mention key of First") require.Equal(t, user4.Id, ids[0], "should've returned mention key of mention")
} else if ids, ok := mentions["@channel"]; !ok || ids[0] != user4.Id { ids, ok = mentions["First"]
t.Fatal("should've returned mention key of @channel") require.True(t, ok)
} else if ids, ok := mentions["@all"]; !ok || ids[0] != user4.Id { require.Equal(t, user4.Id, ids[0], "should've returned mention key of First")
t.Fatal("should've returned mention key of @all") ids, ok = mentions["@channel"]
} require.True(t, ok)
require.Equal(t, user4.Id, ids[0], "should've returned mention key of @channel")
ids, ok = mentions["@all"]
require.True(t, ok)
require.Equal(t, user4.Id, ids[0], "should've returned mention key of @all")
// Channel-wide mentions are ignored on channel level // Channel-wide mentions are ignored on channel level
channelMemberNotifyPropsMap4On := map[string]model.StringMap{ channelMemberNotifyPropsMap4On := map[string]model.StringMap{
@@ -1090,18 +1090,19 @@ func TestGetMentionKeywords(t *testing.T) {
}, },
} }
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap4On) mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap4On)
if len(mentions) != 4 { require.Len(t, mentions, 4, "should've returned four mention keywords")
t.Fatal("should've returned four mention keywords") ids, ok = mentions["user"]
} else if ids, ok := mentions["user"]; !ok || ids[0] != user4.Id { require.True(t, ok)
t.Fatal("should've returned mention key of user") require.Equal(t, user4.Id, ids[0], "should've returned mention key of user")
} else if ids, ok := mentions["@user"]; !ok || ids[0] != user4.Id { ids, ok = mentions["@user"]
t.Fatal("should've returned mention key of @user") require.True(t, ok)
} else if ids, ok := mentions["mention"]; !ok || ids[0] != user4.Id { require.Equal(t, user4.Id, ids[0], "should've returned mention key of @user")
t.Fatal("should've returned mention key of mention") ids, ok = mentions["mention"]
} else if ids, ok := mentions["First"]; !ok || ids[0] != user4.Id { require.True(t, ok)
t.Fatal("should've returned mention key of First") require.Equal(t, user4.Id, ids[0], "should've returned mention key of mention")
} ids, ok = mentions["First"]
require.True(t, ok)
require.Equal(t, user4.Id, ids[0], "should've returned mention key of First")
dup_count := func(list []string) map[string]int { dup_count := func(list []string) map[string]int {
duplicate_frequency := make(map[string]int) duplicate_frequency := make(map[string]int)
@@ -1144,56 +1145,78 @@ func TestGetMentionKeywords(t *testing.T) {
}, },
} }
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap5Off) mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMap5Off)
if len(mentions) != 6 { require.Len(t, mentions, 6, "should've returned six mention keywords")
t.Fatal("should've returned six mention keywords") ids, ok = mentions["user"]
} else if ids, ok := mentions["user"]; !ok || len(ids) != 2 || (ids[0] != user1.Id && ids[1] != user1.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { require.True(t, ok)
t.Fatal("should've mentioned user1 and user4 with user") require.Len(t, ids, 2)
} else if ids := dup_count(mentions["@user"]); len(ids) != 4 || (ids[user1.Id] != 2) || (ids[user4.Id] != 2) { require.False(t, ids[0] != user1.Id && ids[1] != user1.Id, "should've mentioned user1 with user")
t.Fatal("should've mentioned user1 and user4 with @user") require.False(t, ids[0] != user4.Id && ids[1] != user4.Id, "should've mentioned user4 with user")
} else if ids, ok := mentions["mention"]; !ok || len(ids) != 2 || (ids[0] != user1.Id && ids[1] != user1.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { idsMap := dup_count(mentions["@user"])
t.Fatal("should've mentioned user1 and user4 with mention") require.True(t, ok)
} else if ids, ok := mentions["First"]; !ok || len(ids) != 2 || (ids[0] != user2.Id && ids[1] != user2.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { require.Len(t, idsMap, 4)
t.Fatal("should've mentioned user2 and user4 with First") require.Equal(t, idsMap[user1.Id], 2, "should've mentioned user1 with @user")
} else if ids, ok := mentions["@channel"]; !ok || len(ids) != 2 || (ids[0] != user3.Id && ids[1] != user3.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { require.Equal(t, idsMap[user4.Id], 2, "should've mentioned user4 with @user")
t.Fatal("should've mentioned user3 and user4 with @channel")
} else if ids, ok := mentions["@all"]; !ok || len(ids) != 2 || (ids[0] != user3.Id && ids[1] != user3.Id) || (ids[0] != user4.Id && ids[1] != user4.Id) { ids, ok = mentions["mention"]
t.Fatal("should've mentioned user3 and user4 with @all") require.True(t, ok)
} require.Len(t, ids, 2)
require.False(t, ids[0] != user1.Id && ids[1] != user1.Id, "should've mentioned user1 with mention")
require.False(t, ids[0] != user4.Id && ids[1] != user4.Id, "should've mentioned user4 with mention")
ids, ok = mentions["First"]
require.True(t, ok)
require.Len(t, ids, 2)
require.False(t, ids[0] != user2.Id && ids[1] != user2.Id, "should've mentioned user2 with First")
require.False(t, ids[0] != user4.Id && ids[1] != user4.Id, "should've mentioned user4 with First")
ids, ok = mentions["@channel"]
require.True(t, ok)
require.Len(t, ids, 2)
require.False(t, ids[0] != user3.Id && ids[1] != user3.Id, "should've mentioned user3 with @channel")
require.False(t, ids[0] != user4.Id && ids[1] != user4.Id, "should've mentioned user4 with @channel")
ids, ok = mentions["@all"]
require.True(t, ok)
require.Len(t, ids, 2)
require.False(t, ids[0] != user3.Id && ids[1] != user3.Id, "should've mentioned user3 with @all")
require.False(t, ids[0] != user4.Id && ids[1] != user4.Id, "should've mentioned user4 with @all")
// multiple users and more than MaxNotificationsPerChannel // multiple users and more than MaxNotificationsPerChannel
mentions = th.App.getMentionKeywordsInChannel(profiles, false, channelMemberNotifyPropsMap4Off) mentions = th.App.getMentionKeywordsInChannel(profiles, false, channelMemberNotifyPropsMap4Off)
if len(mentions) != 4 { require.Len(t, mentions, 4, "should've returned four mention keywords")
t.Fatal("should've returned four mention keywords", mentions) _, ok = mentions["@channel"]
} else if _, ok := mentions["@channel"]; ok { require.False(t, ok, "should not have mentioned any user with @channel")
t.Fatal("should not have mentioned any user with @channel") _, ok = mentions["@all"]
} else if _, ok := mentions["@all"]; ok { require.False(t, ok, "should not have mentioned any user with @all")
t.Fatal("should not have mentioned any user with @all") _, ok = mentions["@here"]
} else if _, ok := mentions["@here"]; ok { require.False(t, ok, "should not have mentioned any user with @here")
t.Fatal("should not have mentioned any user with @here")
}
// no special mentions // no special mentions
profiles = map[string]*model.User{ profiles = map[string]*model.User{
user1.Id: user1, user1.Id: user1,
} }
mentions = th.App.getMentionKeywordsInChannel(profiles, false, channelMemberNotifyPropsMap4Off) mentions = th.App.getMentionKeywordsInChannel(profiles, false, channelMemberNotifyPropsMap4Off)
if len(mentions) != 3 { require.Len(t, mentions, 3, "should've returned three mention keywords")
t.Fatal("should've returned three mention keywords") ids, ok = mentions["user"]
} else if ids, ok := mentions["user"]; !ok || len(ids) != 1 || ids[0] != user1.Id { require.True(t, ok)
t.Fatal("should've mentioned user1 with user") require.Len(t, ids, 1)
} else if ids, ok := mentions["@user"]; !ok || len(ids) != 2 || ids[0] != user1.Id || ids[1] != user1.Id { require.Equal(t, user1.Id, ids[0], "should've mentioned user1 with user")
t.Fatal("should've mentioned user1 twice with @user") ids, ok = mentions["@user"]
} else if ids, ok := mentions["mention"]; !ok || len(ids) != 1 || ids[0] != user1.Id {
t.Fatal("should've mentioned user1 with mention") require.True(t, ok)
} else if _, ok := mentions["First"]; ok { require.Len(t, ids, 2)
t.Fatal("should not have mentioned user1 with First") require.Equal(t, user1.Id, ids[0], "should've mentioned user1 twice with @user")
} else if _, ok := mentions["@channel"]; ok { require.Equal(t, user1.Id, ids[1], "should've mentioned user1 twice with @user")
t.Fatal("should not have mentioned any user with @channel")
} else if _, ok := mentions["@all"]; ok { ids, ok = mentions["mention"]
t.Fatal("should not have mentioned any user with @all") require.True(t, ok)
} else if _, ok := mentions["@here"]; ok { require.Len(t, ids, 1)
t.Fatal("should not have mentioned any user with @here") require.Equal(t, user1.Id, ids[0], "should've mentioned user1 with user")
}
_, ok = mentions["First"]
require.False(t, ok, "should not have mentioned user1 with First")
_, ok = mentions["@channel"]
require.False(t, ok, "should not have mentioned any user with @channel")
_, ok = mentions["@all"]
require.False(t, ok, "should not have mentioned any user with @all")
_, ok = mentions["@here"]
require.False(t, ok, "should not have mentioned any user with @here")
// user with empty mention keys // user with empty mention keys
userNoMentionKeys := &model.User{ userNoMentionKeys := &model.User{
@@ -1214,7 +1237,7 @@ func TestGetMentionKeywords(t *testing.T) {
profiles = map[string]*model.User{userNoMentionKeys.Id: userNoMentionKeys} profiles = map[string]*model.User{userNoMentionKeys.Id: userNoMentionKeys}
mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMapEmptyOff) mentions = th.App.getMentionKeywordsInChannel(profiles, true, channelMemberNotifyPropsMapEmptyOff)
assert.Equal(t, 1, len(mentions), "should've returned one metion keyword") assert.Equal(t, 1, len(mentions), "should've returned one metion keyword")
ids, ok := mentions["@user"] ids, ok = mentions["@user"]
assert.True(t, ok) assert.True(t, ok)
assert.Equal(t, userNoMentionKeys.Id, ids[0], "should've returned mention key of @user") assert.Equal(t, userNoMentionKeys.Id, ids[0], "should've returned mention key of @user")
} }

1
go.sum
Просмотреть файл

@@ -252,6 +252,7 @@ github.com/mattermost/gosaml2 v0.3.2 h1:kq2dY5qUe6fPPHra171GVlgo+ycBsEog0gZMetxL
github.com/mattermost/gosaml2 v0.3.2/go.mod h1:Z429EIOiEi9kbq6yHoApfzlcXpa6dzRDc6pO+Vy2Ksk= github.com/mattermost/gosaml2 v0.3.2/go.mod h1:Z429EIOiEi9kbq6yHoApfzlcXpa6dzRDc6pO+Vy2Ksk=
github.com/mattermost/ldap v0.0.0-20191128190019-9f62ba4b8d4d h1:2DV7VIlEv6J5R5o6tUcb3ZMKJYeeZuWZL7Rv1m23TgQ= github.com/mattermost/ldap v0.0.0-20191128190019-9f62ba4b8d4d h1:2DV7VIlEv6J5R5o6tUcb3ZMKJYeeZuWZL7Rv1m23TgQ=
github.com/mattermost/ldap v0.0.0-20191128190019-9f62ba4b8d4d/go.mod h1:HLbgMEI5K131jpxGazJ97AxfPDt31osq36YS1oxFQPQ= github.com/mattermost/ldap v0.0.0-20191128190019-9f62ba4b8d4d/go.mod h1:HLbgMEI5K131jpxGazJ97AxfPDt31osq36YS1oxFQPQ=
github.com/mattermost/ldap v3.0.4+incompatible h1:SOeNnz+JNR+foQ3yHkYqijb9MLPhXN2BZP/PdX23VDU=
github.com/mattermost/rsc v0.0.0-20160330161541-bbaefb05eaa0 h1:G9tL6JXRBMzjuD1kkBtcnd42kUiT6QDwxfFYu7adM6o= github.com/mattermost/rsc v0.0.0-20160330161541-bbaefb05eaa0 h1:G9tL6JXRBMzjuD1kkBtcnd42kUiT6QDwxfFYu7adM6o=
github.com/mattermost/rsc v0.0.0-20160330161541-bbaefb05eaa0/go.mod h1:nV5bfVpT//+B1RPD2JvRnxbkLmJEYXmRaaVl15fsXjs= github.com/mattermost/rsc v0.0.0-20160330161541-bbaefb05eaa0/go.mod h1:nV5bfVpT//+B1RPD2JvRnxbkLmJEYXmRaaVl15fsXjs=
github.com/mattermost/viper v1.0.4 h1:cMYOz4PhguscGSPxrSokUtib5HrG4gCpiUh27wyA3d0= github.com/mattermost/viper v1.0.4 h1:cMYOz4PhguscGSPxrSokUtib5HrG4gCpiUh27wyA3d0=