forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic_spec.rb
3122 lines (2466 loc) · 107 KB
/
topic_spec.rb
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
# encoding: utf-8
# frozen_string_literal: true
RSpec.describe Topic do
let(:now) { Time.zone.local(2013, 11, 20, 8, 0) }
fab!(:user) { Fabricate(:user) }
fab!(:user1) { Fabricate(:user) }
fab!(:whisperers_group) { Fabricate(:group) }
fab!(:user2) { Fabricate(:user, groups: [whisperers_group]) }
fab!(:moderator) { Fabricate(:moderator) }
fab!(:coding_horror) { Fabricate(:coding_horror) }
fab!(:evil_trout) { Fabricate(:evil_trout) }
fab!(:admin) { Fabricate(:admin) }
fab!(:group) { Fabricate(:group) }
fab!(:trust_level_2) { Fabricate(:user, trust_level: SiteSetting.min_trust_level_to_allow_invite) }
context 'validations' do
let(:topic) { Fabricate.build(:topic) }
context "#featured_link" do
describe 'when featured_link contains more than a URL' do
it 'should not be valid' do
topic.featured_link = 'http://meta.discourse.org TEST'
expect(topic).to_not be_valid
end
end
describe 'when featured_link is a valid URL' do
it 'should be valid' do
topic.featured_link = 'http://meta.discourse.org'
expect(topic).to be_valid
end
end
end
context "#external_id" do
describe 'when external_id is too long' do
it 'should not be valid' do
topic.external_id = 'a' * (Topic::EXTERNAL_ID_MAX_LENGTH + 1)
expect(topic).to_not be_valid
end
end
describe 'when external_id has invalid characters' do
it 'should not be valid' do
topic.external_id = 'a*&^!@()#'
expect(topic).to_not be_valid
end
end
describe 'when external_id is an empty string' do
it 'should not be valid' do
topic.external_id = ''
expect(topic).to_not be_valid
end
end
describe 'when external_id has already been used' do
it 'should not be valid' do
topic2 = Fabricate(:topic, external_id: 'asdf')
topic.external_id = 'asdf'
expect(topic).to_not be_valid
end
end
describe 'when external_id is nil' do
it 'should be valid' do
topic.external_id = nil
expect(topic).to be_valid
end
end
describe 'when external_id is valid' do
it 'should be valid' do
topic.external_id = 'abc_123-ZXY'
expect(topic).to be_valid
end
end
end
context "#title" do
it { is_expected.to validate_presence_of :title }
describe 'censored words' do
after do
Discourse.redis.flushdb
end
describe 'when title contains censored words' do
after do
WordWatcher.clear_cache!
end
it 'should not be valid' do
['pineapple', 'pen'].each { |w| Fabricate(:watched_word, word: w, action: WatchedWord.actions[:censor]) }
topic.title = 'pen PinEapple apple pen is a complete sentence'
expect(topic).to_not be_valid
expect(topic.errors.full_messages.first).to include(I18n.t(
'errors.messages.contains_censored_words', censored_words: 'pen, pineapple'
))
end
end
describe 'titles with censored words not on boundaries' do
it "should be valid" do
Fabricate(:watched_word, word: 'apple', action: WatchedWord.actions[:censor])
topic.title = "Pineapples are great fruit! Applebee's is a great restaurant"
expect(topic).to be_valid
end
end
describe 'when title does not contain censored words' do
it 'should be valid' do
topic.title = 'The cake is a lie'
expect(topic).to be_valid
end
end
describe 'escape special characters in censored words' do
before do
['co(onut', 'coconut', 'a**le'].each do |w|
Fabricate(:watched_word, word: w, action: WatchedWord.actions[:censor])
end
end
it 'should not be valid' do
topic.title = "I have a co(onut a**le"
expect(topic.valid?).to eq(false)
expect(topic.errors.full_messages.first).to include(I18n.t(
'errors.messages.contains_censored_words',
censored_words: 'co(onut, a**le'
))
end
end
end
describe 'blocked words' do
describe 'when title contains watched words' do
after do
WordWatcher.clear_cache!
end
it 'should not be valid' do
Fabricate(:watched_word, word: 'pineapple', action: WatchedWord.actions[:block])
topic.title = 'pen PinEapple apple pen is a complete sentence'
expect(topic).to_not be_valid
expect(topic.errors.full_messages.first).to include(I18n.t(
'contains_blocked_word', word: 'PinEapple'
))
end
end
end
end
end
it { is_expected.to rate_limit }
context '#visible_post_types' do
let(:types) { Post.types }
before do
SiteSetting.enable_whispers = true
SiteSetting.whispers_allowed_groups = "#{whisperers_group.id}"
end
it "returns the appropriate types for anonymous users" do
post_types = Topic.visible_post_types
expect(post_types).to include(types[:regular])
expect(post_types).to include(types[:moderator_action])
expect(post_types).to include(types[:small_action])
expect(post_types).to_not include(types[:whisper])
end
it "returns the appropriate types for regular users" do
post_types = Topic.visible_post_types(Fabricate.build(:user))
expect(post_types).to include(types[:regular])
expect(post_types).to include(types[:moderator_action])
expect(post_types).to include(types[:small_action])
expect(post_types).to_not include(types[:whisper])
end
it "returns the appropriate types for staff users" do
post_types = Topic.visible_post_types(moderator)
expect(post_types).to include(types[:regular])
expect(post_types).to include(types[:moderator_action])
expect(post_types).to include(types[:small_action])
expect(post_types).to include(types[:whisper])
end
it "returns the appropriate types for whisperer users" do
post_types = Topic.visible_post_types(user2)
expect(post_types).to include(types[:regular])
expect(post_types).to include(types[:moderator_action])
expect(post_types).to include(types[:small_action])
expect(post_types).to include(types[:whisper])
end
end
context 'slug' do
context 'encoded generator' do
before { SiteSetting.slug_generation_method = 'encoded' }
context 'with ascii letters' do
let!(:title) { "hello world topic" }
let!(:slug) { "hello-world-topic" }
let!(:topic) { Fabricate.build(:topic, title: title) }
it "returns a Slug for a title" do
expect(topic.title).to eq(title)
expect(topic.slug).to eq(slug)
end
end
context 'for cjk characters' do
let!(:title) { "熱帶風暴畫眉" }
let!(:topic) { Fabricate.build(:topic, title: title) }
it "returns encoded Slug for a title" do
expect(topic.title).to eq(title)
expect(topic.slug).to eq('%E7%86%B1%E5%B8%B6%E9%A2%A8%E6%9A%B4%E7%95%AB%E7%9C%89')
end
end
context 'for numbers' do
let!(:title) { "123456789" }
let!(:slug) { "topic" }
let!(:topic) { Fabricate.build(:topic, title: title) }
it 'generates default slug' do
Slug.expects(:for).with(title).returns("topic")
expect(Fabricate.build(:topic, title: title).slug).to eq("topic")
end
end
end
context 'none generator' do
let!(:title) { "熱帶風暴畫眉" }
let!(:slug) { "topic" }
let!(:topic) { Fabricate.build(:topic, title: title) }
before { SiteSetting.slug_generation_method = 'none' }
it "returns a Slug for a title" do
Slug.expects(:for).with(title).returns('topic')
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
end
end
context '#ascii_generator' do
before { SiteSetting.slug_generation_method = 'ascii' }
context 'with ascii letters' do
let!(:title) { "hello world topic" }
let!(:slug) { "hello-world-topic" }
let!(:topic) { Fabricate.build(:topic, title: title) }
it "returns a Slug for a title" do
Slug.expects(:for).with(title).returns(slug)
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
end
end
context 'for cjk characters' do
let!(:title) { "熱帶風暴畫眉" }
let!(:slug) { 'topic' }
let!(:topic) { Fabricate.build(:topic, title: title) }
it "returns 'topic' when the slug is empty (say, non-latin characters)" do
Slug.expects(:for).with(title).returns("topic")
expect(Fabricate.build(:topic, title: title).slug).to eq("topic")
end
end
end
context 'slug computed hooks' do
before do
invert_slug = ->(topic, slug, title) { slug.reverse }
Topic.slug_computed_callbacks << invert_slug
end
let!(:title) { "hello test topic" }
let!(:slug) { "hello-test-topic".reverse }
let!(:other_title) { "other title" }
let!(:other_slug) { "other-title".reverse }
let!(:topic) { Fabricate.build(:topic, title: title) }
it "returns a reversed slug for a title" do
expect(topic.title).to eq(title)
expect(topic.slug).to eq(slug)
end
it "returns a reversed slug after the title is changed" do
expect(topic.title).to eq(title)
expect(topic.slug).to eq(slug)
topic.title = other_title
expect(topic.title).to eq(other_title)
expect(topic.slug).to eq(other_slug)
end
after do
Topic.slug_computed_callbacks.clear
end
end
end
context "updating a title to be shorter" do
let!(:topic) { Fabricate(:topic) }
it "doesn't update it to be shorter due to cleaning using TextCleaner" do
topic.title = 'unread glitch'
expect(topic.save).to eq(false)
end
end
context 'private message title' do
before do
SiteSetting.min_topic_title_length = 15
SiteSetting.min_personal_message_title_length = 3
end
it 'allows shorter titles' do
pm = Fabricate.build(:private_message_topic, title: 'a' * SiteSetting.min_personal_message_title_length)
expect(pm).to be_valid
end
it 'but not too short' do
pm = Fabricate.build(:private_message_topic, title: 'a')
expect(pm).to_not be_valid
end
end
context 'admin topic title' do
it 'allows really short titles' do
pm = Fabricate.build(:private_message_topic, user: admin, title: 'a')
expect(pm).to be_valid
end
it 'but not blank' do
pm = Fabricate.build(:private_message_topic, title: '')
expect(pm).to_not be_valid
end
end
context 'topic title uniqueness' do
fab!(:category1) { Fabricate(:category) }
fab!(:category2) { Fabricate(:category) }
fab!(:topic) { Fabricate(:topic, category: category1) }
let(:new_topic) { Fabricate.build(:topic, title: topic.title, category: category1) }
let(:new_topic_different_cat) { Fabricate.build(:topic, title: topic.title, category: category2) }
context "when duplicates aren't allowed" do
before do
SiteSetting.allow_duplicate_topic_titles = false
SiteSetting.allow_duplicate_topic_titles_category = false
end
it "won't allow another topic to be created with the same name" do
expect(new_topic).not_to be_valid
end
it "won't even allow another topic to be created with the same name but different category" do
expect(new_topic_different_cat).not_to be_valid
end
it "won't allow another topic with an upper case title to be created" do
new_topic.title = new_topic.title.upcase
expect(new_topic).not_to be_valid
end
it "allows it when the topic is deleted" do
topic.destroy
expect(new_topic).to be_valid
end
it "allows a private message to be created with the same topic" do
new_topic.archetype = Archetype.private_message
expect(new_topic).to be_valid
end
end
context "when duplicates are allowed" do
before do
SiteSetting.allow_duplicate_topic_titles = true
SiteSetting.allow_duplicate_topic_titles_category = false
end
it "will allow another topic to be created with the same name" do
expect(new_topic).to be_valid
end
end
context "when duplicates are allowed if the category is different" do
before do
SiteSetting.allow_duplicate_topic_titles = false
SiteSetting.allow_duplicate_topic_titles_category = true
end
it "will allow another topic to be created with the same name but different category" do
expect(new_topic_different_cat).to be_valid
end
it "won't allow another topic to be created with the same name in same category" do
expect(new_topic).not_to be_valid
end
it "other errors will not be cleared" do
SiteSetting.min_topic_title_length = 5
topic.update!(title: "more than 5 characters but less than 134")
SiteSetting.min_topic_title_length = 134
new_topic_different_cat.title = "more than 5 characters but less than 134"
expect(new_topic_different_cat).not_to be_valid
expect(new_topic_different_cat.errors[:title]).to include(I18n.t("errors.messages.too_short", count: 134))
end
end
end
context 'html in title' do
def build_topic_with_title(title)
build(:topic, title: title).tap { |t| t.valid? }
end
let(:topic_bold) { build_topic_with_title("Topic with <b>bold</b> text in its title") }
let(:topic_image) { build_topic_with_title("Topic with <img src='something'> image in its title") }
let(:topic_script) { build_topic_with_title("Topic with <script>alert('title')</script> script in its title") }
let(:topic_emoji) { build_topic_with_title("I 💖 candy alot") }
let(:topic_modifier_emoji) { build_topic_with_title("I 👨🌾 candy alot") }
let(:topic_shortcut_emoji) { build_topic_with_title("I love candy :)") }
let(:topic_inline_emoji) { build_topic_with_title("Hello😊World") }
it "escapes script contents" do
expect(topic_script.fancy_title).to eq("Topic with <script>alert(‘title’)</script> script in its title")
end
it "expands emojis" do
expect(topic_emoji.fancy_title).to eq("I :sparkling_heart: candy alot")
end
it "keeps combined emojis" do
expect(topic_modifier_emoji.fancy_title).to eq("I :man_farmer: candy alot")
end
it "escapes bold contents" do
expect(topic_bold.fancy_title).to eq("Topic with <b>bold</b> text in its title")
end
it "escapes image contents" do
expect(topic_image.fancy_title).to eq("Topic with <img src=‘something’> image in its title")
end
it "always escapes title" do
topic_script.title = topic_script.title + "x" * Topic.max_fancy_title_length
expect(topic_script.fancy_title).to eq(ERB::Util.html_escape(topic_script.title))
# not really needed, but just in case
expect(topic_script.fancy_title).not_to include("<script>")
end
context "emoji shortcuts enabled" do
before { SiteSetting.enable_emoji_shortcuts = true }
it "converts emoji shortcuts into emoji" do
expect(topic_shortcut_emoji.fancy_title).to eq("I love candy :slight_smile:")
end
context "emojis disabled" do
before { SiteSetting.enable_emoji = false }
it "does not convert emoji shortcuts" do
expect(topic_shortcut_emoji.fancy_title).to eq("I love candy :)")
end
end
end
context "emoji shortcuts disabled" do
before { SiteSetting.enable_emoji_shortcuts = false }
it "does not convert emoji shortcuts" do
expect(topic_shortcut_emoji.fancy_title).to eq("I love candy :)")
end
end
it "keeps inline emojis if inline emoji setting disabled" do
SiteSetting.enable_inline_emoji_translation = false
expect(topic_inline_emoji.fancy_title).to eq("Hello😊World")
end
it "expands inline emojis if inline emoji setting enabled" do
SiteSetting.enable_inline_emoji_translation = true
expect(topic_inline_emoji.fancy_title).to eq("Hello:blush:World")
end
end
context 'fancy title' do
let(:topic) { Fabricate.build(:topic, title: %{"this topic" -- has ``fancy stuff''}) }
context 'title_fancy_entities disabled' do
before do
SiteSetting.title_fancy_entities = false
end
it "doesn't add entities to the title" do
expect(topic.fancy_title).to eq(""this topic" -- has ``fancy stuff''")
end
end
context 'title_fancy_entities enabled' do
before do
SiteSetting.title_fancy_entities = true
end
it "converts the title to have fancy entities and updates" do
expect(topic.fancy_title).to eq("“this topic” – has “fancy stuff”")
topic.title = "this is my test hello world... yay"
topic.save!
topic.reload
expect(topic.fancy_title).to eq("This is my test hello world… yay")
topic.title = "I made a change to the title"
topic.save!
topic.reload
expect(topic.fancy_title).to eq("I made a change to the title")
# another edge case
topic.title = "this is another edge case"
expect(topic.fancy_title).to eq("this is another edge case")
end
it "works with long title that results in lots of entities" do
long_title = "NEW STOCK PICK: PRCT - LAST PICK UP 233%, NNCO#{"." * 150} ofoum"
topic.title = long_title
expect { topic.save! }.to_not raise_error
expect(topic.fancy_title).to eq(long_title)
end
context 'readonly mode' do
before do
Discourse.enable_readonly_mode
end
after do
Discourse.disable_readonly_mode
end
it 'should not attempt to update `fancy_title`' do
topic.save!
expect(topic.fancy_title).to eq('“this topic” – has “fancy stuff”')
topic.title = "This is a test testing testing"
expect(topic.fancy_title).to eq("This is a test testing testing")
expect(topic.reload.read_attribute(:fancy_title))
.to eq('“this topic” – has “fancy stuff”')
end
end
end
end
context 'category validation' do
fab!(:category) { Fabricate(:category_with_definition) }
context 'allow_uncategorized_topics is false' do
before do
SiteSetting.allow_uncategorized_topics = false
end
it "does not allow nil category" do
topic = Fabricate.build(:topic, category: nil)
expect(topic).not_to be_valid
expect(topic.errors[:category_id]).to be_present
end
it "allows PMs" do
topic = Fabricate.build(:topic, category: nil, archetype: Archetype.private_message)
expect(topic).to be_valid
end
it 'passes for topics with a category' do
expect(Fabricate.build(:topic, category: category)).to be_valid
end
end
context 'allow_uncategorized_topics is true' do
before do
SiteSetting.allow_uncategorized_topics = true
end
it "passes for topics with nil category" do
expect(Fabricate.build(:topic, category: nil)).to be_valid
end
it 'passes for topics with a category' do
expect(Fabricate.build(:topic, category: category)).to be_valid
end
end
end
context '.similar_to' do
fab!(:category) { Fabricate(:category_with_definition) }
it 'returns an empty array with nil params' do
expect(Topic.similar_to(nil, nil)).to eq([])
end
context "with a category definition" do
it "excludes the category definition topic from similar_to" do
expect(Topic.similar_to('category definition for', "no body")).to eq([])
end
end
it 'does not result in a syntax error when removing accents' do
SiteSetting.search_ignore_accents = true
expect(Topic.similar_to('something', "it's")).to eq([])
end
it 'does not result in a syntax error when raw is blank after cooking' do
expect(Topic.similar_to('some title', '#')).to eq([])
end
it 'does not result in invalid statement when prepared data is blank' do
expect(Topic.similar_to('some title', 'https://discourse.org/#INCORRECT#URI')).to be_empty
end
it 'does not result in invalid statement when title is all stopwords for zh_CN' do
SiteSetting.default_locale = "zh_CN"
expect(Topic.similar_to("怎么上自己的", '')).to eq([])
end
context 'with a similar topic' do
fab!(:post) {
SearchIndexer.enable
create_post(title: "Evil trout is the dude who posted this topic")
}
let(:topic) { post.topic }
before do
SearchIndexer.enable
end
it 'returns the similar topic if the title is similar' do
expect(Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?")).to eq([topic])
end
it 'returns the similar topic even if raw is blank' do
expect(Topic.similar_to("has evil trout made any topics?", "")).to eq([topic])
end
it 'matches title against title and raw against raw when searching for topics' do
topic.update!(title: '1 2 3 numbered titles')
post.update!(raw: 'random toy poodle')
expect(Topic.similar_to("unrelated term", "1 2 3 poddle")).to eq([])
end
it 'doesnt match numbered lists against numbers in Post#raw' do
post.update!(raw: <<~RAW)
Internet Explorer 11+ Oct 2013 Google Chrome 32+ Jan 2014 Firefox 27+ Feb 2014 Safari 6.1+ Jul 2012 Safari, iOS 8+ Oct 2014
RAW
post.topic.update!(title: 'Where are we with browser support in 2019?')
topics = Topic.similar_to("Videos broken in composer", <<~RAW)
1. Do something
2. Do something else
3. Do more things
RAW
expect(topics).to eq([])
end
context "secure categories" do
before do
category.update!(read_restricted: true)
topic.update!(category: category)
end
it "doesn't return topics from private categories" do
expect(Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?", user)).to be_blank
end
it "should return the cat since the user can see it" do
Guardian.any_instance.expects(:secure_category_ids).returns([category.id])
expect(Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?", user)).to include(topic)
end
end
end
end
context 'post_numbers' do
let!(:topic) { Fabricate(:topic) }
let!(:p1) { Fabricate(:post, topic: topic, user: topic.user) }
let!(:p2) { Fabricate(:post, topic: topic, user: topic.user) }
let!(:p3) { Fabricate(:post, topic: topic, user: topic.user) }
it "returns the post numbers of the topic" do
expect(topic.post_numbers).to eq([1, 2, 3])
p2.destroy
topic.reload
expect(topic.post_numbers).to eq([1, 3])
end
end
describe '#invite' do
fab!(:topic) { Fabricate(:topic, user: user) }
context 'rate limits' do
before do
SiteSetting.max_topic_invitations_per_day = 1
RateLimiter.enable
end
after do
RateLimiter.clear_all!
end
it "rate limits topic invitations" do
start = Time.now.tomorrow.beginning_of_day
freeze_time(start)
topic = Fabricate(:topic, user: trust_level_2)
topic.invite(topic.user, user.username)
expect {
topic.invite(topic.user, user1.username)
}.to raise_error(RateLimiter::LimitExceeded)
end
it "rate limits PM invitations" do
start = Time.now.tomorrow.beginning_of_day
freeze_time(start)
topic = Fabricate(:private_message_topic, user: trust_level_2)
topic.invite(topic.user, user.username)
expect {
topic.invite(topic.user, user1.username)
}.to raise_error(RateLimiter::LimitExceeded)
end
end
describe 'when username_or_email is not valid' do
it 'should return the right value' do
expect do
expect(topic.invite(user, 'somerandomstring')).to eq(nil)
end.to_not change { topic.allowed_users }
end
end
describe 'when user is already allowed' do
it 'should raise the right error' do
topic.allowed_users << user1
expect { topic.invite(user, user1.username) }
.to raise_error(Topic::UserExists)
end
end
describe 'private message' do
fab!(:user) { trust_level_2 }
fab!(:topic) { Fabricate(:private_message_topic, user: trust_level_2) }
describe 'by username' do
it 'should be able to invite a user' do
expect(topic.invite(user, user1.username)).to eq(true)
expect(topic.allowed_users).to include(user1)
expect(Post.last.action_code).to eq("invited_user")
notification = Notification.last
expect(notification.notification_type)
.to eq(Notification.types[:invited_to_private_message])
expect(topic.remove_allowed_user(user, user1.username)).to eq(true)
expect(topic.reload.allowed_users).to_not include(user1)
expect(Post.last.action_code).to eq("removed_user")
end
it 'should not create a small action if user is already invited through a group' do
group = Fabricate(:group, users: [user, user1])
expect(topic.invite_group(user, group)).to eq(true)
expect { topic.invite(user, user1.username) }
.to change { Notification.count }.by(1)
.and change { Post.where(post_type: Post.types[:small_action]).count }.by(0)
end
context "from a muted user" do
before { Fabricate(:muted_user, user: user1, muted_user: user) }
it 'fails with an error' do
expect { topic.invite(user, user1.username) }
.to raise_error(Topic::NotAllowed)
expect(topic.allowed_users).to_not include(user1)
expect(Post.last).to be_blank
expect(Notification.last).to be_blank
end
end
context "from a ignored user" do
before { Fabricate(:ignored_user, user: user1, ignored_user: user) }
it 'fails with an error' do
expect { topic.invite(user, user1.username) }
.to raise_error(Topic::NotAllowed)
expect(topic.allowed_users).to_not include(user1)
expect(Post.last).to be_blank
expect(Notification.last).to be_blank
end
end
context "when PMs are enabled for TL3 or higher only" do
before do
SiteSetting.min_trust_to_send_messages = 3
end
it 'should raise error' do
expect { topic.invite(user, user1.username) }
.to raise_error(Topic::UserExists)
end
end
context "when invited_user has enabled allow_list" do
fab!(:pm) { Fabricate(:private_message_topic, user: user, topic_allowed_users: [
Fabricate.build(:topic_allowed_user, user: user),
Fabricate.build(:topic_allowed_user, user: user2)
]) }
before do
user1.user_option.update!(enable_allowed_pm_users: true)
end
it 'succeeds when inviter is in allowed list' do
AllowedPmUser.create!(user: user1, allowed_pm_user: user)
expect(topic.invite(user, user1.username)).to eq(true)
end
it 'should raise error when inviter not in allowed list' do
AllowedPmUser.create!(user: user1, allowed_pm_user: user2)
expect { topic.invite(user, user1.username) }
.to raise_error(Topic::NotAllowed)
.with_message(I18n.t("topic_invite.receiver_does_not_allow_pm"))
end
it 'should succeed for staff even when not allowed' do
AllowedPmUser.create!(user: user1, allowed_pm_user: user2)
expect(topic.invite(user1, admin.username)).to eq(true)
end
it 'should raise error when target_user is not in inviters allowed list' do
user.user_option.update!(enable_allowed_pm_users: true)
AllowedPmUser.create!(user: user1, allowed_pm_user: user)
expect { topic.invite(user, user1.username) }
.to raise_error(Topic::NotAllowed)
.with_message(I18n.t("topic_invite.sender_does_not_allow_pm"))
end
it 'succeeds when inviter is in allowed list even though other participants are not in allowed list' do
AllowedPmUser.create!(user: user1, allowed_pm_user: user)
expect(pm.invite(user, user1.username)).to eq(true)
end
end
end
describe 'by email' do
it 'should be able to invite a user' do
expect(topic.invite(user, user1.email)).to eq(true)
expect(topic.allowed_users).to include(user1)
expect(Notification.last.notification_type)
.to eq(Notification.types[:invited_to_private_message])
end
describe 'when user is not found' do
it 'should create the right invite' do
expect(topic.invite(user, 'test@email.com')).to eq(true)
invite = Invite.last
expect(invite.email).to eq('test@email.com')
expect(invite.invited_by).to eq(user)
end
describe 'when user does not have sufficient trust level' do
before { user.update!(trust_level: TrustLevel[1]) }
it 'should not create an invite' do
expect do
expect(topic.invite(user, 'test@email.com')).to eq(nil)
end.to_not change { Invite.count }
end
end
end
end
end
describe 'public topic' do
def expect_the_right_notification_to_be_created(inviter, invitee)
notification = Notification.last
expect(notification.notification_type)
.to eq(Notification.types[:invited_to_topic])
expect(notification.user).to eq(invitee)
expect(notification.topic).to eq(topic)
notification_data = JSON.parse(notification.data)
expect(notification_data["topic_title"]).to eq(topic.title)
expect(notification_data["display_username"]).to eq(inviter.username)
end
describe 'by username' do
it 'should invite user into a topic' do
topic.invite(user, user1.username)
expect_the_right_notification_to_be_created(user, user1)
end
end
describe 'by email' do
it 'should be able to invite a user' do
expect(topic.invite(user, user1.email)).to eq(true)
expect_the_right_notification_to_be_created(user, user1)
end
describe 'when topic belongs to a private category' do
fab!(:category) do
Fabricate(:category_with_definition, groups: [group]).tap do |category|
category.set_permissions(group => :full)
category.save!
end
end
fab!(:topic) { Fabricate(:topic, category: category) }
fab!(:inviter) { Fabricate(:user).tap { |user| group.add_owner(user) } }
fab!(:invitee) { Fabricate(:user) }
describe 'as a group owner' do
it 'should be able to invite a user' do
expect do
expect(topic.invite(inviter, invitee.email, [group.id]))
.to eq(true)
end.to change { Notification.count } &
change { GroupHistory.count }
expect_the_right_notification_to_be_created(inviter, invitee)
group_history = GroupHistory.last
expect(group_history.acting_user).to eq(inviter)
expect(group_history.target_user).to eq(invitee)
expect(group_history.action).to eq(
GroupHistory.actions[:add_user_to_group]
)
end
describe 'when group ids are not given' do
it 'should not invite the user' do
expect do
expect(topic.invite(inviter, invitee.email)).to eq(false)
end.to_not change { Notification.count }
end
end
end
describe 'as a normal user' do
it 'should not be able to invite a user' do
expect do
expect(topic.invite(Fabricate(:user), invitee.email, [group.id]))
.to eq(false)
end.to_not change { Notification.count }
end
end
end