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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
|
#define ENABLE_IMPLICIT_FILEPATH_CONVERSIONS
#include "queuemanager.h"
#include "3rd-party/catch.hpp"
#include "test_helpers/chmod.h"
#include "test_helpers/envvar.h"
#include "test_helpers/misc.h"
#include "test_helpers/tempfile.h"
#include "cache.h"
#include "configcontainer.h"
#include "rssfeed.h"
#include "rssitem.h"
using namespace newsboat;
SCENARIO("Smoke test for QueueManager", "[QueueManager]")
{
GIVEN("A fresh instance of QueueManager") {
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
RssItem item(rsscache.get());
const std::string enclosure_url("https://example.com/podcast.mp3");
item.set_enclosure_url(enclosure_url);
item.set_enclosure_type("audio/mpeg");
RssFeed feed(rsscache.get(), "https://example.com/news.atom");
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
THEN("the queue file is not automatically created") {
REQUIRE_FALSE(test_helpers::file_exists(queue_file.get_path()));
}
WHEN("enqueue_url() is called") {
const auto result = manager.enqueue_url(item, feed);
THEN("the return value indicates success") {
REQUIRE(result.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result.extra_info == "");
}
THEN("the queue file contains an entry") {
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(test_helpers::starts_with(enclosure_url, lines[0]));
REQUIRE(test_helpers::ends_with(R"(/podcast.mp3")", lines[0]));
REQUIRE(lines[1] == "");
}
THEN("the item is marked as enqueued") {
REQUIRE(item.enqueued());
}
}
WHEN("enqueue_url() is called on the same item twice in a row") {
manager.enqueue_url(item, feed);
const auto result = manager.enqueue_url(item, feed);
THEN("the second call indicates that the enclosure is already in the queue") {
REQUIRE(result.status == EnqueueStatus::URL_QUEUED_ALREADY);
REQUIRE(result.extra_info == enclosure_url);
}
THEN("the queue file contains a single entry") {
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(test_helpers::starts_with(enclosure_url, lines[0]));
REQUIRE(test_helpers::ends_with(R"(/podcast.mp3")", lines[0]));
REQUIRE(lines[1] == "");
}
THEN("the item is marked as enqueued") {
REQUIRE(item.enqueued());
}
}
WHEN("enqueue_url() is called for multiple different items") {
const auto result = manager.enqueue_url(item, feed);
RssItem item2(rsscache.get());
item2.set_enclosure_url("https://www.example.com/another.mp3");
item2.set_enclosure_type("audio/mpeg");
const auto result2 = manager.enqueue_url(item2, feed);
RssItem item3(rsscache.get());
item3.set_enclosure_url("https://joe.example.com/vacation.jpg");
item3.set_enclosure_type("image/jpeg");
const auto result3 = manager.enqueue_url(item3, feed);
THEN("return values indicate success") {
REQUIRE(result.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result.extra_info == "");
REQUIRE(result2.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result2.extra_info == "");
REQUIRE(result3.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result3.extra_info == "");
}
THEN("the queue file contains three entries") {
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 4);
REQUIRE(lines[0] != "");
REQUIRE(lines[1] != "");
REQUIRE(lines[2] != "");
REQUIRE(lines[3] == "");
}
THEN("items are marked as enqueued") {
REQUIRE(item.enqueued());
REQUIRE(item2.enqueued());
REQUIRE(item3.enqueued());
}
}
}
}
SCENARIO("enqueue_url() errors if the filename is already used", "[QueueManager]")
{
GIVEN("Pristine QueueManager and two RssItems") {
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
RssItem item1(rsscache.get());
const std::string enclosure_url1("https://example.com/podcast.mp3");
item1.set_enclosure_url(enclosure_url1);
item1.set_enclosure_type("audio/mpeg");
RssItem item2(rsscache.get());
const std::string enclosure_url2("https://example.com/~joe/podcast.mp3");
item2.set_enclosure_url(enclosure_url2);
item2.set_enclosure_type("audio/mpeg");
RssFeed feed(rsscache.get(), "https://example.com/news.atom");
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
WHEN("first item is enqueued") {
const auto result = manager.enqueue_url(item1, feed);
THEN("the return value indicates success") {
REQUIRE(result.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result.extra_info == "");
}
THEN("the queue file contains a corresponding entry") {
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(test_helpers::starts_with(enclosure_url1, lines[0]));
REQUIRE(test_helpers::ends_with(R"(/podcast.mp3")", lines[0]));
REQUIRE(lines[1] == "");
}
THEN("the item is marked as enqueued") {
REQUIRE(item1.enqueued());
}
AND_WHEN("second item is enqueued") {
const auto result = manager.enqueue_url(item2, feed);
THEN("the return value indicates that the filename is already used") {
REQUIRE(result.status == EnqueueStatus::OUTPUT_FILENAME_USED_ALREADY);
// That field contains a path to the temporary directory,
// so we simply check that it's not empty.
REQUIRE(result.extra_info != "");
}
THEN("the queue file still contains a single entry") {
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(test_helpers::starts_with(enclosure_url1, lines[0]));
REQUIRE(test_helpers::ends_with(R"(/podcast.mp3")", lines[0]));
REQUIRE(lines[1] == "");
}
THEN("the item is NOT marked as enqueued") {
REQUIRE_FALSE(item2.enqueued());
}
}
}
}
}
SCENARIO("enqueue_url() errors if the queue file can't be opened for writing",
"[QueueManager]")
{
GIVEN("Pristine QueueManager, an RssItem, and an uneditable queue file") {
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
RssItem item(rsscache.get());
item.set_enclosure_url("https://example.com/podcast.mp3");
item.set_enclosure_type("audio/mpeg");
RssFeed feed(rsscache.get(), "https://example.com/news.atom");
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
test_helpers::copy_file("data/empty-file", queue_file.get_path());
// The file is read-only
test_helpers::Chmod uneditable_queue_file(queue_file.get_path(), 0444);
WHEN("enqueue_url() is called") {
const auto result = manager.enqueue_url(item, feed);
THEN("the return value indicates the file couldn't be written to") {
REQUIRE(result.status == EnqueueStatus::QUEUE_FILE_OPEN_ERROR);
REQUIRE(result.extra_info == queue_file.get_path().to_locale_string());
}
THEN("the item is NOT marked as enqueued") {
REQUIRE_FALSE(item.enqueued());
}
}
}
}
TEST_CASE("QueueManager puts files into a location configured by `download-path`",
"[QueueManager]")
{
ConfigContainer cfg;
SECTION("path with a slash at the end") {
cfg.set_configvalue("download-path", "/tmp/nonexistent-newsboat/");
}
SECTION("path without a slash at the end") {
cfg.set_configvalue("download-path", "/tmp/nonexistent-newsboat");
}
auto rsscache = Cache::in_memory(cfg);
RssItem item1(rsscache.get());
const std::string enclosure_url1("https://example.com/podcast.mp3");
item1.set_enclosure_url(enclosure_url1);
item1.set_enclosure_type("audio/mpeg");
RssItem item2(rsscache.get());
const std::string enclosure_url2("https://example.com/~joe/podcast.ogg");
item2.set_enclosure_url(enclosure_url2);
item2.set_enclosure_type("audio/vorbis");
RssFeed feed(rsscache.get(), "https://example.com/podcasts.atom");
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
const auto result1 = manager.enqueue_url(item1, feed);
REQUIRE(result1.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result1.extra_info == "");
REQUIRE(item1.enqueued());
const auto result2 = manager.enqueue_url(item2, feed);
REQUIRE(result2.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result2.extra_info == "");
REQUIRE(item2.enqueued());
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 3);
REQUIRE(lines[0] ==
R"(https://example.com/podcast.mp3 "/tmp/nonexistent-newsboat/podcast.mp3")");
REQUIRE(lines[1] ==
R"(https://example.com/~joe/podcast.ogg "/tmp/nonexistent-newsboat/podcast.ogg")");
REQUIRE(lines[2] == "");
}
TEST_CASE("QueueManager names files according to the `download-filename-format` setting",
"[QueueManager]")
{
ConfigContainer cfg;
// We set the download-path to a fixed value to ensure that we know
// *exactly* how the result should look.
cfg.set_configvalue("download-path", "/example/");
auto rsscache = Cache::in_memory(cfg);
RssItem item(rsscache.get());
item.set_enclosure_url("https://example.com/~adam/podcast.mp3");
item.set_enclosure_type("audio/mpeg");
auto feed = std::make_shared<RssFeed>(rsscache.get(), "https://example.com/podcasts.atom");
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
SECTION("%n for current feed title, with slashes replaced by underscores") {
cfg.set_configvalue("download-filename-format", "%n");
feed->set_title("Feed title/theme");
manager.enqueue_url(item, *feed);
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(lines[0] ==
R"(https://example.com/~adam/podcast.mp3 "/example/Feed title_theme")");
REQUIRE(lines[1] == "");
}
SECTION("%h for the enclosure URL's hostname") {
cfg.set_configvalue("download-filename-format", "%h");
manager.enqueue_url(item, *feed);
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(lines[0] == R"(https://example.com/~adam/podcast.mp3 "/example/example.com")");
REQUIRE(lines[1] == "");
}
SECTION("%u for the enclosure URL's basename") {
cfg.set_configvalue("download-filename-format", "%u");
manager.enqueue_url(item, *feed);
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(lines[0] == R"(https://example.com/~adam/podcast.mp3 "/example/podcast.mp3")");
REQUIRE(lines[1] == "");
}
SECTION("%F, %m, %b, %d, %H, %M, %S, %y, and %Y to render items's publication date with strftime") {
// %H is sensitive to the timezone, so reset it to UTC for a time being
test_helpers::TzEnvVar tzEnv;
tzEnv.set("UTC");
cfg.set_configvalue("download-filename-format", "%F, %m, %b, %d, %H, %M, %S, %y, and %Y");
// Tue, 06 Apr 2021 15:38:19 +0000
item.set_pubDate(1617723499);
manager.enqueue_url(item, *feed);
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(lines[0] ==
R"(https://example.com/~adam/podcast.mp3 "/example/2021-04-06, 04, Apr, 06, 15, 38, 19, 21, and 2021")");
REQUIRE(lines[1] == "");
}
SECTION("%t for item title, with slashes replaced by underscores") {
cfg.set_configvalue("download-filename-format", "%t");
item.set_title("Rain/snow/sun in a single day");
manager.enqueue_url(item, *feed);
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(lines[0] ==
R"(https://example.com/~adam/podcast.mp3 "/example/Rain_snow_sun in a single day")");
REQUIRE(lines[1] == "");
}
SECTION("%e for enclosure's filename extension") {
cfg.set_configvalue("download-filename-format", "%e");
manager.enqueue_url(item, *feed);
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(lines[0] ==
R"(https://example.com/~adam/podcast.mp3 "/example/mp3")");
REQUIRE(lines[1] == "");
}
SECTION("%N for the feed's title (even if `feed` passed into a function is different)") {
cfg.set_configvalue("download-filename-format", "%N");
SECTION("`feed` argument is irrelevant") {
feed->set_title("Relevant feed");
item.set_feedptr(feed);
RssFeed irrelevant_feed(rsscache.get(), "https://example.com/podcasts.atom");
irrelevant_feed.set_title("Irrelevant");
manager.enqueue_url(item, irrelevant_feed);
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(lines[0] ==
R"(https://example.com/~adam/podcast.mp3 "/example/Relevant feed")");
REQUIRE(lines[1] == "");
}
SECTION("`feed` argument is relevant") {
feed->set_title("Relevant feed");
item.set_feedptr(feed);
manager.enqueue_url(item, *feed);
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(lines[0] ==
R"(https://example.com/~adam/podcast.mp3 "/example/Relevant feed")");
REQUIRE(lines[1] == "");
}
}
}
TEST_CASE("autoenqueue() adds all enclosures of all items to the queue", "[QueueManager]")
{
GIVEN("Pristine QueueManager and a feed of three items") {
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
RssFeed feed(rsscache.get(), "https://example.com/podcasts.atom");
auto item1 = std::make_shared<RssItem>(rsscache.get());
item1->set_enclosure_url("https://example.com/~adam/podcast.mp3");
item1->set_enclosure_type("audio/mpeg");
feed.add_item(item1);
auto item2 = std::make_shared<RssItem>(rsscache.get());
item2->set_enclosure_url("https://example.com/episode.ogg");
item2->set_enclosure_type("audio/vorbis");
feed.add_item(item2);
auto item3 = std::make_shared<RssItem>(rsscache.get());
item3->set_enclosure_url("https://example.com/~fae/painting.jpg");
item3->set_enclosure_type("");
feed.add_item(item3);
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
WHEN("autoenqueue() is called") {
const auto result = manager.autoenqueue(feed);
THEN("the return value indicates success") {
REQUIRE(result.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result.extra_info == "");
}
THEN("the queue file contains three entries") {
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 4);
REQUIRE(lines[0] != "");
REQUIRE(lines[1] != "");
REQUIRE(lines[2] != "");
REQUIRE(lines[3] == "");
}
THEN("items are marked as enqueued") {
REQUIRE(item1->enqueued());
REQUIRE(item2->enqueued());
REQUIRE(item3->enqueued());
}
}
}
}
SCENARIO("autoenqueue() errors if the filename is already used", "[QueueManager]")
{
GIVEN("Pristine QueueManager and a feed of two items") {
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
RssFeed feed(rsscache.get(), "https://example.com/news.atom");
auto item1 = std::make_shared<RssItem>(rsscache.get());
const std::string enclosure_url1("https://example.com/podcast.mp3");
item1->set_enclosure_url(enclosure_url1);
item1->set_enclosure_type("audio/mpeg");
feed.add_item(item1);
auto item2 = std::make_shared<RssItem>(rsscache.get());
const std::string enclosure_url2("https://example.com/~joe/podcast.mp3");
item2->set_enclosure_url(enclosure_url2);
item2->set_enclosure_type("audio/mpeg");
feed.add_item(item2);
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
WHEN("autoenqueue() is called") {
const auto result = manager.autoenqueue(feed);
THEN("the return value indicates that the filename is already used") {
REQUIRE(result.status == EnqueueStatus::OUTPUT_FILENAME_USED_ALREADY);
// That field contains a path to the temporary directory,
// so we simply check that it's not empty.
REQUIRE(result.extra_info != "");
}
THEN("the queue file still contains a single entry") {
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 2);
REQUIRE(test_helpers::starts_with(enclosure_url1, lines[0]));
REQUIRE(test_helpers::ends_with(R"(/podcast.mp3")", lines[0]));
REQUIRE(lines[1] == "");
}
THEN("the first item is enqueued, the second one isn't") {
REQUIRE(item1->enqueued());
REQUIRE_FALSE(item2->enqueued());
}
}
}
}
SCENARIO("autoenqueue() errors if the queue file can't be opened for writing",
"[QueueManager]")
{
GIVEN("Pristine QueueManager, a single-item feed, and an uneditable queue file") {
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
RssFeed feed(rsscache.get(), "https://example.com/news.atom");
auto item = std::make_shared<RssItem>(rsscache.get());
item->set_enclosure_url("https://example.com/podcast.mp3");
item->set_enclosure_type("audio/mpeg");
feed.add_item(item);
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
test_helpers::copy_file("data/empty-file", queue_file.get_path());
// The file is read-only
test_helpers::Chmod uneditable_queue_file(queue_file.get_path(), 0444);
WHEN("autoenqueue() is called") {
const auto result = manager.autoenqueue(feed);
THEN("the return value indicates the file couldn't be written to") {
REQUIRE(result.status == EnqueueStatus::QUEUE_FILE_OPEN_ERROR);
REQUIRE(result.extra_info == queue_file.get_path().to_locale_string());
}
THEN("the item is NOT marked as enqueued") {
REQUIRE_FALSE(item->enqueued());
}
}
}
}
TEST_CASE("autoenqueue() skips already-enqueued items", "[QueueManager]")
{
ConfigContainer cfg;
// We set the download-path to a fixed value to ensure that we know
// *exactly* how the result should look.
cfg.set_configvalue("download-path", "/example/");
auto rsscache = Cache::in_memory(cfg);
RssFeed feed(rsscache.get(), "https://example.com/news.atom");
auto item1 = std::make_shared<RssItem>(rsscache.get());
item1->set_enclosure_url("https://example.com/podcast.mp3");
item1->set_enclosure_type("audio/mpeg");
feed.add_item(item1);
auto item2 = std::make_shared<RssItem>(rsscache.get());
item2->set_enclosure_url("https://example.com/podcast2.mp3");
item2->set_enclosure_type("audio/mpeg");
item2->set_enqueued(true);
feed.add_item(item2);
auto item3 = std::make_shared<RssItem>(rsscache.get());
item3->set_enclosure_url("https://example.com/podcast3.mp3");
item3->set_enclosure_type("audio/mpeg");
feed.add_item(item3);
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
const auto result = manager.autoenqueue(feed);
REQUIRE(result.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result.extra_info == "");
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 3);
REQUIRE(lines[0] == R"(https://example.com/podcast.mp3 "/example/podcast.mp3")");
REQUIRE(lines[1] == R"(https://example.com/podcast3.mp3 "/example/podcast3.mp3")");
REQUIRE(lines[2] == "");
}
TEST_CASE("autoenqueue() only enqueues HTTP and HTTPS URLs", "[QueueManager]")
{
ConfigContainer cfg;
// We set the download-path to a fixed value to ensure that we know
// *exactly* how the result should look.
cfg.set_configvalue("download-path", "/example/");
auto rsscache = Cache::in_memory(cfg);
RssFeed feed(rsscache.get(), "https://example.com/news.atom");
auto item1 = std::make_shared<RssItem>(rsscache.get());
item1->set_enclosure_url("https://example.com/podcast.mp3");
item1->set_enclosure_type("audio/mpeg");
feed.add_item(item1);
auto item2 = std::make_shared<RssItem>(rsscache.get());
item2->set_enclosure_url("http://example.com/podcast2.mp3");
item2->set_enclosure_type("audio/mpeg");
feed.add_item(item2);
auto item3 = std::make_shared<RssItem>(rsscache.get());
item3->set_enclosure_url("ftp://user@example.com/podcast3.mp3");
item3->set_enclosure_type("audio/mpeg");
feed.add_item(item3);
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
const auto result = manager.autoenqueue(feed);
REQUIRE(result.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result.extra_info == "");
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 3);
REQUIRE(lines[0] == R"(https://example.com/podcast.mp3 "/example/podcast.mp3")");
REQUIRE(lines[1] == R"(http://example.com/podcast2.mp3 "/example/podcast2.mp3")");
REQUIRE(lines[2] == "");
}
TEST_CASE("autoenqueue() does not enqueue items with an invalid podcast type",
"[QueueManager]")
{
GIVEN("Pristine QueueManager and a feed of three items with one of them having an image enclosure") {
ConfigContainer cfg;
auto rsscache = Cache::in_memory(cfg);
RssFeed feed(rsscache.get(), "https://example.com/news.atom");
auto item1 = std::make_shared<RssItem>(rsscache.get());
item1->set_enclosure_url("https://example.com/podcast1.mp3");
item1->set_enclosure_type("audio/mpeg");
feed.add_item(item1);
auto item2 = std::make_shared<RssItem>(rsscache.get());
item2->set_enclosure_url("http://example.com/not-a-podcast.jpg");
item2->set_enclosure_type("image/jpeg");
feed.add_item(item2);
auto item3 = std::make_shared<RssItem>(rsscache.get());
item3->set_enclosure_url("https://example.com/podcast2.mp3");
item3->set_enclosure_type("audio/mpeg");
feed.add_item(item3);
auto item4 = std::make_shared<RssItem>(rsscache.get());
item4->set_enclosure_url("https://example.com/podcast3.mp3");
item4->set_enclosure_type("");
feed.add_item(item4);
test_helpers::TempFile queue_file;
QueueManager manager(&cfg, queue_file.get_path());
WHEN("autoenqueue() is called") {
const auto result = manager.autoenqueue(feed);
THEN("the return value indicates success") {
REQUIRE(result.status == EnqueueStatus::QUEUED_SUCCESSFULLY);
REQUIRE(result.extra_info == "");
}
THEN("the queue file contains two entries") {
REQUIRE(test_helpers::file_exists(queue_file.get_path()));
const auto lines = test_helpers::file_contents(queue_file.get_path());
REQUIRE(lines.size() == 4);
REQUIRE(lines[0] != "");
REQUIRE(lines[1] != "");
REQUIRE(lines[2] != "");
REQUIRE(lines[3] == "");
}
THEN("items with an empty or a valid podcast type are marked as enqueued") {
REQUIRE(item1->enqueued());
REQUIRE_FALSE(item2->enqueued());
REQUIRE(item3->enqueued());
REQUIRE(item4->enqueued());
}
}
}
}
|