github.com/vnforks/kid@v5.11.1+incompatible/app/slackimport_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     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/model"
    15  )
    16  
    17  func TestSlackConvertTimeStamp(t *testing.T) {
    18  	assert.EqualValues(t, SlackConvertTimeStamp("1469785419.000033"), 1469785419000)
    19  }
    20  
    21  func TestSlackConvertChannelName(t *testing.T) {
    22  	for _, tc := range []struct {
    23  		nameInput string
    24  		idInput   string
    25  		output    string
    26  	}{
    27  		{"test-channel", "C0G08DLQH", "test-channel"},
    28  		{"_test_channel_", "C0G04DLQH", "test_channel"},
    29  		{"__test", "C0G07DLQH", "test"},
    30  		{"-t", "C0G06DLQH", "slack-channel-t"},
    31  		{"a", "C0G05DLQH", "slack-channel-a"},
    32  		{"случайный", "C0G05DLQD", "c0g05dlqd"},
    33  	} {
    34  		assert.Equal(t, SlackConvertChannelName(tc.nameInput, tc.idInput), tc.output, "nameInput = %v", tc.nameInput)
    35  	}
    36  }
    37  
    38  func TestSlackConvertUserMentions(t *testing.T) {
    39  	users := []SlackUser{
    40  		{Id: "U00000A0A", Username: "firstuser"},
    41  		{Id: "U00000B1B", Username: "seconduser"},
    42  	}
    43  
    44  	posts := map[string][]SlackPost{
    45  		"test-channel": {
    46  			{
    47  				Text: "<!channel>: Hi guys.",
    48  			},
    49  			{
    50  				Text: "Calling <!here|@here>.",
    51  			},
    52  			{
    53  				Text: "Yo <!everyone>.",
    54  			},
    55  			{
    56  				Text: "Regular user test <@U00000B1B|seconduser> and <@U00000A0A>.",
    57  			},
    58  		},
    59  	}
    60  
    61  	expectedPosts := map[string][]SlackPost{
    62  		"test-channel": {
    63  			{
    64  				Text: "@channel: Hi guys.",
    65  			},
    66  			{
    67  				Text: "Calling @here.",
    68  			},
    69  			{
    70  				Text: "Yo @all.",
    71  			},
    72  			{
    73  				Text: "Regular user test @seconduser and @firstuser.",
    74  			},
    75  		},
    76  	}
    77  
    78  	assert.Equal(t, expectedPosts, SlackConvertUserMentions(users, posts))
    79  }
    80  
    81  func TestSlackConvertChannelMentions(t *testing.T) {
    82  	channels := []SlackChannel{
    83  		{Id: "C000AA00A", Name: "one"},
    84  		{Id: "C000BB11B", Name: "two"},
    85  	}
    86  
    87  	posts := map[string][]SlackPost{
    88  		"test-channel": {
    89  			{
    90  				Text: "Go to <#C000AA00A>.",
    91  			},
    92  			{
    93  				User: "U00000A0A",
    94  				Text: "Try <#C000BB11B|two> for this.",
    95  			},
    96  		},
    97  	}
    98  
    99  	expectedPosts := map[string][]SlackPost{
   100  		"test-channel": {
   101  			{
   102  				Text: "Go to ~one.",
   103  			},
   104  			{
   105  				User: "U00000A0A",
   106  				Text: "Try ~two for this.",
   107  			},
   108  		},
   109  	}
   110  
   111  	assert.Equal(t, expectedPosts, SlackConvertChannelMentions(channels, posts))
   112  }
   113  
   114  func TestSlackParseChannels(t *testing.T) {
   115  	file, err := os.Open("tests/slack-import-test-channels.json")
   116  	require.NoError(t, err)
   117  	defer file.Close()
   118  
   119  	channels, err := SlackParseChannels(file)
   120  	require.NoError(t, err)
   121  	assert.Equal(t, 6, len(channels))
   122  }
   123  
   124  func TestSlackParseUsers(t *testing.T) {
   125  	file, err := os.Open("tests/slack-import-test-users.json")
   126  	require.NoError(t, err)
   127  	defer file.Close()
   128  
   129  	users, err := SlackParseUsers(file)
   130  	require.NoError(t, err)
   131  	assert.Equal(t, 11, len(users))
   132  }
   133  
   134  func TestSlackParsePosts(t *testing.T) {
   135  	file, err := os.Open("tests/slack-import-test-posts.json")
   136  	require.NoError(t, err)
   137  	defer file.Close()
   138  
   139  	posts, err := SlackParsePosts(file)
   140  	require.NoError(t, err)
   141  	assert.Equal(t, 8, len(posts))
   142  }
   143  
   144  func TestSlackSanitiseChannelProperties(t *testing.T) {
   145  	c1 := model.Channel{
   146  		DisplayName: "display-name",
   147  		Name:        "name",
   148  		Purpose:     "The channel purpose",
   149  		Header:      "The channel header",
   150  	}
   151  
   152  	c1s := SlackSanitiseChannelProperties(c1)
   153  	assert.Equal(t, c1, c1s)
   154  
   155  	c2 := model.Channel{
   156  		DisplayName: strings.Repeat("abcdefghij", 7),
   157  		Name:        strings.Repeat("abcdefghij", 7),
   158  		Purpose:     strings.Repeat("0123456789", 30),
   159  		Header:      strings.Repeat("0123456789", 120),
   160  	}
   161  
   162  	c2s := SlackSanitiseChannelProperties(c2)
   163  	assert.Equal(t, model.Channel{
   164  		DisplayName: strings.Repeat("abcdefghij", 6) + "abcd",
   165  		Name:        strings.Repeat("abcdefghij", 6) + "abcd",
   166  		Purpose:     strings.Repeat("0123456789", 25),
   167  		Header:      strings.Repeat("0123456789", 102) + "0123",
   168  	}, c2s)
   169  }
   170  
   171  func TestSlackConvertPostsMarkup(t *testing.T) {
   172  	input := make(map[string][]SlackPost)
   173  	input["test"] = []SlackPost{
   174  		{
   175  			Text: "This message contains a link to <https://google.com|Google>.",
   176  		},
   177  		{
   178  			Text: "This message contains a mailto link to <mailto:me@example.com|me@example.com> in it.",
   179  		},
   180  		{
   181  			Text: "This message contains a *bold* word.",
   182  		},
   183  		{
   184  			Text: "This is not a * bold * word.",
   185  		},
   186  		{
   187  			Text: `There is *no bold word
   188  in this*.`,
   189  		},
   190  		{
   191  			Text: "*This* is not a*bold* word.*This* is a bold word, *and* this; *and* this too.",
   192  		},
   193  		{
   194  			Text: "This message contains a ~strikethrough~ word.",
   195  		},
   196  		{
   197  			Text: "This is not a ~ strikethrough ~ word.",
   198  		},
   199  		{
   200  			Text: `There is ~no strikethrough word
   201  in this~.`,
   202  		},
   203  		{
   204  			Text: "~This~ is not a~strikethrough~ word.~This~ is a strikethrough word, ~and~ this; ~and~ this too.",
   205  		},
   206  		{
   207  			Text: `This message contains multiple paragraphs blockquotes
   208  &gt;&gt;&gt;first
   209  second
   210  third`,
   211  		},
   212  		{
   213  			Text: `This message contains single paragraph blockquotes
   214  &gt;something
   215  &gt;another thing`,
   216  		},
   217  		{
   218  			Text: "This message has no > block quote",
   219  		},
   220  	}
   221  
   222  	expectedOutput := make(map[string][]SlackPost)
   223  	expectedOutput["test"] = []SlackPost{
   224  		{
   225  			Text: "This message contains a link to [Google](https://google.com).",
   226  		},
   227  		{
   228  			Text: "This message contains a mailto link to [me@example.com](mailto:me@example.com) in it.",
   229  		},
   230  		{
   231  			Text: "This message contains a **bold** word.",
   232  		},
   233  		{
   234  			Text: "This is not a * bold * word.",
   235  		},
   236  		{
   237  			Text: `There is *no bold word
   238  in this*.`,
   239  		},
   240  		{
   241  			Text: "**This** is not a*bold* word.**This** is a bold word, **and** this; **and** this too.",
   242  		},
   243  		{
   244  			Text: "This message contains a ~~strikethrough~~ word.",
   245  		},
   246  		{
   247  			Text: "This is not a ~ strikethrough ~ word.",
   248  		},
   249  		{
   250  			Text: `There is ~no strikethrough word
   251  in this~.`,
   252  		},
   253  		{
   254  			Text: "~~This~~ is not a~strikethrough~ word.~~This~~ is a strikethrough word, ~~and~~ this; ~~and~~ this too.",
   255  		},
   256  		{
   257  			Text: `This message contains multiple paragraphs blockquotes
   258  >first
   259  >second
   260  >third`,
   261  		},
   262  		{
   263  			Text: `This message contains single paragraph blockquotes
   264  >something
   265  >another thing`,
   266  		},
   267  		{
   268  			Text: "This message has no > block quote",
   269  		},
   270  	}
   271  
   272  	assert.Equal(t, expectedOutput, SlackConvertPostsMarkup(input))
   273  }