summaryrefslogtreecommitdiffstats
path: root/api/slackimport_test.go
blob: 4da1fa54e8eac5aba255af6b6c58b0ffe96af0f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package api

import (
	"os"
	"testing"
)

func TestSlackConvertTimeStamp(t *testing.T) {

	testTimeStamp := "1469785419.000033"

	result := SlackConvertTimeStamp(testTimeStamp)

	if result != 1469785419000 {
		t.Fatalf("Unexpected timestamp value %v returned.", result)
	}
}

func TestSlackConvertChannelName(t *testing.T) {
	var testData = []struct {
		input  string
		output string
	}{
		{"test-channel", "test-channel"},
		{"_test_channel_", "test_channel"},
		{"__test", "test"},
		{"-t", "slack-channel-t"},
		{"a", "slack-channel-a"},
	}

	for _, td := range testData {
		if td.output != SlackConvertChannelName(td.input) {
			t.Fatalf("Did not convert channel name correctly: %v", td.input)
		}
	}
}

func TestSlackConvertUserMentions(t *testing.T) {
	users := []SlackUser{
		{Id: "U00000A0A", Username: "firstuser"},
		{Id: "U00000B1B", Username: "seconduser"},
	}

	posts := map[string][]SlackPost{
		"test-channel": {
			{
				Text: "<!channel>: Hi guys.",
			},
			{
				Text: "Calling <!here|@here>.",
			},
			{
				Text: "Yo <!everyone>.",
			},
			{
				Text: "Regular user test <@U00000B1B|seconduser> and <@U00000A0A>.",
			},
		},
	}

	expectedPosts := map[string][]SlackPost{
		"test-channel": {
			{
				Text: "@channel: Hi guys.",
			},
			{
				Text: "Calling @here.",
			},
			{
				Text: "Yo @all.",
			},
			{
				Text: "Regular user test @seconduser and @firstuser.",
			},
		},
	}

	convertedPosts := SlackConvertUserMentions(users, posts)

	for channelName, channelPosts := range convertedPosts {
		for postIdx, post := range channelPosts {
			if post.Text != expectedPosts[channelName][postIdx].Text {
				t.Fatalf("Converted post text not as expected: %v", post.Text)
			}
		}
	}
}

func TestSlackConvertChannelMentions(t *testing.T) {
	channels := []SlackChannel{
		{Id: "C000AA00A", Name: "one"},
		{Id: "C000BB11B", Name: "two"},
	}

	posts := map[string][]SlackPost{
		"test-channel": {
			{
				Text: "Go to <#C000AA00A>.",
			},
			{
				User: "U00000A0A",
				Text: "Try <#C000BB11B|two> for this.",
			},
		},
	}

	expectedPosts := map[string][]SlackPost{
		"test-channel": {
			{
				Text: "Go to !one.",
			},
			{
				Text: "Try !two for this.",
			},
		},
	}

	convertedPosts := SlackConvertChannelMentions(channels, posts)

	for channelName, channelPosts := range convertedPosts {
		for postIdx, post := range channelPosts {
			if post.Text != expectedPosts[channelName][postIdx].Text {
				t.Fatalf("Converted post text not as expected: %v", post.Text)
			}
		}
	}

}

func TestSlackParseChannels(t *testing.T) {
	file, err := os.Open("../tests/slack-import-test-channels.json")
	if err != nil {
		t.Fatalf("Failed to open data file: %v", err)
	}

	channels, err := SlackParseChannels(file)
	if err != nil {
		t.Fatalf("Error occurred parsing channels: %v", err)
	}

	if len(channels) != 6 {
		t.Fatalf("Unexpected number of channels: %v", len(channels))
	}
}

func TestSlackParseUsers(t *testing.T) {
	file, err := os.Open("../tests/slack-import-test-users.json")
	if err != nil {
		t.Fatalf("Failed to open data file: %v", err)
	}

	users, err := SlackParseUsers(file)
	if err != nil {
		t.Fatalf("Error occurred parsing users: %v", err)
	}

	if len(users) != 11 {
		t.Fatalf("Unexpected number of users: %v", len(users))
	}
}

func TestSlackParsePosts(t *testing.T) {
	file, err := os.Open("../tests/slack-import-test-posts.json")
	if err != nil {
		t.Fatalf("Failed to open data file: %v", err)
	}

	posts, err := SlackParsePosts(file)
	if err != nil {
		t.Fatalf("Error occurred parsing posts: %v", err)
	}

	if len(posts) != 8 {
		t.Fatalf("Unexpected number of posts: %v", len(posts))
	}
}