forked from rusq/slackdump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_parse_test.go
162 lines (158 loc) · 3.57 KB
/
url_parse_test.go
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
package slackdump
import (
"reflect"
"testing"
)
func TestParseURL(t *testing.T) {
type args struct {
slackURL string
}
tests := []struct {
name string
args args
want *urlInfo
wantErr bool
}{
{
name: "channel",
args: args{"https://ora600.slack.com/archives/CHM82GF99"},
want: &urlInfo{Channel: "CHM82GF99"},
wantErr: false,
},
{
name: "thread",
args: args{"https://ora600.slack.com/archives/CHM82GF99/p1577694990000400"},
want: &urlInfo{Channel: "CHM82GF99", ThreadTS: "1577694990.000400"},
wantErr: false,
},
{
name: "thread with extra data in the URL",
args: args{"https://ora600.slack.com/archives/CHM82GF99/p1577694990000400/xxxx"},
want: nil,
wantErr: true,
},
{
name: "malformed thread id",
args: args{"https://ora600.slack.com/archives/CHM82GF99/1577694990000400"},
want: nil,
wantErr: true,
},
{
name: "invalid thread id",
args: args{"https://ora600.slack.com/archives/CHM82GF99/p15776949900x0400"},
want: nil,
wantErr: true,
},
{
name: "DM",
args: args{"https://ora600.slack.com/archives/DL98HT3QA"},
want: &urlInfo{Channel: "DL98HT3QA"},
wantErr: false,
},
{
name: "Invalid url",
args: args{"https://example.com"},
want: nil,
wantErr: true,
},
{
name: "invalid slack url",
args: args{"https://app.slack.com/client/THX2HTY8U/CHM82GF99/thread/CHM82GF99-1577694990.000400"},
want: nil,
wantErr: true,
},
{
name: "malformed",
args: args{"https://ora600.slack.com/archives/"},
want: nil,
wantErr: true,
},
{
name: "not a url",
args: args{"C123454321"},
want: nil,
wantErr: true,
},
{
name: "empty",
args: args{""},
want: nil,
wantErr: true,
},
{
name: "binary junk",
args: args{"\x02"},
want: nil,
wantErr: true,
},
{
name: "thread",
args: args{"https://xxxxxx.slack.com/archives/CHANNEL/p1645551829244659"},
want: &urlInfo{Channel: "CHANNEL", ThreadTS: "1645551829.244659"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseURL(tt.args.slackURL)
if (err != nil) != tt.wantErr {
t.Errorf("ParseURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseURL() got = %v, want %v", got, tt.want)
}
})
}
}
func TestURLInfo_IsThread(t *testing.T) {
type fields struct {
Channel string
ThreadTS string
}
tests := []struct {
name string
fields fields
want bool
}{
{"yes", fields{Channel: "x", ThreadTS: "x"}, true},
{"no", fields{Channel: "x", ThreadTS: ""}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := urlInfo{
Channel: tt.fields.Channel,
ThreadTS: tt.fields.ThreadTS,
}
if got := u.IsThread(); got != tt.want {
t.Errorf("URLInfo.IsThread() = %v, want %v", got, tt.want)
}
})
}
}
func TestURLInfo_IsValid(t *testing.T) {
type fields struct {
Channel string
ThreadTS string
}
tests := []struct {
name string
fields fields
want bool
}{
{"channel", fields{Channel: "x"}, true},
{"thread", fields{Channel: "x", ThreadTS: "y"}, true},
{"invalid", fields{ThreadTS: "y"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := urlInfo{
Channel: tt.fields.Channel,
ThreadTS: tt.fields.ThreadTS,
}
if got := u.IsValid(); got != tt.want {
t.Errorf("URLInfo.IsValid() = %v, want %v", got, tt.want)
}
})
}
}