PLT-5427: Import ChannelMember Notify Props. (#5329)

Этот коммит содержится в:
George Goldberg
2017-02-08 16:19:38 +00:00
коммит произвёл Corey Hulen
родитель ff2a36e9c0
Коммит 8277945363
3 изменённых файлов: 104 добавлений и 35 удалений

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

@@ -66,8 +66,14 @@ type UserTeamImportData struct {
} }
type UserChannelImportData struct { type UserChannelImportData struct {
Name *string `json:"name"` Name *string `json:"name"`
Roles *string `json:"roles"` Roles *string `json:"roles"`
NotifyProps *UserChannelNotifyPropsImportData `json:"notify_props"`
}
type UserChannelNotifyPropsImportData struct {
Desktop *string `json:"desktop"`
MarkUnread *string `json:"mark_unread"`
} }
// //
@@ -472,6 +478,22 @@ func ImportUserChannels(user *model.User, team *model.Team, data *[]UserChannelI
return err return err
} }
} }
if cdata.NotifyProps != nil {
notifyProps := member.NotifyProps
if cdata.NotifyProps.Desktop != nil {
notifyProps["desktop"] = *cdata.NotifyProps.Desktop
}
if cdata.NotifyProps.MarkUnread != nil {
notifyProps["mark_unread"] = *cdata.NotifyProps.MarkUnread
}
if _, err := UpdateChannelMemberNotifyProps(notifyProps, channel.Id, user.Id); err != nil {
return err
}
}
} }
return nil return nil
@@ -563,6 +585,16 @@ func validateUserChannelsImportData(data *[]UserChannelImportData) *model.AppErr
if cdata.Roles != nil && !model.IsValidUserRoles(*cdata.Roles) { if cdata.Roles != nil && !model.IsValidUserRoles(*cdata.Roles) {
return model.NewAppError("BulkImport", "app.import.validate_user_channels_import_data.invalid_roles.error", nil, "", http.StatusBadRequest) return model.NewAppError("BulkImport", "app.import.validate_user_channels_import_data.invalid_roles.error", nil, "", http.StatusBadRequest)
} }
if cdata.NotifyProps != nil {
if cdata.NotifyProps.Desktop != nil && !model.IsChannelNotifyLevelValid(*cdata.NotifyProps.Desktop) {
return model.NewAppError("BulkImport", "app.import.validate_user_channels_import_data.invalid_notify_props_desktop.error", nil, "", http.StatusBadRequest)
}
if cdata.NotifyProps.MarkUnread != nil && !model.IsChannelMarkUnreadLevelValid(*cdata.NotifyProps.MarkUnread) {
return model.NewAppError("BulkImport", "app.import.validate_user_channels_import_data.invalid_notify_props_mark_unread.error", nil, "", http.StatusBadRequest)
}
}
} }
return nil return nil

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

