/
index.coffee
1005 lines (935 loc) · 34.8 KB
/
index.coffee
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
_ = require 'underscore'
rewire = require 'rewire'
moment = require 'moment'
{ db, fabricate, empty, fixtures } = require '../../../../test/helpers/db'
Article = rewire '../../model/index'
{ ObjectId } = require 'mongojs'
express = require 'express'
fabricateGravity = require('antigravity').fabricate
gravity = require('antigravity').server
app = require('express')()
bodyParser = require 'body-parser'
sinon = require 'sinon'
search = require '../../../../lib/elasticsearch'
describe 'Article', ->
before (done) ->
app.use '/__gravity', gravity
@server = app.listen 5000, ->
search.client.indices.create
index: 'articles_' + process.env.NODE_ENV
, ->
done()
after ->
@server.close()
search.client.indices.delete
index: 'articles_' + process.env.NODE_ENV
beforeEach (done) ->
empty ->
fabricate 'articles', _.times(10, -> {}), ->
done()
describe '#where', ->
it 'can return all articles along with total and counts', (done) ->
Article.where {}, (err, res) ->
{ total, count, results } = res
total.should.equal 10
count.should.equal 10
results[0].title.should.equal 'Top Ten Booths'
done()
it 'can return articles by an author', (done) ->
fabricate 'articles', {
author_id: aid = ObjectId '4d8cd73191a5c50ce220002a'
title: 'Hello Wurld'
}, ->
Article.where { author_id: aid.toString() }, (err, res) ->
{ total, count, results } = res
total.should.equal 11
count.should.equal 1
results[0].title.should.equal 'Hello Wurld'
done()
it 'can return articles by published', (done) ->
fabricate 'articles', _.times(3, -> { published: false, title: 'Moo baz' }), ->
Article.where { published: false }, (err, { total, count, results }) ->
total.should.equal 13
count.should.equal 3
results[0].title.should.equal 'Moo baz'
done()
it 'can change skip and limit', (done) ->
fabricate 'articles', [
{ title: 'Hello Wurld' }
{ title: 'Foo Baz' }
], ->
Article.where { offset: 1, limit: 3 }, (err, { results }) ->
results[0].title.should.equal 'Hello Wurld'
done()
it 'sorts by -updated_at by default', (done) ->
fabricate 'articles', [
{ title: 'Hello Wurld', updated_at: moment().add(1, 'days').format() }
], ->
Article.where {}, (err, { results }) ->
results[0].title.should.equal 'Hello Wurld'
done()
it 'can find articles featured to an artist', (done) ->
fabricate 'articles', [
{
title: 'Foo'
featured_artist_ids: [ObjectId('4dc98d149a96300001003033')]
}
{
title: 'Bar'
primary_featured_artist_ids: [ObjectId('4dc98d149a96300001003033')]
}
{
title: 'Baz'
featured_artist_ids: [ObjectId('4dc98d149a96300001003033'), 'abc']
primary_featured_artist_ids: [ObjectId('4dc98d149a96300001003033')]
}
], ->
Article.where(
{ artist_id: '4dc98d149a96300001003033' }
(err, { results }) ->
_.pluck(results, 'title').sort().join('').should.equal 'BarBazFoo'
done()
)
it 'can find articles featured to an artwork', (done) ->
fabricate 'articles', [
{
title: 'Foo'
featured_artwork_ids: [ObjectId('4dc98d149a96300001003033')]
}
{
title: 'Baz'
featured_artwork_ids: [ObjectId('4dc98d149a96300001003033'), 'abc']
}
], ->
Article.where(
{ artwork_id: '4dc98d149a96300001003033' }
(err, { results }) ->
_.pluck(results, 'title').sort().join('').should.equal 'BazFoo'
done()
)
it 'can find articles sorted by an attr', (done) ->
db.articles.drop ->
fabricate 'articles', [
{ title: 'C' }, { title: 'A' }, { title: 'B' }
], ->
Article.where(
{ sort: '-title' }
(err, { results }) ->
_.pluck(results, 'title').sort().join('').should.containEql 'ABC'
done()
)
it 'can find articles added to multiple fairs', (done) ->
fabricate 'articles', [
{ title: 'C', fair_ids: [ObjectId('4dc98d149a96300001003033')] }
{ title: 'A', fair_ids: [ObjectId('4dc98d149a96300001003033')] }
{ title: 'B', fair_ids: [ObjectId('4dc98d149a96300001003032')] }
], ->
Article.where(
{ fair_ids: ['4dc98d149a96300001003033', '4dc98d149a96300001003032'] }
(err, { results }) ->
_.pluck(results, 'title').sort().join('').should.equal 'ABC'
done()
)
it 'can find articles by a fair', (done) ->
fabricate 'articles', [
{ title: 'C', fair_ids: [ObjectId('4dc98d149a96300001003033')] }
{ title: 'A', fair_ids: [ObjectId('4dc98d149a96300001003033')] }
], ->
Article.where(
{ fair_id: '4dc98d149a96300001003033' }
(err, { results }) ->
_.pluck(results, 'title').sort().join('').should.equal 'AC'
done()
)
it 'can return articles by a fair programming id', (done) ->
fabricate 'articles', {
author_id: aid = ObjectId '4d8cd73191a5c50ce220002a'
title: 'Hello Wurld'
fair_programming_ids: [ ObjectId('52617c6c8b3b81f094000013') ]
}, ->
Article.where { fair_programming_id: '52617c6c8b3b81f094000013' }, (err, res) ->
{ total, count, results } = res
total.should.equal 11
count.should.equal 1
results[0].title.should.equal 'Hello Wurld'
done()
it 'can return articles by a fair artsy id', (done) ->
fabricate 'articles', {
author_id: aid = ObjectId '4d8cd73191a5c50ce220002a'
title: 'Hello Wurld'
fair_artsy_ids: [ ObjectId('53da550a726169083c0a0700') ]
}, ->
Article.where { fair_artsy_id: '53da550a726169083c0a0700' }, (err, res) ->
{ total, count, results } = res
total.should.equal 11
count.should.equal 1
results[0].title.should.equal 'Hello Wurld'
done()
it 'can return articles by a fair about id', (done) ->
fabricate 'articles', {
author_id: aid = ObjectId '4d8cd73191a5c50ce220002a'
title: 'Hello Wurld'
fair_about_ids: [ ObjectId('53da550a726169083c0a0700') ]
}, ->
Article.where { fair_about_id: '53da550a726169083c0a0700' }, (err, res) ->
{ total, count, results } = res
total.should.equal 11
count.should.equal 1
results[0].title.should.equal 'Hello Wurld'
done()
it 'can find articles added to a partner', (done) ->
fabricate 'articles', [
{ title: 'Foo', partner_ids: [ObjectId('4dc98d149a96300001003033')] }
{ title: 'Bar', partner_ids: [ObjectId('4dc98d149a96300001003033')] }
{ title: 'Baz', partner_ids: [ObjectId('4dc98d149a96300001003031')] }
], ->
Article.where(
{ partner_id: '4dc98d149a96300001003033' }
(err, { results }) ->
_.pluck(results, 'title').sort().join('').should.equal 'BarFoo'
done()
)
it 'can find articles by query', (done) ->
fabricate 'articles', [
{ thumbnail_title: 'Foo' }
{ thumbnail_title: 'Bar' }
{ thumbnail_title: 'Baz' }
], ->
Article.where(
{ q: 'fo' }
(err, { results }) ->
results[0].thumbnail_title.should.equal 'Foo'
done()
)
it 'can find articles by all_by_author', (done) ->
fabricate 'articles', [
{
author_id: ObjectId '4d8cd73191a5c50ce220002a'
contributing_authors: [ {id: ObjectId '4d8cd73191a5c50ce220002b'}]
title: 'Hello Wurld'
}
{
author_id: ObjectId '4d8cd73191a5c50ce220002b'
title: 'Hello Waaarld'
}
], =>
Article.where { all_by_author: '4d8cd73191a5c50ce220002b' }, (err, res) ->
{ total, count, results } = res
total.should.equal 12
count.should.equal 2
results[0].title.should.equal 'Hello Waaarld'
results[1].title.should.equal 'Hello Wurld'
done()
it 'can find articles by super article', (done) ->
fabricate 'articles', [
{
author_id: ObjectId '4d8cd73191a5c50ce220002a'
title: 'Hello Wurld'
is_super_article: false
}
{
author_id: ObjectId '4d8cd73191a5c50ce220002b'
title: 'Hello Waaarldie'
is_super_article: true
}
{
author_id: ObjectId '4d8cd73191a5c50ce220002b'
title: 'Hello Waaarld'
}
], =>
Article.where { is_super_article: true }, (err, res) ->
{ total, count, results } = res
total.should.equal 13
count.should.equal 1
results[0].title.should.equal 'Hello Waaarldie'
done()
it 'can find super article by article (opposite of the above test!)', (done) ->
superArticleId = ObjectId '4d7ab73191a5c50ce220001c'
childArticleId = ObjectId '4d8cd73191a5c50ce111111a'
fabricate 'articles', [
{
_id: childArticleId
author_id: ObjectId '4d8cd73191a5c50ce220002a'
title: 'Child Article'
is_super_article: false
}
{
_id: superArticleId
author_id: ObjectId '4d8cd73191a5c50ce220002b'
title: 'Super Article'
is_super_article: true
super_article:
related_articles: [childArticleId]
}
], =>
Article.where { super_article_for: childArticleId.toString() }, (err, res) ->
{ total, count, results } = res
total.should.equal 12
count.should.equal 1
results[0].title.should.equal 'Super Article'
done()
it 'can find articles by tags', (done) ->
fabricate 'articles', [
{
author_id: ObjectId '4d8cd73191a5c50ce220002a'
tags: [ 'pickle', 'samosa' ]
title: 'Hello Wuuuurld - Food'
}
{
author_id: ObjectId '4d8cd73191a5c50ce220002b'
tags: [ 'pickle', 'muffin' ]
title: 'Hello Waaarld - Food'
}
{
author_id: ObjectId '4d8cd73191a5c50ce220002c'
tags: [ 'muffin', 'lentils' ]
title: 'Hello Weeeerld - Food'
}
{
author_id: ObjectId '4d8cd73191a5c50ce220002e'
tags: [ 'radio', 'pixels' ]
title: 'Hello I am Weiiird - Electronics'
}
], =>
Article.where { tags: ['pickle', 'muffin'] }, (err, res) ->
{ total, count, results } = res
total.should.equal 14
count.should.equal 3
results[0].title.should.equal 'Hello Weeeerld - Food'
results[1].title.should.equal 'Hello Waaarld - Food'
results[2].title.should.equal 'Hello Wuuuurld - Food'
done()
it 'can find articles by artist biography', (done) ->
fabricate 'articles', [
{
title: 'Hello Wurld'
published: true
biography_for_artist_id: ObjectId '5086df098523e60002000016'
}
], ->
Article.where {
published: true
biography_for_artist_id: '5086df098523e60002000016'
}, (err, res) ->
{ total, count, results } = res
count.should.equal 1
results[0].title.should.equal 'Hello Wurld'
done()
it 'can find articles by channel_id', (done) ->
fabricate 'articles', [
{
title: 'Hello Wurld'
published: true
channel_id: ObjectId '5086df098523e60002000016'
}
], ->
Article.where {
published: true
channel_id: '5086df098523e60002000016'
}, (err, res) ->
{ total, count, results } = res
count.should.equal 1
results[0].title.should.equal 'Hello Wurld'
done()
it 'can find articles by partner_channel_id', (done) ->
fabricate 'articles', [
{
title: 'Hello Wurld'
published: true
partner_channel_id: ObjectId '5086df098523e60002000016'
}
], ->
Article.where {
published: true
channel_id: '5086df098523e60002000016'
}, (err, res) ->
{ total, count, results } = res
count.should.equal 1
results[0].title.should.equal 'Hello Wurld'
done()
describe '#find', ->
it 'finds an article by an id string', (done) ->
fabricate 'articles', { _id: ObjectId('5086df098523e60002000018') }, ->
Article.find '5086df098523e60002000018', (err, article) ->
article._id.toString().should.equal '5086df098523e60002000018'
done()
it 'can lookup an article by slug', (done) ->
fabricate 'articles', {
_id: ObjectId('5086df098523e60002000018')
slugs: ['foo-bar']
}, ->
Article.find 'foo-bar', (err, article) ->
article._id.toString().should.equal '5086df098523e60002000018'
done()
describe '#save', ->
it 'saves valid article input data', (done) ->
Article.save {
title: 'Top Ten Shows'
thumbnail_title: 'Ten Shows'
author_id: '5086df098523e60002000018'
id: '5086df098523e60002002222'
channel_id: '5086df098523e60002002223'
}, 'foo', (err, article) ->
article.title.should.equal 'Top Ten Shows'
article.channel_id.toString().should.equal '5086df098523e60002002223'
db.articles.count (err, count) ->
count.should.equal 11
done()
it 'requires an author', (done) ->
Article.save {
title: 'Top Ten Shows'
thumbnail_title: 'Ten Shows'
}, 'foo', (err, article) ->
err.message.should.containEql '"author_id" is required'
done()
it 'adds an updated_at as a date', (done) ->
Article.save {
title: 'Top Ten Shows'
thumbnail_title: 'Ten Shows'
author_id: '5086df098523e60002000018'
}, 'foo', (err, article) ->
article.updated_at.should.be.an.instanceOf(Date)
moment(article.updated_at).format('YYYY').should.equal moment().format('YYYY')
done()
it 'includes the id for a new article', (done) ->
Article.save {
title: 'Top Ten Shows'
thumbnail_title: 'Ten Shows'
author_id: '5086df098523e60002000018'
}, 'foo', (err, article) ->
return done err if err
(article._id?).should.be.ok
done()
it 'adds a slug based off the thumbnail title', (done) ->
Article.save {
title: 'Top Ten Shows'
thumbnail_title: 'Ten Shows'
author_id: '5086df098523e60002000018'
author: name: 'Craig Spaeth'
}, 'foo', (err, article) ->
return done err if err
article.slugs[0].should.equal 'craig-spaeth-ten-shows'
done()
it 'adds a slug based off a user and thumbnail title', (done) ->
fabricate 'users', { name: 'Molly'}, (err, @user) ->
Article.save {
thumbnail_title: 'Foo Baz'
author_id: @user._id
author: name: @user.name
}, 'foo', (err, article) ->
return done err if err
article.slugs[0].should.equal 'molly-foo-baz'
done()
it 'saves slug history when publishing', (done) ->
fabricate 'users', { name: 'Molly'}, (err, @user) ->
Article.save {
thumbnail_title: 'Foo Baz'
author_id: @user._id
published: false
author: name: @user.name
}, 'foo', (err, article) =>
return done err if err
Article.save {
id: article._id.toString()
thumbnail_title: 'Foo Bar Baz'
author_id: @user._id
published: true
author: name: @user.name
}, 'foo', (err, article) ->
return done err if err
article.slugs.join('').should.equal 'molly-foo-bazmolly-foo-bar-baz'
Article.find article.slugs[0], (err, article) ->
article.thumbnail_title.should.equal 'Foo Bar Baz'
done()
it 'appends the date to an article URL when its slug already exists', (done) ->
fabricate 'articles', {
id: '5086df098523e60002002222'
slugs: ['craig-spaeth-heyo']
author: name: 'Craig Spaeth'
}, ->
Article.save {
thumbnail_title: 'heyo'
author_id: '5086df098523e60002000018'
published_at: '01-01-99'
id: '5086df098523e60002002222'
author: name: 'Craig Spaeth'
}, 'foo', (err, article) ->
return done err if err
article.slugs[0].should.equal 'craig-spaeth-heyo-01-01-99'
db.articles.count (err, count) ->
count.should.equal 12
done()
it 'saves published_at when the article is published', (done) ->
Article.save {
title: 'Top Ten Shows'
thumbnail_title: 'Ten Shows'
author_id: '5086df098523e60002000018'
published: true
id: '5086df098523e60002002222'
}, 'foo', (err, article) ->
article.published_at.should.be.an.instanceOf(Date)
moment(article.published_at).format('YYYY').should
.equal moment().format('YYYY')
done()
it 'updates published_at when admin changes it', (done) ->
Article.save {
title: 'Top Ten Shows'
thumbnail_title: 'Ten Shows'
author_id: '5086df098523e60002000018'
published: true
}, 'foo', (err, article) =>
return done err if err
Article.save {
id: article._id.toString()
author_id: '5086df098523e60002000018'
published_at: moment().add(1, 'year').toDate()
}, 'foo', (err, updatedArticle) ->
return done err if err
updatedArticle.published_at.should.be.an.instanceOf(Date)
moment(updatedArticle.published_at).format('YYYY').should
.equal moment().add(1, 'year').format('YYYY')
done()
it 'doesnt save a fair unless explictly set', (done) ->
Article.save {
title: 'Top Ten Shows'
thumbnail_title: 'Ten Shows'
author_id: '5086df098523e60002000018'
fair_ids: []
published: true
}, 'foo', (err, article) ->
(article.fair_ids?).should.not.be.ok
done()
it 'escapes xss', (done) ->
body = '<h2>Hi</h2><h3>Hello</h3><p><b>Hola</b></p><p><i>Guten Tag</i></p><ol><li>Bonjour<br></li><li><a href="http://www.foo.com">Bonjour2</a></li></ol><ul><li>Aloha</li><li>Aloha Again</li></ul><h2><b><i>Good bye</i></b></h2><p><b><i>Adios</i></b></p><h3>Alfiederzen</h3><p><a href="http://foo.com">Aloha</a></p>'
badBody = '<script>alert(foo)</script><h2>Hi</h2><h3>Hello</h3><p><b>Hola</b></p><p><i>Guten Tag</i></p><ol><li>Bonjour<br></li><li><a href="http://www.foo.com">Bonjour2</a></li></ol><ul><li>Aloha</li><li>Aloha Again</li></ul><h2><b><i>Good bye</i></b></h2><p><b><i>Adios</i></b></p><h3>Alfiederzen</h3><p><a href="http://foo.com">Aloha</a></p>'
Article.save {
author_id: '5086df098523e60002000018'
hero_section:
type: 'image'
caption: '<p>abcd abcd</p><svg/onload=alert(1)>'
lead_paragraph: '<p>abcd abcd</p><svg/onload=alert(1)>'
sections: [
{
type: 'text'
body: body
}
{
type: 'text'
body: badBody
}
{
type: 'image'
caption: '<p>abcd abcd</p><svg/onload=alert(1)>'
}
{
type: 'slideshow'
items: [
{
type: 'image'
caption: '<p>abcd abcd</p><svg/onload=alert(1)>'
}
]
}
{
type: 'embed'
url: 'http://maps.google.com'
height: '400'
mobile_height: '300'
}
]
}, 'foo', (err, article) ->
article.lead_paragraph.should.equal '<p>abcd abcd</p><svg onload="alert(1)"/>'
article.hero_section.caption.should.equal '<p>abcd abcd</p><svg onload="alert(1)"/>'
article.sections[0].body.should.equal body
article.sections[1].body.should.equal '<script>alert(foo)</script>' + body
article.sections[2].caption.should.equal '<p>abcd abcd</p><svg onload="alert(1)"/>'
article.sections[3].items[0].caption.should.equal '<p>abcd abcd</p><svg onload="alert(1)"/>'
article.sections[4].url.should.equal 'http://maps.google.com'
article.sections[4].height.should.equal '400'
article.sections[4].mobile_height.should.equal '300'
done()
it 'doesnt escape smart quotes', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
title: 'Dr. Evil demands “one million dollars”.'
thumbnail_title: 'Dr. Evil demands “one billion dollars”.'
}, 'foo', (err, article) ->
article.title.should.containEql '“'
article.thumbnail_title.should.containEql '“'
done()
it 'fixes anchors urls', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
sections: [
{
type: 'text'
body: '<a href="foo.com">Foo</a>'
}
{
type: 'text'
body: '<a href="www.bar.com">Foo</a>'
}
]
}, 'foo', (err, article) ->
article.sections[0].body.should.equal '<a href="http://foo.com">Foo</a>'
article.sections[1].body.should.equal '<a href="http://www.bar.com">Foo</a>'
done()
it 'retains non-text sections', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
sections: [
{
type: 'text'
body: '<a href="foo.com">Foo</a>'
}
{
type: 'image'
url: 'http://foo.com'
}
{
type: 'video'
url: 'foo.com/watch'
}
{
type: 'slideshow'
items: [
{
type: 'video'
url: 'foo.com/watch'
}
]
}
]
}, 'foo', (err, article) ->
article.sections[0].body.should.equal '<a href="http://foo.com">Foo</a>'
article.sections[1].url.should.equal 'http://foo.com'
article.sections[2].url.should.equal 'http://foo.com/watch'
article.sections[3].items[0].url.should.equal 'http://foo.com/watch'
done()
it 'maintains the original slug when publishing with a new title', (done) ->
fabricate 'users', { name: 'Molly'}, (err, @user) ->
Article.save {
thumbnail_title: 'Foo Baz'
author_id: @user._id
published: true
author: name: @user.name
}, 'foo', (err, article) =>
return done err if err
Article.save {
id: article._id.toString()
thumbnail_title: 'Foo Bar Baz'
author_id: @user._id
published: true
}, 'foo', (err, article) ->
return done err if err
article.slugs.join('').should.equal 'molly-foo-baz'
article.thumbnail_title.should.equal 'Foo Bar Baz'
done()
it 'generates keywords on publish', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
primary_featured_artist_ids: ['4d8b92b34eb68a1b2c0003f4']
featured_artist_ids: ['52868347b202a37bb000072a']
fair_ids: ['5530e72f7261696238050000']
partner_ids: ['4f5228a1de92d1000100076e']
tags: ['cool', 'art']
contributing_authors: [{name: 'kana'}]
published: true
}, 'foo', (err, article) ->
return done err if err
article.keywords.join(',').should.equal 'cool,art,Pablo Picasso,Pablo Picasso,Armory Show 2013,Gagosian Gallery,kana'
done()
it 'indexes the article in elasticsearch on save', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
title: 'foo article'
published: true
channel_id: '5086df098523e60002000018'
}, 'foo', (err, article) ->
return done err if err
setTimeout( =>
search.client.search(
index: search.index
q: 'name:foo'
, (error, response) ->
response.hits.hits[0]._source.name.should.equal 'foo article'
done()
)
, 1000)
it 'saves Super Articles', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
is_super_article: true
super_article: {
partner_link: 'http://partnerlink.com'
partner_logo: 'http://partnerlink.com/logo.jpg'
partner_fullscreen_header_logo: 'http://partnerlink.com/blacklogo.jpg'
partner_link_title: 'Download The App'
partner_logo_link: 'http://itunes'
secondary_partner_logo: 'http://secondarypartner.com/logo.png'
secondary_logo_text: 'In Partnership With'
secondary_logo_link: 'http://secondary'
footer_blurb: 'This is a Footer Blurb'
related_articles: [ '5530e72f7261696238050000' ]
}
published: true
}, 'foo', (err, article) ->
return done err if err
article.super_article.partner_link.should.equal 'http://partnerlink.com'
article.super_article.partner_logo.should.equal 'http://partnerlink.com/logo.jpg'
article.super_article.partner_link_title.should.equal 'Download The App'
article.super_article.partner_logo_link.should.equal 'http://itunes'
article.super_article.partner_fullscreen_header_logo.should.equal 'http://partnerlink.com/blacklogo.jpg'
article.super_article.secondary_partner_logo.should.equal 'http://secondarypartner.com/logo.png'
article.super_article.secondary_logo_text.should.equal 'In Partnership With'
article.super_article.secondary_logo_link.should.equal 'http://secondary'
article.super_article.footer_blurb.should.equal 'This is a Footer Blurb'
article.is_super_article.should.equal true
article.super_article.related_articles.length.should.equal 1
done()
it 'type casts ObjectId over articles', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
super_article: {
related_articles: [ '5530e72f7261696238050000' ]
}
published: true
fair_programming_ids: [ '52617c6c8b3b81f094000013' ]
fair_artsy_ids: [ '53da550a726169083c0a0700' ]
fair_about_ids: [ '53da550a726169083c0a0700' ]
}, 'foo', (err, article) ->
return done err if err
(article.author_id instanceof ObjectId).should.be.true()
(article.super_article.related_articles[0] instanceof ObjectId).should.be.true()
done()
it 'saves a callout section', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
sections: [
{
type: 'callout'
text: 'The Title Goes Here'
article: ''
thumbnail_url: 'http://image.jpg'
hide_image: false
},
{
type: 'text'
body: 'badBody'
},
{
type: 'callout'
text: ''
article: '53da550a726169083c0a0700'
thumbnail_url: ''
},
{
type: 'text'
body: 'badBody'
}
]
published: true
}, 'foo', (err, article) ->
return done err if err
article.sections[0].type.should.equal 'callout'
article.sections[0].text.should.equal 'The Title Goes Here'
article.sections[0].thumbnail_url.should.equal 'http://image.jpg'
article.sections[1].type.should.equal 'text'
article.sections[3].type.should.equal 'text'
article.sections[2].article.should.equal '53da550a726169083c0a0700'
done()
it 'saves an article that does not have sections in input', (done) ->
fabricate 'articles',
_id: ObjectId('5086df098523e60002000018')
id: '5086df098523e60002000018'
author_id: ObjectId('5086df098523e60002000018')
published: false
author: {
name: 'Kana Abe'
}
sections: [
{
type: 'text'
body: 'The start of a new article'
}
{
type: 'image'
url: 'https://image.png'
caption: 'Trademarked'
}
]
, ->
Article.save {
_id: ObjectId('5086df098523e60002000018')
id: '5086df098523e60002000018'
author_id: '5086df098523e60002000018'
published: true
}, 'foo', (err, article) ->
article.published.should.be.true()
article.sections.length.should.equal 2
article.sections[0].body.should.containEql 'The start of a new article'
done()
it 'saves a TOC section', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
sections: [
{
type: 'toc'
links: [
{ name: 'kana', value: 'Kana' }
{ name: 'andy warhol', value: 'Andy Warhol' }
]
}
]
published: true
}, 'foo', (err, article) ->
return done err if err
article.sections[0].type.should.equal 'toc'
article.sections[0].links[0].name.should.equal 'kana'
article.sections[0].links[0].value.should.equal 'Kana'
article.sections[0].links[1].name.should.equal 'andy warhol'
article.sections[0].links[1].value.should.equal 'Andy Warhol'
done()
it 'saves an image set section', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
sections: [
{
type: 'image_set'
images: [
{
type: 'image'
url: 'https://image.png'
caption: 'Trademarked'
}
{
type: 'artwork'
id: '123'
slug: 'andy-warhol'
title: 'The Piece'
date: '2015-04-01'
image: 'http://image.png'
}
]
}
]
}, 'foo', (err, article) ->
return done err if err
article.sections[0].type.should.equal 'image_set'
article.sections[0].images[0].type.should.equal 'image'
article.sections[0].images[0].url.should.equal 'https://image.png'
article.sections[0].images[1].type.should.equal 'artwork'
article.sections[0].images[1].id.should.equal '123'
article.sections[0].images[1].slug.should.equal 'andy-warhol'
done()
it 'saves layouts', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
layout: 'left'
}, 'foo', (err, article) ->
return done err if err
article.layout.should.equal 'left'
done()
it 'it defaults to center if layout is not specified', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
}, 'foo', (err, article) ->
return done err if err
article.layout.should.equal 'center'
done()
it 'saves the channel_id', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
channel_id: '5086df098523e60002000015'
}, 'foo', (err, article) ->
return done err if err
article.channel_id.toString().should.equal '5086df098523e60002000015'
done()
it 'saves the partner_channel_id', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
partner_channel_id: '5086df098523e60002000015'
}, 'foo', (err, article) ->
return done err if err
article.partner_channel_id.toString().should.equal '5086df098523e60002000015'
done()
it 'saves the author', (done) ->
Article.save {
author:
name: 'Jon Snow'
id: '5086df098523e60002000018'
author_id: '5086df098523e60002000018'
}, 'foo', (err, article) ->
return done err if err
article.author.id.toString().should.equal '5086df098523e60002000018'
article.author.name.should.equal 'Jon Snow'
done()
it 'saves a description', (done) ->
Article.save {
author_id: '5086df098523e60002000018'
channel_id: '5086df098523e60002000015'
description: 'Just before the lines start forming, we predict where they will go.'
}, 'foo', (err, article) ->
return done err if err
article.description.should.containEql 'lines start forming'
done()
describe '#publishScheduledArticles', ->
it 'calls #save on each article that needs to be published', (done) ->
fabricate 'articles',
_id: ObjectId('54276766fd4f50996aeca2b8')
author_id: ObjectId('5086df098523e60002000018')
published: false
scheduled_publish_at: moment('2016-01-01').toDate()
author:
name: 'Kana Abe'
sections: [
{
type: 'text'
body: 'The start of a new article'
}
{
type: 'image'
url: 'https://image.png'
caption: 'Trademarked'
}
]
, ->
Article.publishScheduledArticles (err, results) ->
results[0].published.should.be.true()
results[0].published_at.toString().should.equal moment('2016-01-01').toDate().toString()
results[0].sections[0].body.should.containEql 'The start of a new article'
results[0].sections[1].url.should.containEql 'https://image.png'
done()
describe "#destroy", ->
it 'removes an article', (done) ->
fabricate 'articles', { _id: ObjectId('5086df098523e60002000018') }, ->
Article.destroy '5086df098523e60002000018', (err) ->
db.articles.count (err, count) ->
count.should.equal 10
done()
it 'removes the article from elasticsearch', (done) ->
fabricate 'articles', { _id: ObjectId('5086df098523e60002000019'), title: 'quux' }, ->
setTimeout( =>
Article.destroy '5086df098523e60002000019', (err) ->
setTimeout( =>
search.client.search(
index: search.index
q: 'title:quux'
, (error, response) ->
response.hits.hits.length.should.equal 0
done()
)
, 1000)
, 1000)
describe '#present', ->
it 'converts _id to id', ->
data = Article.present _.extend fixtures().articles, _id: 'foo'
(data._id?).should.not.be.ok
data.id.should.equal 'foo'
describe '#presentCollection', ->
it 'shows a total/count/results hash for arrays of articles', ->