github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/app/slackimport_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 "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 ) 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, "O") 120 require.NoError(t, err) 121 assert.Equal(t, 6, len(channels)) 122 } 123 124 func TestSlackParseDirectMessages(t *testing.T) { 125 file, err := os.Open("tests/slack-import-test-direct-messages.json") 126 require.NoError(t, err) 127 defer file.Close() 128 129 channels, err := SlackParseChannels(file, "D") 130 require.NoError(t, err) 131 assert.Equal(t, 4, len(channels)) 132 } 133 134 func TestSlackParsePrivateChannels(t *testing.T) { 135 file, err := os.Open("tests/slack-import-test-private-channels.json") 136 require.NoError(t, err) 137 defer file.Close() 138 139 channels, err := SlackParseChannels(file, "P") 140 require.NoError(t, err) 141 assert.Equal(t, 1, len(channels)) 142 } 143 144 func TestSlackParseGroupDirectMessages(t *testing.T) { 145 file, err := os.Open("tests/slack-import-test-group-direct-messages.json") 146 require.NoError(t, err) 147 defer file.Close() 148 149 channels, err := SlackParseChannels(file, "G") 150 require.NoError(t, err) 151 assert.Equal(t, 3, len(channels)) 152 } 153 154 func TestSlackParseUsers(t *testing.T) { 155 file, err := os.Open("tests/slack-import-test-users.json") 156 require.NoError(t, err) 157 defer file.Close() 158 159 users, err := SlackParseUsers(file) 160 require.NoError(t, err) 161 assert.Equal(t, 11, len(users)) 162 } 163 164 func TestSlackParsePosts(t *testing.T) { 165 file, err := os.Open("tests/slack-import-test-posts.json") 166 require.NoError(t, err) 167 defer file.Close() 168 169 posts, err := SlackParsePosts(file) 170 require.NoError(t, err) 171 assert.Equal(t, 9, len(posts)) 172 } 173 174 func TestSlackParseMultipleAttachments(t *testing.T) { 175 file, err := os.Open("tests/slack-import-test-posts.json") 176 require.NoError(t, err) 177 defer file.Close() 178 179 posts, err := SlackParsePosts(file) 180 require.NoError(t, err) 181 assert.Equal(t, 2, len(posts[8].Files)) 182 } 183 184 func TestSlackSanitiseChannelProperties(t *testing.T) { 185 c1 := model.Channel{ 186 DisplayName: "display-name", 187 Name: "name", 188 Purpose: "The channel purpose", 189 Header: "The channel header", 190 } 191 192 c1s := SlackSanitiseChannelProperties(c1) 193 assert.Equal(t, c1, c1s) 194 195 c2 := model.Channel{ 196 DisplayName: strings.Repeat("abcdefghij", 7), 197 Name: strings.Repeat("abcdefghij", 7), 198 Purpose: strings.Repeat("0123456789", 30), 199 Header: strings.Repeat("0123456789", 120), 200 } 201 202 c2s := SlackSanitiseChannelProperties(c2) 203 assert.Equal(t, model.Channel{ 204 DisplayName: strings.Repeat("abcdefghij", 6) + "abcd", 205 Name: strings.Repeat("abcdefghij", 6) + "abcd", 206 Purpose: strings.Repeat("0123456789", 25), 207 Header: strings.Repeat("0123456789", 102) + "0123", 208 }, c2s) 209 } 210 211 func TestSlackConvertPostsMarkup(t *testing.T) { 212 input := make(map[string][]SlackPost) 213 input["test"] = []SlackPost{ 214 { 215 Text: "This message contains a link to <https://google.com|Google>.", 216 }, 217 { 218 Text: "This message contains a mailto link to <mailto:me@example.com|me@example.com> in it.", 219 }, 220 { 221 Text: "This message contains a *bold* word.", 222 }, 223 { 224 Text: "This is not a * bold * word.", 225 }, 226 { 227 Text: `There is *no bold word 228 in this*.`, 229 }, 230 { 231 Text: "*This* is not a*bold* word.*This* is a bold word, *and* this; *and* this too.", 232 }, 233 { 234 Text: "This message contains a ~strikethrough~ word.", 235 }, 236 { 237 Text: "This is not a ~ strikethrough ~ word.", 238 }, 239 { 240 Text: `There is ~no strikethrough word 241 in this~.`, 242 }, 243 { 244 Text: "~This~ is not a~strikethrough~ word.~This~ is a strikethrough word, ~and~ this; ~and~ this too.", 245 }, 246 { 247 Text: `This message contains multiple paragraphs blockquotes 248 >>>first 249 second 250 third`, 251 }, 252 { 253 Text: `This message contains single paragraph blockquotes 254 >something 255 >another thing`, 256 }, 257 { 258 Text: "This message has no > block quote", 259 }, 260 } 261 262 expectedOutput := make(map[string][]SlackPost) 263 expectedOutput["test"] = []SlackPost{ 264 { 265 Text: "This message contains a link to [Google](https://google.com).", 266 }, 267 { 268 Text: "This message contains a mailto link to [me@example.com](mailto:me@example.com) in it.", 269 }, 270 { 271 Text: "This message contains a **bold** word.", 272 }, 273 { 274 Text: "This is not a * bold * word.", 275 }, 276 { 277 Text: `There is *no bold word 278 in this*.`, 279 }, 280 { 281 Text: "**This** is not a*bold* word.**This** is a bold word, **and** this; **and** this too.", 282 }, 283 { 284 Text: "This message contains a ~~strikethrough~~ word.", 285 }, 286 { 287 Text: "This is not a ~ strikethrough ~ word.", 288 }, 289 { 290 Text: `There is ~no strikethrough word 291 in this~.`, 292 }, 293 { 294 Text: "~~This~~ is not a~strikethrough~ word.~~This~~ is a strikethrough word, ~~and~~ this; ~~and~~ this too.", 295 }, 296 { 297 Text: `This message contains multiple paragraphs blockquotes 298 >first 299 >second 300 >third`, 301 }, 302 { 303 Text: `This message contains single paragraph blockquotes 304 >something 305 >another thing`, 306 }, 307 { 308 Text: "This message has no > block quote", 309 }, 310 } 311 312 assert.Equal(t, expectedOutput, SlackConvertPostsMarkup(input)) 313 } 314 315 func TestOldImportChannel(t *testing.T) { 316 th := Setup(t).InitBasic() 317 defer th.TearDown() 318 319 u1 := th.CreateUser() 320 u2 := th.CreateUser() 321 t.Run("No panic on direct channel", func(t *testing.T) { 322 ch := th.CreateDmChannel(u1) 323 users := map[string]*model.User{ 324 th.BasicUser.Id: th.BasicUser, 325 } 326 sCh := SlackChannel{ 327 Id: "someid", 328 Members: []string{u1.Id, "randomID"}, 329 Creator: "randomID2", 330 } 331 332 _ = th.App.oldImportChannel(ch, sCh, users) 333 }) 334 335 t.Run("No panic on direct channel with 1 member", func(t *testing.T) { 336 ch := th.CreateDmChannel(u1) 337 users := map[string]*model.User{ 338 th.BasicUser.Id: th.BasicUser, 339 } 340 sCh := SlackChannel{ 341 Id: "someid", 342 Members: []string{th.BasicUser.Id}, 343 Creator: "randomID2", 344 } 345 346 _ = th.App.oldImportChannel(ch, sCh, users) 347 }) 348 349 t.Run("No panic on group channel", func(t *testing.T) { 350 ch := th.CreateGroupChannel(u1, u2) 351 users := map[string]*model.User{ 352 th.BasicUser.Id: th.BasicUser, 353 } 354 sCh := SlackChannel{ 355 Id: "someid", 356 Members: []string{th.BasicUser.Id}, 357 Creator: "randomID2", 358 } 359 _ = th.App.oldImportChannel(ch, sCh, users) 360 }) 361 }