mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
1409 lines
42 KiB
Go
1409 lines
42 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
"github.com/mattermost/mattermost-server/utils/fileutils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestImportValidateSchemeImportData(t *testing.T) {
|
|
// Test with minimum required valid properties and team scope.
|
|
data := SchemeImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
Scope: ptrStr("team"),
|
|
DefaultTeamAdminRole: &RoleImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
Permissions: &[]string{"invite_user"},
|
|
},
|
|
DefaultTeamUserRole: &RoleImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
Permissions: &[]string{"invite_user"},
|
|
},
|
|
DefaultChannelAdminRole: &RoleImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
Permissions: &[]string{"invite_user"},
|
|
},
|
|
DefaultChannelUserRole: &RoleImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
Permissions: &[]string{"invite_user"},
|
|
},
|
|
}
|
|
if err := validateSchemeImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.", err)
|
|
}
|
|
|
|
// Test with various invalid names.
|
|
data.Name = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid name.")
|
|
}
|
|
|
|
data.Name = ptrStr("")
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid name.")
|
|
}
|
|
|
|
data.Name = ptrStr(strings.Repeat("1234567890", 100))
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid name.")
|
|
}
|
|
|
|
data.Name = ptrStr("name")
|
|
// Test with invalid display name.
|
|
data.DisplayName = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid display name.")
|
|
}
|
|
|
|
data.DisplayName = ptrStr("")
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid display name.")
|
|
}
|
|
|
|
data.DisplayName = ptrStr(strings.Repeat("1234567890", 100))
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid display name.")
|
|
}
|
|
|
|
data.DisplayName = ptrStr("display name")
|
|
|
|
// Test with various missing roles.
|
|
data.DefaultTeamAdminRole = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing role.")
|
|
}
|
|
|
|
data.DefaultTeamAdminRole = &RoleImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
Permissions: &[]string{"invite_user"},
|
|
}
|
|
data.DefaultTeamUserRole = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing role.")
|
|
}
|
|
|
|
data.DefaultTeamUserRole = &RoleImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
Permissions: &[]string{"invite_user"},
|
|
}
|
|
data.DefaultChannelAdminRole = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing role.")
|
|
}
|
|
|
|
data.DefaultChannelAdminRole = &RoleImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
Permissions: &[]string{"invite_user"},
|
|
}
|
|
data.DefaultChannelUserRole = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing role.")
|
|
}
|
|
|
|
data.DefaultChannelUserRole = &RoleImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
Permissions: &[]string{"invite_user"},
|
|
}
|
|
|
|
// Test with various invalid roles.
|
|
data.DefaultTeamAdminRole.Name = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid role.")
|
|
}
|
|
|
|
data.DefaultTeamAdminRole.Name = ptrStr("name")
|
|
data.DefaultTeamUserRole.Name = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid role.")
|
|
}
|
|
|
|
data.DefaultTeamUserRole.Name = ptrStr("name")
|
|
data.DefaultChannelAdminRole.Name = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid role.")
|
|
}
|
|
|
|
data.DefaultChannelAdminRole.Name = ptrStr("name")
|
|
data.DefaultChannelUserRole.Name = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid role.")
|
|
}
|
|
|
|
data.DefaultChannelUserRole.Name = ptrStr("name")
|
|
|
|
// Change to a Channel scope role, and check with missing or extra roles again.
|
|
data.Scope = ptrStr("channel")
|
|
data.DefaultTeamAdminRole = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to spurious role.")
|
|
}
|
|
|
|
data.DefaultTeamAdminRole = &RoleImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
Permissions: &[]string{"invite_user"},
|
|
}
|
|
data.DefaultTeamUserRole = nil
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to spurious role.")
|
|
}
|
|
|
|
data.DefaultTeamAdminRole = nil
|
|
if err := validateSchemeImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded.")
|
|
}
|
|
|
|
// Test with all combinations of optional parameters.
|
|
data.Description = ptrStr(strings.Repeat("1234567890", 1024))
|
|
if err := validateSchemeImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid description.")
|
|
}
|
|
|
|
data.Description = ptrStr("description")
|
|
if err := validateSchemeImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded.")
|
|
}
|
|
}
|
|
|
|
func TestImportValidateRoleImportData(t *testing.T) {
|
|
// Test with minimum required valid properties.
|
|
data := RoleImportData{
|
|
Name: ptrStr("name"),
|
|
DisplayName: ptrStr("display name"),
|
|
}
|
|
if err := validateRoleImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.", err)
|
|
}
|
|
|
|
// Test with various invalid names.
|
|
data.Name = nil
|
|
if err := validateRoleImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid name.")
|
|
}
|
|
|
|
data.Name = ptrStr("")
|
|
if err := validateRoleImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid name.")
|
|
}
|
|
|
|
data.Name = ptrStr(strings.Repeat("1234567890", 100))
|
|
if err := validateRoleImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid name.")
|
|
}
|
|
|
|
data.Name = ptrStr("name")
|
|
// Test with invalid display name.
|
|
data.DisplayName = nil
|
|
if err := validateRoleImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid display name.")
|
|
}
|
|
|
|
data.DisplayName = ptrStr("")
|
|
if err := validateRoleImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid display name.")
|
|
}
|
|
|
|
data.DisplayName = ptrStr(strings.Repeat("1234567890", 100))
|
|
if err := validateRoleImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid display name.")
|
|
}
|
|
|
|
data.DisplayName = ptrStr("display name")
|
|
|
|
// Test with various valid/invalid permissions.
|
|
data.Permissions = &[]string{}
|
|
if err := validateRoleImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.", err)
|
|
}
|
|
|
|
data.Permissions = &[]string{"invite_user", "add_user_to_team"}
|
|
if err := validateRoleImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.", err)
|
|
}
|
|
|
|
data.Permissions = &[]string{"invite_user", "add_user_to_team", "derp"}
|
|
if err := validateRoleImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to invalid permission.", err)
|
|
}
|
|
|
|
data.Permissions = &[]string{"invite_user", "add_user_to_team"}
|
|
|
|
// Test with various valid/invalid descriptions.
|
|
data.Description = ptrStr(strings.Repeat("1234567890", 1024))
|
|
if err := validateRoleImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to invalid description.", err)
|
|
}
|
|
|
|
data.Description = ptrStr("description")
|
|
if err := validateRoleImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.", err)
|
|
}
|
|
}
|
|
|
|
func TestImportValidateTeamImportData(t *testing.T) {
|
|
|
|
// Test with minimum required valid properties.
|
|
data := TeamImportData{
|
|
Name: ptrStr("teamname"),
|
|
DisplayName: ptrStr("Display Name"),
|
|
Type: ptrStr("O"),
|
|
}
|
|
if err := validateTeamImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Test with various invalid names.
|
|
data = TeamImportData{
|
|
DisplayName: ptrStr("Display Name"),
|
|
Type: ptrStr("O"),
|
|
}
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing name.")
|
|
}
|
|
|
|
data.Name = ptrStr(strings.Repeat("abcdefghij", 7))
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to too long name.")
|
|
}
|
|
|
|
data.Name = ptrStr("login")
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to reserved word in name.")
|
|
}
|
|
|
|
data.Name = ptrStr("Test::''ASD")
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to non alphanum characters in name.")
|
|
}
|
|
|
|
data.Name = ptrStr("A")
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to short name.")
|
|
}
|
|
|
|
// Test team various invalid display names.
|
|
data = TeamImportData{
|
|
Name: ptrStr("teamname"),
|
|
Type: ptrStr("O"),
|
|
}
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing display_name.")
|
|
}
|
|
|
|
data.DisplayName = ptrStr("")
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to empty display_name.")
|
|
}
|
|
|
|
data.DisplayName = ptrStr(strings.Repeat("abcdefghij", 7))
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to too long display_name.")
|
|
}
|
|
|
|
// Test with various valid and invalid types.
|
|
data = TeamImportData{
|
|
Name: ptrStr("teamname"),
|
|
DisplayName: ptrStr("Display Name"),
|
|
}
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing type.")
|
|
}
|
|
|
|
data.Type = ptrStr("A")
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid type.")
|
|
}
|
|
|
|
data.Type = ptrStr("I")
|
|
if err := validateTeamImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with valid type.")
|
|
}
|
|
|
|
// Test with all the combinations of optional parameters.
|
|
data = TeamImportData{
|
|
Name: ptrStr("teamname"),
|
|
DisplayName: ptrStr("Display Name"),
|
|
Type: ptrStr("O"),
|
|
Description: ptrStr("The team description."),
|
|
AllowOpenInvite: ptrBool(true),
|
|
}
|
|
if err := validateTeamImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with valid optional properties.")
|
|
}
|
|
|
|
data.AllowOpenInvite = ptrBool(false)
|
|
if err := validateTeamImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with allow open invites false.")
|
|
}
|
|
|
|
data.Description = ptrStr(strings.Repeat("abcdefghij ", 26))
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to too long description.")
|
|
}
|
|
|
|
// Test with an empty scheme name.
|
|
data.Description = ptrStr("abcdefg")
|
|
data.Scheme = ptrStr("")
|
|
if err := validateTeamImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to empty scheme name.")
|
|
}
|
|
|
|
// Test with a valid scheme name.
|
|
data.Scheme = ptrStr("abcdefg")
|
|
if err := validateTeamImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with valid scheme name.")
|
|
}
|
|
}
|
|
|
|
func TestImportValidateChannelImportData(t *testing.T) {
|
|
|
|
// Test with minimum required valid properties.
|
|
data := ChannelImportData{
|
|
Team: ptrStr("teamname"),
|
|
Name: ptrStr("channelname"),
|
|
DisplayName: ptrStr("Display Name"),
|
|
Type: ptrStr("O"),
|
|
}
|
|
if err := validateChannelImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Test with missing team.
|
|
data = ChannelImportData{
|
|
Name: ptrStr("channelname"),
|
|
DisplayName: ptrStr("Display Name"),
|
|
Type: ptrStr("O"),
|
|
}
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing team.")
|
|
}
|
|
|
|
// Test with various invalid names.
|
|
data = ChannelImportData{
|
|
Team: ptrStr("teamname"),
|
|
DisplayName: ptrStr("Display Name"),
|
|
Type: ptrStr("O"),
|
|
}
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing name.")
|
|
}
|
|
|
|
data.Name = ptrStr(strings.Repeat("abcdefghij", 7))
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to too long name.")
|
|
}
|
|
|
|
data.Name = ptrStr("Test::''ASD")
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to non alphanum characters in name.")
|
|
}
|
|
|
|
data.Name = ptrStr("A")
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to short name.")
|
|
}
|
|
|
|
// Test team various invalid display names.
|
|
data = ChannelImportData{
|
|
Team: ptrStr("teamname"),
|
|
Name: ptrStr("channelname"),
|
|
Type: ptrStr("O"),
|
|
}
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing display_name.")
|
|
}
|
|
|
|
data.DisplayName = ptrStr("")
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to empty display_name.")
|
|
}
|
|
|
|
data.DisplayName = ptrStr(strings.Repeat("abcdefghij", 7))
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to too long display_name.")
|
|
}
|
|
|
|
// Test with various valid and invalid types.
|
|
data = ChannelImportData{
|
|
Team: ptrStr("teamname"),
|
|
Name: ptrStr("channelname"),
|
|
DisplayName: ptrStr("Display Name"),
|
|
}
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to missing type.")
|
|
}
|
|
|
|
data.Type = ptrStr("A")
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid type.")
|
|
}
|
|
|
|
data.Type = ptrStr("P")
|
|
if err := validateChannelImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with valid type.")
|
|
}
|
|
|
|
// Test with all the combinations of optional parameters.
|
|
data = ChannelImportData{
|
|
Team: ptrStr("teamname"),
|
|
Name: ptrStr("channelname"),
|
|
DisplayName: ptrStr("Display Name"),
|
|
Type: ptrStr("O"),
|
|
Header: ptrStr("Channel Header Here"),
|
|
Purpose: ptrStr("Channel Purpose Here"),
|
|
}
|
|
if err := validateChannelImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with valid optional properties.")
|
|
}
|
|
|
|
data.Header = ptrStr(strings.Repeat("abcdefghij ", 103))
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to too long header.")
|
|
}
|
|
|
|
data.Header = ptrStr("Channel Header Here")
|
|
data.Purpose = ptrStr(strings.Repeat("abcdefghij ", 26))
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to too long purpose.")
|
|
}
|
|
|
|
// Test with an empty scheme name.
|
|
data.Purpose = ptrStr("abcdefg")
|
|
data.Scheme = ptrStr("")
|
|
if err := validateChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to empty scheme name.")
|
|
}
|
|
|
|
// Test with a valid scheme name.
|
|
data.Scheme = ptrStr("abcdefg")
|
|
if err := validateChannelImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with valid scheme name.")
|
|
}
|
|
}
|
|
|
|
func TestImportValidateUserImportData(t *testing.T) {
|
|
|
|
// Test with minimum required valid properties.
|
|
data := UserImportData{
|
|
Username: ptrStr("bob"),
|
|
Email: ptrStr("bob@example.com"),
|
|
}
|
|
if err := validateUserImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Invalid Usernames.
|
|
data.Username = nil
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to nil Username.")
|
|
}
|
|
|
|
data.Username = ptrStr("")
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to 0 length Username.")
|
|
}
|
|
|
|
data.Username = ptrStr(strings.Repeat("abcdefghij", 7))
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to too long Username.")
|
|
}
|
|
|
|
data.Username = ptrStr("i am a username with spaces and !!!")
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to invalid characters in Username.")
|
|
}
|
|
|
|
data.Username = ptrStr("bob")
|
|
|
|
// Unexisting Picture Image
|
|
data.ProfileImage = ptrStr("not-existing-file")
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to not existing profile image file.")
|
|
}
|
|
data.ProfileImage = nil
|
|
|
|
// Invalid Emails
|
|
data.Email = nil
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to nil Email.")
|
|
}
|
|
|
|
data.Email = ptrStr("")
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to 0 length Email.")
|
|
}
|
|
|
|
data.Email = ptrStr(strings.Repeat("abcdefghij", 13))
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to too long Email.")
|
|
}
|
|
|
|
data.Email = ptrStr("bob@example.com")
|
|
|
|
// Empty AuthService indicates user/password auth.
|
|
data.AuthService = ptrStr("")
|
|
checkNoError(t, validateUserImportData(&data))
|
|
|
|
data.AuthService = ptrStr("saml")
|
|
data.AuthData = ptrStr(strings.Repeat("abcdefghij", 15))
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to too long auth data.")
|
|
}
|
|
|
|
data.AuthData = ptrStr("bobbytables")
|
|
if err := validateUserImportData(&data); err != nil {
|
|
t.Fatal("Validation should have succeeded with valid auth service and auth data.")
|
|
}
|
|
|
|
// Test a valid User with all fields populated.
|
|
testsDir, _ := fileutils.FindDir("tests")
|
|
data = UserImportData{
|
|
ProfileImage: ptrStr(filepath.Join(testsDir, "test.png")),
|
|
Username: ptrStr("bob"),
|
|
Email: ptrStr("bob@example.com"),
|
|
AuthService: ptrStr("ldap"),
|
|
AuthData: ptrStr("bob"),
|
|
Nickname: ptrStr("BobNick"),
|
|
FirstName: ptrStr("Bob"),
|
|
LastName: ptrStr("Blob"),
|
|
Position: ptrStr("The Boss"),
|
|
Roles: ptrStr("system_user"),
|
|
Locale: ptrStr("en"),
|
|
}
|
|
if err := validateUserImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Test various invalid optional field values.
|
|
data.Nickname = ptrStr(strings.Repeat("abcdefghij", 7))
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to too long Nickname.")
|
|
}
|
|
data.Nickname = ptrStr("BobNick")
|
|
|
|
data.FirstName = ptrStr(strings.Repeat("abcdefghij", 7))
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to too long First Name.")
|
|
}
|
|
data.FirstName = ptrStr("Bob")
|
|
|
|
data.LastName = ptrStr(strings.Repeat("abcdefghij", 7))
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to too long Last name.")
|
|
}
|
|
data.LastName = ptrStr("Blob")
|
|
|
|
data.Position = ptrStr(strings.Repeat("abcdefghij", 13))
|
|
if err := validateUserImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to too long Position.")
|
|
}
|
|
data.Position = ptrStr("The Boss")
|
|
|
|
data.Roles = nil
|
|
if err := validateUserImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
data.Roles = ptrStr("")
|
|
if err := validateUserImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
data.Roles = ptrStr("system_user")
|
|
|
|
// Try various valid/invalid notify props.
|
|
data.NotifyProps = &UserNotifyPropsImportData{}
|
|
|
|
data.NotifyProps.Desktop = ptrStr("invalid")
|
|
checkError(t, validateUserImportData(&data))
|
|
|
|
data.NotifyProps.Desktop = ptrStr(model.USER_NOTIFY_ALL)
|
|
data.NotifyProps.DesktopSound = ptrStr("invalid")
|
|
checkError(t, validateUserImportData(&data))
|
|
|
|
data.NotifyProps.DesktopSound = ptrStr("true")
|
|
data.NotifyProps.Email = ptrStr("invalid")
|
|
checkError(t, validateUserImportData(&data))
|
|
|
|
data.NotifyProps.Email = ptrStr("true")
|
|
data.NotifyProps.Mobile = ptrStr("invalid")
|
|
checkError(t, validateUserImportData(&data))
|
|
|
|
data.NotifyProps.Mobile = ptrStr(model.USER_NOTIFY_ALL)
|
|
data.NotifyProps.MobilePushStatus = ptrStr("invalid")
|
|
checkError(t, validateUserImportData(&data))
|
|
|
|
data.NotifyProps.MobilePushStatus = ptrStr(model.STATUS_ONLINE)
|
|
data.NotifyProps.ChannelTrigger = ptrStr("invalid")
|
|
checkError(t, validateUserImportData(&data))
|
|
|
|
data.NotifyProps.ChannelTrigger = ptrStr("true")
|
|
data.NotifyProps.CommentsTrigger = ptrStr("invalid")
|
|
checkError(t, validateUserImportData(&data))
|
|
|
|
data.NotifyProps.CommentsTrigger = ptrStr(model.COMMENTS_NOTIFY_ROOT)
|
|
data.NotifyProps.MentionKeys = ptrStr("valid")
|
|
checkNoError(t, validateUserImportData(&data))
|
|
|
|
//Test the emai batching interval validators
|
|
//Happy paths
|
|
data.EmailInterval = ptrStr("immediately")
|
|
checkNoError(t, validateUserImportData(&data))
|
|
|
|
data.EmailInterval = ptrStr("fifteen")
|
|
checkNoError(t, validateUserImportData(&data))
|
|
|
|
data.EmailInterval = ptrStr("hour")
|
|
checkNoError(t, validateUserImportData(&data))
|
|
|
|
//Invalid values
|
|
data.EmailInterval = ptrStr("invalid")
|
|
checkError(t, validateUserImportData(&data))
|
|
|
|
data.EmailInterval = ptrStr("")
|
|
checkError(t, validateUserImportData(&data))
|
|
}
|
|
|
|
func TestImportValidateUserTeamsImportData(t *testing.T) {
|
|
|
|
// Invalid Name.
|
|
data := []UserTeamImportData{
|
|
{
|
|
Roles: ptrStr("team_admin team_user"),
|
|
},
|
|
}
|
|
if err := validateUserTeamsImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid name.")
|
|
}
|
|
data[0].Name = ptrStr("teamname")
|
|
|
|
// Valid (nil roles)
|
|
data[0].Roles = nil
|
|
if err := validateUserTeamsImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with empty roles.")
|
|
}
|
|
|
|
// Valid (empty roles)
|
|
data[0].Roles = ptrStr("")
|
|
if err := validateUserTeamsImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with empty roles.")
|
|
}
|
|
|
|
// Valid (with roles)
|
|
data[0].Roles = ptrStr("team_admin team_user")
|
|
if err := validateUserTeamsImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with valid roles.")
|
|
}
|
|
|
|
// Valid (with JSON string of theme)
|
|
data[0].Theme = ptrStr(`{"awayIndicator":"#DBBD4E","buttonBg":"#23A1FF","buttonColor":"#FFFFFF","centerChannelBg":"#ffffff","centerChannelColor":"#333333","codeTheme":"github","image":"/static/files/a4a388b38b32678e83823ef1b3e17766.png","linkColor":"#2389d7","mentionBg":"#2389d7","mentionColor":"#ffffff","mentionHighlightBg":"#fff2bb","mentionHighlightLink":"#2f81b7","newMessageSeparator":"#FF8800","onlineIndicator":"#7DBE00","sidebarBg":"#fafafa","sidebarHeaderBg":"#3481B9","sidebarHeaderTextColor":"#ffffff","sidebarText":"#333333","sidebarTextActiveBorder":"#378FD2","sidebarTextActiveColor":"#111111","sidebarTextHoverBg":"#e6f2fa","sidebarUnreadText":"#333333","type":"Mattermost"}`)
|
|
if err := validateUserTeamsImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with valid theme.")
|
|
}
|
|
|
|
// Invalid (invalid JSON string of theme)
|
|
data[0].Theme = ptrStr(`This is the invalid string which cannot be marshalled to JSON object :) + {"#DBBD4E","buttonBg", "#23A1FF", buttonColor`)
|
|
if err := validateUserTeamsImportData(&data); err == nil {
|
|
t.Fatal("Should have fail with invalid JSON string of theme.")
|
|
}
|
|
|
|
// Invalid (valid JSON but invalid theme description)
|
|
data[0].Theme = ptrStr(`{"somekey": 25, "json_obj1": {"color": "#DBBD4E","buttonBg": "#23A1FF"}}`)
|
|
if err := validateUserTeamsImportData(&data); err == nil {
|
|
t.Fatal("Should have fail with valid JSON which contains invalid string of theme description.")
|
|
}
|
|
data[0].Theme = nil
|
|
}
|
|
|
|
func TestImportValidateUserChannelsImportData(t *testing.T) {
|
|
|
|
// Invalid Name.
|
|
data := []UserChannelImportData{
|
|
{
|
|
Roles: ptrStr("channel_admin channel_user"),
|
|
},
|
|
}
|
|
if err := validateUserChannelsImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to invalid name.")
|
|
}
|
|
data[0].Name = ptrStr("channelname")
|
|
|
|
// Valid (nil roles)
|
|
data[0].Roles = nil
|
|
if err := validateUserChannelsImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with empty roles.")
|
|
}
|
|
|
|
// Valid (empty roles)
|
|
data[0].Roles = ptrStr("")
|
|
if err := validateUserChannelsImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with empty roles.")
|
|
}
|
|
|
|
// Valid (with roles)
|
|
data[0].Roles = ptrStr("channel_admin channel_user")
|
|
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 mobile notify props.
|
|
data[0].NotifyProps.Desktop = ptrStr("mention")
|
|
data[0].NotifyProps.Mobile = ptrStr("invalid")
|
|
if err := validateUserChannelsImportData(&data); err == nil {
|
|
t.Fatal("Should have failed with invalid mobile notify props.")
|
|
}
|
|
|
|
// Invalid mark_unread notify props.
|
|
data[0].NotifyProps.Mobile = 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.")
|
|
}
|
|
|
|
// Valid notify props.
|
|
data[0].NotifyProps.MarkUnread = ptrStr("mention")
|
|
if err := validateUserChannelsImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with valid notify props.")
|
|
}
|
|
}
|
|
|
|
func TestImportValidateReactionImportData(t *testing.T) {
|
|
// Test with minimum required valid properties.
|
|
parentCreateAt := model.GetMillis() - 100
|
|
data := ReactionImportData{
|
|
User: ptrStr("username"),
|
|
EmojiName: ptrStr("emoji"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateReactionImportData(&data, parentCreateAt); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Test with missing required properties.
|
|
data = ReactionImportData{
|
|
EmojiName: ptrStr("emoji"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateReactionImportData(&data, parentCreateAt); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = ReactionImportData{
|
|
User: ptrStr("username"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateReactionImportData(&data, parentCreateAt); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = ReactionImportData{
|
|
User: ptrStr("username"),
|
|
EmojiName: ptrStr("emoji"),
|
|
}
|
|
if err := validateReactionImportData(&data, parentCreateAt); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
// Test with invalid emoji name.
|
|
data = ReactionImportData{
|
|
User: ptrStr("username"),
|
|
EmojiName: ptrStr(strings.Repeat("1234567890", 500)),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateReactionImportData(&data, parentCreateAt); err == nil {
|
|
t.Fatal("Should have failed due to too long emoji name.")
|
|
}
|
|
|
|
// Test with invalid CreateAt
|
|
data = ReactionImportData{
|
|
User: ptrStr("username"),
|
|
EmojiName: ptrStr("emoji"),
|
|
CreateAt: ptrInt64(0),
|
|
}
|
|
if err := validateReactionImportData(&data, parentCreateAt); err == nil {
|
|
t.Fatal("Should have failed due to 0 create-at value.")
|
|
}
|
|
|
|
data = ReactionImportData{
|
|
User: ptrStr("username"),
|
|
EmojiName: ptrStr("emoji"),
|
|
CreateAt: ptrInt64(parentCreateAt - 100),
|
|
}
|
|
if err := validateReactionImportData(&data, parentCreateAt); err == nil {
|
|
t.Fatal("Should have failed due parent with newer create-at value.")
|
|
}
|
|
}
|
|
|
|
func TestImportValidateReplyImportData(t *testing.T) {
|
|
// Test with minimum required valid properties.
|
|
parentCreateAt := model.GetMillis() - 100
|
|
maxPostSize := 10000
|
|
data := ReplyImportData{
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Test with missing required properties.
|
|
data = ReplyImportData{
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = ReplyImportData{
|
|
User: ptrStr("username"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = ReplyImportData{
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
}
|
|
if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
// Test with invalid message.
|
|
data = ReplyImportData{
|
|
User: ptrStr("username"),
|
|
Message: ptrStr(strings.Repeat("0", maxPostSize+1)),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to too long message.")
|
|
}
|
|
|
|
// Test with invalid CreateAt
|
|
data = ReplyImportData{
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(0),
|
|
}
|
|
if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to 0 create-at value.")
|
|
}
|
|
|
|
data = ReplyImportData{
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(parentCreateAt - 100),
|
|
}
|
|
if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due parent with newer create-at value.")
|
|
}
|
|
}
|
|
|
|
func TestImportValidatePostImportData(t *testing.T) {
|
|
maxPostSize := 10000
|
|
|
|
// Test with minimum required valid properties.
|
|
data := PostImportData{
|
|
Team: ptrStr("teamname"),
|
|
Channel: ptrStr("channelname"),
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validatePostImportData(&data, maxPostSize); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Test with missing required properties.
|
|
data = PostImportData{
|
|
Channel: ptrStr("channelname"),
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validatePostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = PostImportData{
|
|
Team: ptrStr("teamname"),
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validatePostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = PostImportData{
|
|
Team: ptrStr("teamname"),
|
|
Channel: ptrStr("channelname"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validatePostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = PostImportData{
|
|
Team: ptrStr("teamname"),
|
|
Channel: ptrStr("channelname"),
|
|
User: ptrStr("username"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validatePostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = PostImportData{
|
|
Team: ptrStr("teamname"),
|
|
Channel: ptrStr("channelname"),
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
}
|
|
if err := validatePostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
// Test with invalid message.
|
|
data = PostImportData{
|
|
Team: ptrStr("teamname"),
|
|
Channel: ptrStr("channelname"),
|
|
User: ptrStr("username"),
|
|
Message: ptrStr(strings.Repeat("0", maxPostSize+1)),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validatePostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to too long message.")
|
|
}
|
|
|
|
// Test with invalid CreateAt
|
|
data = PostImportData{
|
|
Team: ptrStr("teamname"),
|
|
Channel: ptrStr("channelname"),
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(0),
|
|
}
|
|
if err := validatePostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to 0 create-at value.")
|
|
}
|
|
|
|
// Test with valid all optional parameters.
|
|
reactions := []ReactionImportData{ReactionImportData{
|
|
User: ptrStr("username"),
|
|
EmojiName: ptrStr("emoji"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}}
|
|
replies := []ReplyImportData{ReplyImportData{
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}}
|
|
data = PostImportData{
|
|
Team: ptrStr("teamname"),
|
|
Channel: ptrStr("channelname"),
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
Reactions: &reactions,
|
|
Replies: &replies,
|
|
}
|
|
if err := validatePostImportData(&data, maxPostSize); err != nil {
|
|
t.Fatal("Should have succeeded.")
|
|
}
|
|
}
|
|
|
|
func TestImportValidateDirectChannelImportData(t *testing.T) {
|
|
|
|
// Test with valid number of members for direct message.
|
|
data := DirectChannelImportData{
|
|
Members: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
}
|
|
if err := validateDirectChannelImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Test with valid number of members for group message.
|
|
data = DirectChannelImportData{
|
|
Members: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
}
|
|
if err := validateDirectChannelImportData(&data); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Test with all the combinations of optional parameters.
|
|
data = DirectChannelImportData{
|
|
Members: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
Header: ptrStr("Channel Header Here"),
|
|
}
|
|
if err := validateDirectChannelImportData(&data); err != nil {
|
|
t.Fatal("Should have succeeded with valid optional properties.")
|
|
}
|
|
|
|
// Test with invalid Header.
|
|
data.Header = ptrStr(strings.Repeat("abcdefghij ", 103))
|
|
if err := validateDirectChannelImportData(&data); err == nil {
|
|
t.Fatal("Should have failed due to too long header.")
|
|
}
|
|
|
|
// Test with different combinations of invalid member counts.
|
|
data = DirectChannelImportData{
|
|
Members: &[]string{},
|
|
}
|
|
if err := validateDirectChannelImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to invalid number of members.")
|
|
}
|
|
|
|
data = DirectChannelImportData{
|
|
Members: &[]string{
|
|
model.NewId(),
|
|
},
|
|
}
|
|
if err := validateDirectChannelImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to invalid number of members.")
|
|
}
|
|
|
|
data = DirectChannelImportData{
|
|
Members: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
}
|
|
if err := validateDirectChannelImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to invalid number of members.")
|
|
}
|
|
|
|
// Test with invalid FavoritedBy
|
|
member1 := model.NewId()
|
|
member2 := model.NewId()
|
|
data = DirectChannelImportData{
|
|
Members: &[]string{
|
|
member1,
|
|
member2,
|
|
},
|
|
FavoritedBy: &[]string{
|
|
member1,
|
|
model.NewId(),
|
|
},
|
|
}
|
|
if err := validateDirectChannelImportData(&data); err == nil {
|
|
t.Fatal("Validation should have failed due to non-member favorited.")
|
|
}
|
|
|
|
// Test with valid FavoritedBy
|
|
data = DirectChannelImportData{
|
|
Members: &[]string{
|
|
member1,
|
|
member2,
|
|
},
|
|
FavoritedBy: &[]string{
|
|
member1,
|
|
member2,
|
|
},
|
|
}
|
|
if err := validateDirectChannelImportData(&data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestImportValidateDirectPostImportData(t *testing.T) {
|
|
maxPostSize := 10000
|
|
|
|
// Test with minimum required valid properties.
|
|
data := DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Test with missing required properties.
|
|
data = DirectPostImportData{
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
User: ptrStr("username"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to missing required property.")
|
|
}
|
|
|
|
// Test with invalid numbers of channel members.
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to unsuitable number of members.")
|
|
}
|
|
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
model.NewId(),
|
|
},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to unsuitable number of members.")
|
|
}
|
|
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to unsuitable number of members.")
|
|
}
|
|
|
|
// Test with group message number of members.
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err != nil {
|
|
t.Fatal("Validation failed but should have been valid.")
|
|
}
|
|
|
|
// Test with invalid message.
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr(strings.Repeat("0", maxPostSize+1)),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to too long message.")
|
|
}
|
|
|
|
// Test with invalid CreateAt
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
model.NewId(),
|
|
model.NewId(),
|
|
},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(0),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Should have failed due to 0 create-at value.")
|
|
}
|
|
|
|
// Test with invalid FlaggedBy
|
|
member1 := model.NewId()
|
|
member2 := model.NewId()
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
member1,
|
|
member2,
|
|
},
|
|
FlaggedBy: &[]string{
|
|
member1,
|
|
model.NewId(),
|
|
},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
|
|
t.Fatal("Validation should have failed due to non-member flagged.")
|
|
}
|
|
|
|
// Test with valid FlaggedBy
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
member1,
|
|
member2,
|
|
},
|
|
FlaggedBy: &[]string{
|
|
member1,
|
|
member2,
|
|
},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Test with valid all optional parameters.
|
|
reactions := []ReactionImportData{ReactionImportData{
|
|
User: ptrStr("username"),
|
|
EmojiName: ptrStr("emoji"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}}
|
|
replies := []ReplyImportData{ReplyImportData{
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
}}
|
|
data = DirectPostImportData{
|
|
ChannelMembers: &[]string{
|
|
member1,
|
|
member2,
|
|
},
|
|
FlaggedBy: &[]string{
|
|
member1,
|
|
member2,
|
|
},
|
|
User: ptrStr("username"),
|
|
Message: ptrStr("message"),
|
|
CreateAt: ptrInt64(model.GetMillis()),
|
|
Reactions: &reactions,
|
|
Replies: &replies,
|
|
}
|
|
|
|
if err := validateDirectPostImportData(&data, maxPostSize); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestImportValidateEmojiImportData(t *testing.T) {
|
|
data := EmojiImportData{
|
|
Name: ptrStr("parrot"),
|
|
Image: ptrStr("/path/to/image"),
|
|
}
|
|
|
|
err := validateEmojiImportData(&data)
|
|
assert.Nil(t, err, "Validation should succeed")
|
|
|
|
*data.Name = "smiley"
|
|
err = validateEmojiImportData(&data)
|
|
assert.NotNil(t, err)
|
|
|
|
*data.Name = ""
|
|
err = validateEmojiImportData(&data)
|
|
assert.NotNil(t, err)
|
|
|
|
*data.Name = ""
|
|
*data.Image = ""
|
|
err = validateEmojiImportData(&data)
|
|
assert.NotNil(t, err)
|
|
|
|
*data.Image = "/path/to/image"
|
|
data.Name = nil
|
|
err = validateEmojiImportData(&data)
|
|
assert.NotNil(t, err)
|
|
|
|
data.Name = ptrStr("parrot")
|
|
data.Image = nil
|
|
err = validateEmojiImportData(&data)
|
|
assert.NotNil(t, err)
|
|
}
|