require 'rails_helper'
require_dependency 'post_destroyer'
describe Post do
before { Oneboxer.stubs :onebox }
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
# 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?
Fabricate.build(:post, args)
end
it { is_expected.to validate_presence_of :raw }
# Min/max body lengths, respecting padding
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) }
it { is_expected.to rate_limit }
let(:topic) { Fabricate(:topic) }
let(:post_args) do
{ user: topic.user, topic: topic }
end
describe 'scopes' do
describe '#by_newest' do
it 'returns posts ordered by created_at desc' do
2.times do |t|
Fabricate(:post, created_at: t.seconds.from_now)
end
expect(Post.by_newest.first.created_at).to be > Post.by_newest.last.created_at
end
end
describe '#with_user' do
it 'gives you a user' do
Fabricate(:post, user: Fabricate.build(:user))
expect(Post.with_user.first.user).to be_a User
end
end
end
describe "revisions and deleting/recovery" do
context 'a post without links' do
let(:post) { Fabricate(:post, post_args) }
before do
post.trash!
post.reload
end
it "doesn't create a new revision when deleted" do
expect(post.revisions.count).to eq(0)
end
describe "recovery" do
before do
post.recover!
post.reload
end
it "doesn't create a new revision when recovered" do
expect(post.revisions.count).to eq(0)
end
end
end
context 'a post with links' do
let(:post) { Fabricate(:post_with_external_links) }
before do
post.trash!
post.reload
end
describe 'recovery' do
it 'recreates the topic_link records' do
TopicLink.expects(:extract_from).with(post)
post.recover!
end
end
end
end
describe 'flagging helpers' do
let(:post) { Fabricate(:post) }
let(:user) { Fabricate(:coding_horror) }
let(:admin) { Fabricate(:admin) }
it 'isFlagged is accurate' do
PostAction.act(user, post, PostActionType.types[:off_topic])
post.reload
expect(post.is_flagged?).to eq(true)
PostAction.remove_act(user, post, PostActionType.types[:off_topic])
post.reload
expect(post.is_flagged?).to eq(false)
end
it 'has_active_flag is accurate' do
PostAction.act(user, post, PostActionType.types[:spam])
post.reload
expect(post.has_active_flag?).to eq(true)
PostAction.defer_flags!(post, admin)
post.reload
expect(post.has_active_flag?).to eq(false)
end
end
describe "maximum images" do
let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) }
let(:post_no_images) { Fabricate.build(:post, post_args.merge(user: newuser)) }
let(:post_one_image) { post_with_body("![sherlock](http://bbc.co.uk/sherlock.jpg)", newuser) }
let(:post_two_images) { post_with_body("
", newuser) }
let(:post_with_avatars) { post_with_body('
', newuser) }
let(:post_with_favicon) { post_with_body('
', newuser) }
let(:post_with_thumbnail) { post_with_body('
', newuser) }
let(:post_with_two_classy_images) { post_with_body("
", newuser) }
it "returns 0 images for an empty post" do
expect(Fabricate.build(:post).image_count).to eq(0)
end
it "finds images from markdown" do
expect(post_one_image.image_count).to eq(1)
end
it "finds images from HTML" do
expect(post_two_images.image_count).to eq(2)
end
it "doesn't count avatars as images" do
expect(post_with_avatars.image_count).to eq(0)
end
it "doesn't count favicons as images" do
PrettyText.stubs(:cook).returns(post_with_favicon.raw)
expect(post_with_favicon.image_count).to eq(0)
end
it "doesn't count thumbnails as images" do
PrettyText.stubs(:cook).returns(post_with_thumbnail.raw)
expect(post_with_thumbnail.image_count).to eq(0)
end
it "doesn't count whitelisted images" do
Post.stubs(:white_listed_image_classes).returns(["classy"])
# I dislike this, but passing in a custom whitelist is hard
PrettyText.stubs(:cook).returns(post_with_two_classy_images.raw)
expect(post_with_two_classy_images.image_count).to eq(0)
end
context "validation" do
before do
SiteSetting.newuser_max_images = 1
end
context 'newuser' do
it "allows a new user to post below the limit" do
expect(post_one_image).to be_valid
end
it "doesn't allow more than the maximum" do
expect(post_two_images).not_to be_valid
end
it "doesn't allow a new user to edit their post to insert an image" do
post_no_images.user.trust_level = TrustLevel[0]
post_no_images.save
expect {
post_no_images.revise(post_no_images.user, raw: post_two_images.raw)
post_no_images.reload
}.not_to change(post_no_images, :raw)
end
end
it "allows more images from a not-new account" do
post_two_images.user.trust_level = TrustLevel[1]
expect(post_two_images).to be_valid
end
end
end
describe "maximum attachments" do
let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) }
let(:post_no_attachments) { Fabricate.build(:post, post_args.merge(user: newuser)) }
let(:post_one_attachment) { post_with_body('file.txt', newuser) }
let(:post_two_attachments) { post_with_body('errors.log model.3ds', newuser) }
it "returns 0 attachments for an empty post" do
expect(Fabricate.build(:post).attachment_count).to eq(0)
end
it "finds attachments from HTML" do
expect(post_two_attachments.attachment_count).to eq(2)
end
context "validation" do
before do
SiteSetting.newuser_max_attachments = 1
end
context 'newuser' do
it "allows a new user to post below the limit" do
expect(post_one_attachment).to be_valid
end
it "doesn't allow more than the maximum" do
expect(post_two_attachments).not_to be_valid
end
it "doesn't allow a new user to edit their post to insert an attachment" do
post_no_attachments.user.trust_level = TrustLevel[0]
post_no_attachments.save
expect {
post_no_attachments.revise(post_no_attachments.user, raw: post_two_attachments.raw)
post_no_attachments.reload
}.not_to change(post_no_attachments, :raw)
end
end
it "allows more attachments from a not-new account" do
post_two_attachments.user.trust_level = TrustLevel[1]
expect(post_two_attachments).to be_valid
end
end
end
context "links" do
let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) }
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("disney reddit", 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
expect(no_links.raw_links).to be_blank
end
it "finds a link within markdown" do
expect(one_link.raw_links).to eq(["http://www.imdb.com/name/nm2225369"])
end
it "can find two links from html" do
expect(two_links.raw_links).to eq(["http://disneyland.disney.go.com/", "http://reddit.com"])
end
it "can find three links without markup" do
expect(three_links.raw_links).to eq(["http://discourse.org", "http://discourse.org/another_url", "http://www.imdb.com/name/nm2225369"])
end
end
describe "linked_hosts" do
it "returns blank with no links" do
expect(no_links.linked_hosts).to be_blank
end
it "returns the host and a count for links" do
expect(two_links.linked_hosts).to eq("disneyland.disney.go.com" => 1, "reddit.com" => 1)
end
it "it counts properly with more than one link on the same host" do
expect(three_links.linked_hosts).to eq("discourse.org" => 1, "www.imdb.com" => 1)
end
end
describe "total host usage" do
it "has none for a regular post" do
expect(no_links.total_hosts_usage).to be_blank
end
context "with a previous host" do
let(:user) { old_post.newuser }
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
expect(two_links.total_hosts_usage).to eq('disneyland.disney.go.com' => 2, 'reddit.com' => 1)
end
end
end
end
describe "maximum links" do
let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) }
let(:post_one_link) { post_with_body("[sherlock](http://www.bbc.co.uk/programmes/b018ttws)", newuser) }
let(:post_two_links) { post_with_body("discourse twitter", newuser) }
let(:post_with_mentions) { post_with_body("hello @#{newuser.username} how are you doing?", newuser) }
it "returns 0 links for an empty post" do
expect(Fabricate.build(:post).link_count).to eq(0)
end
it "returns 0 links for a post with mentions" do
expect(post_with_mentions.link_count).to eq(0)
end
it "finds links from markdown" do
expect(post_one_link.link_count).to eq(1)
end
it "finds links from HTML" do
expect(post_two_links.link_count).to eq(2)
end
context "validation" do
before do
SiteSetting.newuser_max_links = 1
end
context 'newuser' do
it "returns true when within the amount of links allowed" do
expect(post_one_link).to be_valid
end
it "doesn't allow more links than allowed" do
expect(post_two_links).not_to be_valid
end
end
it "allows multiple images for basic accounts" do
post_two_links.user.trust_level = TrustLevel[1]
expect(post_two_links).to be_valid
end
end
end
describe "@mentions" do
context 'raw_mentions' do
it "returns an empty array with no matches" do
post = Fabricate.build(:post, post_args.merge(raw: "Hello Jake and Finn!"))
expect(post.raw_mentions).to eq([])
end
it "returns lowercase unique versions of the mentions" do
post = Fabricate.build(:post, post_args.merge(raw: "@Jake @Finn @Jake"))
expect(post.raw_mentions).to eq(['jake', 'finn'])
end
it "ignores pre" do
# we need to force an inline
post = Fabricate.build(:post, post_args.merge(raw: "p
@Jake@Finn")) expect(post.raw_mentions).to eq(['finn']) end it "catches content between pre tags" do # per common mark we need to force an inline post = Fabricate.build(:post, post_args.merge(raw: "a
hello@Finn ")) expect(post.raw_mentions).to eq(['finn']) end it "ignores code" do post = Fabricate.build(:post, post_args.merge(raw: "@Jake `@Finn`")) expect(post.raw_mentions).to eq(['jake']) end it "ignores quotes" do post = Fabricate.build(:post, post_args.merge(raw: "[quote=\"Evil Trout\"]\n@Jake\n[/quote]\n@Finn")) expect(post.raw_mentions).to eq(['finn']) end it "handles underscore in username" do post = Fabricate.build(:post, post_args.merge(raw: "@Jake @Finn @Jake_Old")) expect(post.raw_mentions).to eq(['jake', 'finn', 'jake_old']) end it "handles hyphen in groupname" do post = Fabricate.build(:post, post_args.merge(raw: "@org-board")) expect(post.raw_mentions).to eq(['org-board']) end end context "max mentions" do let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) } 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) } context 'new user' do before do SiteSetting.newuser_max_mentions_per_post = 1 SiteSetting.max_mentions_per_post = 5 end it "allows a new user to have newuser_max_mentions_per_post mentions" do expect(post_with_one_mention).to be_valid end it "doesn't allow a new user to have more than newuser_max_mentions_per_post mentions" do expect(post_with_two_mentions).not_to be_valid end end context "not a new user" do before do SiteSetting.newuser_max_mentions_per_post = 0 SiteSetting.max_mentions_per_post = 1 end it "allows vmax_mentions_per_post mentions" do post_with_one_mention.user.trust_level = TrustLevel[1] expect(post_with_one_mention).to be_valid end it "doesn't allow to have more than max_mentions_per_post mentions" do post_with_two_mentions.user.trust_level = TrustLevel[1] expect(post_with_two_mentions).not_to be_valid end end end end context 'validation' do it 'validates our default post' do expect(Fabricate.build(:post, post_args)).to be_valid end it 'create blank posts as invalid' do expect(Fabricate.build(:post, raw: "")).not_to be_valid end end context "raw_hash" do let(:raw) { "this is our test post body" } let(:post) { post_with_body(raw) } it "returns a value" do expect(post.raw_hash).to be_present end it "returns blank for a nil body" do post.raw = nil expect(post.raw_hash).to be_blank end it "returns the same value for the same raw" do expect(post.raw_hash).to eq(post_with_body(raw).raw_hash) end it "returns a different value for a different raw" do expect(post.raw_hash).not_to eq(post_with_body("something else").raw_hash) end it "returns a different value with different text case" do expect(post.raw_hash).not_to eq(post_with_body("THIS is OUR TEST post BODy").raw_hash) end end context 'revise' do let(:post) { Fabricate(:post, post_args) } let(:first_version_at) { post.last_version_at } it 'has no revision' do 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) end describe 'with the same body' do it "doesn't change version" do expect { post.revise(post.user, raw: post.raw); post.reload }.not_to change(post, :version) end end describe 'ninja editing & edit windows' do before { SiteSetting.editing_grace_period = 1.minute.to_i } it 'works' do revised_at = post.updated_at + 2.minutes new_revised_at = revised_at + 2.minutes # ninja edit post.revise(post.user, { raw: 'updated body' }, revised_at: post.updated_at + 10.seconds) post.reload 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) # revision much later post.revise(post.user, { raw: 'another updated body' }, revised_at: revised_at) post.reload 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) # new edit window post.revise(post.user, { raw: 'yet another updated body' }, revised_at: revised_at + 10.seconds) post.reload 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) # after second window post.revise(post.user, { raw: 'yet another, another updated body' }, revised_at: new_revised_at) post.reload 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) end end describe 'rate limiter' do let(:changed_by) { Fabricate(:coding_horror) } it "triggers a rate limiter" do EditRateLimiter.any_instance.expects(:performed!) post.revise(changed_by, raw: 'updated body') end end describe 'with a new body' do let(:changed_by) { Fabricate(:coding_horror) } let!(:result) { post.revise(changed_by, raw: 'updated body') } it 'acts correctly' do 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 end context 'second poster posts again quickly' do it 'is a ninja edit, because the second poster posted again quickly' do SiteSetting.expects(:editing_grace_period).returns(1.minute.to_i) post.revise(changed_by, { raw: 'yet another updated body' }, revised_at: post.updated_at + 10.seconds) post.reload expect(post.version).to eq(2) expect(post.public_version).to eq(2) expect(post.revisions.size).to eq(1) end end end end describe 'before save' do let(:cooked) { "" } let(:post) do Fabricate(:post, raw: "