MM-37575: Handle nil value in override_username (#18057)

* MM-37575: Handle nil value in override_username

Since props is a map[string]interface{}, an ", ok" check doesn't
really help because even if a key is not present, by default, a map
will always return the default value of the value, which would be a
nil interface.

So we can just check for the value of the map instead.

https://mattermost.atlassian.net/browse/MM-37575

```release-note
NONE
```

* Fix for integer case as well

```release-note
NONE
```

* Update app/notification.go

Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>

Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>
Этот коммит содержится в:
Agniva De Sarker
2021-08-05 14:25:07 +05:30
коммит произвёл GitHub
родитель 20a73757d0
Коммит 7c22202330
2 изменённых файлов: 28 добавлений и 2 удалений

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

@@ -1068,8 +1068,10 @@ func (n *PostNotification) GetSenderName(userNameFormat string, overridesAllowed
}
if overridesAllowed && n.Channel.Type != model.ChannelTypeDirect {
if value, ok := n.Post.GetProps()["override_username"]; ok && n.Post.GetProp("from_webhook") == "true" {
return value.(string)
if value := n.Post.GetProps()["override_username"]; value != nil && n.Post.GetProp("from_webhook") == "true" {
if s, ok := value.(string); ok {
return s
}
}
}

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

@@ -1799,6 +1799,20 @@ func TestPostNotificationGetSenderName(t *testing.T) {
},
}
overriddenPost2 := &model.Post{
Props: model.StringInterface{
"override_username": nil,
"from_webhook": "true",
},
}
overriddenPost3 := &model.Post{
Props: model.StringInterface{
"override_username": 10,
"from_webhook": "true",
},
}
for name, testCase := range map[string]struct {
channel *model.Channel
post *model.Post
@@ -1841,6 +1855,16 @@ func TestPostNotificationGetSenderName(t *testing.T) {
allowOverrides: false,
expected: "@" + sender.Username,
},
"nil override_username": {
post: overriddenPost2,
allowOverrides: true,
expected: "@" + sender.Username,
},
"integer override_username": {
post: overriddenPost3,
allowOverrides: true,
expected: "@" + sender.Username,
},
} {
t.Run(name, func(t *testing.T) {
channel := defaultChannel