github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/import_validators_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/utils"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestImportValidateSchemeImportData(t *testing.T) {
    17  	// Test with minimum required valid properties and team scope.
    18  	data := SchemeImportData{
    19  		Name:        ptrStr("name"),
    20  		DisplayName: ptrStr("display name"),
    21  		Scope:       ptrStr("team"),
    22  		DefaultTeamAdminRole: &RoleImportData{
    23  			Name:        ptrStr("name"),
    24  			DisplayName: ptrStr("display name"),
    25  			Permissions: &[]string{"invite_user"},
    26  		},
    27  		DefaultTeamUserRole: &RoleImportData{
    28  			Name:        ptrStr("name"),
    29  			DisplayName: ptrStr("display name"),
    30  			Permissions: &[]string{"invite_user"},
    31  		},
    32  		DefaultChannelAdminRole: &RoleImportData{
    33  			Name:        ptrStr("name"),
    34  			DisplayName: ptrStr("display name"),
    35  			Permissions: &[]string{"invite_user"},
    36  		},
    37  		DefaultChannelUserRole: &RoleImportData{
    38  			Name:        ptrStr("name"),
    39  			DisplayName: ptrStr("display name"),
    40  			Permissions: &[]string{"invite_user"},
    41  		},
    42  	}
    43  	if err := validateSchemeImportData(&data); err != nil {
    44  		t.Fatal("Validation failed but should have been valid.", err)
    45  	}
    46  
    47  	// Test with various invalid names.
    48  	data.Name = nil
    49  	if err := validateSchemeImportData(&data); err == nil {
    50  		t.Fatal("Should have failed due to invalid name.")
    51  	}
    52  
    53  	data.Name = ptrStr("")
    54  	if err := validateSchemeImportData(&data); err == nil {
    55  		t.Fatal("Should have failed due to invalid name.")
    56  	}
    57  
    58  	data.Name = ptrStr(strings.Repeat("1234567890", 100))
    59  	if err := validateSchemeImportData(&data); err == nil {
    60  		t.Fatal("Should have failed due to invalid name.")
    61  	}
    62  
    63  	data.Name = ptrStr("name")
    64  	// Test with invalid display name.
    65  	data.DisplayName = nil
    66  	if err := validateSchemeImportData(&data); err == nil {
    67  		t.Fatal("Should have failed due to invalid display name.")
    68  	}
    69  
    70  	data.DisplayName = ptrStr("")
    71  	if err := validateSchemeImportData(&data); err == nil {
    72  		t.Fatal("Should have failed due to invalid display name.")
    73  	}
    74  
    75  	data.DisplayName = ptrStr(strings.Repeat("1234567890", 100))
    76  	if err := validateSchemeImportData(&data); err == nil {
    77  		t.Fatal("Should have failed due to invalid display name.")
    78  	}
    79  
    80  	data.DisplayName = ptrStr("display name")
    81  
    82  	// Test with various missing roles.
    83  	data.DefaultTeamAdminRole = nil
    84  	if err := validateSchemeImportData(&data); err == nil {
    85  		t.Fatal("Should have failed due to missing role.")
    86  	}
    87  
    88  	data.DefaultTeamAdminRole = &RoleImportData{
    89  		Name:        ptrStr("name"),
    90  		DisplayName: ptrStr("display name"),
    91  		Permissions: &[]string{"invite_user"},
    92  	}
    93  	data.DefaultTeamUserRole = nil
    94  	if err := validateSchemeImportData(&data); err == nil {
    95  		t.Fatal("Should have failed due to missing role.")
    96  	}
    97  
    98  	data.DefaultTeamUserRole = &RoleImportData{
    99  		Name:        ptrStr("name"),
   100  		DisplayName: ptrStr("display name"),
   101  		Permissions: &[]string{"invite_user"},
   102  	}
   103  	data.DefaultChannelAdminRole = nil
   104  	if err := validateSchemeImportData(&data); err == nil {
   105  		t.Fatal("Should have failed due to missing role.")
   106  	}
   107  
   108  	data.DefaultChannelAdminRole = &RoleImportData{
   109  		Name:        ptrStr("name"),
   110  		DisplayName: ptrStr("display name"),
   111  		Permissions: &[]string{"invite_user"},
   112  	}
   113  	data.DefaultChannelUserRole = nil
   114  	if err := validateSchemeImportData(&data); err == nil {
   115  		t.Fatal("Should have failed due to missing role.")
   116  	}
   117  
   118  	data.DefaultChannelUserRole = &RoleImportData{
   119  		Name:        ptrStr("name"),
   120  		DisplayName: ptrStr("display name"),
   121  		Permissions: &[]string{"invite_user"},
   122  	}
   123  
   124  	// Test with various invalid roles.
   125  	data.DefaultTeamAdminRole.Name = nil
   126  	if err := validateSchemeImportData(&data); err == nil {
   127  		t.Fatal("Should have failed due to invalid role.")
   128  	}
   129  
   130  	data.DefaultTeamAdminRole.Name = ptrStr("name")
   131  	data.DefaultTeamUserRole.Name = nil
   132  	if err := validateSchemeImportData(&data); err == nil {
   133  		t.Fatal("Should have failed due to invalid role.")
   134  	}
   135  
   136  	data.DefaultTeamUserRole.Name = ptrStr("name")
   137  	data.DefaultChannelAdminRole.Name = nil
   138  	if err := validateSchemeImportData(&data); err == nil {
   139  		t.Fatal("Should have failed due to invalid role.")
   140  	}
   141  
   142  	data.DefaultChannelAdminRole.Name = ptrStr("name")
   143  	data.DefaultChannelUserRole.Name = nil
   144  	if err := validateSchemeImportData(&data); err == nil {
   145  		t.Fatal("Should have failed due to invalid role.")
   146  	}
   147  
   148  	data.DefaultChannelUserRole.Name = ptrStr("name")
   149  
   150  	// Change to a Channel scope role, and check with missing or extra roles again.
   151  	data.Scope = ptrStr("channel")
   152  	data.DefaultTeamAdminRole = nil
   153  	if err := validateSchemeImportData(&data); err == nil {
   154  		t.Fatal("Should have failed due to spurious role.")
   155  	}
   156  
   157  	data.DefaultTeamAdminRole = &RoleImportData{
   158  		Name:        ptrStr("name"),
   159  		DisplayName: ptrStr("display name"),
   160  		Permissions: &[]string{"invite_user"},
   161  	}
   162  	data.DefaultTeamUserRole = nil
   163  	if err := validateSchemeImportData(&data); err == nil {
   164  		t.Fatal("Should have failed due to spurious role.")
   165  	}
   166  
   167  	data.DefaultTeamAdminRole = nil
   168  	if err := validateSchemeImportData(&data); err != nil {
   169  		t.Fatal("Should have succeeded.")
   170  	}
   171  
   172  	// Test with all combinations of optional parameters.
   173  	data.Description = ptrStr(strings.Repeat("1234567890", 1024))
   174  	if err := validateSchemeImportData(&data); err == nil {
   175  		t.Fatal("Should have failed due to invalid description.")
   176  	}
   177  
   178  	data.Description = ptrStr("description")
   179  	if err := validateSchemeImportData(&data); err != nil {
   180  		t.Fatal("Should have succeeded.")
   181  	}
   182  }
   183  
   184  func TestImportValidateRoleImportData(t *testing.T) {
   185  	// Test with minimum required valid properties.
   186  	data := RoleImportData{
   187  		Name:        ptrStr("name"),
   188  		DisplayName: ptrStr("display name"),
   189  	}
   190  	if err := validateRoleImportData(&data); err != nil {
   191  		t.Fatal("Validation failed but should have been valid.", err)
   192  	}
   193  
   194  	// Test with various invalid names.
   195  	data.Name = nil
   196  	if err := validateRoleImportData(&data); err == nil {
   197  		t.Fatal("Should have failed due to invalid name.")
   198  	}
   199  
   200  	data.Name = ptrStr("")
   201  	if err := validateRoleImportData(&data); err == nil {
   202  		t.Fatal("Should have failed due to invalid name.")
   203  	}
   204  
   205  	data.Name = ptrStr(strings.Repeat("1234567890", 100))
   206  	if err := validateRoleImportData(&data); err == nil {
   207  		t.Fatal("Should have failed due to invalid name.")
   208  	}
   209  
   210  	data.Name = ptrStr("name")
   211  	// Test with invalid display name.
   212  	data.DisplayName = nil
   213  	if err := validateRoleImportData(&data); err == nil {
   214  		t.Fatal("Should have failed due to invalid display name.")
   215  	}
   216  
   217  	data.DisplayName = ptrStr("")
   218  	if err := validateRoleImportData(&data); err == nil {
   219  		t.Fatal("Should have failed due to invalid display name.")
   220  	}
   221  
   222  	data.DisplayName = ptrStr(strings.Repeat("1234567890", 100))
   223  	if err := validateRoleImportData(&data); err == nil {
   224  		t.Fatal("Should have failed due to invalid display name.")
   225  	}
   226  
   227  	data.DisplayName = ptrStr("display name")
   228  
   229  	// Test with various valid/invalid permissions.
   230  	data.Permissions = &[]string{}
   231  	if err := validateRoleImportData(&data); err != nil {
   232  		t.Fatal("Validation failed but should have been valid.", err)
   233  	}
   234  
   235  	data.Permissions = &[]string{"invite_user", "add_user_to_team"}
   236  	if err := validateRoleImportData(&data); err != nil {
   237  		t.Fatal("Validation failed but should have been valid.", err)
   238  	}
   239  
   240  	data.Permissions = &[]string{"invite_user", "add_user_to_team", "derp"}
   241  	if err := validateRoleImportData(&data); err == nil {
   242  		t.Fatal("Validation should have failed due to invalid permission.", err)
   243  	}
   244  
   245  	data.Permissions = &[]string{"invite_user", "add_user_to_team"}
   246  
   247  	// Test with various valid/invalid descriptions.
   248  	data.Description = ptrStr(strings.Repeat("1234567890", 1024))
   249  	if err := validateRoleImportData(&data); err == nil {
   250  		t.Fatal("Validation should have failed due to invalid description.", err)
   251  	}
   252  
   253  	data.Description = ptrStr("description")
   254  	if err := validateRoleImportData(&data); err != nil {
   255  		t.Fatal("Validation failed but should have been valid.", err)
   256  	}
   257  }
   258  
   259  func TestImportValidateTeamImportData(t *testing.T) {
   260  
   261  	// Test with minimum required valid properties.
   262  	data := TeamImportData{
   263  		Name:        ptrStr("teamname"),
   264  		DisplayName: ptrStr("Display Name"),
   265  		Type:        ptrStr("O"),
   266  	}
   267  	if err := validateTeamImportData(&data); err != nil {
   268  		t.Fatal("Validation failed but should have been valid.")
   269  	}
   270  
   271  	// Test with various invalid names.
   272  	data = TeamImportData{
   273  		DisplayName: ptrStr("Display Name"),
   274  		Type:        ptrStr("O"),
   275  	}
   276  	if err := validateTeamImportData(&data); err == nil {
   277  		t.Fatal("Should have failed due to missing name.")
   278  	}
   279  
   280  	data.Name = ptrStr(strings.Repeat("abcdefghij", 7))
   281  	if err := validateTeamImportData(&data); err == nil {
   282  		t.Fatal("Should have failed due to too long name.")
   283  	}
   284  
   285  	data.Name = ptrStr("login")
   286  	if err := validateTeamImportData(&data); err == nil {
   287  		t.Fatal("Should have failed due to reserved word in name.")
   288  	}
   289  
   290  	data.Name = ptrStr("Test::''ASD")
   291  	if err := validateTeamImportData(&data); err == nil {
   292  		t.Fatal("Should have failed due to non alphanum characters in name.")
   293  	}
   294  
   295  	data.Name = ptrStr("A")
   296  	if err := validateTeamImportData(&data); err == nil {
   297  		t.Fatal("Should have failed due to short name.")
   298  	}
   299  
   300  	// Test team various invalid display names.
   301  	data = TeamImportData{
   302  		Name: ptrStr("teamname"),
   303  		Type: ptrStr("O"),
   304  	}
   305  	if err := validateTeamImportData(&data); err == nil {
   306  		t.Fatal("Should have failed due to missing display_name.")
   307  	}
   308  
   309  	data.DisplayName = ptrStr("")
   310  	if err := validateTeamImportData(&data); err == nil {
   311  		t.Fatal("Should have failed due to empty display_name.")
   312  	}
   313  
   314  	data.DisplayName = ptrStr(strings.Repeat("abcdefghij", 7))
   315  	if err := validateTeamImportData(&data); err == nil {
   316  		t.Fatal("Should have failed due to too long display_name.")
   317  	}
   318  
   319  	// Test with various valid and invalid types.
   320  	data = TeamImportData{
   321  		Name:        ptrStr("teamname"),
   322  		DisplayName: ptrStr("Display Name"),
   323  	}
   324  	if err := validateTeamImportData(&data); err == nil {
   325  		t.Fatal("Should have failed due to missing type.")
   326  	}
   327  
   328  	data.Type = ptrStr("A")
   329  	if err := validateTeamImportData(&data); err == nil {
   330  		t.Fatal("Should have failed due to invalid type.")
   331  	}
   332  
   333  	data.Type = ptrStr("I")
   334  	if err := validateTeamImportData(&data); err != nil {
   335  		t.Fatal("Should have succeeded with valid type.")
   336  	}
   337  
   338  	// Test with all the combinations of optional parameters.
   339  	data = TeamImportData{
   340  		Name:            ptrStr("teamname"),
   341  		DisplayName:     ptrStr("Display Name"),
   342  		Type:            ptrStr("O"),
   343  		Description:     ptrStr("The team description."),
   344  		AllowOpenInvite: ptrBool(true),
   345  	}
   346  	if err := validateTeamImportData(&data); err != nil {
   347  		t.Fatal("Should have succeeded with valid optional properties.")
   348  	}
   349  
   350  	data.AllowOpenInvite = ptrBool(false)
   351  	if err := validateTeamImportData(&data); err != nil {
   352  		t.Fatal("Should have succeeded with allow open invites false.")
   353  	}
   354  
   355  	data.Description = ptrStr(strings.Repeat("abcdefghij ", 26))
   356  	if err := validateTeamImportData(&data); err == nil {
   357  		t.Fatal("Should have failed due to too long description.")
   358  	}
   359  
   360  	// Test with an empty scheme name.
   361  	data.Description = ptrStr("abcdefg")
   362  	data.Scheme = ptrStr("")
   363  	if err := validateTeamImportData(&data); err == nil {
   364  		t.Fatal("Should have failed due to empty scheme name.")
   365  	}
   366  
   367  	// Test with a valid scheme name.
   368  	data.Scheme = ptrStr("abcdefg")
   369  	if err := validateTeamImportData(&data); err != nil {
   370  		t.Fatal("Should have succeeded with valid scheme name.")
   371  	}
   372  }
   373  
   374  func TestImportValidateChannelImportData(t *testing.T) {
   375  
   376  	// Test with minimum required valid properties.
   377  	data := ChannelImportData{
   378  		Team:        ptrStr("teamname"),
   379  		Name:        ptrStr("channelname"),
   380  		DisplayName: ptrStr("Display Name"),
   381  		Type:        ptrStr("O"),
   382  	}
   383  	if err := validateChannelImportData(&data); err != nil {
   384  		t.Fatal("Validation failed but should have been valid.")
   385  	}
   386  
   387  	// Test with missing team.
   388  	data = ChannelImportData{
   389  		Name:        ptrStr("channelname"),
   390  		DisplayName: ptrStr("Display Name"),
   391  		Type:        ptrStr("O"),
   392  	}
   393  	if err := validateChannelImportData(&data); err == nil {
   394  		t.Fatal("Should have failed due to missing team.")
   395  	}
   396  
   397  	// Test with various invalid names.
   398  	data = ChannelImportData{
   399  		Team:        ptrStr("teamname"),
   400  		DisplayName: ptrStr("Display Name"),
   401  		Type:        ptrStr("O"),
   402  	}
   403  	if err := validateChannelImportData(&data); err == nil {
   404  		t.Fatal("Should have failed due to missing name.")
   405  	}
   406  
   407  	data.Name = ptrStr(strings.Repeat("abcdefghij", 7))
   408  	if err := validateChannelImportData(&data); err == nil {
   409  		t.Fatal("Should have failed due to too long name.")
   410  	}
   411  
   412  	data.Name = ptrStr("Test::''ASD")
   413  	if err := validateChannelImportData(&data); err == nil {
   414  		t.Fatal("Should have failed due to non alphanum characters in name.")
   415  	}
   416  
   417  	data.Name = ptrStr("A")
   418  	if err := validateChannelImportData(&data); err == nil {
   419  		t.Fatal("Should have failed due to short name.")
   420  	}
   421  
   422  	// Test team various invalid display names.
   423  	data = ChannelImportData{
   424  		Team: ptrStr("teamname"),
   425  		Name: ptrStr("channelname"),
   426  		Type: ptrStr("O"),
   427  	}
   428  	if err := validateChannelImportData(&data); err == nil {
   429  		t.Fatal("Should have failed due to missing display_name.")
   430  	}
   431  
   432  	data.DisplayName = ptrStr("")
   433  	if err := validateChannelImportData(&data); err == nil {
   434  		t.Fatal("Should have failed due to empty display_name.")
   435  	}
   436  
   437  	data.DisplayName = ptrStr(strings.Repeat("abcdefghij", 7))
   438  	if err := validateChannelImportData(&data); err == nil {
   439  		t.Fatal("Should have failed due to too long display_name.")
   440  	}
   441  
   442  	// Test with various valid and invalid types.
   443  	data = ChannelImportData{
   444  		Team:        ptrStr("teamname"),
   445  		Name:        ptrStr("channelname"),
   446  		DisplayName: ptrStr("Display Name"),
   447  	}
   448  	if err := validateChannelImportData(&data); err == nil {
   449  		t.Fatal("Should have failed due to missing type.")
   450  	}
   451  
   452  	data.Type = ptrStr("A")
   453  	if err := validateChannelImportData(&data); err == nil {
   454  		t.Fatal("Should have failed due to invalid type.")
   455  	}
   456  
   457  	data.Type = ptrStr("P")
   458  	if err := validateChannelImportData(&data); err != nil {
   459  		t.Fatal("Should have succeeded with valid type.")
   460  	}
   461  
   462  	// Test with all the combinations of optional parameters.
   463  	data = ChannelImportData{
   464  		Team:        ptrStr("teamname"),
   465  		Name:        ptrStr("channelname"),
   466  		DisplayName: ptrStr("Display Name"),
   467  		Type:        ptrStr("O"),
   468  		Header:      ptrStr("Channel Header Here"),
   469  		Purpose:     ptrStr("Channel Purpose Here"),
   470  	}
   471  	if err := validateChannelImportData(&data); err != nil {
   472  		t.Fatal("Should have succeeded with valid optional properties.")
   473  	}
   474  
   475  	data.Header = ptrStr(strings.Repeat("abcdefghij ", 103))
   476  	if err := validateChannelImportData(&data); err == nil {
   477  		t.Fatal("Should have failed due to too long header.")
   478  	}
   479  
   480  	data.Header = ptrStr("Channel Header Here")
   481  	data.Purpose = ptrStr(strings.Repeat("abcdefghij ", 26))
   482  	if err := validateChannelImportData(&data); err == nil {
   483  		t.Fatal("Should have failed due to too long purpose.")
   484  	}
   485  
   486  	// Test with an empty scheme name.
   487  	data.Purpose = ptrStr("abcdefg")
   488  	data.Scheme = ptrStr("")
   489  	if err := validateChannelImportData(&data); err == nil {
   490  		t.Fatal("Should have failed due to empty scheme name.")
   491  	}
   492  
   493  	// Test with a valid scheme name.
   494  	data.Scheme = ptrStr("abcdefg")
   495  	if err := validateChannelImportData(&data); err != nil {
   496  		t.Fatal("Should have succeeded with valid scheme name.")
   497  	}
   498  }
   499  
   500  func TestImportValidateUserImportData(t *testing.T) {
   501  
   502  	// Test with minimum required valid properties.
   503  	data := UserImportData{
   504  		Username: ptrStr("bob"),
   505  		Email:    ptrStr("bob@example.com"),
   506  	}
   507  	if err := validateUserImportData(&data); err != nil {
   508  		t.Fatal("Validation failed but should have been valid.")
   509  	}
   510  
   511  	// Invalid Usernames.
   512  	data.Username = nil
   513  	if err := validateUserImportData(&data); err == nil {
   514  		t.Fatal("Validation should have failed due to nil Username.")
   515  	}
   516  
   517  	data.Username = ptrStr("")
   518  	if err := validateUserImportData(&data); err == nil {
   519  		t.Fatal("Validation should have failed due to 0 length Username.")
   520  	}
   521  
   522  	data.Username = ptrStr(strings.Repeat("abcdefghij", 7))
   523  	if err := validateUserImportData(&data); err == nil {
   524  		t.Fatal("Validation should have failed due to too long Username.")
   525  	}
   526  
   527  	data.Username = ptrStr("i am a username with spaces and !!!")
   528  	if err := validateUserImportData(&data); err == nil {
   529  		t.Fatal("Validation should have failed due to invalid characters in Username.")
   530  	}
   531  
   532  	data.Username = ptrStr("bob")
   533  
   534  	// Unexisting Picture Image
   535  	data.ProfileImage = ptrStr("not-existing-file")
   536  	if err := validateUserImportData(&data); err == nil {
   537  		t.Fatal("Validation should have failed due to not existing profile image file.")
   538  	}
   539  	data.ProfileImage = nil
   540  
   541  	// Invalid Emails
   542  	data.Email = nil
   543  	if err := validateUserImportData(&data); err == nil {
   544  		t.Fatal("Validation should have failed due to nil Email.")
   545  	}
   546  
   547  	data.Email = ptrStr("")
   548  	if err := validateUserImportData(&data); err == nil {
   549  		t.Fatal("Validation should have failed due to 0 length Email.")
   550  	}
   551  
   552  	data.Email = ptrStr(strings.Repeat("abcdefghij", 13))
   553  	if err := validateUserImportData(&data); err == nil {
   554  		t.Fatal("Validation should have failed due to too long Email.")
   555  	}
   556  
   557  	data.Email = ptrStr("bob@example.com")
   558  
   559  	data.AuthService = ptrStr("")
   560  	if err := validateUserImportData(&data); err == nil {
   561  		t.Fatal("Validation should have failed due to 0-length auth service.")
   562  	}
   563  
   564  	data.AuthService = ptrStr("saml")
   565  	data.AuthData = ptrStr(strings.Repeat("abcdefghij", 15))
   566  	if err := validateUserImportData(&data); err == nil {
   567  		t.Fatal("Validation should have failed due to too long auth data.")
   568  	}
   569  
   570  	data.AuthData = ptrStr("bobbytables")
   571  	if err := validateUserImportData(&data); err != nil {
   572  		t.Fatal("Validation should have succeeded with valid auth service and auth data.")
   573  	}
   574  
   575  	// Test a valid User with all fields populated.
   576  	testsDir, _ := utils.FindDir("tests")
   577  	data = UserImportData{
   578  		ProfileImage: ptrStr(filepath.Join(testsDir, "test.png")),
   579  		Username:     ptrStr("bob"),
   580  		Email:        ptrStr("bob@example.com"),
   581  		AuthService:  ptrStr("ldap"),
   582  		AuthData:     ptrStr("bob"),
   583  		Nickname:     ptrStr("BobNick"),
   584  		FirstName:    ptrStr("Bob"),
   585  		LastName:     ptrStr("Blob"),
   586  		Position:     ptrStr("The Boss"),
   587  		Roles:        ptrStr("system_user"),
   588  		Locale:       ptrStr("en"),
   589  	}
   590  	if err := validateUserImportData(&data); err != nil {
   591  		t.Fatal("Validation failed but should have been valid.")
   592  	}
   593  
   594  	// Test various invalid optional field values.
   595  	data.Nickname = ptrStr(strings.Repeat("abcdefghij", 7))
   596  	if err := validateUserImportData(&data); err == nil {
   597  		t.Fatal("Validation should have failed due to too long Nickname.")
   598  	}
   599  	data.Nickname = ptrStr("BobNick")
   600  
   601  	data.FirstName = ptrStr(strings.Repeat("abcdefghij", 7))
   602  	if err := validateUserImportData(&data); err == nil {
   603  		t.Fatal("Validation should have failed due to too long First Name.")
   604  	}
   605  	data.FirstName = ptrStr("Bob")
   606  
   607  	data.LastName = ptrStr(strings.Repeat("abcdefghij", 7))
   608  	if err := validateUserImportData(&data); err == nil {
   609  		t.Fatal("Validation should have failed due to too long Last name.")
   610  	}
   611  	data.LastName = ptrStr("Blob")
   612  
   613  	data.Position = ptrStr(strings.Repeat("abcdefghij", 13))
   614  	if err := validateUserImportData(&data); err == nil {
   615  		t.Fatal("Validation should have failed due to too long Position.")
   616  	}
   617  	data.Position = ptrStr("The Boss")
   618  
   619  	data.Roles = nil
   620  	if err := validateUserImportData(&data); err != nil {
   621  		t.Fatal("Validation failed but should have been valid.")
   622  	}
   623  
   624  	data.Roles = ptrStr("")
   625  	if err := validateUserImportData(&data); err != nil {
   626  		t.Fatal("Validation failed but should have been valid.")
   627  	}
   628  	data.Roles = ptrStr("system_user")
   629  
   630  	// Try various valid/invalid notify props.
   631  	data.NotifyProps = &UserNotifyPropsImportData{}
   632  
   633  	data.NotifyProps.Desktop = ptrStr("invalid")
   634  	checkError(t, validateUserImportData(&data))
   635  
   636  	data.NotifyProps.Desktop = ptrStr(model.USER_NOTIFY_ALL)
   637  	data.NotifyProps.DesktopSound = ptrStr("invalid")
   638  	checkError(t, validateUserImportData(&data))
   639  
   640  	data.NotifyProps.DesktopSound = ptrStr("true")
   641  	data.NotifyProps.Email = ptrStr("invalid")
   642  	checkError(t, validateUserImportData(&data))
   643  
   644  	data.NotifyProps.Email = ptrStr("true")
   645  	data.NotifyProps.Mobile = ptrStr("invalid")
   646  	checkError(t, validateUserImportData(&data))
   647  
   648  	data.NotifyProps.Mobile = ptrStr(model.USER_NOTIFY_ALL)
   649  	data.NotifyProps.MobilePushStatus = ptrStr("invalid")
   650  	checkError(t, validateUserImportData(&data))
   651  
   652  	data.NotifyProps.MobilePushStatus = ptrStr(model.STATUS_ONLINE)
   653  	data.NotifyProps.ChannelTrigger = ptrStr("invalid")
   654  	checkError(t, validateUserImportData(&data))
   655  
   656  	data.NotifyProps.ChannelTrigger = ptrStr("true")
   657  	data.NotifyProps.CommentsTrigger = ptrStr("invalid")
   658  	checkError(t, validateUserImportData(&data))
   659  
   660  	data.NotifyProps.CommentsTrigger = ptrStr(model.COMMENTS_NOTIFY_ROOT)
   661  	data.NotifyProps.MentionKeys = ptrStr("valid")
   662  	checkNoError(t, validateUserImportData(&data))
   663  }
   664  
   665  func TestImportValidateUserTeamsImportData(t *testing.T) {
   666  
   667  	// Invalid Name.
   668  	data := []UserTeamImportData{
   669  		{
   670  			Roles: ptrStr("team_admin team_user"),
   671  		},
   672  	}
   673  	if err := validateUserTeamsImportData(&data); err == nil {
   674  		t.Fatal("Should have failed due to invalid name.")
   675  	}
   676  	data[0].Name = ptrStr("teamname")
   677  
   678  	// Valid (nil roles)
   679  	data[0].Roles = nil
   680  	if err := validateUserTeamsImportData(&data); err != nil {
   681  		t.Fatal("Should have succeeded with empty roles.")
   682  	}
   683  
   684  	// Valid (empty roles)
   685  	data[0].Roles = ptrStr("")
   686  	if err := validateUserTeamsImportData(&data); err != nil {
   687  		t.Fatal("Should have succeeded with empty roles.")
   688  	}
   689  
   690  	// Valid (with roles)
   691  	data[0].Roles = ptrStr("team_admin team_user")
   692  	if err := validateUserTeamsImportData(&data); err != nil {
   693  		t.Fatal("Should have succeeded with valid roles.")
   694  	}
   695  
   696  	// Valid (with JSON string of theme)
   697  	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"}`)
   698  	if err := validateUserTeamsImportData(&data); err != nil {
   699  		t.Fatal("Should have succeeded with valid theme.")
   700  	}
   701  
   702  	// Invalid (invalid JSON string of theme)
   703  	data[0].Theme = ptrStr(`This is the invalid string which cannot be marshalled to JSON object :) + {"#DBBD4E","buttonBg", "#23A1FF", buttonColor`)
   704  	if err := validateUserTeamsImportData(&data); err == nil {
   705  		t.Fatal("Should have fail with invalid JSON string of theme.")
   706  	}
   707  
   708  	// Invalid (valid JSON but invalid theme description)
   709  	data[0].Theme = ptrStr(`{"somekey": 25, "json_obj1": {"color": "#DBBD4E","buttonBg": "#23A1FF"}}`)
   710  	if err := validateUserTeamsImportData(&data); err == nil {
   711  		t.Fatal("Should have fail with valid JSON which contains invalid string of theme description.")
   712  	}
   713  	data[0].Theme = nil
   714  }
   715  
   716  func TestImportValidateUserChannelsImportData(t *testing.T) {
   717  
   718  	// Invalid Name.
   719  	data := []UserChannelImportData{
   720  		{
   721  			Roles: ptrStr("channel_admin channel_user"),
   722  		},
   723  	}
   724  	if err := validateUserChannelsImportData(&data); err == nil {
   725  		t.Fatal("Should have failed due to invalid name.")
   726  	}
   727  	data[0].Name = ptrStr("channelname")
   728  
   729  	// Valid (nil roles)
   730  	data[0].Roles = nil
   731  	if err := validateUserChannelsImportData(&data); err != nil {
   732  		t.Fatal("Should have succeeded with empty roles.")
   733  	}
   734  
   735  	// Valid (empty roles)
   736  	data[0].Roles = ptrStr("")
   737  	if err := validateUserChannelsImportData(&data); err != nil {
   738  		t.Fatal("Should have succeeded with empty roles.")
   739  	}
   740  
   741  	// Valid (with roles)
   742  	data[0].Roles = ptrStr("channel_admin channel_user")
   743  	if err := validateUserChannelsImportData(&data); err != nil {
   744  		t.Fatal("Should have succeeded with valid roles.")
   745  	}
   746  
   747  	// Empty notify props.
   748  	data[0].NotifyProps = &UserChannelNotifyPropsImportData{}
   749  	if err := validateUserChannelsImportData(&data); err != nil {
   750  		t.Fatal("Should have succeeded with empty notify props.")
   751  	}
   752  
   753  	// Invalid desktop notify props.
   754  	data[0].NotifyProps.Desktop = ptrStr("invalid")
   755  	if err := validateUserChannelsImportData(&data); err == nil {
   756  		t.Fatal("Should have failed with invalid desktop notify props.")
   757  	}
   758  
   759  	// Invalid mobile notify props.
   760  	data[0].NotifyProps.Desktop = ptrStr("mention")
   761  	data[0].NotifyProps.Mobile = ptrStr("invalid")
   762  	if err := validateUserChannelsImportData(&data); err == nil {
   763  		t.Fatal("Should have failed with invalid mobile notify props.")
   764  	}
   765  
   766  	// Invalid mark_unread notify props.
   767  	data[0].NotifyProps.Mobile = ptrStr("mention")
   768  	data[0].NotifyProps.MarkUnread = ptrStr("invalid")
   769  	if err := validateUserChannelsImportData(&data); err == nil {
   770  		t.Fatal("Should have failed with invalid mark_unread notify props.")
   771  	}
   772  
   773  	// Valid notify props.
   774  	data[0].NotifyProps.MarkUnread = ptrStr("mention")
   775  	if err := validateUserChannelsImportData(&data); err != nil {
   776  		t.Fatal("Should have succeeded with valid notify props.")
   777  	}
   778  }
   779  
   780  func TestImportValidateReactionImportData(t *testing.T) {
   781  	// Test with minimum required valid properties.
   782  	parentCreateAt := model.GetMillis() - 100
   783  	data := ReactionImportData{
   784  		User:      ptrStr("username"),
   785  		EmojiName: ptrStr("emoji"),
   786  		CreateAt:  ptrInt64(model.GetMillis()),
   787  	}
   788  	if err := validateReactionImportData(&data, parentCreateAt); err != nil {
   789  		t.Fatal("Validation failed but should have been valid.")
   790  	}
   791  
   792  	// Test with missing required properties.
   793  	data = ReactionImportData{
   794  		EmojiName: ptrStr("emoji"),
   795  		CreateAt:  ptrInt64(model.GetMillis()),
   796  	}
   797  	if err := validateReactionImportData(&data, parentCreateAt); err == nil {
   798  		t.Fatal("Should have failed due to missing required property.")
   799  	}
   800  
   801  	data = ReactionImportData{
   802  		User:     ptrStr("username"),
   803  		CreateAt: ptrInt64(model.GetMillis()),
   804  	}
   805  	if err := validateReactionImportData(&data, parentCreateAt); err == nil {
   806  		t.Fatal("Should have failed due to missing required property.")
   807  	}
   808  
   809  	data = ReactionImportData{
   810  		User:      ptrStr("username"),
   811  		EmojiName: ptrStr("emoji"),
   812  	}
   813  	if err := validateReactionImportData(&data, parentCreateAt); err == nil {
   814  		t.Fatal("Should have failed due to missing required property.")
   815  	}
   816  
   817  	// Test with invalid emoji name.
   818  	data = ReactionImportData{
   819  		User:      ptrStr("username"),
   820  		EmojiName: ptrStr(strings.Repeat("1234567890", 500)),
   821  		CreateAt:  ptrInt64(model.GetMillis()),
   822  	}
   823  	if err := validateReactionImportData(&data, parentCreateAt); err == nil {
   824  		t.Fatal("Should have failed due to too long emoji name.")
   825  	}
   826  
   827  	// Test with invalid CreateAt
   828  	data = ReactionImportData{
   829  		User:      ptrStr("username"),
   830  		EmojiName: ptrStr("emoji"),
   831  		CreateAt:  ptrInt64(0),
   832  	}
   833  	if err := validateReactionImportData(&data, parentCreateAt); err == nil {
   834  		t.Fatal("Should have failed due to 0 create-at value.")
   835  	}
   836  
   837  	data = ReactionImportData{
   838  		User:      ptrStr("username"),
   839  		EmojiName: ptrStr("emoji"),
   840  		CreateAt:  ptrInt64(parentCreateAt - 100),
   841  	}
   842  	if err := validateReactionImportData(&data, parentCreateAt); err == nil {
   843  		t.Fatal("Should have failed due parent with newer create-at value.")
   844  	}
   845  }
   846  
   847  func TestImportValidateReplyImportData(t *testing.T) {
   848  	// Test with minimum required valid properties.
   849  	parentCreateAt := model.GetMillis() - 100
   850  	maxPostSize := 10000
   851  	data := ReplyImportData{
   852  		User:     ptrStr("username"),
   853  		Message:  ptrStr("message"),
   854  		CreateAt: ptrInt64(model.GetMillis()),
   855  	}
   856  	if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err != nil {
   857  		t.Fatal("Validation failed but should have been valid.")
   858  	}
   859  
   860  	// Test with missing required properties.
   861  	data = ReplyImportData{
   862  		Message:  ptrStr("message"),
   863  		CreateAt: ptrInt64(model.GetMillis()),
   864  	}
   865  	if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
   866  		t.Fatal("Should have failed due to missing required property.")
   867  	}
   868  
   869  	data = ReplyImportData{
   870  		User:     ptrStr("username"),
   871  		CreateAt: ptrInt64(model.GetMillis()),
   872  	}
   873  	if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
   874  		t.Fatal("Should have failed due to missing required property.")
   875  	}
   876  
   877  	data = ReplyImportData{
   878  		User:    ptrStr("username"),
   879  		Message: ptrStr("message"),
   880  	}
   881  	if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
   882  		t.Fatal("Should have failed due to missing required property.")
   883  	}
   884  
   885  	// Test with invalid message.
   886  	data = ReplyImportData{
   887  		User:     ptrStr("username"),
   888  		Message:  ptrStr(strings.Repeat("0", maxPostSize+1)),
   889  		CreateAt: ptrInt64(model.GetMillis()),
   890  	}
   891  	if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
   892  		t.Fatal("Should have failed due to too long message.")
   893  	}
   894  
   895  	// Test with invalid CreateAt
   896  	data = ReplyImportData{
   897  		User:     ptrStr("username"),
   898  		Message:  ptrStr("message"),
   899  		CreateAt: ptrInt64(0),
   900  	}
   901  	if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
   902  		t.Fatal("Should have failed due to 0 create-at value.")
   903  	}
   904  
   905  	data = ReplyImportData{
   906  		User:     ptrStr("username"),
   907  		Message:  ptrStr("message"),
   908  		CreateAt: ptrInt64(parentCreateAt - 100),
   909  	}
   910  	if err := validateReplyImportData(&data, parentCreateAt, maxPostSize); err == nil {
   911  		t.Fatal("Should have failed due parent with newer create-at value.")
   912  	}
   913  }
   914  
   915  func TestImportValidatePostImportData(t *testing.T) {
   916  	maxPostSize := 10000
   917  
   918  	// Test with minimum required valid properties.
   919  	data := PostImportData{
   920  		Team:     ptrStr("teamname"),
   921  		Channel:  ptrStr("channelname"),
   922  		User:     ptrStr("username"),
   923  		Message:  ptrStr("message"),
   924  		CreateAt: ptrInt64(model.GetMillis()),
   925  	}
   926  	if err := validatePostImportData(&data, maxPostSize); err != nil {
   927  		t.Fatal("Validation failed but should have been valid.")
   928  	}
   929  
   930  	// Test with missing required properties.
   931  	data = PostImportData{
   932  		Channel:  ptrStr("channelname"),
   933  		User:     ptrStr("username"),
   934  		Message:  ptrStr("message"),
   935  		CreateAt: ptrInt64(model.GetMillis()),
   936  	}
   937  	if err := validatePostImportData(&data, maxPostSize); err == nil {
   938  		t.Fatal("Should have failed due to missing required property.")
   939  	}
   940  
   941  	data = PostImportData{
   942  		Team:     ptrStr("teamname"),
   943  		User:     ptrStr("username"),
   944  		Message:  ptrStr("message"),
   945  		CreateAt: ptrInt64(model.GetMillis()),
   946  	}
   947  	if err := validatePostImportData(&data, maxPostSize); err == nil {
   948  		t.Fatal("Should have failed due to missing required property.")
   949  	}
   950  
   951  	data = PostImportData{
   952  		Team:     ptrStr("teamname"),
   953  		Channel:  ptrStr("channelname"),
   954  		Message:  ptrStr("message"),
   955  		CreateAt: ptrInt64(model.GetMillis()),
   956  	}
   957  	if err := validatePostImportData(&data, maxPostSize); err == nil {
   958  		t.Fatal("Should have failed due to missing required property.")
   959  	}
   960  
   961  	data = PostImportData{
   962  		Team:     ptrStr("teamname"),
   963  		Channel:  ptrStr("channelname"),
   964  		User:     ptrStr("username"),
   965  		CreateAt: ptrInt64(model.GetMillis()),
   966  	}
   967  	if err := validatePostImportData(&data, maxPostSize); err == nil {
   968  		t.Fatal("Should have failed due to missing required property.")
   969  	}
   970  
   971  	data = PostImportData{
   972  		Team:    ptrStr("teamname"),
   973  		Channel: ptrStr("channelname"),
   974  		User:    ptrStr("username"),
   975  		Message: ptrStr("message"),
   976  	}
   977  	if err := validatePostImportData(&data, maxPostSize); err == nil {
   978  		t.Fatal("Should have failed due to missing required property.")
   979  	}
   980  
   981  	// Test with invalid message.
   982  	data = PostImportData{
   983  		Team:     ptrStr("teamname"),
   984  		Channel:  ptrStr("channelname"),
   985  		User:     ptrStr("username"),
   986  		Message:  ptrStr(strings.Repeat("0", maxPostSize+1)),
   987  		CreateAt: ptrInt64(model.GetMillis()),
   988  	}
   989  	if err := validatePostImportData(&data, maxPostSize); err == nil {
   990  		t.Fatal("Should have failed due to too long message.")
   991  	}
   992  
   993  	// Test with invalid CreateAt
   994  	data = PostImportData{
   995  		Team:     ptrStr("teamname"),
   996  		Channel:  ptrStr("channelname"),
   997  		User:     ptrStr("username"),
   998  		Message:  ptrStr("message"),
   999  		CreateAt: ptrInt64(0),
  1000  	}
  1001  	if err := validatePostImportData(&data, maxPostSize); err == nil {
  1002  		t.Fatal("Should have failed due to 0 create-at value.")
  1003  	}
  1004  
  1005  	// Test with valid all optional parameters.
  1006  	reactions := []ReactionImportData{ReactionImportData{
  1007  		User:      ptrStr("username"),
  1008  		EmojiName: ptrStr("emoji"),
  1009  		CreateAt:  ptrInt64(model.GetMillis()),
  1010  	}}
  1011  	replies := []ReplyImportData{ReplyImportData{
  1012  		User:     ptrStr("username"),
  1013  		Message:  ptrStr("message"),
  1014  		CreateAt: ptrInt64(model.GetMillis()),
  1015  	}}
  1016  	data = PostImportData{
  1017  		Team:      ptrStr("teamname"),
  1018  		Channel:   ptrStr("channelname"),
  1019  		User:      ptrStr("username"),
  1020  		Message:   ptrStr("message"),
  1021  		CreateAt:  ptrInt64(model.GetMillis()),
  1022  		Reactions: &reactions,
  1023  		Replies:   &replies,
  1024  	}
  1025  	if err := validatePostImportData(&data, maxPostSize); err != nil {
  1026  		t.Fatal("Should have succeeded.")
  1027  	}
  1028  }
  1029  
  1030  func TestImportValidateDirectChannelImportData(t *testing.T) {
  1031  
  1032  	// Test with valid number of members for direct message.
  1033  	data := DirectChannelImportData{
  1034  		Members: &[]string{
  1035  			model.NewId(),
  1036  			model.NewId(),
  1037  		},
  1038  	}
  1039  	if err := validateDirectChannelImportData(&data); err != nil {
  1040  		t.Fatal("Validation failed but should have been valid.")
  1041  	}
  1042  
  1043  	// Test with valid number of members for group message.
  1044  	data = DirectChannelImportData{
  1045  		Members: &[]string{
  1046  			model.NewId(),
  1047  			model.NewId(),
  1048  			model.NewId(),
  1049  		},
  1050  	}
  1051  	if err := validateDirectChannelImportData(&data); err != nil {
  1052  		t.Fatal("Validation failed but should have been valid.")
  1053  	}
  1054  
  1055  	// Test with all the combinations of optional parameters.
  1056  	data = DirectChannelImportData{
  1057  		Members: &[]string{
  1058  			model.NewId(),
  1059  			model.NewId(),
  1060  		},
  1061  		Header: ptrStr("Channel Header Here"),
  1062  	}
  1063  	if err := validateDirectChannelImportData(&data); err != nil {
  1064  		t.Fatal("Should have succeeded with valid optional properties.")
  1065  	}
  1066  
  1067  	// Test with invalid Header.
  1068  	data.Header = ptrStr(strings.Repeat("abcdefghij ", 103))
  1069  	if err := validateDirectChannelImportData(&data); err == nil {
  1070  		t.Fatal("Should have failed due to too long header.")
  1071  	}
  1072  
  1073  	// Test with different combinations of invalid member counts.
  1074  	data = DirectChannelImportData{
  1075  		Members: &[]string{},
  1076  	}
  1077  	if err := validateDirectChannelImportData(&data); err == nil {
  1078  		t.Fatal("Validation should have failed due to invalid number of members.")
  1079  	}
  1080  
  1081  	data = DirectChannelImportData{
  1082  		Members: &[]string{
  1083  			model.NewId(),
  1084  		},
  1085  	}
  1086  	if err := validateDirectChannelImportData(&data); err == nil {
  1087  		t.Fatal("Validation should have failed due to invalid number of members.")
  1088  	}
  1089  
  1090  	data = DirectChannelImportData{
  1091  		Members: &[]string{
  1092  			model.NewId(),
  1093  			model.NewId(),
  1094  			model.NewId(),
  1095  			model.NewId(),
  1096  			model.NewId(),
  1097  			model.NewId(),
  1098  			model.NewId(),
  1099  			model.NewId(),
  1100  			model.NewId(),
  1101  		},
  1102  	}
  1103  	if err := validateDirectChannelImportData(&data); err == nil {
  1104  		t.Fatal("Validation should have failed due to invalid number of members.")
  1105  	}
  1106  
  1107  	// Test with invalid FavoritedBy
  1108  	member1 := model.NewId()
  1109  	member2 := model.NewId()
  1110  	data = DirectChannelImportData{
  1111  		Members: &[]string{
  1112  			member1,
  1113  			member2,
  1114  		},
  1115  		FavoritedBy: &[]string{
  1116  			member1,
  1117  			model.NewId(),
  1118  		},
  1119  	}
  1120  	if err := validateDirectChannelImportData(&data); err == nil {
  1121  		t.Fatal("Validation should have failed due to non-member favorited.")
  1122  	}
  1123  
  1124  	// Test with valid FavoritedBy
  1125  	data = DirectChannelImportData{
  1126  		Members: &[]string{
  1127  			member1,
  1128  			member2,
  1129  		},
  1130  		FavoritedBy: &[]string{
  1131  			member1,
  1132  			member2,
  1133  		},
  1134  	}
  1135  	if err := validateDirectChannelImportData(&data); err != nil {
  1136  		t.Fatal(err)
  1137  	}
  1138  }
  1139  
  1140  func TestImportValidateDirectPostImportData(t *testing.T) {
  1141  	maxPostSize := 10000
  1142  
  1143  	// Test with minimum required valid properties.
  1144  	data := DirectPostImportData{
  1145  		ChannelMembers: &[]string{
  1146  			model.NewId(),
  1147  			model.NewId(),
  1148  		},
  1149  		User:     ptrStr("username"),
  1150  		Message:  ptrStr("message"),
  1151  		CreateAt: ptrInt64(model.GetMillis()),
  1152  	}
  1153  	if err := validateDirectPostImportData(&data, maxPostSize); err != nil {
  1154  		t.Fatal("Validation failed but should have been valid.")
  1155  	}
  1156  
  1157  	// Test with missing required properties.
  1158  	data = DirectPostImportData{
  1159  		User:     ptrStr("username"),
  1160  		Message:  ptrStr("message"),
  1161  		CreateAt: ptrInt64(model.GetMillis()),
  1162  	}
  1163  	if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
  1164  		t.Fatal("Should have failed due to missing required property.")
  1165  	}
  1166  
  1167  	data = DirectPostImportData{
  1168  		ChannelMembers: &[]string{
  1169  			model.NewId(),
  1170  			model.NewId(),
  1171  		},
  1172  		Message:  ptrStr("message"),
  1173  		CreateAt: ptrInt64(model.GetMillis()),
  1174  	}
  1175  	if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
  1176  		t.Fatal("Should have failed due to missing required property.")
  1177  	}
  1178  
  1179  	data = DirectPostImportData{
  1180  		ChannelMembers: &[]string{
  1181  			model.NewId(),
  1182  			model.NewId(),
  1183  		},
  1184  		User:     ptrStr("username"),
  1185  		CreateAt: ptrInt64(model.GetMillis()),
  1186  	}
  1187  	if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
  1188  		t.Fatal("Should have failed due to missing required property.")
  1189  	}
  1190  
  1191  	data = DirectPostImportData{
  1192  		ChannelMembers: &[]string{
  1193  			model.NewId(),
  1194  			model.NewId(),
  1195  		},
  1196  		User:    ptrStr("username"),
  1197  		Message: ptrStr("message"),
  1198  	}
  1199  	if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
  1200  		t.Fatal("Should have failed due to missing required property.")
  1201  	}
  1202  
  1203  	// Test with invalid numbers of channel members.
  1204  	data = DirectPostImportData{
  1205  		ChannelMembers: &[]string{},
  1206  		User:           ptrStr("username"),
  1207  		Message:        ptrStr("message"),
  1208  		CreateAt:       ptrInt64(model.GetMillis()),
  1209  	}
  1210  	if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
  1211  		t.Fatal("Should have failed due to unsuitable number of members.")
  1212  	}
  1213  
  1214  	data = DirectPostImportData{
  1215  		ChannelMembers: &[]string{
  1216  			model.NewId(),
  1217  		},
  1218  		User:     ptrStr("username"),
  1219  		Message:  ptrStr("message"),
  1220  		CreateAt: ptrInt64(model.GetMillis()),
  1221  	}
  1222  	if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
  1223  		t.Fatal("Should have failed due to unsuitable number of members.")
  1224  	}
  1225  
  1226  	data = DirectPostImportData{
  1227  		ChannelMembers: &[]string{
  1228  			model.NewId(),
  1229  			model.NewId(),
  1230  			model.NewId(),
  1231  			model.NewId(),
  1232  			model.NewId(),
  1233  			model.NewId(),
  1234  			model.NewId(),
  1235  			model.NewId(),
  1236  			model.NewId(),
  1237  			model.NewId(),
  1238  		},
  1239  		User:     ptrStr("username"),
  1240  		Message:  ptrStr("message"),
  1241  		CreateAt: ptrInt64(model.GetMillis()),
  1242  	}
  1243  	if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
  1244  		t.Fatal("Should have failed due to unsuitable number of members.")
  1245  	}
  1246  
  1247  	// Test with group message number of members.
  1248  	data = DirectPostImportData{
  1249  		ChannelMembers: &[]string{
  1250  			model.NewId(),
  1251  			model.NewId(),
  1252  			model.NewId(),
  1253  		},
  1254  		User:     ptrStr("username"),
  1255  		Message:  ptrStr("message"),
  1256  		CreateAt: ptrInt64(model.GetMillis()),
  1257  	}
  1258  	if err := validateDirectPostImportData(&data, maxPostSize); err != nil {
  1259  		t.Fatal("Validation failed but should have been valid.")
  1260  	}
  1261  
  1262  	// Test with invalid message.
  1263  	data = DirectPostImportData{
  1264  		ChannelMembers: &[]string{
  1265  			model.NewId(),
  1266  			model.NewId(),
  1267  		},
  1268  		User:     ptrStr("username"),
  1269  		Message:  ptrStr(strings.Repeat("0", maxPostSize+1)),
  1270  		CreateAt: ptrInt64(model.GetMillis()),
  1271  	}
  1272  	if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
  1273  		t.Fatal("Should have failed due to too long message.")
  1274  	}
  1275  
  1276  	// Test with invalid CreateAt
  1277  	data = DirectPostImportData{
  1278  		ChannelMembers: &[]string{
  1279  			model.NewId(),
  1280  			model.NewId(),
  1281  		},
  1282  		User:     ptrStr("username"),
  1283  		Message:  ptrStr("message"),
  1284  		CreateAt: ptrInt64(0),
  1285  	}
  1286  	if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
  1287  		t.Fatal("Should have failed due to 0 create-at value.")
  1288  	}
  1289  
  1290  	// Test with invalid FlaggedBy
  1291  	member1 := model.NewId()
  1292  	member2 := model.NewId()
  1293  	data = DirectPostImportData{
  1294  		ChannelMembers: &[]string{
  1295  			member1,
  1296  			member2,
  1297  		},
  1298  		FlaggedBy: &[]string{
  1299  			member1,
  1300  			model.NewId(),
  1301  		},
  1302  		User:     ptrStr("username"),
  1303  		Message:  ptrStr("message"),
  1304  		CreateAt: ptrInt64(model.GetMillis()),
  1305  	}
  1306  	if err := validateDirectPostImportData(&data, maxPostSize); err == nil {
  1307  		t.Fatal("Validation should have failed due to non-member flagged.")
  1308  	}
  1309  
  1310  	// Test with valid FlaggedBy
  1311  	data = DirectPostImportData{
  1312  		ChannelMembers: &[]string{
  1313  			member1,
  1314  			member2,
  1315  		},
  1316  		FlaggedBy: &[]string{
  1317  			member1,
  1318  			member2,
  1319  		},
  1320  		User:     ptrStr("username"),
  1321  		Message:  ptrStr("message"),
  1322  		CreateAt: ptrInt64(model.GetMillis()),
  1323  	}
  1324  	if err := validateDirectPostImportData(&data, maxPostSize); err != nil {
  1325  		t.Fatal(err)
  1326  	}
  1327  
  1328  	// Test with valid all optional parameters.
  1329  	reactions := []ReactionImportData{ReactionImportData{
  1330  		User:      ptrStr("username"),
  1331  		EmojiName: ptrStr("emoji"),
  1332  		CreateAt:  ptrInt64(model.GetMillis()),
  1333  	}}
  1334  	replies := []ReplyImportData{ReplyImportData{
  1335  		User:     ptrStr("username"),
  1336  		Message:  ptrStr("message"),
  1337  		CreateAt: ptrInt64(model.GetMillis()),
  1338  	}}
  1339  	data = DirectPostImportData{
  1340  		ChannelMembers: &[]string{
  1341  			member1,
  1342  			member2,
  1343  		},
  1344  		FlaggedBy: &[]string{
  1345  			member1,
  1346  			member2,
  1347  		},
  1348  		User:      ptrStr("username"),
  1349  		Message:   ptrStr("message"),
  1350  		CreateAt:  ptrInt64(model.GetMillis()),
  1351  		Reactions: &reactions,
  1352  		Replies:   &replies,
  1353  	}
  1354  
  1355  	if err := validateDirectPostImportData(&data, maxPostSize); err != nil {
  1356  		t.Fatal(err)
  1357  	}
  1358  }
  1359  
  1360  func TestImportValidateEmojiImportData(t *testing.T) {
  1361  	data := EmojiImportData{
  1362  		Name:  ptrStr("parrot"),
  1363  		Image: ptrStr("/path/to/image"),
  1364  	}
  1365  
  1366  	err := validateEmojiImportData(&data)
  1367  	assert.Nil(t, err, "Validation should succeed")
  1368  
  1369  	*data.Name = "smiley"
  1370  	err = validateEmojiImportData(&data)
  1371  	assert.NotNil(t, err)
  1372  
  1373  	*data.Name = ""
  1374  	err = validateEmojiImportData(&data)
  1375  	assert.NotNil(t, err)
  1376  
  1377  	*data.Name = ""
  1378  	*data.Image = ""
  1379  	err = validateEmojiImportData(&data)
  1380  	assert.NotNil(t, err)
  1381  
  1382  	*data.Image = "/path/to/image"
  1383  	data.Name = nil
  1384  	err = validateEmojiImportData(&data)
  1385  	assert.NotNil(t, err)
  1386  
  1387  	data.Name = ptrStr("parrot")
  1388  	data.Image = nil
  1389  	err = validateEmojiImportData(&data)
  1390  	assert.NotNil(t, err)
  1391  }