2019-04-30 08:27:42 +08:00
# frozen_string_literal: true
2015-10-11 17:41:23 +08:00
require 'rails_helper'
2013-02-06 03:16:51 +08:00
describe Post do
2013-06-19 15:19:42 +08:00
before { Oneboxer . stubs :onebox }
2019-12-18 13:51:57 +08:00
let ( :upload_path ) { Discourse . store . upload_path }
2016-01-08 18:53:52 +08:00
describe '#hidden_reasons' do
context " verify enum sequence " do
before do
@hidden_reasons = Post . hidden_reasons
end
it " 'flag_threshold_reached' should be at 1st position " do
expect ( @hidden_reasons [ :flag_threshold_reached ] ) . to eq ( 1 )
end
it " 'flagged_by_tl3_user' should be at 4th position " do
expect ( @hidden_reasons [ :flagged_by_tl3_user ] ) . to eq ( 4 )
end
end
end
describe '#types' do
context " verify enum sequence " do
before do
@types = Post . types
end
it " 'regular' should be at 1st position " do
expect ( @types [ :regular ] ) . to eq ( 1 )
end
it " 'whisper' should be at 4th position " do
expect ( @types [ :whisper ] ) . to eq ( 4 )
end
end
end
describe '#cook_methods' do
context " verify enum sequence " do
before do
@cook_methods = Post . cook_methods
end
it " 'regular' should be at 1st position " do
expect ( @cook_methods [ :regular ] ) . to eq ( 1 )
end
it " 'email' should be at 3rd position " do
expect ( @cook_methods [ :email ] ) . to eq ( 3 )
end
end
end
2013-05-11 04:58:23 +08:00
# Help us build a post with a raw body
def post_with_body ( body , user = nil )
args = post_args . merge ( raw : body )
args [ :user ] = user if user . present?
2019-05-09 11:28:28 +08:00
Fabricate . build ( :post , args )
2013-05-11 04:58:23 +08:00
end
2015-01-06 00:04:23 +08:00
it { is_expected . to validate_presence_of :raw }
2013-02-06 03:16:51 +08:00
# Min/max body lengths, respecting padding
2015-01-06 00:04:23 +08:00
it { is_expected . not_to allow_value ( " x " ) . for ( :raw ) }
it { is_expected . not_to allow_value ( " x " * ( SiteSetting . max_post_length + 1 ) ) . for ( :raw ) }
it { is_expected . not_to allow_value ( ( " " * SiteSetting . min_post_length ) + " x " ) . for ( :raw ) }
2013-02-06 03:16:51 +08:00
2015-01-06 00:04:23 +08:00
it { is_expected . to rate_limit }
2013-02-06 03:16:51 +08:00
let ( :topic ) { Fabricate ( :topic ) }
let ( :post_args ) do
2013-12-12 10:41:34 +08:00
{ user : topic . user , topic : topic }
2013-02-06 03:16:51 +08:00
end
2013-02-22 02:20:00 +08:00
describe 'scopes' do
describe '#by_newest' do
it 'returns posts ordered by created_at desc' do
2013-08-21 03:45:58 +08:00
2 . times do | t |
2019-05-09 11:28:28 +08:00
Fabricate ( :post , created_at : t . seconds . from_now )
2013-08-21 03:45:58 +08:00
end
2015-01-06 00:04:23 +08:00
expect ( Post . by_newest . first . created_at ) . to be > Post . by_newest . last . created_at
2013-02-22 02:20:00 +08:00
end
end
describe '#with_user' do
it 'gives you a user' do
2019-05-09 11:28:28 +08:00
Fabricate ( :post , user : Fabricate . build ( :user ) )
2015-01-06 00:04:23 +08:00
expect ( Post . with_user . first . user ) . to be_a User
2013-02-22 02:20:00 +08:00
end
end
end
2013-12-12 10:41:34 +08:00
describe " revisions and deleting/recovery " do
2013-03-14 00:35:55 +08:00
2013-06-14 01:41:45 +08:00
context 'a post without links' do
2019-05-09 11:28:28 +08:00
let ( :post ) { Fabricate ( :post , post_args ) }
2013-06-14 01:41:45 +08:00
before do
post . trash!
post . reload
end
2013-03-14 00:35:55 +08:00
2013-12-12 10:41:34 +08:00
it " doesn't create a new revision when deleted " do
2015-01-06 00:04:23 +08:00
expect ( post . revisions . count ) . to eq ( 0 )
2013-06-14 01:41:45 +08:00
end
describe " recovery " do
before do
post . recover!
post . reload
end
2013-12-12 10:41:34 +08:00
it " doesn't create a new revision when recovered " do
2015-01-06 00:04:23 +08:00
expect ( post . revisions . count ) . to eq ( 0 )
2013-06-14 01:41:45 +08:00
end
end
2013-03-14 00:35:55 +08:00
end
2013-06-14 01:41:45 +08:00
context 'a post with links' do
let ( :post ) { Fabricate ( :post_with_external_links ) }
2013-03-14 00:35:55 +08:00
before do
2013-06-14 01:41:45 +08:00
post . trash!
2013-03-14 00:35:55 +08:00
post . reload
end
2013-06-14 01:41:45 +08:00
describe 'recovery' do
it 'recreates the topic_link records' do
TopicLink . expects ( :extract_from ) . with ( post )
post . recover!
end
2013-03-14 00:35:55 +08:00
end
end
2020-11-11 20:49:53 +08:00
end
2013-03-14 00:35:55 +08:00
2020-11-11 20:49:53 +08:00
context 'a post with notices' do
let ( :post ) do
post = Fabricate ( :post , post_args )
post . upsert_custom_fields ( Post :: NOTICE = > { type : Post . notices [ :returning_user ] , last_posted_at : 1 . day . ago } )
post
2019-03-08 16:48:35 +08:00
end
2020-11-11 20:49:53 +08:00
it 'will have its notice cleared when post is trashed' do
expect { post . trash! } . to change { post . custom_fields } . to ( { } )
end
2013-03-14 00:35:55 +08:00
end
2020-01-16 11:50:27 +08:00
describe " with_secure_media? " do
let ( :topic ) { Fabricate ( :topic ) }
let! ( :post ) { Fabricate ( :post , topic : topic ) }
it " returns false if secure media is not enabled " do
expect ( post . with_secure_media? ) . to eq ( false )
end
context " when secure media is enabled " do
2020-09-14 19:32:25 +08:00
before do
setup_s3
SiteSetting . authorized_extensions = " pdf|png|jpg|csv "
SiteSetting . secure_media = true
end
2020-01-16 11:50:27 +08:00
context " if login_required " do
before { SiteSetting . login_required = true }
it " returns true " do
expect ( post . with_secure_media? ) . to eq ( true )
end
end
context " if the topic category is read_restricted " do
let ( :category ) { Fabricate ( :private_category , group : Fabricate ( :group ) ) }
before do
topic . change_category_to_id ( category . id )
end
it " returns true " do
expect ( post . with_secure_media? ) . to eq ( true )
end
end
context " if the post is in a PM topic " do
let ( :topic ) { Fabricate ( :private_message_topic ) }
it " returns true " do
expect ( post . with_secure_media? ) . to eq ( true )
end
end
end
end
2013-02-26 00:42:20 +08:00
describe 'flagging helpers' do
2019-05-09 11:28:28 +08:00
fab! ( :post ) { Fabricate ( :post ) }
2019-05-07 11:12:20 +08:00
fab! ( :user ) { Fabricate ( :coding_horror ) }
fab! ( :admin ) { Fabricate ( :admin ) }
2016-03-31 01:27:34 +08:00
2018-07-27 03:12:12 +08:00
it 'is_flagged? is accurate' do
2019-01-04 01:03:01 +08:00
PostActionCreator . off_topic ( user , post )
expect ( post . reload . is_flagged? ) . to eq ( true )
2013-02-26 00:42:20 +08:00
2019-01-04 01:03:01 +08:00
PostActionDestroyer . destroy ( user , post , :off_topic )
expect ( post . reload . is_flagged? ) . to eq ( false )
2013-02-07 12:15:48 +08:00
end
2016-03-31 01:27:34 +08:00
2018-07-27 03:12:12 +08:00
it 'is_flagged? is true if flag was deferred' do
2019-01-04 01:03:01 +08:00
result = PostActionCreator . off_topic ( user , post )
result . reviewable . perform ( admin , :ignore )
expect ( post . reload . is_flagged? ) . to eq ( true )
2018-07-27 03:12:12 +08:00
end
it 'is_flagged? is true if flag was cleared' do
2019-01-04 01:03:01 +08:00
result = PostActionCreator . off_topic ( user , post )
result . reviewable . perform ( admin , :disagree )
expect ( post . reload . is_flagged? ) . to eq ( true )
2018-07-27 03:12:12 +08:00
end
2019-01-04 01:03:01 +08:00
it 'reviewable_flag is nil when ignored' do
result = PostActionCreator . spam ( user , post )
expect ( post . reviewable_flag ) . to eq ( result . reviewable )
2016-03-31 01:27:34 +08:00
2019-01-04 01:03:01 +08:00
result . reviewable . perform ( admin , :ignore )
expect ( post . reviewable_flag ) . to be_nil
2016-03-31 01:27:34 +08:00
end
2018-07-27 03:12:12 +08:00
2019-01-04 01:03:01 +08:00
it 'reviewable_flag is nil when disagreed' do
result = PostActionCreator . spam ( user , post )
expect ( post . reviewable_flag ) . to eq ( result . reviewable )
result . reviewable . perform ( admin , :disagree )
expect ( post . reload . reviewable_flag ) . to be_nil
2018-07-27 03:12:12 +08:00
end
2013-02-07 12:15:48 +08:00
end
2013-02-06 03:16:51 +08:00
2020-08-08 00:08:59 +08:00
describe " maximum media embeds " do
2019-05-07 11:12:20 +08:00
fab! ( :newuser ) { Fabricate ( :user , trust_level : TrustLevel [ 0 ] ) }
2019-05-09 11:28:28 +08:00
let ( :post_no_images ) { Fabricate . build ( :post , post_args . merge ( user : newuser ) ) }
2013-05-11 04:58:23 +08:00
let ( :post_one_image ) { post_with_body ( " ![sherlock](http://bbc.co.uk/sherlock.jpg) " , newuser ) }
let ( :post_two_images ) { post_with_body ( " <img src='http://discourse.org/logo.png'> <img src='http://bbc.co.uk/sherlock.jpg'> " , newuser ) }
let ( :post_with_avatars ) { post_with_body ( '<img alt="smiley" title=":smiley:" src="/assets/emoji/smiley.png" class="avatar"> <img alt="wink" title=":wink:" src="/assets/emoji/wink.png" class="avatar">' , newuser ) }
2021-04-12 11:57:39 +08:00
let ( :post_with_favicon ) { post_with_body ( '<img src="/images/favicons/discourse.png" class="favicon">' , newuser ) }
2018-02-21 09:41:27 +08:00
let ( :post_image_within_quote ) { post_with_body ( '[quote]<img src="coolimage.png">[/quote]' , newuser ) }
2018-03-07 00:46:12 +08:00
let ( :post_image_within_code ) { post_with_body ( '<code><img src="coolimage.png"></code>' , newuser ) }
let ( :post_image_within_pre ) { post_with_body ( '<pre><img src="coolimage.png"></pre>' , newuser ) }
2013-05-11 04:58:23 +08:00
let ( :post_with_thumbnail ) { post_with_body ( '<img src="/assets/emoji/smiley.png" class="thumbnail">' , newuser ) }
let ( :post_with_two_classy_images ) { post_with_body ( " <img src='http://discourse.org/logo.png' class='classy'> <img src='http://bbc.co.uk/sherlock.jpg' class='classy'> " , newuser ) }
2020-08-08 00:08:59 +08:00
let ( :post_with_two_embedded_media ) { post_with_body ( '<video width="950" height="700" controls><source src="https://bbc.co.uk/news.mp4" type="video/mp4"></video><audio controls><source type="audio/mpeg" src="https://example.com/audio.mp3"></audio>' , newuser ) }
2013-02-06 03:16:51 +08:00
it " returns 0 images for an empty post " do
2020-08-08 00:08:59 +08:00
expect ( Fabricate . build ( :post ) . embedded_media_count ) . to eq ( 0 )
2013-02-06 03:16:51 +08:00
end
it " finds images from markdown " do
2020-08-08 00:08:59 +08:00
expect ( post_one_image . embedded_media_count ) . to eq ( 1 )
2013-02-06 03:16:51 +08:00
end
it " finds images from HTML " do
2020-08-08 00:08:59 +08:00
expect ( post_two_images . embedded_media_count ) . to eq ( 2 )
2013-02-06 03:16:51 +08:00
end
2013-02-12 15:43:48 +08:00
it " doesn't count avatars as images " do
2020-08-08 00:08:59 +08:00
expect ( post_with_avatars . embedded_media_count ) . to eq ( 0 )
2013-02-06 03:16:51 +08:00
end
2018-02-21 09:00:06 +08:00
it " allows images by default " do
expect ( post_one_image ) . to be_valid
end
2020-08-08 00:08:59 +08:00
it " doesn't allow more than `min_trust_to_post_embedded_media` " do
SiteSetting . min_trust_to_post_embedded_media = 4
2018-02-21 09:00:06 +08:00
post_one_image . user . trust_level = 3
expect ( post_one_image ) . not_to be_valid
end
2020-08-08 00:08:59 +08:00
it " doesn't allow more than `min_trust_to_post_embedded_media` in a quote " do
SiteSetting . min_trust_to_post_embedded_media = 4
2018-02-21 09:41:27 +08:00
post_one_image . user . trust_level = 3
expect ( post_image_within_quote ) . not_to be_valid
end
2020-08-08 00:08:59 +08:00
it " doesn't allow more than `min_trust_to_post_embedded_media` in code " do
SiteSetting . min_trust_to_post_embedded_media = 4
2018-03-07 00:46:12 +08:00
post_one_image . user . trust_level = 3
expect ( post_image_within_code ) . not_to be_valid
end
2020-08-08 00:08:59 +08:00
it " doesn't allow more than `min_trust_to_post_embedded_media` in pre " do
SiteSetting . min_trust_to_post_embedded_media = 4
2018-03-07 00:46:12 +08:00
post_one_image . user . trust_level = 3
expect ( post_image_within_pre ) . not_to be_valid
end
2020-08-08 00:08:59 +08:00
it " doesn't allow more than `min_trust_to_post_embedded_media` " do
SiteSetting . min_trust_to_post_embedded_media = 4
2018-02-21 09:00:06 +08:00
post_one_image . user . trust_level = 4
expect ( post_one_image ) . to be_valid
end
2013-04-12 00:36:45 +08:00
it " doesn't count favicons as images " do
2017-10-16 07:46:01 +08:00
PrettyText . stubs ( :cook ) . returns ( post_with_favicon . raw )
2020-08-08 00:08:59 +08:00
expect ( post_with_favicon . embedded_media_count ) . to eq ( 0 )
2013-04-12 00:36:45 +08:00
end
it " doesn't count thumbnails as images " do
2017-10-16 07:46:01 +08:00
PrettyText . stubs ( :cook ) . returns ( post_with_thumbnail . raw )
2020-08-08 00:08:59 +08:00
expect ( post_with_thumbnail . embedded_media_count ) . to eq ( 0 )
2013-04-12 00:36:45 +08:00
end
2020-07-27 08:23:54 +08:00
it " doesn't count allowlisted images " do
Post . stubs ( :allowed_image_classes ) . returns ( [ " classy " ] )
# I dislike this, but passing in a custom allowlist is hard
2017-10-16 07:46:01 +08:00
PrettyText . stubs ( :cook ) . returns ( post_with_two_classy_images . raw )
2020-08-08 00:08:59 +08:00
expect ( post_with_two_classy_images . embedded_media_count ) . to eq ( 0 )
end
it " counts video and audio as embedded media " do
expect ( post_with_two_embedded_media . embedded_media_count ) . to eq ( 2 )
2013-02-12 15:43:48 +08:00
end
2013-02-06 03:16:51 +08:00
context " validation " do
2013-03-26 00:24:46 +08:00
before do
2020-08-08 00:08:59 +08:00
SiteSetting . newuser_max_embedded_media = 1
2013-02-06 03:16:51 +08:00
end
2013-04-18 07:11:13 +08:00
context 'newuser' do
it " allows a new user to post below the limit " do
2015-01-06 00:04:23 +08:00
expect ( post_one_image ) . to be_valid
2013-03-26 00:24:46 +08:00
end
2020-08-08 00:08:59 +08:00
it " doesn't allow more than the maximum number of images " do
2015-01-06 00:04:23 +08:00
expect ( post_two_images ) . not_to be_valid
2013-03-26 00:24:46 +08:00
end
2020-08-08 00:08:59 +08:00
it " doesn't allow more than the maximum number of embedded media items " do
expect ( post_with_two_embedded_media ) . not_to be_valid
end
2013-04-18 07:11:13 +08:00
it " doesn't allow a new user to edit their post to insert an image " do
2014-09-05 13:20:39 +08:00
post_no_images . user . trust_level = TrustLevel [ 0 ]
2013-03-26 00:24:46 +08:00
post_no_images . save
2015-01-06 00:04:23 +08:00
expect {
2014-10-28 05:06:43 +08:00
post_no_images . revise ( post_no_images . user , raw : post_two_images . raw )
2013-03-26 00:24:46 +08:00
post_no_images . reload
2015-01-06 00:04:23 +08:00
} . not_to change ( post_no_images , :raw )
2013-03-26 00:24:46 +08:00
end
2013-02-06 03:16:51 +08:00
end
2013-04-18 07:11:13 +08:00
it " allows more images from a not-new account " do
2014-09-05 13:20:39 +08:00
post_two_images . user . trust_level = TrustLevel [ 1 ]
2015-01-06 00:04:23 +08:00
expect ( post_two_images ) . to be_valid
2013-02-06 03:16:51 +08:00
end
end
end
2013-07-22 08:39:17 +08:00
describe " maximum attachments " do
2019-05-07 11:12:20 +08:00
fab! ( :newuser ) { Fabricate ( :user , trust_level : TrustLevel [ 0 ] ) }
2019-05-09 11:28:28 +08:00
let ( :post_no_attachments ) { Fabricate . build ( :post , post_args . merge ( user : newuser ) ) }
2019-12-18 13:51:57 +08:00
let ( :post_one_attachment ) { post_with_body ( " <a class='attachment' href='/ #{ upload_path } /1/2082985.txt'>file.txt</a> " , newuser ) }
let ( :post_two_attachments ) { post_with_body ( " <a class='attachment' href='/ #{ upload_path } /2/20947092.log'>errors.log</a> <a class='attachment' href='/ #{ upload_path } /3/283572385.3ds'>model.3ds</a> " , newuser ) }
2013-07-22 08:39:17 +08:00
it " returns 0 attachments for an empty post " do
2019-05-09 11:28:28 +08:00
expect ( Fabricate . build ( :post ) . attachment_count ) . to eq ( 0 )
2013-07-22 08:39:17 +08:00
end
it " finds attachments from HTML " do
2015-01-06 00:04:23 +08:00
expect ( post_two_attachments . attachment_count ) . to eq ( 2 )
2013-07-22 08:39:17 +08:00
end
context " validation " do
before do
2017-07-07 14:09:14 +08:00
SiteSetting . newuser_max_attachments = 1
2013-07-22 08:39:17 +08:00
end
context 'newuser' do
it " allows a new user to post below the limit " do
2015-01-06 00:04:23 +08:00
expect ( post_one_attachment ) . to be_valid
2013-07-22 08:39:17 +08:00
end
it " doesn't allow more than the maximum " do
2015-01-06 00:04:23 +08:00
expect ( post_two_attachments ) . not_to be_valid
2013-07-22 08:39:17 +08:00
end
it " doesn't allow a new user to edit their post to insert an attachment " do
2014-09-05 13:20:39 +08:00
post_no_attachments . user . trust_level = TrustLevel [ 0 ]
2013-07-22 08:39:17 +08:00
post_no_attachments . save
2015-01-06 00:04:23 +08:00
expect {
2014-10-28 05:06:43 +08:00
post_no_attachments . revise ( post_no_attachments . user , raw : post_two_attachments . raw )
2013-07-22 08:39:17 +08:00
post_no_attachments . reload
2015-01-06 00:04:23 +08:00
} . not_to change ( post_no_attachments , :raw )
2013-07-22 08:39:17 +08:00
end
end
it " allows more attachments from a not-new account " do
2014-09-05 13:20:39 +08:00
post_two_attachments . user . trust_level = TrustLevel [ 1 ]
2015-01-06 00:04:23 +08:00
expect ( post_two_attachments ) . to be_valid
2013-07-22 08:39:17 +08:00
end
end
end
2013-05-11 04:58:23 +08:00
context " links " do
2019-05-07 11:12:20 +08:00
fab! ( :newuser ) { Fabricate ( :user , trust_level : TrustLevel [ 0 ] ) }
2013-05-11 04:58:23 +08:00
let ( :no_links ) { post_with_body ( " hello world my name is evil trout " , newuser ) }
let ( :one_link ) { post_with_body ( " [jlawr](http://www.imdb.com/name/nm2225369) " , newuser ) }
let ( :two_links ) { post_with_body ( " <a href='http://disneyland.disney.go.com/'>disney</a> <a href='http://reddit.com'>reddit</a> " , newuser ) }
let ( :three_links ) { post_with_body ( " http://discourse.org and http://discourse.org/another_url and http://www.imdb.com/name/nm2225369 " , newuser ) }
describe " raw_links " do
it " returns a blank collection for a post with no links " do
2015-01-06 00:04:23 +08:00
expect ( no_links . raw_links ) . to be_blank
2013-05-11 04:58:23 +08:00
end
it " finds a link within markdown " do
2015-01-06 00:04:23 +08:00
expect ( one_link . raw_links ) . to eq ( [ " http://www.imdb.com/name/nm2225369 " ] )
2013-05-11 04:58:23 +08:00
end
it " can find two links from html " do
2015-01-06 00:04:23 +08:00
expect ( two_links . raw_links ) . to eq ( [ " http://disneyland.disney.go.com/ " , " http://reddit.com " ] )
2013-05-11 04:58:23 +08:00
end
it " can find three links without markup " do
2015-01-06 00:04:23 +08:00
expect ( three_links . raw_links ) . to eq ( [ " http://discourse.org " , " http://discourse.org/another_url " , " http://www.imdb.com/name/nm2225369 " ] )
2013-05-11 04:58:23 +08:00
end
end
describe " linked_hosts " do
it " returns blank with no links " do
2015-01-06 00:04:23 +08:00
expect ( no_links . linked_hosts ) . to be_blank
2013-05-11 04:58:23 +08:00
end
it " returns the host and a count for links " do
2015-01-06 00:04:23 +08:00
expect ( two_links . linked_hosts ) . to eq ( " disneyland.disney.go.com " = > 1 , " reddit.com " = > 1 )
2013-05-11 04:58:23 +08:00
end
it " it counts properly with more than one link on the same host " do
2015-01-06 00:04:23 +08:00
expect ( three_links . linked_hosts ) . to eq ( " discourse.org " = > 1 , " www.imdb.com " = > 1 )
2013-05-11 04:58:23 +08:00
end
end
describe " total host usage " do
it " has none for a regular post " do
2015-01-06 00:04:23 +08:00
expect ( no_links . total_hosts_usage ) . to be_blank
2013-05-11 04:58:23 +08:00
end
context " with a previous host " do
let ( :another_disney_link ) { post_with_body ( " [radiator springs](http://disneyland.disney.go.com/disney-california-adventure/radiator-springs-racers/) " , newuser ) }
before do
another_disney_link . save
TopicLink . extract_from ( another_disney_link )
end
it " contains the new post's links, PLUS the previous one " do
2015-01-06 00:04:23 +08:00
expect ( two_links . total_hosts_usage ) . to eq ( 'disneyland.disney.go.com' = > 2 , 'reddit.com' = > 1 )
2013-05-11 04:58:23 +08:00
end
end
end
end
2018-02-07 07:07:24 +08:00
describe " maximums " do
2019-05-07 11:12:20 +08:00
fab! ( :newuser ) { Fabricate ( :user , trust_level : TrustLevel [ 0 ] ) }
2013-05-11 04:58:23 +08:00
let ( :post_one_link ) { post_with_body ( " [sherlock](http://www.bbc.co.uk/programmes/b018ttws) " , newuser ) }
2018-02-09 07:26:56 +08:00
let ( :post_onebox ) { post_with_body ( " http://www.google.com " , newuser ) }
2018-03-07 00:46:12 +08:00
let ( :post_code_link ) { post_with_body ( " <code>http://www.google.com</code> " , newuser ) }
2013-05-11 04:58:23 +08:00
let ( :post_two_links ) { post_with_body ( " <a href='http://discourse.org'>discourse</a> <a href='http://twitter.com'>twitter</a> " , newuser ) }
let ( :post_with_mentions ) { post_with_body ( " hello @ #{ newuser . username } how are you doing? " , newuser ) }
2013-02-06 03:16:51 +08:00
2013-03-26 01:12:15 +08:00
it " returns 0 links for an empty post " do
2019-05-09 11:28:28 +08:00
expect ( Fabricate . build ( :post ) . link_count ) . to eq ( 0 )
2013-02-06 03:16:51 +08:00
end
2013-03-26 01:12:15 +08:00
it " returns 0 links for a post with mentions " do
2015-01-06 00:04:23 +08:00
expect ( post_with_mentions . link_count ) . to eq ( 0 )
2013-03-26 01:12:15 +08:00
end
it " finds links from markdown " do
2015-01-06 00:04:23 +08:00
expect ( post_one_link . link_count ) . to eq ( 1 )
2013-02-06 03:16:51 +08:00
end
2013-03-26 01:12:15 +08:00
it " finds links from HTML " do
2015-01-06 00:04:23 +08:00
expect ( post_two_links . link_count ) . to eq ( 2 )
2013-02-06 03:16:51 +08:00
end
context " validation " do
2013-03-26 00:24:46 +08:00
before do
2017-07-07 14:09:14 +08:00
SiteSetting . newuser_max_links = 1
2013-02-06 03:16:51 +08:00
end
2013-04-18 07:11:13 +08:00
context 'newuser' do
2013-03-26 00:24:46 +08:00
it " returns true when within the amount of links allowed " do
2015-01-06 00:04:23 +08:00
expect ( post_one_link ) . to be_valid
2013-03-26 00:24:46 +08:00
end
it " doesn't allow more links than allowed " do
2015-01-06 00:04:23 +08:00
expect ( post_two_links ) . not_to be_valid
2013-03-26 00:24:46 +08:00
end
2013-02-06 03:16:51 +08:00
end
2018-02-07 07:07:24 +08:00
it " allows multiple links for basic accounts " do
2014-09-05 13:20:39 +08:00
post_two_links . user . trust_level = TrustLevel [ 1 ]
2015-01-06 00:04:23 +08:00
expect ( post_two_links ) . to be_valid
2013-02-06 03:16:51 +08:00
end
2013-03-26 00:24:46 +08:00
2018-02-09 07:26:56 +08:00
context " min_trust_to_post_links " do
it " considers oneboxes links " do
SiteSetting . min_trust_to_post_links = 3
post_onebox . user . trust_level = TrustLevel [ 2 ]
expect ( post_onebox ) . not_to be_valid
end
2018-03-07 00:46:12 +08:00
it " considers links within code " do
SiteSetting . min_trust_to_post_links = 3
post_onebox . user . trust_level = TrustLevel [ 2 ]
expect ( post_code_link ) . not_to be_valid
end
2018-02-09 07:26:56 +08:00
it " doesn't allow allow links if `min_trust_to_post_links` is not met " do
SiteSetting . min_trust_to_post_links = 2
post_two_links . user . trust_level = TrustLevel [ 1 ]
expect ( post_one_link ) . not_to be_valid
end
2018-06-14 02:57:32 +08:00
2020-07-27 08:23:54 +08:00
it " will skip the check for allowlisted domains " do
SiteSetting . allowed_link_domains = 'www.bbc.co.uk'
2018-06-14 02:57:32 +08:00
SiteSetting . min_trust_to_post_links = 2
post_two_links . user . trust_level = TrustLevel [ 1 ]
expect ( post_one_link ) . to be_valid
end
2018-02-07 07:07:24 +08:00
end
2013-02-06 03:16:51 +08:00
end
end
2013-03-26 00:24:46 +08:00
describe " @mentions " do
2013-02-06 03:16:51 +08:00
context 'raw_mentions' do
it " returns an empty array with no matches " do
2019-05-09 11:28:28 +08:00
post = Fabricate . build ( :post , post_args . merge ( raw : " Hello Jake and Finn! " ) )
2015-01-06 00:04:23 +08:00
expect ( post . raw_mentions ) . to eq ( [ ] )
2013-02-06 03:16:51 +08:00
end
it " returns lowercase unique versions of the mentions " do
2019-05-09 11:28:28 +08:00
post = Fabricate . build ( :post , post_args . merge ( raw : " @Jake @Finn @Jake " ) )
2015-01-06 00:04:23 +08:00
expect ( post . raw_mentions ) . to eq ( [ 'jake' , 'finn' ] )
2013-02-06 03:16:51 +08:00
end
it " ignores pre " do
2017-07-14 20:27:28 +08:00
# we need to force an inline
2019-05-09 11:28:28 +08:00
post = Fabricate . build ( :post , post_args . merge ( raw : " p <pre>@Jake</pre> @Finn " ) )
2015-01-06 00:04:23 +08:00
expect ( post . raw_mentions ) . to eq ( [ 'finn' ] )
2013-02-06 03:16:51 +08:00
end
it " catches content between pre tags " do
2017-07-14 20:27:28 +08:00
# per common mark we need to force an inline
2019-05-09 11:28:28 +08:00
post = Fabricate . build ( :post , post_args . merge ( raw : " a <pre>hello</pre> @Finn <pre></pre> " ) )
2015-01-06 00:04:23 +08:00
expect ( post . raw_mentions ) . to eq ( [ 'finn' ] )
2013-02-06 03:16:51 +08:00
end
it " ignores code " do
2019-05-09 11:28:28 +08:00
post = Fabricate . build ( :post , post_args . merge ( raw : " @Jake `@Finn` " ) )
2015-01-06 00:04:23 +08:00
expect ( post . raw_mentions ) . to eq ( [ 'jake' ] )
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
it " ignores quotes " do
2019-05-09 11:28:28 +08:00
post = Fabricate . build ( :post , post_args . merge ( raw : " [quote= \" Evil Trout \" ] \n @Jake \n [/quote] \n @Finn " ) )
2015-01-06 00:04:23 +08:00
expect ( post . raw_mentions ) . to eq ( [ 'finn' ] )
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
2013-03-24 08:57:00 +08:00
it " handles underscore in username " do
2019-05-09 11:28:28 +08:00
post = Fabricate . build ( :post , post_args . merge ( raw : " @Jake @Finn @Jake_Old " ) )
2015-01-06 00:04:23 +08:00
expect ( post . raw_mentions ) . to eq ( [ 'jake' , 'finn' , 'jake_old' ] )
2013-03-24 08:57:00 +08:00
end
2016-02-24 03:57:54 +08:00
it " handles hyphen in groupname " do
2019-05-09 11:28:28 +08:00
post = Fabricate . build ( :post , post_args . merge ( raw : " @org-board " ) )
2016-02-24 03:57:54 +08:00
expect ( post . raw_mentions ) . to eq ( [ 'org-board' ] )
end
2013-02-06 03:16:51 +08:00
end
2013-03-26 00:24:46 +08:00
context " max mentions " do
2019-05-07 11:12:20 +08:00
fab! ( :newuser ) { Fabricate ( :user , trust_level : TrustLevel [ 0 ] ) }
2013-05-11 04:58:23 +08:00
let ( :post_with_one_mention ) { post_with_body ( " @Jake is the person I'm mentioning " , newuser ) }
let ( :post_with_two_mentions ) { post_with_body ( " @Jake @Finn are the people I'm mentioning " , newuser ) }
2013-03-26 00:24:46 +08:00
2013-04-18 07:11:13 +08:00
context 'new user' do
2013-03-26 00:24:46 +08:00
before do
2017-07-07 14:09:14 +08:00
SiteSetting . newuser_max_mentions_per_post = 1
SiteSetting . max_mentions_per_post = 5
2013-03-26 00:24:46 +08:00
end
2013-04-18 07:11:13 +08:00
it " allows a new user to have newuser_max_mentions_per_post mentions " do
2015-01-06 00:04:23 +08:00
expect ( post_with_one_mention ) . to be_valid
2013-03-26 00:24:46 +08:00
end
2013-04-18 07:11:13 +08:00
it " doesn't allow a new user to have more than newuser_max_mentions_per_post mentions " do
2015-01-06 00:04:23 +08:00
expect ( post_with_two_mentions ) . not_to be_valid
2013-03-26 00:24:46 +08:00
end
2013-02-06 03:16:51 +08:00
end
2013-04-18 07:11:13 +08:00
context " not a new user " do
2013-03-26 00:24:46 +08:00
before do
2017-07-07 14:09:14 +08:00
SiteSetting . newuser_max_mentions_per_post = 0
SiteSetting . max_mentions_per_post = 1
2013-03-26 00:24:46 +08:00
end
it " allows vmax_mentions_per_post mentions " do
2014-09-05 13:20:39 +08:00
post_with_one_mention . user . trust_level = TrustLevel [ 1 ]
2015-01-06 00:04:23 +08:00
expect ( post_with_one_mention ) . to be_valid
2013-03-26 00:24:46 +08:00
end
it " doesn't allow to have more than max_mentions_per_post mentions " do
2014-09-05 13:20:39 +08:00
post_with_two_mentions . user . trust_level = TrustLevel [ 1 ]
2015-01-06 00:04:23 +08:00
expect ( post_with_two_mentions ) . not_to be_valid
2013-03-26 00:24:46 +08:00
end
2013-02-06 03:16:51 +08:00
end
2013-03-26 00:24:46 +08:00
2013-02-06 03:16:51 +08:00
end
end
2013-06-13 16:18:17 +08:00
context 'validation' do
it 'validates our default post' do
2019-05-09 11:28:28 +08:00
expect ( Fabricate . build ( :post , post_args ) ) . to be_valid
2013-06-13 16:18:17 +08:00
end
2017-01-16 13:24:47 +08:00
it 'create blank posts as invalid' do
2019-05-09 11:28:28 +08:00
expect ( Fabricate . build ( :post , raw : " " ) ) . not_to be_valid
2013-06-13 16:18:17 +08:00
end
2013-02-06 03:16:51 +08:00
end
context " raw_hash " do
let ( :raw ) { " this is our test post body " }
2013-05-11 04:58:23 +08:00
let ( :post ) { post_with_body ( raw ) }
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
it " returns a value " do
2015-01-06 00:04:23 +08:00
expect ( post . raw_hash ) . to be_present
2013-02-06 03:16:51 +08:00
end
it " returns blank for a nil body " do
post . raw = nil
2015-01-06 00:04:23 +08:00
expect ( post . raw_hash ) . to be_blank
2013-02-06 03:16:51 +08:00
end
it " returns the same value for the same raw " do
2015-01-06 00:04:23 +08:00
expect ( post . raw_hash ) . to eq ( post_with_body ( raw ) . raw_hash )
2013-02-06 03:16:51 +08:00
end
it " returns a different value for a different raw " do
2015-01-06 00:04:23 +08:00
expect ( post . raw_hash ) . not_to eq ( post_with_body ( " something else " ) . raw_hash )
2013-02-06 03:16:51 +08:00
end
2013-06-18 03:57:13 +08:00
it " returns a different value with different text case " do
2015-01-06 00:04:23 +08:00
expect ( post . raw_hash ) . not_to eq ( post_with_body ( " THIS is OUR TEST post BODy " ) . raw_hash )
2013-02-06 03:16:51 +08:00
end
end
context 'revise' do
2019-05-09 11:28:28 +08:00
let ( :post ) { Fabricate ( :post , post_args ) }
2013-02-06 03:16:51 +08:00
let ( :first_version_at ) { post . last_version_at }
2013-12-12 10:41:34 +08:00
it 'has no revision' do
2015-01-06 00:04:23 +08:00
expect ( post . revisions . size ) . to eq ( 0 )
expect ( first_version_at ) . to be_present
expect ( post . revise ( post . user , raw : post . raw ) ) . to eq ( false )
2013-02-06 03:16:51 +08:00
end
2013-04-22 15:45:03 +08:00
describe 'with the same body' do
2013-12-12 10:41:34 +08:00
it " doesn't change version " do
2015-01-06 00:04:23 +08:00
expect { post . revise ( post . user , raw : post . raw ) ; post . reload } . not_to change ( post , :version )
2013-02-06 03:16:51 +08:00
end
end
2014-10-28 05:06:43 +08:00
describe 'ninja editing & edit windows' do
2013-02-06 03:16:51 +08:00
2017-07-07 14:09:14 +08:00
before { SiteSetting . editing_grace_period = 1 . minute . to_i }
2013-04-30 20:54:30 +08:00
2014-10-28 05:06:43 +08:00
it 'works' do
revised_at = post . updated_at + 2 . minutes
new_revised_at = revised_at + 2 . minutes
2013-02-06 03:16:51 +08:00
2014-10-28 05:06:43 +08:00
# ninja edit
post . revise ( post . user , { raw : 'updated body' } , revised_at : post . updated_at + 10 . seconds )
2013-02-06 03:16:51 +08:00
post . reload
2015-01-06 00:04:23 +08:00
expect ( post . version ) . to eq ( 1 )
expect ( post . public_version ) . to eq ( 1 )
expect ( post . revisions . size ) . to eq ( 0 )
expect ( post . last_version_at . to_i ) . to eq ( first_version_at . to_i )
2013-02-06 03:16:51 +08:00
2014-10-28 05:06:43 +08:00
# revision much later
post . revise ( post . user , { raw : 'another updated body' } , revised_at : revised_at )
post . reload
2015-01-06 00:04:23 +08:00
expect ( post . version ) . to eq ( 2 )
expect ( post . public_version ) . to eq ( 2 )
expect ( post . revisions . size ) . to eq ( 1 )
expect ( post . last_version_at . to_i ) . to eq ( revised_at . to_i )
2013-02-06 03:16:51 +08:00
2014-10-28 05:06:43 +08:00
# new edit window
post . revise ( post . user , { raw : 'yet another updated body' } , revised_at : revised_at + 10 . seconds )
post . reload
2015-01-06 00:04:23 +08:00
expect ( post . version ) . to eq ( 2 )
expect ( post . public_version ) . to eq ( 2 )
expect ( post . revisions . size ) . to eq ( 1 )
expect ( post . last_version_at . to_i ) . to eq ( revised_at . to_i )
2013-02-06 03:16:51 +08:00
2014-10-28 05:06:43 +08:00
# after second window
post . revise ( post . user , { raw : 'yet another, another updated body' } , revised_at : new_revised_at )
post . reload
2015-01-06 00:04:23 +08:00
expect ( post . version ) . to eq ( 3 )
expect ( post . public_version ) . to eq ( 3 )
expect ( post . revisions . size ) . to eq ( 2 )
expect ( post . last_version_at . to_i ) . to eq ( new_revised_at . to_i )
2013-02-06 03:16:51 +08:00
end
end
describe 'rate limiter' do
let ( :changed_by ) { Fabricate ( :coding_horror ) }
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
it " triggers a rate limiter " do
2013-02-09 23:33:07 +08:00
EditRateLimiter . any_instance . expects ( :performed! )
2014-10-28 05:06:43 +08:00
post . revise ( changed_by , raw : 'updated body' )
2013-02-06 03:16:51 +08:00
end
end
describe 'with a new body' do
let ( :changed_by ) { Fabricate ( :coding_horror ) }
2014-10-28 05:06:43 +08:00
let! ( :result ) { post . revise ( changed_by , raw : 'updated body' ) }
2013-02-06 03:16:51 +08:00
2013-04-22 15:45:03 +08:00
it 'acts correctly' do
2015-01-06 00:04:23 +08:00
expect ( result ) . to eq ( true )
expect ( post . raw ) . to eq ( 'updated body' )
expect ( post . invalidate_oneboxes ) . to eq ( true )
expect ( post . version ) . to eq ( 2 )
expect ( post . public_version ) . to eq ( 2 )
expect ( post . revisions . size ) . to eq ( 1 )
expect ( post . revisions . first . user ) . to be_present
2013-02-06 03:16:51 +08:00
end
context 'second poster posts again quickly' do
2014-10-28 05:06:43 +08:00
it 'is a ninja edit, because the second poster posted again quickly' do
2018-03-07 13:44:21 +08:00
SiteSetting . editing_grace_period = 1 . minute . to_i
2014-10-28 05:06:43 +08:00
post . revise ( changed_by , { raw : 'yet another updated body' } , revised_at : post . updated_at + 10 . seconds )
2013-02-06 03:16:51 +08:00
post . reload
2015-01-06 00:04:23 +08:00
expect ( post . version ) . to eq ( 2 )
expect ( post . public_version ) . to eq ( 2 )
expect ( post . revisions . size ) . to eq ( 1 )
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
end
end
2014-10-28 05:06:43 +08:00
2013-02-06 03:16:51 +08:00
end
2016-10-24 12:02:38 +08:00
describe 'before save' do
2019-12-18 13:51:57 +08:00
let ( :cooked ) { " <p><div class= \" lightbox-wrapper \" ><a data-download-href= \" //localhost:3000/ #{ upload_path } /34784374092783e2fef84b8bc96d9b54c11ceea0 \" href= \" //localhost:3000/ #{ upload_path } /original/1X/34784374092783e2fef84b8bc96d9b54c11ceea0.gif \" class= \" lightbox \" title= \" Sword reworks.gif \" ><img src= \" //localhost:3000/ #{ upload_path } /optimized/1X/34784374092783e2fef84b8bc96d9b54c11ceea0_1_690x276.gif \" width= \" 690 \" height= \" 276 \" ><div class= \" meta \" > \n <span class= \" filename \" >Sword reworks.gif</span><span class= \" informations \" >1000x400 1000 KB</span><span class= \" expand \" ></span> \n </div></a></div></p> " }
2016-10-24 12:02:38 +08:00
let ( :post ) do
2019-05-09 11:28:28 +08:00
Fabricate ( :post ,
2019-12-18 13:51:57 +08:00
raw : " <img src= \" / #{ upload_path } /original/1X/34784374092783e2fef84b8bc96d9b54c11ceea0.gif \" width= \" 690 \" height= \" 276 \" > " ,
2016-10-24 12:02:38 +08:00
cooked : cooked
)
end
it 'should not cook the post if raw has not been changed' do
post . save!
expect ( post . cooked ) . to eq ( cooked )
end
end
2013-02-06 03:16:51 +08:00
describe 'after save' do
2019-05-09 11:28:28 +08:00
let ( :post ) { Fabricate ( :post , post_args ) }
2013-02-06 03:16:51 +08:00
2013-04-22 15:45:03 +08:00
it " has correct info set " do
2015-01-06 00:04:23 +08:00
expect ( post . user_deleted? ) . to eq ( false )
expect ( post . post_number ) . to be_present
expect ( post . excerpt ) . to be_present
expect ( post . post_type ) . to eq ( Post . types [ :regular ] )
expect ( post . revisions ) . to be_blank
expect ( post . cooked ) . to be_present
expect ( post . external_id ) . to be_present
expect ( post . quote_count ) . to eq ( 0 )
expect ( post . replies ) . to be_blank
2013-02-06 03:16:51 +08:00
end
2013-03-19 03:54:08 +08:00
describe 'extract_quoted_post_numbers' do
2013-02-06 03:16:51 +08:00
2019-05-09 11:28:28 +08:00
let! ( :post ) { Fabricate ( :post , post_args ) }
let ( :reply ) { Fabricate . build ( :post , post_args ) }
2013-02-06 03:16:51 +08:00
it " finds the quote when in the same topic " do
reply . raw = " [quote= \" EvilTrout, post: #{ post . post_number } , topic: #{ post . topic_id } \" ]hello[/quote] "
reply . extract_quoted_post_numbers
2015-01-06 00:04:23 +08:00
expect ( reply . quoted_post_numbers ) . to eq ( [ post . post_number ] )
2013-02-06 03:16:51 +08:00
end
it " doesn't find the quote in a different topic " do
reply . raw = " [quote= \" EvilTrout, post: #{ post . post_number } , topic: #{ post . topic_id + 1 } \" ]hello[/quote] "
reply . extract_quoted_post_numbers
2015-01-06 00:04:23 +08:00
expect ( reply . quoted_post_numbers ) . to be_blank
2013-02-06 03:16:51 +08:00
end
2018-07-10 16:17:28 +08:00
it " doesn't find the quote in the same post " do
2019-05-09 11:28:28 +08:00
reply = Fabricate . build ( :post , post_args . merge ( post_number : 646 ) )
2018-07-10 16:17:28 +08:00
reply . raw = " [quote= \" EvilTrout, post: #{ reply . post_number } , topic: #{ post . topic_id } \" ]hello[/quote] "
reply . extract_quoted_post_numbers
expect ( reply . quoted_post_numbers ) . to be_blank
end
2013-02-06 03:16:51 +08:00
end
describe 'a new reply' do
2019-05-07 11:12:20 +08:00
fab! ( :topic ) { Fabricate ( :topic ) }
2013-03-19 03:54:08 +08:00
let ( :other_user ) { Fabricate ( :coding_horror ) }
let ( :reply_text ) { " [quote= \" Evil Trout, post:1 \" ] \n hello \n [/quote] \n Hmmm! " }
2019-05-09 11:28:28 +08:00
let! ( :post ) { PostCreator . new ( topic . user , raw : Fabricate . build ( :post ) . raw , topic_id : topic . id ) . create }
2013-03-19 03:54:08 +08:00
let! ( :reply ) { PostCreator . new ( other_user , raw : reply_text , topic_id : topic . id , reply_to_post_number : post . post_number ) . create }
2013-02-06 03:16:51 +08:00
it 'has a quote' do
2015-01-06 00:04:23 +08:00
expect ( reply . quote_count ) . to eq ( 1 )
2013-02-06 03:16:51 +08:00
end
it 'has a reply to the user of the original user' do
2015-01-06 00:04:23 +08:00
expect ( reply . reply_to_user ) . to eq ( post . user )
2013-02-06 03:16:51 +08:00
end
it 'increases the reply count of the parent' do
post . reload
2015-01-06 00:04:23 +08:00
expect ( post . reply_count ) . to eq ( 1 )
2013-02-06 03:16:51 +08:00
end
it 'increases the reply count of the topic' do
topic . reload
2015-01-06 00:04:23 +08:00
expect ( topic . reply_count ) . to eq ( 1 )
2013-02-06 03:16:51 +08:00
end
it 'is the child of the parent post' do
2015-01-06 00:04:23 +08:00
expect ( post . replies ) . to eq ( [ reply ] )
2013-02-06 03:16:51 +08:00
end
it " doesn't change the post count when you edit the reply " do
reply . raw = 'updated raw'
reply . save
post . reload
2015-01-06 00:04:23 +08:00
expect ( post . reply_count ) . to eq ( 1 )
2013-02-06 03:16:51 +08:00
end
context 'a multi-quote reply' do
2013-03-19 03:54:08 +08:00
let! ( :multi_reply ) do
raw = " [quote= \" Evil Trout, post:1 \" ]post1 quote[/quote] \n Aha! \n [quote= \" Evil Trout, post:2 \" ]post2 quote[/quote] \n Neat-o "
PostCreator . new ( other_user , raw : raw , topic_id : topic . id , reply_to_post_number : post . post_number ) . create
end
2013-02-06 03:16:51 +08:00
2013-04-22 15:45:03 +08:00
it 'has the correct info set' do
2015-01-06 00:04:23 +08:00
expect ( multi_reply . quote_count ) . to eq ( 2 )
expect ( post . replies . include? ( multi_reply ) ) . to eq ( true )
expect ( reply . replies . include? ( multi_reply ) ) . to eq ( true )
2013-02-06 03:16:51 +08:00
end
end
end
end
2013-11-19 01:48:26 +08:00
context 'summary' do
2019-05-09 11:28:28 +08:00
let! ( :p1 ) { Fabricate ( :post , post_args . merge ( score : 4 , percent_rank : 0 . 33 ) ) }
let! ( :p2 ) { Fabricate ( :post , post_args . merge ( score : 10 , percent_rank : 0 . 66 ) ) }
let! ( :p3 ) { Fabricate ( :post , post_args . merge ( score : 5 , percent_rank : 0 . 99 ) ) }
fab! ( :p4 ) { Fabricate ( :post , percent_rank : 0 . 99 ) }
2013-02-06 03:16:51 +08:00
2013-11-19 01:48:26 +08:00
it " returns the OP and posts above the threshold in summary mode " do
2017-07-07 14:09:14 +08:00
SiteSetting . summary_percent_filter = 66
2018-06-21 12:00:54 +08:00
expect ( Post . summary ( topic . id ) . order ( :post_number ) ) . to eq ( [ p1 , p2 ] )
expect ( Post . summary ( p4 . topic . id ) ) . to eq ( [ p4 ] )
2013-02-06 03:16:51 +08:00
end
end
context 'sort_order' do
context 'regular topic' do
2019-05-09 11:28:28 +08:00
let! ( :p1 ) { Fabricate ( :post , post_args ) }
let! ( :p2 ) { Fabricate ( :post , post_args ) }
let! ( :p3 ) { Fabricate ( :post , post_args ) }
2013-02-06 03:16:51 +08:00
it 'defaults to created order' do
2015-01-06 00:04:23 +08:00
expect ( Post . regular_order ) . to eq ( [ p1 , p2 , p3 ] )
2013-02-06 03:16:51 +08:00
end
end
2013-08-07 05:42:36 +08:00
end
context " reply_history " do
2019-05-09 11:28:28 +08:00
let! ( :p1 ) { Fabricate ( :post , post_args ) }
let! ( :p2 ) { Fabricate ( :post , post_args . merge ( reply_to_post_number : p1 . post_number ) ) }
let! ( :p3 ) { Fabricate ( :post , post_args ) }
let! ( :p4 ) { Fabricate ( :post , post_args . merge ( reply_to_post_number : p2 . post_number ) ) }
2013-08-07 05:42:36 +08:00
it " returns the posts in reply to this post " do
2015-01-06 00:04:23 +08:00
expect ( p4 . reply_history ) . to eq ( [ p1 , p2 ] )
expect ( p4 . reply_history ( 1 ) ) . to eq ( [ p2 ] )
expect ( p3 . reply_history ) . to be_blank
expect ( p2 . reply_history ) . to eq ( [ p1 ] )
2013-08-07 05:42:36 +08:00
end
2013-02-06 03:16:51 +08:00
end
2017-12-14 05:12:06 +08:00
context " reply_ids " do
2019-05-07 11:12:20 +08:00
fab! ( :topic ) { Fabricate ( :topic ) }
2019-05-09 11:28:28 +08:00
let! ( :p1 ) { Fabricate ( :post , topic : topic , post_number : 1 ) }
let! ( :p2 ) { Fabricate ( :post , topic : topic , post_number : 2 , reply_to_post_number : 1 ) }
let! ( :p3 ) { Fabricate ( :post , topic : topic , post_number : 3 ) }
let! ( :p4 ) { Fabricate ( :post , topic : topic , post_number : 4 , reply_to_post_number : 2 ) }
let! ( :p5 ) { Fabricate ( :post , topic : topic , post_number : 5 , reply_to_post_number : 4 ) }
let! ( :p6 ) { Fabricate ( :post , topic : topic , post_number : 6 ) }
2017-12-14 05:12:06 +08:00
before {
PostReply . create! ( post : p1 , reply : p2 )
PostReply . create! ( post : p2 , reply : p4 )
2017-12-14 07:43:48 +08:00
PostReply . create! ( post : p2 , reply : p6 ) # simulates p6 quoting p2
PostReply . create! ( post : p3 , reply : p5 ) # simulates p5 quoting p3
2017-12-14 05:12:06 +08:00
PostReply . create! ( post : p4 , reply : p5 )
2017-12-15 07:38:14 +08:00
PostReply . create! ( post : p6 , reply : p6 ) # https://meta.discourse.org/t/topic-quoting-itself-displays-reply-indicator/76085
2017-12-14 05:12:06 +08:00
}
it " returns the reply ids and their level " do
2017-12-14 07:43:48 +08:00
expect ( p1 . reply_ids ) . to eq ( [ { id : p2 . id , level : 1 } , { id : p4 . id , level : 2 } , { id : p6 . id , level : 2 } ] )
expect ( p2 . reply_ids ) . to eq ( [ { id : p4 . id , level : 1 } , { id : p6 . id , level : 1 } ] )
expect ( p3 . reply_ids ) . to be_empty # has no replies
expect ( p4 . reply_ids ) . to be_empty # p5 replies to 2 posts (p4 and p3)
expect ( p5 . reply_ids ) . to be_empty # has no replies
2017-12-15 07:38:14 +08:00
expect ( p6 . reply_ids ) . to be_empty # quotes itself
2017-12-14 05:12:06 +08:00
end
2020-02-04 02:12:27 +08:00
it " ignores posts moved to other topics " do
p2 . update_column ( :topic_id , Fabricate ( :topic ) . id )
expect ( p1 . reply_ids ) . to be_blank
end
2020-02-04 02:34:35 +08:00
it " doesn't include the same reply twice " do
PostReply . create! ( post : p4 , reply : p1 )
expect ( p1 . reply_ids . size ) . to eq ( 4 )
end
2018-04-21 05:05:51 +08:00
it " does not skip any replies " do
expect ( p1 . reply_ids ( only_replies_to_single_post : false ) ) . to eq ( [ { id : p2 . id , level : 1 } , { id : p4 . id , level : 2 } , { id : p5 . id , level : 3 } , { id : p6 . id , level : 2 } ] )
expect ( p2 . reply_ids ( only_replies_to_single_post : false ) ) . to eq ( [ { id : p4 . id , level : 1 } , { id : p5 . id , level : 2 } , { id : p6 . id , level : 1 } ] )
expect ( p3 . reply_ids ( only_replies_to_single_post : false ) ) . to eq ( [ { id : p5 . id , level : 1 } ] )
expect ( p4 . reply_ids ( only_replies_to_single_post : false ) ) . to eq ( [ { id : p5 . id , level : 1 } ] )
expect ( p5 . reply_ids ( only_replies_to_single_post : false ) ) . to be_empty # has no replies
expect ( p6 . reply_ids ( only_replies_to_single_post : false ) ) . to be_empty # quotes itself
end
2017-12-14 05:12:06 +08:00
end
2013-04-22 15:45:03 +08:00
describe 'urls' do
2013-05-11 04:58:23 +08:00
it 'no-ops for empty list' do
2015-01-06 00:04:23 +08:00
expect ( Post . urls ( [ ] ) ) . to eq ( { } )
2013-04-22 15:45:03 +08:00
end
2013-05-11 04:58:23 +08:00
# integration test -> should move to centralized integration test
it 'finds urls for posts presented' do
2019-05-09 11:28:28 +08:00
p1 = Fabricate ( :post )
p2 = Fabricate ( :post )
2015-01-06 00:04:23 +08:00
expect ( Post . urls ( [ p1 . id , p2 . id ] ) ) . to eq ( p1 . id = > p1 . url , p2 . id = > p2 . url )
2013-04-22 15:45:03 +08:00
end
end
2013-10-15 22:21:30 +08:00
describe " details " do
it " adds details " do
post = Fabricate . build ( :post )
post . add_detail ( " key " , " value " )
2015-01-06 00:04:23 +08:00
expect ( post . post_details . size ) . to eq ( 1 )
expect ( post . post_details . first . key ) . to eq ( " key " )
expect ( post . post_details . first . value ) . to eq ( " value " )
2013-10-15 22:21:30 +08:00
end
it " can find a post by a detail " do
detail = Fabricate ( :post_detail )
post = detail . post
2015-01-06 00:04:23 +08:00
expect ( Post . find_by_detail ( detail . key , detail . value ) . id ) . to eq ( post . id )
2013-10-15 22:21:30 +08:00
end
end
2014-01-16 00:34:17 +08:00
describe " cooking " do
let ( :post ) { Fabricate . build ( :post , post_args . merge ( raw : " please read my blog http://blog.example.com " ) ) }
2018-09-17 10:02:20 +08:00
it " should unconditionally follow links for staff " do
SiteSetting . tl3_links_no_follow = true
post . user . trust_level = 1
post . user . moderator = true
post . save
expect ( post . cooked ) . not_to match ( / nofollow / )
end
2014-01-16 00:34:17 +08:00
it " should add nofollow to links in the post for trust levels below 3 " do
post . user . trust_level = 2
post . save
2020-09-10 23:59:51 +08:00
expect ( post . cooked ) . to match ( / noopener nofollow ugc / )
2014-01-16 00:34:17 +08:00
end
2014-09-05 04:16:46 +08:00
it " when tl3_links_no_follow is false, should not add nofollow for trust level 3 and higher " do
2017-07-07 14:09:14 +08:00
SiteSetting . tl3_links_no_follow = false
2014-01-16 00:34:17 +08:00
post . user . trust_level = 3
post . save
2015-01-06 00:04:23 +08:00
expect ( post . cooked ) . not_to match ( / nofollow / )
2014-01-16 00:34:17 +08:00
end
2014-07-15 01:34:21 +08:00
2014-09-05 04:16:46 +08:00
it " when tl3_links_no_follow is true, should add nofollow for trust level 3 and higher " do
2017-07-07 14:09:14 +08:00
SiteSetting . tl3_links_no_follow = true
2014-07-15 01:34:21 +08:00
post . user . trust_level = 3
post . save
2020-09-10 23:59:51 +08:00
expect ( post . cooked ) . to match ( / noopener nofollow ugc / )
2014-07-15 01:34:21 +08:00
end
2018-11-22 16:01:03 +08:00
describe 'mentions' do
2019-05-07 11:12:20 +08:00
fab! ( :group ) do
2018-11-22 16:01:03 +08:00
Fabricate ( :group ,
2019-12-12 19:13:40 +08:00
visibility_level : Group . visibility_levels [ :members ] ,
2018-11-22 16:01:03 +08:00
mentionable_level : Group :: ALIAS_LEVELS [ :members_mods_and_admins ]
)
end
2018-11-26 12:57:07 +08:00
before do
2019-03-14 22:47:38 +08:00
Jobs . run_immediately!
2018-11-26 12:57:07 +08:00
end
2018-11-22 16:01:03 +08:00
describe 'when user can not mention a group' do
2020-02-19 04:45:02 +08:00
it " should not create the mention with the notify class " do
2019-05-09 11:28:28 +08:00
post = Fabricate ( :post , raw : " hello @ #{ group . name } " )
2018-11-26 12:57:07 +08:00
post . trigger_post_process
post . reload
2018-11-22 16:01:03 +08:00
expect ( post . cooked ) . to eq (
2020-02-19 04:45:02 +08:00
%Q|<p>hello <a class="mention-group" href="/groups/#{group.name}">@#{group.name}</a></p>|
2018-11-22 16:01:03 +08:00
)
end
end
describe 'when user can mention a group' do
before do
group . add ( post . user )
end
2019-07-09 05:12:09 +08:00
it 'should create the mention' do
post . update! ( raw : " hello @ #{ group . name } " )
post . trigger_post_process
post . reload
expect ( post . cooked ) . to eq (
2020-02-19 04:45:02 +08:00
%Q|<p>hello <a class="mention-group notify" href="/groups/#{group.name}">@#{group.name}</a></p>|
2019-07-09 05:12:09 +08:00
)
end
end
describe 'when group owner can mention a group' do
before do
group . update! ( mentionable_level : Group :: ALIAS_LEVELS [ :owners_mods_and_admins ] )
group . add_owner ( post . user )
end
2018-11-22 16:01:03 +08:00
it 'should create the mention' do
post . update! ( raw : " hello @ #{ group . name } " )
2018-11-26 12:57:07 +08:00
post . trigger_post_process
post . reload
2018-11-22 16:01:03 +08:00
expect ( post . cooked ) . to eq (
2020-02-19 04:45:02 +08:00
%Q|<p>hello <a class="mention-group notify" href="/groups/#{group.name}">@#{group.name}</a></p>|
2018-11-22 16:01:03 +08:00
)
end
end
end
2014-01-16 00:34:17 +08:00
end
2014-02-27 12:43:45 +08:00
describe " has_host_spam " do
2018-06-20 00:15:27 +08:00
let ( :raw ) { " hello from my site http://www.example.net http:// #{ GlobalSetting . hostname } http:// #{ RailsMultisite :: ConnectionManagement . current_hostname } " }
2017-08-11 05:18:57 +08:00
2014-02-27 12:43:45 +08:00
it " correctly detects host spam " do
2019-05-09 11:28:28 +08:00
post = Fabricate ( :post , raw : raw )
2014-02-27 12:43:45 +08:00
2018-06-20 00:15:27 +08:00
expect ( post . total_hosts_usage ) . to eq ( " www.example.net " = > 1 )
2014-02-27 12:43:45 +08:00
post . acting_user . trust_level = 0
2015-01-06 00:04:23 +08:00
expect ( post . has_host_spam? ) . to eq ( false )
2014-02-27 12:43:45 +08:00
2014-05-28 10:30:43 +08:00
SiteSetting . newuser_spam_host_threshold = 1
2014-02-27 12:43:45 +08:00
2015-01-06 00:04:23 +08:00
expect ( post . has_host_spam? ) . to eq ( true )
2014-02-27 12:43:45 +08:00
2020-07-27 08:23:54 +08:00
SiteSetting . allowed_spam_host_domains = " bla.com|boo.com | example.net "
2015-01-06 00:04:23 +08:00
expect ( post . has_host_spam? ) . to eq ( false )
2017-08-11 05:18:57 +08:00
end
it " doesn't punish staged users " do
SiteSetting . newuser_spam_host_threshold = 1
user = Fabricate ( :user , staged : true , trust_level : 0 )
2019-05-09 11:28:28 +08:00
post = Fabricate ( :post , raw : raw , user : user )
2017-08-11 05:18:57 +08:00
expect ( post . has_host_spam? ) . to eq ( false )
end
2018-06-20 00:41:10 +08:00
it " punishes previously staged users that were created within 1 day " do
2018-06-16 01:45:53 +08:00
SiteSetting . newuser_spam_host_threshold = 1
2018-06-19 08:05:04 +08:00
SiteSetting . newuser_max_links = 3
2018-06-16 01:45:53 +08:00
user = Fabricate ( :user , staged : true , trust_level : 0 )
2018-06-19 08:05:04 +08:00
user . created_at = 1 . hour . ago
2020-03-17 23:48:24 +08:00
user . unstage!
2019-05-09 11:28:28 +08:00
post = Fabricate ( :post , raw : raw , user : user )
2018-06-19 08:05:04 +08:00
expect ( post . has_host_spam? ) . to eq ( true )
end
2018-06-20 00:41:10 +08:00
it " doesn't punish previously staged users over 1 day old " do
2018-06-19 08:05:04 +08:00
SiteSetting . newuser_spam_host_threshold = 1
SiteSetting . newuser_max_links = 3
user = Fabricate ( :user , staged : true , trust_level : 0 )
2020-03-11 05:13:17 +08:00
user . created_at = 2 . days . ago
2020-03-17 23:48:24 +08:00
user . unstage!
2019-05-09 11:28:28 +08:00
post = Fabricate ( :post , raw : raw , user : user )
2018-06-16 01:45:53 +08:00
expect ( post . has_host_spam? ) . to eq ( false )
end
2017-08-11 05:18:57 +08:00
it " ignores private messages " do
SiteSetting . newuser_spam_host_threshold = 1
user = Fabricate ( :user , trust_level : 0 )
2019-05-09 11:28:28 +08:00
post = Fabricate ( :post , raw : raw , user : user , topic : Fabricate ( :private_message_topic , user : user ) )
2017-08-11 05:18:57 +08:00
expect ( post . has_host_spam? ) . to eq ( false )
2014-02-27 12:43:45 +08:00
end
end
2014-04-25 21:14:05 +08:00
it " has custom fields " do
2019-05-09 11:28:28 +08:00
post = Fabricate ( :post )
2015-01-06 00:04:23 +08:00
expect ( post . custom_fields [ " a " ] ) . to eq ( nil )
2014-04-25 21:14:05 +08:00
post . custom_fields [ " Tommy " ] = " Hanks "
post . custom_fields [ " Vincent " ] = " Vega "
post . save
post = Post . find ( post . id )
2015-01-06 00:04:23 +08:00
expect ( post . custom_fields ) . to eq ( " Tommy " = > " Hanks " , " Vincent " = > " Vega " )
2014-04-25 21:14:05 +08:00
end
2020-05-23 12:56:13 +08:00
describe " # excerpt_for_topic " do
it " returns a topic excerpt, defaulting to 220 chars " do
expected_excerpt = " This is a sample post with semi-long raw content. The raw content is also more than \n two hundred characters to satisfy any test conditions that require content longer \n than the typical test post raw content. It really is… "
post = Fabricate ( :post_with_long_raw_content )
post . rebake!
excerpt = post . excerpt_for_topic
expect ( excerpt ) . to eq ( expected_excerpt )
end
it " respects the site setting for topic excerpt " do
SiteSetting . topic_excerpt_maxlength = 10
expected_excerpt = " This is a … "
post = Fabricate ( :post_with_long_raw_content )
post . rebake!
excerpt = post . excerpt_for_topic
expect ( excerpt ) . to eq ( expected_excerpt )
end
end
2014-05-28 10:30:43 +08:00
describe " # rebake! " do
it " will rebake a post correctly " do
post = create_post
2015-01-06 00:04:23 +08:00
expect ( post . baked_at ) . not_to eq ( nil )
2014-05-28 10:30:43 +08:00
first_baked = post . baked_at
first_cooked = post . cooked
2018-06-19 14:13:14 +08:00
DB . exec ( " UPDATE posts SET cooked = 'frogs' WHERE id = ? " , [ post . id ] )
2014-05-28 10:30:43 +08:00
post . reload
2014-09-23 00:55:13 +08:00
post . expects ( :publish_change_to_clients! ) . with ( :rebaked )
2014-05-28 10:30:43 +08:00
result = post . rebake!
2020-03-26 23:32:41 +08:00
expect ( post . baked_at ) . not_to eq_time ( first_baked )
2015-01-06 00:04:23 +08:00
expect ( post . cooked ) . to eq ( first_cooked )
expect ( result ) . to eq ( true )
2014-05-28 10:30:43 +08:00
end
2020-05-23 12:56:13 +08:00
it " updates the topic excerpt at the same time if it is the OP " do
post = create_post
post . topic . update ( excerpt : " test " )
DB . exec ( " UPDATE posts SET cooked = 'frogs' WHERE id = ? " , [ post . id ] )
post . reload
result = post . rebake!
post . topic . reload
expect ( post . topic . excerpt ) . not_to eq ( " test " )
end
it " does not update the topic excerpt if the post is not the OP " do
post = create_post
post2 = create_post
post . topic . update ( excerpt : " test " )
result = post2 . rebake!
post . topic . reload
expect ( post . topic . excerpt ) . to eq ( " test " )
end
2020-06-01 13:04:16 +08:00
it " works with posts in deleted topics " do
post = create_post
post . topic . trash!
post . reload
post . rebake!
end
2014-05-28 10:30:43 +08:00
end
2016-08-20 03:27:12 +08:00
describe " # set_owner " do
2019-05-09 11:28:28 +08:00
fab! ( :post ) { Fabricate ( :post ) }
2019-05-07 11:12:20 +08:00
fab! ( :coding_horror ) { Fabricate ( :coding_horror ) }
2016-08-20 03:27:12 +08:00
it " will change owner of a post correctly " do
post . set_owner ( coding_horror , Discourse . system_user )
post . reload
expect ( post . user ) . to eq ( coding_horror )
expect ( post . revisions . size ) . to eq ( 1 )
end
it " skips creating new post revision if skip_revision is true " do
post . set_owner ( coding_horror , Discourse . system_user , true )
post . reload
expect ( post . user ) . to eq ( coding_horror )
expect ( post . revisions . size ) . to eq ( 0 )
end
2017-09-14 22:15:07 +08:00
it " uses default locale for edit reason " do
I18n . locale = 'de'
post . set_owner ( coding_horror , Discourse . system_user )
post . reload
expected_reason = I18n . with_locale ( SiteSetting . default_locale ) do
2018-08-20 18:26:19 +08:00
I18n . t ( 'change_owner.post_revision_text' )
2017-09-14 22:15:07 +08:00
end
expect ( post . edit_reason ) . to eq ( expected_reason )
end
2016-08-20 03:27:12 +08:00
end
2014-05-28 10:30:43 +08:00
describe " .rebake_old " do
it " will catch posts it needs to rebake " do
post = create_post
2014-05-30 12:45:39 +08:00
post . update_columns ( baked_at : Time . new ( 2000 , 1 , 1 ) , baked_version : - 1 )
2014-05-28 10:30:43 +08:00
Post . rebake_old ( 100 )
post . reload
2015-01-06 00:04:23 +08:00
expect ( post . baked_at ) . to be > 1 . day . ago
2014-05-30 12:45:39 +08:00
baked = post . baked_at
Post . rebake_old ( 100 )
post . reload
2020-03-11 05:13:17 +08:00
expect ( post . baked_at ) . to eq_time ( baked )
2014-05-28 10:30:43 +08:00
end
2019-01-04 06:24:46 +08:00
it " will rate limit globally " do
post1 = create_post
post2 = create_post
post3 = create_post
Post . where ( id : [ post1 . id , post2 . id , post3 . id ] ) . update_all ( baked_version : - 1 )
global_setting :max_old_rebakes_per_15_minutes , 2
RateLimiter . clear_all_global!
RateLimiter . enable
Post . rebake_old ( 100 )
expect ( post3 . reload . baked_version ) . not_to eq ( - 1 )
expect ( post2 . reload . baked_version ) . not_to eq ( - 1 )
expect ( post1 . reload . baked_version ) . to eq ( - 1 )
end
2014-05-28 10:30:43 +08:00
end
2021-01-12 03:56:08 +08:00
describe " .hide! " do
after do
Discourse . redis . flushdb
end
it " should ignore the duplicate check " do
p1 = Fabricate ( :post )
p2 = Fabricate ( :post , user : p1 . user )
SiteSetting . unique_posts_mins = 10
p1 . store_unique_post_key
p2 . reload . hide! ( PostActionType . types [ :off_topic ] )
expect ( p2 ) . to be_hidden
end
end
2014-08-11 16:48:00 +08:00
describe " .unhide! " do
2017-07-07 14:09:14 +08:00
before { SiteSetting . unique_posts_mins = 5 }
2014-08-11 16:48:00 +08:00
2014-11-12 23:34:30 +08:00
it " will unhide the first post & make the topic visible " do
hidden_topic = Fabricate ( :topic , visible : false )
post = create_post ( topic : hidden_topic )
2014-08-11 16:48:00 +08:00
post . update_columns ( hidden : true , hidden_at : Time . now , hidden_reason_id : 1 )
post . reload
2015-01-06 00:04:23 +08:00
expect ( post . hidden ) . to eq ( true )
2014-08-11 16:48:00 +08:00
2014-09-23 00:55:13 +08:00
post . expects ( :publish_change_to_clients! ) . with ( :acted )
2014-08-11 16:48:00 +08:00
post . unhide!
2014-11-12 23:34:30 +08:00
2014-08-11 16:48:00 +08:00
post . reload
2014-11-12 23:34:30 +08:00
hidden_topic . reload
2014-08-11 16:48:00 +08:00
2015-01-06 00:04:23 +08:00
expect ( post . hidden ) . to eq ( false )
expect ( hidden_topic . visible ) . to eq ( true )
2014-08-11 16:48:00 +08:00
end
end
2014-11-12 23:34:30 +08:00
it " will unhide the post but will keep the topic invisible/unlisted " do
hidden_topic = Fabricate ( :topic , visible : false )
2017-03-30 00:24:46 +08:00
create_post ( topic : hidden_topic )
2014-11-12 23:34:30 +08:00
second_post = create_post ( topic : hidden_topic )
second_post . update_columns ( hidden : true , hidden_at : Time . now , hidden_reason_id : 1 )
second_post . expects ( :publish_change_to_clients! ) . with ( :acted )
second_post . unhide!
second_post . reload
hidden_topic . reload
2015-01-06 00:04:23 +08:00
expect ( second_post . hidden ) . to eq ( false )
expect ( hidden_topic . visible ) . to eq ( false )
2014-11-12 23:34:30 +08:00
end
2018-05-29 07:34:12 +08:00
it " automatically orders post revisions by number ascending " do
2019-05-09 11:28:28 +08:00
post = Fabricate ( :post )
2018-05-29 07:34:12 +08:00
post . revisions . create! ( user_id : 1 , post_id : post . id , number : 2 )
post . revisions . create! ( user_id : 1 , post_id : post . id , number : 1 )
expect ( post . revisions . pluck ( :number ) ) . to eq ( [ 1 , 2 ] )
end
2019-11-18 09:25:42 +08:00
describe 'uploads' do
2019-05-29 09:00:25 +08:00
fab! ( :video_upload ) { Fabricate ( :upload , extension : " mp4 " ) }
fab! ( :image_upload ) { Fabricate ( :upload ) }
fab! ( :audio_upload ) { Fabricate ( :upload , extension : " ogg " ) }
fab! ( :attachment_upload ) { Fabricate ( :upload , extension : " csv " ) }
fab! ( :attachment_upload_2 ) { Fabricate ( :upload ) }
2019-06-19 09:10:50 +08:00
fab! ( :attachment_upload_3 ) { Fabricate ( :upload , extension : nil ) }
2018-09-06 09:58:01 +08:00
2020-10-09 19:51:24 +08:00
let ( :base_url ) { " #{ Discourse . base_url_no_prefix } #{ Discourse . base_path } " }
2019-05-04 03:46:20 +08:00
let ( :video_url ) { " #{ base_url } #{ video_upload . url } " }
let ( :audio_url ) { " #{ base_url } #{ audio_upload . url } " }
2019-11-18 09:25:42 +08:00
let ( :raw_multiple ) do
2018-09-06 09:58:01 +08:00
<< ~ RAW
< a href = " #{ attachment_upload . url } " > Link < / a>
2019-05-29 09:00:25 +08:00
[ test | attachment ] ( #{attachment_upload_2.short_url})
2019-06-19 09:10:50 +08:00
[ test3 | attachment ] ( #{attachment_upload_3.short_url})
2018-09-06 09:58:01 +08:00
< img src = " #{ image_upload . url } " >
< video width = " 100% " height = " 100% " controls >
2019-05-04 03:46:20 +08:00
< source src = " #{ video_url } " >
< a href = " #{ video_url } " > #{video_url}</a>
2018-09-06 09:58:01 +08:00
< / video>
< audio controls >
2019-05-04 03:46:20 +08:00
< source src = " #{ audio_url } " >
< a href = " #{ audio_url } " > #{audio_url}</a>
2018-09-06 09:58:01 +08:00
< / audio>
RAW
end
2019-11-18 09:25:42 +08:00
let ( :post ) { Fabricate ( :post , raw : raw_multiple ) }
2018-09-06 09:58:01 +08:00
2020-06-19 23:45:08 +08:00
it " removes post uploads on destroy " do
post . link_post_uploads
post . trash!
expect ( PostUpload . count ) . to eq ( 6 )
post . destroy!
expect ( PostUpload . count ) . to eq ( 0 )
end
2019-11-18 09:25:42 +08:00
context " # link_post_uploads " do
it " finds all the uploads in the post " do
post . custom_fields [ Post :: DOWNLOADED_IMAGES ] = {
2019-12-18 13:51:57 +08:00
" / #{ upload_path } /original/1X/1/1234567890123456.csv " : attachment_upload . id
2019-11-18 09:25:42 +08:00
}
2018-09-06 09:58:01 +08:00
2019-11-18 09:25:42 +08:00
post . save_custom_fields
post . link_post_uploads
expect ( PostUpload . where ( post : post ) . pluck ( :upload_id ) ) . to contain_exactly (
video_upload . id ,
image_upload . id ,
audio_upload . id ,
attachment_upload . id ,
attachment_upload_2 . id ,
attachment_upload_3 . id
)
end
2018-09-06 09:58:01 +08:00
2019-11-18 09:25:42 +08:00
it " cleans the reverse index up for the current post " do
post . link_post_uploads
post_uploads_ids = post . post_uploads . pluck ( :id )
post . link_post_uploads
expect ( post . reload . post_uploads . pluck ( :id ) ) . to_not contain_exactly (
post_uploads_ids
)
end
2020-01-16 11:50:27 +08:00
context " when secure media is enabled " do
2020-09-14 19:32:25 +08:00
before do
setup_s3
SiteSetting . authorized_extensions = " pdf|png|jpg|csv "
SiteSetting . secure_media = true
end
2020-01-16 11:50:27 +08:00
it " sets the access_control_post_id on uploads in the post that don't already have the value set " do
other_post = Fabricate ( :post )
video_upload . update ( access_control_post_id : other_post . id )
audio_upload . update ( access_control_post_id : other_post . id )
post . link_post_uploads
image_upload . reload
video_upload . reload
expect ( image_upload . access_control_post_id ) . to eq ( post . id )
expect ( video_upload . access_control_post_id ) . not_to eq ( post . id )
end
2020-02-14 09:17:09 +08:00
context " for custom emoji " do
before do
CustomEmoji . create ( name : " meme " , upload : image_upload )
end
it " never sets an access control post because they should not be secure " do
post . link_post_uploads
expect ( image_upload . reload . access_control_post_id ) . to eq ( nil )
end
end
2020-01-16 11:50:27 +08:00
end
2018-09-06 09:58:01 +08:00
end
2019-11-18 09:25:42 +08:00
context '#update_uploads_secure_status' do
fab! ( :user ) { Fabricate ( :user , trust_level : 0 ) }
let ( :raw ) do
<< ~ RAW
< a href = " #{ attachment_upload . url } " > Link < / a>
< img src = " #{ image_upload . url } " >
RAW
end
2018-09-06 09:58:01 +08:00
2019-11-18 09:25:42 +08:00
before do
2020-09-14 19:32:25 +08:00
setup_s3
SiteSetting . authorized_extensions = " pdf|png|jpg|csv "
SiteSetting . secure_media = true
2019-11-18 09:25:42 +08:00
2020-09-14 19:32:25 +08:00
attachment_upload . update! ( original_filename : " hello.csv " )
2019-11-18 09:25:42 +08:00
2020-09-14 19:32:25 +08:00
stub_upload ( attachment_upload )
stub_upload ( image_upload )
2019-11-18 09:25:42 +08:00
end
2018-09-06 09:58:01 +08:00
2020-03-26 05:16:02 +08:00
it " marks image and attachment uploads as secure in PMs when secure_media is ON " do
SiteSetting . secure_media = true
2019-11-18 09:25:42 +08:00
post = Fabricate ( :post , raw : raw , user : user , topic : Fabricate ( :private_message_topic , user : user ) )
post . link_post_uploads
2021-01-29 07:03:44 +08:00
post . update_uploads_secure_status ( source : " test " )
2019-11-18 09:25:42 +08:00
expect ( PostUpload . where ( post : post ) . joins ( :upload ) . pluck ( :upload_id , :secure ) ) . to contain_exactly (
2020-03-26 05:16:02 +08:00
[ attachment_upload . id , true ] ,
2019-11-18 09:25:42 +08:00
[ image_upload . id , true ]
)
end
it " marks image uploads as not secure in PMs when when secure_media is ON " do
SiteSetting . secure_media = false
post = Fabricate ( :post , raw : raw , user : user , topic : Fabricate ( :private_message_topic , user : user ) )
post . link_post_uploads
2021-01-29 07:03:44 +08:00
post . update_uploads_secure_status ( source : " test " )
2019-11-18 09:25:42 +08:00
expect ( PostUpload . where ( post : post ) . joins ( :upload ) . pluck ( :upload_id , :secure ) ) . to contain_exactly (
[ attachment_upload . id , false ] ,
[ image_upload . id , false ]
)
end
it " marks attachments as secure when relevant setting is enabled " do
2020-02-21 08:01:32 +08:00
SiteSetting . secure_media = true
private_category = Fabricate ( :private_category , group : Fabricate ( :group ) )
post = Fabricate ( :post , raw : raw , user : user , topic : Fabricate ( :topic , user : user , category : private_category ) )
2019-11-18 09:25:42 +08:00
post . link_post_uploads
2021-01-29 07:03:44 +08:00
post . update_uploads_secure_status ( source : " test " )
2019-11-18 09:25:42 +08:00
expect ( PostUpload . where ( post : post ) . joins ( :upload ) . pluck ( :upload_id , :secure ) ) . to contain_exactly (
[ attachment_upload . id , true ] ,
2020-02-21 08:01:32 +08:00
[ image_upload . id , true ]
2019-11-18 09:25:42 +08:00
)
end
it " does not mark an upload as secure if it has already been used in a public topic " do
post = Fabricate ( :post , raw : raw , user : user , topic : Fabricate ( :topic , user : user ) )
post . link_post_uploads
2021-01-29 07:03:44 +08:00
post . update_uploads_secure_status ( source : " test " )
2019-11-18 09:25:42 +08:00
pm = Fabricate ( :post , raw : raw , user : user , topic : Fabricate ( :private_message_topic , user : user ) )
pm . link_post_uploads
2021-01-29 07:03:44 +08:00
pm . update_uploads_secure_status ( source : " test " )
2019-11-18 09:25:42 +08:00
expect ( PostUpload . where ( post : pm ) . joins ( :upload ) . pluck ( :upload_id , :secure ) ) . to contain_exactly (
[ attachment_upload . id , false ] ,
[ image_upload . id , false ]
)
end
2018-09-06 09:58:01 +08:00
end
end
2019-03-28 14:28:01 +08:00
context 'topic updated_at' do
let :topic do
create_post . topic
end
def updates_topic_updated_at
2020-03-11 05:13:17 +08:00
time = freeze_time 1 . day . from_now
2019-03-28 14:28:01 +08:00
result = yield
topic . reload
expect ( topic . updated_at ) . to eq_time ( time )
result
end
it " will update topic updated_at for all topic related events " do
SiteSetting . enable_whispers = true
post = updates_topic_updated_at do
create_post ( topic_id : topic . id , post_type : Post . types [ :whisper ] )
end
updates_topic_updated_at do
PostDestroyer . new ( Discourse . system_user , post ) . destroy
end
updates_topic_updated_at do
PostDestroyer . new ( Discourse . system_user , post ) . recover
end
end
end
2019-04-15 04:24:55 +08:00
context " have_uploads " do
it " should find all posts with the upload " do
ids = [ ]
2019-12-18 13:51:57 +08:00
ids << Fabricate ( :post , cooked : " A post with upload <img src='/ #{ upload_path } /1/defghijklmno.png'> " ) . id
ids << Fabricate ( :post , cooked : " A post with optimized image <img src='/ #{ upload_path } /_optimized/601/961/defghijklmno.png'> " ) . id
2019-05-09 11:28:28 +08:00
Fabricate ( :post )
2019-12-18 13:51:57 +08:00
ids << Fabricate ( :post , cooked : " A post with upload <img src='/ #{ upload_path } /original/1X/abc/defghijklmno.png'> " ) . id
2019-05-09 11:28:28 +08:00
ids << Fabricate ( :post , cooked : " A post with upload link <a href='https://cdn.example.com/original/1X/abc/defghijklmno.png'> " ) . id
ids << Fabricate ( :post , cooked : " A post with optimized image <img src='https://cdn.example.com/bucket/optimized/1X/abc/defghijklmno.png'> " ) . id
Fabricate ( :post , cooked : " A post with external link <a href='https://example.com/wp-content/uploads/abcdef.gif'> " )
2019-05-16 19:09:38 +08:00
ids << Fabricate ( :post , cooked : 'A post with missing upload <img src="https://cdn.example.com/images/transparent.png" data-orig-src="upload://defghijklmno.png">' ) . id
2019-09-25 01:47:59 +08:00
ids << Fabricate ( :post , cooked : 'A post with video upload <video width="100%" height="100%" controls=""><source src="https://cdn.example.com/uploads/short-url/XefghijklmU9.mp4"><a href="https://cdn.example.com/uploads/short-url/XefghijklmU9.mp4">https://cdn.example.com/uploads/short-url/XefghijklmU9.mp4</a></video>' ) . id
2019-04-15 04:24:55 +08:00
expect ( Post . have_uploads . order ( :id ) . pluck ( :id ) ) . to eq ( ids )
end
end
2019-06-11 22:25:02 +08:00
describe '#each_upload_url' do
it " correctly identifies all upload urls " do
2019-09-22 18:02:28 +08:00
SiteSetting . authorized_extensions = " * "
2019-06-11 22:25:02 +08:00
upload1 = Fabricate ( :upload )
upload2 = Fabricate ( :upload )
2019-09-22 18:02:28 +08:00
upload3 = Fabricate ( :video_upload )
2019-10-14 15:19:11 +08:00
upload4 = Fabricate ( :upload )
2020-03-10 21:01:40 +08:00
upload5 = Fabricate ( :upload )
upload6 = Fabricate ( :video_upload )
upload7 = Fabricate ( :upload , extension : " vtt " )
2019-07-31 10:00:52 +08:00
set_cdn_url " https://awesome.com/somepath "
post = Fabricate ( :post , raw : << ~ RAW )
2019-09-22 18:02:28 +08:00
A post with image , video and link upload .
2019-07-31 10:00:52 +08:00
! [ ] ( #{upload1.short_url})
2019-10-14 15:19:11 +08:00
" #{ GlobalSetting . cdn_url } #{ upload4 . url } "
2019-07-31 10:00:52 +08:00
< a href = '#{Discourse.base_url}#{upload2.url}' > Link to upload < / a>
! [ ] ( http : / /ex ample . com / external . png )
2019-09-22 18:02:28 +08:00
#{Discourse.base_url}#{upload3.short_path}
2020-03-10 21:01:40 +08:00
< video poster = " #{ Discourse . base_url } #{ upload5 . url } " >
< source src = " #{ Discourse . base_url } #{ upload6 . url } " type = " video/mp4 " / >
< track src = " #{ Discourse . base_url } #{ upload7 . url } " label = " English " kind = " subtitles " srclang = " en " default / >
< / video>
2019-07-31 10:00:52 +08:00
RAW
urls = [ ]
paths = [ ]
post . each_upload_url do | src , path , _ |
urls << src
paths << path
end
expect ( urls ) . to contain_exactly (
" #{ GlobalSetting . cdn_url } #{ upload1 . url } " ,
2019-10-14 15:19:11 +08:00
" #{ GlobalSetting . cdn_url } #{ upload4 . url } " ,
2019-09-22 18:02:28 +08:00
" #{ Discourse . base_url } #{ upload2 . url } " ,
2020-03-10 21:01:40 +08:00
" #{ Discourse . base_url } #{ upload3 . short_path } " ,
" #{ Discourse . base_url } #{ upload5 . url } " ,
" #{ Discourse . base_url } #{ upload6 . url } " ,
" #{ Discourse . base_url } #{ upload7 . url } "
2019-07-31 10:00:52 +08:00
)
expect ( paths ) . to contain_exactly (
upload1 . url ,
2019-10-14 15:19:11 +08:00
upload4 . url ,
2019-09-22 18:02:28 +08:00
upload2 . url ,
2020-03-10 21:01:40 +08:00
nil ,
upload5 . url ,
upload6 . url ,
upload7 . url
2019-07-31 10:00:52 +08:00
)
2019-06-11 22:25:02 +08:00
end
2020-08-11 00:59:29 +08:00
it " correctly identifies secure uploads " do
2020-09-14 19:32:25 +08:00
setup_s3
SiteSetting . authorized_extensions = " pdf|png|jpg|csv "
SiteSetting . secure_media = true
2020-08-11 00:59:29 +08:00
upload1 = Fabricate ( :upload_s3 , secure : true )
upload2 = Fabricate ( :upload_s3 , secure : true )
# Test including domain:
upload1_url = UrlHelper . cook_url ( upload1 . url , secure : true )
# Test without domain:
upload2_path = URI . parse ( UrlHelper . cook_url ( upload2 . url , secure : true ) ) . path
post = Fabricate ( :post , raw : << ~ RAW )
< img src = " #{ upload1_url } " / >
< img src = " #{ upload2_path } " / >
RAW
sha1s = [ ]
post . each_upload_url do | src , path , sha |
sha1s << sha
end
expect ( sha1s ) . to contain_exactly ( upload1 . sha1 , upload2 . sha1 )
end
2019-09-02 17:41:22 +08:00
it " correctly identifies missing uploads with short url " do
upload = Fabricate ( :upload )
url = upload . short_url
sha1 = upload . sha1
upload . destroy!
post = Fabricate ( :post , raw : " ![upload]( #{ url } ) " )
urls = [ ]
paths = [ ]
sha1s = [ ]
2019-09-02 18:49:20 +08:00
post . each_upload_url do | src , path , sha |
2019-09-02 17:41:22 +08:00
urls << src
paths << path
2019-09-02 18:49:20 +08:00
sha1s << sha
2019-09-02 17:41:22 +08:00
end
expect ( urls ) . to contain_exactly ( url )
expect ( paths ) . to contain_exactly ( nil )
expect ( sha1s ) . to contain_exactly ( sha1 )
end
2019-06-11 22:25:02 +08:00
it " should skip external urls with upload url in query string " do
2020-09-14 19:32:25 +08:00
setup_s3
2019-06-11 22:25:02 +08:00
urls = [ ]
upload = Fabricate ( :upload_s3 )
post = Fabricate ( :post , raw : " <a href='https://link.example.com/redirect?url= #{ Discourse . store . cdn_url ( upload . url ) } '>Link to upload</a> " )
post . each_upload_url { | src , _ , _ | urls << src }
expect ( urls ) . to be_empty
end
end
2021-03-04 05:33:58 +08:00
describe " # publish_changes_to_client! " do
fab! ( :user1 ) { Fabricate ( :user ) }
fab! ( :user3 ) { Fabricate ( :user ) }
fab! ( :topic ) { Fabricate ( :private_message_topic , user : user1 ) }
fab! ( :post ) { Fabricate ( :post , topic : topic ) }
fab! ( :group_user ) { Fabricate ( :group_user , user : user3 ) }
fab! ( :topic_allowed_group ) { Fabricate ( :topic_allowed_group , topic : topic , group : group_user . group ) }
let ( :user2 ) { topic . allowed_users . last }
it 'send message to all users participating in private conversation' do
freeze_time
message = {
id : post . id ,
post_number : post . post_number ,
updated_at : Time . now ,
user_id : post . user_id ,
last_editor_id : post . last_editor_id ,
type : :created ,
version : post . version
}
MessageBus . expects ( :publish ) . with ( " /topic/ #{ topic . id } " , message , user_ids : [ user1 . id , user2 . id , user3 . id ] ) . once
post . publish_change_to_clients! ( :created )
end
end
2013-02-06 03:16:51 +08:00
end