-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpcd_test.go
365 lines (313 loc) · 9.47 KB
/
pcd_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright © 2018 Kristof Vannotten <kristof@vannotten.be>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package pcd
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"os"
urlpath "path"
"path/filepath"
"strings"
"testing"
)
func TestSync(t *testing.T) {
ts := testServer()
defer ts.Close()
podcast := &Podcast{
ID: 1,
Name: "test",
Feed: ts.URL,
Path: randomPath(t),
}
if err := podcast.Sync(); err != nil {
t.Errorf("Expected to be able to sync, but could not sync: %#v", err)
}
}
func TestSyncBadRequest(t *testing.T) {
podcast := &Podcast{
ID: 1,
Name: "test",
Feed: "foo",
}
if err := podcast.Sync(); err != ErrRequestFailed {
t.Errorf("Expected %#v, but got: %#v", ErrRequestFailed, err)
}
}
func TestSyncPathIssue(t *testing.T) {
// test is running as root and will fail
// so just skip it
if os.Geteuid() == 0 {
t.Skip()
}
ts := testServer()
defer ts.Close()
podcast := &Podcast{
ID: 1,
Name: "test",
Feed: ts.URL,
Path: "/root/access/required",
}
if err := podcast.Sync(); err != ErrFilesystemError {
t.Errorf("Expected %#v, but got: %#v", ErrFilesystemError, err)
}
}
func TestCredentials(t *testing.T) {
ts := testServerWithBasicAuth("test", "foo")
podcast := &Podcast{
ID: 1,
Name: "test",
Feed: ts.URL,
Username: "incorrect",
Password: "incorrect",
}
if err := podcast.Sync(); err != ErrAccessDenied {
t.Errorf("Expected %#v, but got: %#v", ErrAccessDenied, err)
}
}
func TestFeedRequestIssues(t *testing.T) {
table := []struct {
name string
got int
want error
}{
{"feed not found", http.StatusNotFound, ErrFeedNotFound},
{"issue with server", http.StatusInternalServerError, ErrRequestFailed},
{"random non-200", http.StatusBadRequest, ErrRequestFailed},
}
for _, e := range table {
t.Run(e.name, func(t *testing.T) {
ts := testServerWithStatusCode(e.got)
podcast := &Podcast{
Feed: ts.URL,
}
if err := podcast.Sync(); err != e.want {
t.Errorf("Expected %#v, but got: %#v", e.want, err)
}
ts.Close()
})
}
}
func TestEpisodes(t *testing.T) {
ts := testServer()
podcast := &Podcast{
Name: "test",
Feed: ts.URL,
Path: randomPath(t),
}
if err := podcast.Sync(); err != nil {
t.Errorf("Expected no error, but got: %#v", err)
}
if len(podcast.Episodes) != 1 {
t.Errorf("Expected to have 1 episode, but got: %d", len(podcast.Episodes))
}
if podcast.Episodes[0].Title != "Title of Podcast Episode" {
t.Errorf("Expected episode to have title 'Title of Podcast Episode', but got: %s", podcast.Episodes[0].Title)
}
}
func TestPodcastString(t *testing.T) {
now := "Thu, 10 Nov 2016 19:41:48 -0700"
podcast := &Podcast{
Name: "test",
Episodes: []Episode{{Title: "foo", Date: now}},
}
if !strings.Contains(podcast.String(), podcast.Name) {
t.Error("Expected podcast name to be in the output")
}
if !strings.Contains(podcast.String(), podcast.Episodes[0].Title) {
t.Error("Expected episode title to be in the output")
}
if !strings.Contains(podcast.String(), podcast.Episodes[0].Date) {
t.Error("Expected date to be in the output")
}
t.Run("extra long name", func(t *testing.T) {
podcast.Episodes[0].Title = "this is a really long name that should be truncated to something shorter to fit the screen of the user"
if !strings.Contains(podcast.String(), podcast.Episodes[0].Title[0:titleLength-4]) {
t.Error("Expected truncated episode title to be in the output")
}
})
}
func TestLoad(t *testing.T) {
ts := testServer()
defer ts.Close()
podcast := &Podcast{
ID: 1,
Name: "test",
Feed: ts.URL,
Path: randomPath(t),
}
if err := podcast.Sync(); err != nil {
t.Errorf("Could not sync podcast: %#v", err)
}
podcast.Episodes = nil
if len(podcast.Episodes) != 0 {
t.Errorf("Episodes should be empty")
}
faultyFeedPath := randomPath(t)
f, err := os.Create(filepath.Join(faultyFeedPath, ".feed"))
if err != nil {
t.Error("Error while creating temporary file...")
}
f.WriteString("invalid data")
f.Close()
table := []struct {
name string
path string
err error
checkEpisodes bool
}{
{"valid load", podcast.Path, nil, true},
{"valid path but no .feed file", randomPath(t), ErrCouldNotReadFromCache, false},
{"invalid path", "/root/access", ErrCouldNotReadFromCache, false},
{"valid path but faulty .feed file", faultyFeedPath, ErrCouldNotReadFromCache, false},
}
for _, e := range table {
t.Run(e.name, func(t *testing.T) {
podcast.Path = e.path
if err := podcast.Load(); err != e.err {
t.Errorf("Expected %#v, but got: %#v", e.err, err)
}
if e.checkEpisodes && len(podcast.Episodes) != 1 {
t.Errorf("Expected 1 podcast episode to be present, but got: %d", len(podcast.Episodes))
}
})
}
}
func TestDownload(t *testing.T) {
r := strings.NewReader(Podcastfeed)
episodes, err := parseEpisodes(r)
if err != nil {
t.Errorf("Expected no error, but got: %#v", err)
}
episode := episodes[0]
ts := testServer()
episode.URL = ts.URL + "/sample.mp3"
if err := episode.Download(randomPath(t), nil, ""); err != nil {
t.Errorf("Expected to be able to download episode, but got: %#v", err)
}
}
func TestFilenameTemplateDownload(t *testing.T) {
r := strings.NewReader(Podcastfeed)
episodes, err := parseEpisodes(r)
if err != nil {
t.Errorf("Expected no error, but got: %#v", err)
}
episode := episodes[0]
ts := testServer()
table := []struct {
name string
episode *Episode
fileNameTemplate string
episodeURL string
expectedTitle string
}{
{"filenameTest", &episode, "{{ .name }}", ts.URL + "/randomFile.mp3", "randomFile.mp3"},
{"titleExtension", &Episode{Title: "Fooman"}, "{{ .title }}{{ .ext }}", ts.URL + "/randomFile.mp3", "Fooman.mp3"},
{"Invalid Title", &Episode{Title: "this/is/a&test/""}, "{{ .title }}{{ .ext }}", ts.URL + "/randomFile.mp3", "this_is_a_amp_test__#34.mp3"},
{"Sugar title", &Episode{Title: "What is sugar? 'Added' sugar? 'Natural' sugar?"}, "{{ .title }}{{ .ext }}", ts.URL + "/podcast.mp3", "What is sugar? 'Added' sugar? 'Natural' sugar?.mp3"},
}
for _, e := range table {
t.Run(e.name, func(t *testing.T) {
parsedTitle := urlpath.Base(e.episodeURL)
str := parseFilenameTemplate(e.fileNameTemplate, e.episode, parsedTitle)
if str != e.expectedTitle {
t.Errorf("Expected title to be %s, but got %s", e.expectedTitle, str)
}
e.episode.URL = e.episodeURL
if err := e.episode.Download(randomPath(t), nil, e.fileNameTemplate); err != nil {
t.Errorf("Expected to be able to download episode, but got: %#v", err)
}
})
}
}
func TestInvalidDownload(t *testing.T) {
table := []struct {
name string
episode *Episode
path string
writer io.Writer
err error
}{
{"invalid url", &Episode{URL: "invalid"}, randomPath(t), nil, ErrCouldNotDownload},
{"invalid status", &Episode{URL: testServerWithStatusCode(404).URL + "/sample.mp3"}, randomPath(t), nil, ErrCouldNotDownload},
{"invalid path", &Episode{URL: testServer().URL}, "/root/access", nil, ErrFilesystemError},
}
for _, e := range table {
t.Run(e.name, func(t *testing.T) {
if err := e.episode.Download(e.path, nil, ""); err != e.err {
t.Errorf("Expected %#v, but got %#v", e.err, err)
}
})
}
}
func TestParseEpisodes(t *testing.T) {
table := []struct {
name string
feed io.Reader
err error
hasEpisodes bool
title string
}{
{"valid feed", strings.NewReader(Podcastfeed), nil, true, "Title of Podcast Episode"},
{"invalid feed should return error", strings.NewReader("some invalid text"), ErrCouldNotParseContent, false, ""},
{"invalid episode should just continue", strings.NewReader(invalidEpisodesFeed), nil, false, ""},
}
for _, e := range table {
t.Run(e.name, func(t *testing.T) {
episodes, err := parseEpisodes(e.feed)
if err != e.err {
t.Errorf("Expected %#v, but got: %#v", e.err, err)
}
if e.hasEpisodes {
if episodes == nil {
t.Errorf("Expected episodes to not be nil")
}
if len(episodes) != 1 {
t.Errorf("Expected 1 episode, but got: %#v", len(episodes))
}
if episodes[0].Title != e.title {
t.Errorf("Expected title to be '%s', but got %s", e.title, episodes[0].Title)
}
}
})
}
}
func TestGobEncodeAndDecode(t *testing.T) {
episode := Episode{
Title: "test",
}
content, err := toGOB64([]Episode{episode})
if err != nil {
t.Errorf("Didn't expect an error, but got: %#v", err)
}
data, err := ioutil.ReadAll(content)
if err != nil {
t.Errorf("Didn't expect an error, but got: %#v", err)
}
episodes, err := fromGOB64(bytes.NewBuffer(data))
if err != nil {
t.Errorf("Didn't expect an error, but got: %#v", err)
}
if episodes == nil {
t.Errorf("Expected episodes to not be nil")
}
if len(episodes) != 1 {
t.Errorf("Expected 1 episode, but got: %#v", len(episodes))
}
if episodes[0].Title != episode.Title {
t.Errorf("Expected title to be %s, but got %s", episode.Title, episodes[0].Title)
}
}