Skip to content

Commit

Permalink
Merge ddb108c into f7d5a77
Browse files Browse the repository at this point in the history
  • Loading branch information
grundleborg committed Feb 7, 2017
2 parents f7d5a77 + ddb108c commit f99a163
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 35 deletions.
36 changes: 34 additions & 2 deletions app/import.go
Expand Up @@ -66,8 +66,14 @@ type UserTeamImportData struct {
}

type UserChannelImportData struct {
Name *string `json:"name"`
Roles *string `json:"roles"`
Name *string `json:"name"`
Roles *string `json:"roles"`
NotifyProps *UserChannelNotifyPropsImportData `json:"notify_props"`
}

type UserChannelNotifyPropsImportData struct {
Desktop *string `json:"desktop"`
MarkUnread *string `json:"mark_unread"`
}

//
Expand Down Expand Up @@ -472,6 +478,22 @@ func ImportUserChannels(user *model.User, team *model.Team, data *[]UserChannelI
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
Expand Down Expand Up @@ -563,6 +585,16 @@ func validateUserChannelsImportData(data *[]UserChannelImportData) *model.AppErr
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)
}

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
Expand Down
95 changes: 62 additions & 33 deletions app/import_test.go
Expand Up @@ -5,9 +5,9 @@ package app

import (
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
"strings"
"testing"
"github.com/mattermost/platform/utils"
)

func ptrStr(s string) *string {
Expand Down Expand Up @@ -423,6 +423,31 @@ func TestImportValidateUserChannelsImportData(t *testing.T) {
if err := validateUserChannelsImportData(&data); err != nil {
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) {
Expand Down Expand Up @@ -543,7 +568,7 @@ func TestImportImportChannel(t *testing.T) {
DisplayName: ptrStr("Display Name"),
Type: ptrStr("O"),
}, false)
team, err := GetTeamByName(teamName);
team, err := GetTeamByName(teamName)
if err != nil {
t.Fatalf("Failed to get team from database.")
}
Expand Down Expand Up @@ -723,7 +748,7 @@ func TestImportImportUser(t *testing.T) {
// Do a valid user in dry-run mode.
data = UserImportData{
Username: ptrStr(model.NewId()),
Email: ptrStr(model.NewId() + "@example.com"),
Email: ptrStr(model.NewId() + "@example.com"),
}
if err := ImportUser(&data, true); err != nil {
t.Fatalf("Should have succeeded to import valid user.")
Expand Down Expand Up @@ -758,20 +783,20 @@ func TestImportImportUser(t *testing.T) {
// Do a valid user in apply mode.
username := model.NewId()
data = UserImportData{
Username: &username,
Email: ptrStr(model.NewId() + "@example.com"),
Nickname: ptrStr(model.NewId()),
Username: &username,
Email: ptrStr(model.NewId() + "@example.com"),
Nickname: ptrStr(model.NewId()),
FirstName: ptrStr(model.NewId()),
LastName: ptrStr(model.NewId()),
Position: ptrStr(model.NewId()),
LastName: ptrStr(model.NewId()),
Position: ptrStr(model.NewId()),
}
if err := ImportUser(&data, false); err != nil {
t.Fatalf("Should have succeeded to import valid user.")
}

// Check that one more user is in the DB.
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")
}
} else {
Expand All @@ -790,7 +815,7 @@ func TestImportImportUser(t *testing.T) {
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.")
}

Expand Down Expand Up @@ -827,7 +852,7 @@ func TestImportImportUser(t *testing.T) {

// Check user count the same.
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")
}
} else {
Expand All @@ -846,7 +871,7 @@ func TestImportImportUser(t *testing.T) {
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.")
}

Expand Down Expand Up @@ -874,31 +899,31 @@ func TestImportImportUser(t *testing.T) {
DisplayName: ptrStr("Display Name"),
Type: ptrStr("O"),
}, false)
team, err := GetTeamByName(teamName);
team, err := GetTeamByName(teamName)
if err != nil {
t.Fatalf("Failed to get team from database.")
}

channelName := model.NewId()
ImportChannel(&ChannelImportData{
Team: &teamName,
Name: &channelName,
Team: &teamName,
Name: &channelName,
DisplayName: ptrStr("Display Name"),
Type: ptrStr("O"),
Type: ptrStr("O"),
}, false)
channel, err := GetChannelByName(channelName, team.Id);
channel, err := GetChannelByName(channelName, team.Id)
if err != nil {
t.Fatalf("Failed to get channel from database.")
}

username = model.NewId()
data = UserImportData{
Username: &username,
Email: ptrStr(model.NewId() + "@example.com"),
Nickname: ptrStr(model.NewId()),
Username: &username,
Email: ptrStr(model.NewId() + "@example.com"),
Nickname: ptrStr(model.NewId()),
FirstName: ptrStr(model.NewId()),
LastName: ptrStr(model.NewId()),
Position: ptrStr(model.NewId()),
LastName: ptrStr(model.NewId()),
Position: ptrStr(model.NewId()),
}

teamMembers, err := GetTeamMembers(team.Id, 0, 1000)
Expand Down Expand Up @@ -1076,7 +1101,7 @@ func TestImportImportUser(t *testing.T) {
// Check only new team member object created because dry run mode.
if tmc, err := GetTeamMembers(team.Id, 0, 1000); err != nil {
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")
}

Expand All @@ -1087,7 +1112,7 @@ func TestImportImportUser(t *testing.T) {
}

// Check team member properties.
user, err := GetUserByUsername(username);
user, err := GetUserByUsername(username)
if err != nil {
t.Fatalf("Failed to get user from database.")
}
Expand Down Expand Up @@ -1115,32 +1140,36 @@ func TestImportImportUser(t *testing.T) {
// Check only new channel member object created because dry run mode.
if tmc, err := GetTeamMembers(team.Id, 0, 1000); err != nil {
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")
}

if cmc, err := GetChannelMemberCount(channel.Id); err != nil {
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")
}

// Check channel member properties.
if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil {
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")
}

// Test with the properties of the team and channel membership changed.
data.Teams = &[]UserTeamImportData{
{
Name: &teamName,
Name: &teamName,
Roles: ptrStr("team_user team_admin"),
Channels: &[]UserChannelImportData{
{
Name: &channelName,
Name: &channelName,
Roles: ptrStr("channel_user channel_admin"),
NotifyProps: &UserChannelNotifyPropsImportData{
Desktop: ptrStr("mention"),
MarkUnread: ptrStr("mention"),
},
},
},
},
Expand All @@ -1157,21 +1186,21 @@ func TestImportImportUser(t *testing.T) {
}

if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil {
t.Fatalf("Failed to get channel member from database.")
} else if channelMember.Roles != "channel_user channel_admin" {
t.Fatalf("Failed to get channel member Desktop from database.")
} 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")
}

// No more new member objects.
if tmc, err := GetTeamMembers(team.Id, 0, 1000); err != nil {
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")
}

if cmc, err := GetChannelMemberCount(channel.Id); err != nil {
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")
}
}
Expand Down
8 changes: 8 additions & 0 deletions i18n/en.json
Expand Up @@ -2967,6 +2967,14 @@
"id": "app.import.validate_user_channels_import_data.invalid_roles.error",
"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",
"translation": "Ability to create new teams"
Expand Down

0 comments on commit f99a163

Please sign in to comment.