@@ -5,9 +5,9 @@ package app
import ( import (
"github.com/mattermost/platform/model" "github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
"strings" "strings"
"testing" "testing"
"github.com/mattermost/platform/utils"
) )
func ptrStr(s string) *string { func ptrStr(s string) *string {
@@ -423,6 +423,31 @@ func TestImportValidateUserChannelsImportData(t *testing.T) {
if err := validateUserChannelsImportData(&data); err != nil { if err := validateUserChannelsImportData(&data); err != nil {
t.Fatal("Should have succeeded with valid roles.") t.Fatal("Should have succeeded with valid roles.")
} }
// Empty notify props.
data[0].NotifyProps = &UserChannelNotifyPropsImportData{}
if err := validateUserChannelsImportData(&data); err != nil {
t.Fatal("Should have succeeded with empty notify props.")
}
// Invalid desktop notify props.
data[0].NotifyProps.Desktop = ptrStr("invalid")
if err := validateUserChannelsImportData(&data); err == nil {
t.Fatal("Should have failed with invalid desktop notify props.")
}
// Invalid desktop notify props.
data[0].NotifyProps.Desktop = ptrStr("mention")
data[0].NotifyProps.MarkUnread = ptrStr("invalid")
if err := validateUserChannelsImportData(&data); err == nil {
t.Fatal("Should have failed with invalid mark_unread notify props.")
}
// Empty notify props.
data[0].NotifyProps.MarkUnread = ptrStr("mention")
if err := validateUserChannelsImportData(&data); err != nil {
t.Fatal("Should have succeeded with valid notify props.")
}
} }
func TestImportImportTeam(t *testing.T) { func TestImportImportTeam(t *testing.T) {
@@ -543,7 +568,7 @@ func TestImportImportChannel(t *testing.T) {
DisplayName: ptrStr("Display Name"), DisplayName: ptrStr("Display Name"),
Type: ptrStr("O"), Type: ptrStr("O"),
}, false) }, false)
team, err := GetTeamByName(teamName); team, err := GetTeamByName(teamName)
if err != nil { if err != nil {
t.Fatalf("Failed to get team from database.") t.Fatalf("Failed to get team from database.")
} }
@@ -723,7 +748,7 @@ func TestImportImportUser(t *testing.T) {
// Do a valid user in dry-run mode. // Do a valid user in dry-run mode.
data = UserImportData{ data = UserImportData{
Username: ptrStr(model.NewId()), Username: ptrStr(model.NewId()),
Email: ptrStr(model.NewId() + "@example.com"), Email: ptrStr(model.NewId() + "@example.com"),
} }
if err := ImportUser(&data, true); err != nil { if err := ImportUser(&data, true); err != nil {
t.Fatalf("Should have succeeded to import valid user.") t.Fatalf("Should have succeeded to import valid user.")
@@ -758,12 +783,12 @@ func TestImportImportUser(t *testing.T) {
// Do a valid user in apply mode. // Do a valid user in apply mode.
username := model.NewId() username := model.NewId()
data = UserImportData{ data = UserImportData{
Username: &username, Username: &username,
Email: ptrStr(model.NewId() + "@example.com"), Email: ptrStr(model.NewId() + "@example.com"),
Nickname: ptrStr(model.NewId()), Nickname: ptrStr(model.NewId()),
FirstName: ptrStr(model.NewId()), FirstName: ptrStr(model.NewId()),
LastName: ptrStr(model.NewId()), LastName: ptrStr(model.NewId()),
Position: ptrStr(model.NewId()), Position: ptrStr(model.NewId()),
} }
if err := ImportUser(&data, false); err != nil { if err := ImportUser(&data, false); err != nil {
t.Fatalf("Should have succeeded to import valid user.") t.Fatalf("Should have succeeded to import valid user.")
@@ -771,7 +796,7 @@ func TestImportImportUser(t *testing.T) {
// Check that one more user is in the DB. // Check that one more user is in the DB.
if r := <-Srv.Store.User().GetTotalUsersCount(); r.Err == nil { if r := <-Srv.Store.User().GetTotalUsersCount(); r.Err == nil {
if r.Data.(int64) != userCount + 1 { if r.Data.(int64) != userCount+1 {
t.Fatalf("Unexpected number of users") t.Fatalf("Unexpected number of users")
} }
} else { } else {
@@ -790,7 +815,7 @@ func TestImportImportUser(t *testing.T) {
t.Fatalf("Expected Auth Service to be empty.") t.Fatalf("Expected Auth Service to be empty.")
} }
if ! (user.AuthData == nil || *user.AuthData == "") { if !(user.AuthData == nil || *user.AuthData == "") {
t.Fatalf("Expected AuthData to be empty.") t.Fatalf("Expected AuthData to be empty.")
} }
@@ -827,7 +852,7 @@ func TestImportImportUser(t *testing.T) {
// Check user count the same. // Check user count the same.
if r := <-Srv.Store.User().GetTotalUsersCount(); r.Err == nil { if r := <-Srv.Store.User().GetTotalUsersCount(); r.Err == nil {
if r.Data.(int64) != userCount + 1 { if r.Data.(int64) != userCount+1 {
t.Fatalf("Unexpected number of users") t.Fatalf("Unexpected number of users")
} }
} else { } else {
@@ -846,7 +871,7 @@ func TestImportImportUser(t *testing.T) {
t.Fatalf("Expected Auth Service to be ldap \"%v\"", user.AuthService) t.Fatalf("Expected Auth Service to be ldap \"%v\"", user.AuthService)
} }
if ! (user.AuthData == data.AuthData || *user.AuthData == *data.AuthData) { if !(user.AuthData == data.AuthData || *user.AuthData == *data.AuthData) {
t.Fatalf("Expected AuthData to be set.") t.Fatalf("Expected AuthData to be set.")
} }
@@ -874,31 +899,31 @@ func TestImportImportUser(t *testing.T) {
DisplayName: ptrStr("Display Name"), DisplayName: ptrStr("Display Name"),
Type: ptrStr("O"), Type: ptrStr("O"),
}, false) }, false)
team, err := GetTeamByName(teamName); team, err := GetTeamByName(teamName)
if err != nil { if err != nil {
t.Fatalf("Failed to get team from database.") t.Fatalf("Failed to get team from database.")
} }
channelName := model.NewId() channelName := model.NewId()
ImportChannel(&ChannelImportData{ ImportChannel(&ChannelImportData{
Team: &teamName, Team: &teamName,
Name: &channelName, Name: &channelName,
DisplayName: ptrStr("Display Name"), DisplayName: ptrStr("Display Name"),
Type: ptrStr("O"), Type: ptrStr("O"),
}, false) }, false)
channel, err := GetChannelByName(channelName, team.Id); channel, err := GetChannelByName(channelName, team.Id)
if err != nil { if err != nil {
t.Fatalf("Failed to get channel from database.") t.Fatalf("Failed to get channel from database.")
} }
username = model.NewId() username = model.NewId()
data = UserImportData{ data = UserImportData{
Username: &username, Username: &username,
Email: ptrStr(model.NewId() + "@example.com"), Email: ptrStr(model.NewId() + "@example.com"),
Nickname: ptrStr(model.NewId()), Nickname: ptrStr(model.NewId()),
FirstName: ptrStr(model.NewId()), FirstName: ptrStr(model.NewId()),
LastName: ptrStr(model.NewId()), LastName: ptrStr(model.NewId()),
Position: ptrStr(model.NewId()), Position: ptrStr(model.NewId()),
} }
teamMembers, err := GetTeamMembers(team.Id, 0, 1000) teamMembers, err := GetTeamMembers(team.Id, 0, 1000)
@@ -1076,7 +1101,7 @@ func TestImportImportUser(t *testing.T) {
// Check only new team member object created because dry run mode. // Check only new team member object created because dry run mode.
if tmc, err := GetTeamMembers(team.Id, 0, 1000); err != nil { if tmc, err := GetTeamMembers(team.Id, 0, 1000); err != nil {
t.Fatalf("Failed to get Team Member Count") t.Fatalf("Failed to get Team Member Count")
} else if len(tmc) != teamMemberCount + 1 { } else if len(tmc) != teamMemberCount+1 {
t.Fatalf("Number of team members not as expected") t.Fatalf("Number of team members not as expected")
} }
@@ -1087,7 +1112,7 @@ func TestImportImportUser(t *testing.T) {
} }
// Check team member properties. // Check team member properties.
user, err := GetUserByUsername(username); user, err := GetUserByUsername(username)
if err != nil { if err != nil {
t.Fatalf("Failed to get user from database.") t.Fatalf("Failed to get user from database.")
} }
@@ -1115,32 +1140,36 @@ func TestImportImportUser(t *testing.T) {
// Check only new channel member object created because dry run mode. // Check only new channel member object created because dry run mode.
if tmc, err := GetTeamMembers(team.Id, 0, 1000); err != nil { if tmc, err := GetTeamMembers(team.Id, 0, 1000); err != nil {
t.Fatalf("Failed to get Team Member Count") t.Fatalf("Failed to get Team Member Count")
} else if len(tmc) != teamMemberCount + 1 { } else if len(tmc) != teamMemberCount+1 {
t.Fatalf("Number of team members not as expected") t.Fatalf("Number of team members not as expected")
} }
if cmc, err := GetChannelMemberCount(channel.Id); err != nil { if cmc, err := GetChannelMemberCount(channel.Id); err != nil {
t.Fatalf("Failed to get Channel Member Count") t.Fatalf("Failed to get Channel Member Count")
} else if cmc != channelMemberCount + 1 { } else if cmc != channelMemberCount+1 {
t.Fatalf("Number of channel members not as expected") t.Fatalf("Number of channel members not as expected")
} }
// Check channel member properties. // Check channel member properties.
if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil { if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil {
t.Fatalf("Failed to get channel member from database.") t.Fatalf("Failed to get channel member from database.")
} else if channelMember.Roles != "channel_user" { } else if channelMember.Roles != "channel_user" || channelMember.NotifyProps["desktop"] != "default" || channelMember.NotifyProps["mark_unread"] != "all" {
t.Fatalf("Channel member properties not as expected") t.Fatalf("Channel member properties not as expected")
} }
// Test with the properties of the team and channel membership changed. // Test with the properties of the team and channel membership changed.
data.Teams = &[]UserTeamImportData{ data.Teams = &[]UserTeamImportData{
{ {
Name: &teamName, Name: &teamName,
Roles: ptrStr("team_user team_admin"), Roles: ptrStr("team_user team_admin"),
Channels: &[]UserChannelImportData{ Channels: &[]UserChannelImportData{
{ {
Name: &channelName, Name: &channelName,
Roles: ptrStr("channel_user channel_admin"), Roles: ptrStr("channel_user channel_admin"),
NotifyProps: &UserChannelNotifyPropsImportData{
Desktop: ptrStr("mention"),
MarkUnread: ptrStr("mention"),
},
}, },
}, },
}, },
@@ -1157,21 +1186,21 @@ func TestImportImportUser(t *testing.T) {
} }
if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil { if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil {
t.Fatalf("Failed to get channel member from database.") t.Fatalf("Failed to get channel member Desktop from database.")
} else if channelMember.Roles != "channel_user channel_admin" { } else if channelMember.Roles != "channel_user channel_admin" && channelMember.NotifyProps["desktop"] == "mention" && channelMember.NotifyProps["mark_unread"] == "mention" {
t.Fatalf("Channel member properties not as expected") t.Fatalf("Channel member properties not as expected")
} }
// No more new member objects. // No more new member objects.
if tmc, err := GetTeamMembers(team.Id, 0, 1000); err != nil { if tmc, err := GetTeamMembers(team.Id, 0, 1000); err != nil {
t.Fatalf("Failed to get Team Member Count") t.Fatalf("Failed to get Team Member Count")
} else if len(tmc) != teamMemberCount + 1 { } else if len(tmc) != teamMemberCount+1 {
t.Fatalf("Number of team members not as expected") t.Fatalf("Number of team members not as expected")
} }
if cmc, err := GetChannelMemberCount(channel.Id); err != nil { if cmc, err := GetChannelMemberCount(channel.Id); err != nil {
t.Fatalf("Failed to get Channel Member Count") t.Fatalf("Failed to get Channel Member Count")
} else if cmc != channelMemberCount + 1 { } else if cmc != channelMemberCount+1 {
t.Fatalf("Number of channel members not as expected") t.Fatalf("Number of channel members not as expected")
} }
} }

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

@@ -2967,6 +2967,14 @@
"id": "app.import.validate_user_channels_import_data.invalid_roles.error", "id": "app.import.validate_user_channels_import_data.invalid_roles.error",
"translation": "Invalid roles for User's Channel Membership." "translation": "Invalid roles for User's Channel Membership."
}, },
{
"id": "app.import.validate_user_channels_import_data.invalid_notify_props_desktop.error",
"translation": "Invalid Desktop NotifyProps for User's Channel Membership."
},
{
"id": "app.import.validate_user_channels_import_data.invalid_notify_props_mark_unread.error",
"translation": "Invalid MarkUnread NotifyProps for User's Channel Membership."
},
{ {
"id": "authentication.permissions.create_team_roles.description", "id": "authentication.permissions.create_team_roles.description",
"translation": "Ability to create new teams" "translation": "Ability to create new teams"