github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/services/slackimport/slackimport_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package slackimport
     5  
     6  import (
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/mattermost/mattermost-server/v5/model"
    15  	"github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
    16  )
    17  
    18  func TestSlackConvertTimeStamp(t *testing.T) {
    19  	assert.EqualValues(t, slackConvertTimeStamp("1469785419.000033"), 1469785419000)
    20  }
    21  
    22  func TestSlackConvertChannelName(t *testing.T) {
    23  	for _, tc := range []struct {
    24  		nameInput string
    25  		idInput   string
    26  		output    string
    27  	}{
    28  		{"test-channel", "C0G08DLQH", "test-channel"},
    29  		{"_test_channel_", "C0G04DLQH", "test_channel"},
    30  		{"__test", "C0G07DLQH", "test"},
    31  		{"-t", "C0G06DLQH", "slack-channel-t"},
    32  		{"a", "C0G05DLQH", "slack-channel-a"},
    33  		{"случайный", "C0G05DLQD", "c0g05dlqd"},
    34  	} {
    35  		assert.Equal(t, slackConvertChannelName(tc.nameInput, tc.idInput), tc.output, "nameInput = %v", tc.nameInput)
    36  	}
    37  }
    38  
    39  func TestSlackConvertUserMentions(t *testing.T) {
    40  	users := []slackUser{
    41  		{Id: "U00000A0A", Username: "firstuser"},
    42  		{Id: "U00000B1B", Username: "seconduser"},
    43  	}
    44  
    45  	posts := map[string][]slackPost{
    46  		"test-channel": {
    47  			{
    48  				Text: "<!channel>: Hi guys.",
    49  			},
    50  			{
    51  				Text: "Calling <!here|@here>.",
    52  			},
    53  			{
    54  				Text: "Yo <!everyone>.",
    55  			},
    56  			{
    57  				Text: "Regular user test <@U00000B1B|seconduser> and <@U00000A0A>.",
    58  			},
    59  		},
    60  	}
    61  
    62  	expectedPosts := map[string][]slackPost{
    63  		"test-channel": {
    64  			{
    65  				Text: "@channel: Hi guys.",
    66  			},
    67  			{
    68  				Text: "Calling @here.",
    69  			},
    70  			{
    71  				Text: "Yo @all.",
    72  			},
    73  			{
    74  				Text: "Regular user test @seconduser and @firstuser.",
    75  			},
    76  		},
    77  	}
    78  
    79  	assert.Equal(t, expectedPosts, slackConvertUserMentions(users, posts))
    80  }
    81  
    82  func TestSlackConvertChannelMentions(t *testing.T) {
    83  	channels := []slackChannel{
    84  		{Id: "C000AA00A", Name: "one"},
    85  		{Id: "C000BB11B", Name: "two"},
    86  	}
    87  
    88  	posts := map[string][]slackPost{
    89  		"test-channel": {
    90  			{
    91  				Text: "Go to <#C000AA00A>.",
    92  			},
    93  			{
    94  				User: "U00000A0A",
    95  				Text: "Try <#C000BB11B|two> for this.",
    96  			},
    97  		},
    98  	}
    99  
   100  	expectedPosts := map[string][]slackPost{
   101  		"test-channel": {
   102  			{
   103  				Text: "Go to ~one.",
   104  			},
   105  			{
   106  				User: "U00000A0A",
   107  				Text: "Try ~two for this.",
   108  			},
   109  		},
   110  	}
   111  
   112  	assert.Equal(t, expectedPosts, slackConvertChannelMentions(channels, posts))
   113  }
   114  
   115  func TestSlackParseChannels(t *testing.T) {
   116  	file, err := os.Open("tests/slack-import-test-channels.json")
   117  	require.NoError(t, err)
   118  	defer file.Close()
   119  
   120  	channels, err := slackParseChannels(file, "O")
   121  	require.NoError(t, err)
   122  	assert.Equal(t, 6, len(channels))
   123  }
   124  
   125  func TestSlackParseDirectMessages(t *testing.T) {
   126  	file, err := os.Open("tests/slack-import-test-direct-messages.json")
   127  	require.NoError(t, err)
   128  	defer file.Close()
   129  
   130  	channels, err := slackParseChannels(file, "D")
   131  	require.NoError(t, err)
   132  	assert.Equal(t, 4, len(channels))
   133  }
   134  
   135  func TestSlackParsePrivateChannels(t *testing.T) {
   136  	file, err := os.Open("tests/slack-import-test-private-channels.json")
   137  	require.NoError(t, err)
   138  	defer file.Close()
   139  
   140  	channels, err := slackParseChannels(file, "P")
   141  	require.NoError(t, err)
   142  	assert.Equal(t, 1, len(channels))
   143  }
   144  
   145  func TestSlackParseGroupDirectMessages(t *testing.T) {
   146  	file, err := os.Open("tests/slack-import-test-group-direct-messages.json")
   147  	require.NoError(t, err)
   148  	defer file.Close()
   149  
   150  	channels, err := slackParseChannels(file, "G")
   151  	require.NoError(t, err)
   152  	assert.Equal(t, 3, len(channels))
   153  }
   154  
   155  func TestSlackParseUsers(t *testing.T) {
   156  	file, err := os.Open("tests/slack-import-test-users.json")
   157  	require.NoError(t, err)
   158  	defer file.Close()
   159  
   160  	users, err := slackParseUsers(file)
   161  	require.NoError(t, err)
   162  	assert.Equal(t, 11, len(users))
   163  }
   164  
   165  func TestSlackParsePosts(t *testing.T) {
   166  	file, err := os.Open("tests/slack-import-test-posts.json")
   167  	require.NoError(t, err)
   168  	defer file.Close()
   169  
   170  	posts, err := slackParsePosts(file)
   171  	require.NoError(t, err)
   172  	assert.Equal(t, 9, len(posts))
   173  }
   174  
   175  func TestSlackParseMultipleAttachments(t *testing.T) {
   176  	file, err := os.Open("tests/slack-import-test-posts.json")
   177  	require.NoError(t, err)
   178  	defer file.Close()
   179  
   180  	posts, err := slackParsePosts(file)
   181  	require.NoError(t, err)
   182  	assert.Equal(t, 2, len(posts[8].Files))
   183  }
   184  
   185  func TestSlackSanitiseChannelProperties(t *testing.T) {
   186  	c1 := model.Channel{
   187  		DisplayName: "display-name",
   188  		Name:        "name",
   189  		Purpose:     "The channel purpose",
   190  		Header:      "The channel header",
   191  	}
   192  
   193  	c1s := slackSanitiseChannelProperties(c1)
   194  	assert.Equal(t, c1, c1s)
   195  
   196  	c2 := model.Channel{
   197  		DisplayName: strings.Repeat("abcdefghij", 7),
   198  		Name:        strings.Repeat("abcdefghij", 7),
   199  		Purpose:     strings.Repeat("0123456789", 30),
   200  		Header:      strings.Repeat("0123456789", 120),
   201  	}
   202  
   203  	c2s := slackSanitiseChannelProperties(c2)
   204  	assert.Equal(t, model.Channel{
   205  		DisplayName: strings.Repeat("abcdefghij", 6) + "abcd",
   206  		Name:        strings.Repeat("abcdefghij", 6) + "abcd",
   207  		Purpose:     strings.Repeat("0123456789", 25),
   208  		Header:      strings.Repeat("0123456789", 102) + "0123",
   209  	}, c2s)
   210  }
   211  
   212  func TestSlackConvertPostsMarkup(t *testing.T) {
   213  	input := make(map[string][]slackPost)
   214  	input["test"] = []slackPost{
   215  		{
   216  			Text: "This message contains a link to <https://google.com|Google>.",
   217  		},
   218  		{
   219  			Text: "This message contains a mailto link to <mailto:me@example.com|me@example.com> in it.",
   220  		},
   221  		{
   222  			Text: "This message contains a *bold* word.",
   223  		},
   224  		{
   225  			Text: "This is not a * bold * word.",
   226  		},
   227  		{
   228  			Text: `There is *no bold word
   229  in this*.`,
   230  		},
   231  		{
   232  			Text: "*This* is not a*bold* word.*This* is a bold word, *and* this; *and* this too.",
   233  		},
   234  		{
   235  			Text: "This message contains a ~strikethrough~ word.",
   236  		},
   237  		{
   238  			Text: "This is not a ~ strikethrough ~ word.",
   239  		},
   240  		{
   241  			Text: `There is ~no strikethrough word
   242  in this~.`,
   243  		},
   244  		{
   245  			Text: "~This~ is not a~strikethrough~ word.~This~ is a strikethrough word, ~and~ this; ~and~ this too.",
   246  		},
   247  		{
   248  			Text: `This message contains multiple paragraphs blockquotes
   249  &gt;&gt;&gt;first
   250  second
   251  third`,
   252  		},
   253  		{
   254  			Text: `This message contains single paragraph blockquotes
   255  &gt;something
   256  &gt;another thing`,
   257  		},
   258  		{
   259  			Text: "This message has no > block quote",
   260  		},
   261  	}
   262  
   263  	expectedOutput := make(map[string][]slackPost)
   264  	expectedOutput["test"] = []slackPost{
   265  		{
   266  			Text: "This message contains a link to [Google](https://google.com).",
   267  		},
   268  		{
   269  			Text: "This message contains a mailto link to [me@example.com](mailto:me@example.com) in it.",
   270  		},
   271  		{
   272  			Text: "This message contains a **bold** word.",
   273  		},
   274  		{
   275  			Text: "This is not a * bold * word.",
   276  		},
   277  		{
   278  			Text: `There is *no bold word
   279  in this*.`,
   280  		},
   281  		{
   282  			Text: "**This** is not a*bold* word.**This** is a bold word, **and** this; **and** this too.",
   283  		},
   284  		{
   285  			Text: "This message contains a ~~strikethrough~~ word.",
   286  		},
   287  		{
   288  			Text: "This is not a ~ strikethrough ~ word.",
   289  		},
   290  		{
   291  			Text: `There is ~no strikethrough word
   292  in this~.`,
   293  		},
   294  		{
   295  			Text: "~~This~~ is not a~strikethrough~ word.~~This~~ is a strikethrough word, ~~and~~ this; ~~and~~ this too.",
   296  		},
   297  		{
   298  			Text: `This message contains multiple paragraphs blockquotes
   299  >first
   300  >second
   301  >third`,
   302  		},
   303  		{
   304  			Text: `This message contains single paragraph blockquotes
   305  >something
   306  >another thing`,
   307  		},
   308  		{
   309  			Text: "This message has no > block quote",
   310  		},
   311  	}
   312  
   313  	assert.Equal(t, expectedOutput, slackConvertPostsMarkup(input))
   314  }
   315  
   316  func TestOldImportChannel(t *testing.T) {
   317  	u1 := &model.User{
   318  		Id:       model.NewId(),
   319  		Username: "test-user-1",
   320  	}
   321  	u2 := &model.User{
   322  		Id:       model.NewId(),
   323  		Username: "test-user-2",
   324  	}
   325  	store := &mocks.Store{}
   326  	config := &model.Config{}
   327  	config.SetDefaults()
   328  
   329  	t.Run("No panic on direct channel", func(t *testing.T) {
   330  		//ch := th.CreateDmChannel(u1)
   331  		ch := &model.Channel{
   332  			Type: model.CHANNEL_DIRECT,
   333  			Name: "test-channel",
   334  		}
   335  		users := map[string]*model.User{
   336  			u2.Id: u2,
   337  		}
   338  		sCh := slackChannel{
   339  			Id:      "someid",
   340  			Members: []string{u1.Id, "randomID"},
   341  			Creator: "randomID2",
   342  		}
   343  
   344  		actions := Actions{}
   345  
   346  		importer := New(store, actions, config)
   347  		_ = importer.oldImportChannel(ch, sCh, users)
   348  	})
   349  
   350  	t.Run("No panic on direct channel with 1 member", func(t *testing.T) {
   351  		ch := &model.Channel{
   352  			Type: model.CHANNEL_DIRECT,
   353  			Name: "test-channel",
   354  		}
   355  		users := map[string]*model.User{
   356  			u1.Id: u1,
   357  		}
   358  		sCh := slackChannel{
   359  			Id:      "someid",
   360  			Members: []string{u1.Id},
   361  			Creator: "randomID2",
   362  		}
   363  
   364  		actions := Actions{}
   365  
   366  		importer := New(store, actions, config)
   367  		_ = importer.oldImportChannel(ch, sCh, users)
   368  	})
   369  
   370  	t.Run("No panic on group channel", func(t *testing.T) {
   371  		ch := &model.Channel{
   372  			Type: model.CHANNEL_GROUP,
   373  			Name: "test-channel",
   374  		}
   375  		users := map[string]*model.User{
   376  			u1.Id: u1,
   377  		}
   378  		sCh := slackChannel{
   379  			Id:      "someid",
   380  			Members: []string{u1.Id},
   381  			Creator: "randomID2",
   382  		}
   383  		actions := Actions{}
   384  
   385  		importer := New(store, actions, config)
   386  		_ = importer.oldImportChannel(ch, sCh, users)
   387  	})
   388  }