Remove RSpec given.

This commit is contained in:
Guo Xiang Tan
2016-12-13 09:59:38 +08:00
parent 53086fdb98
commit 18f400e652
9 changed files with 315 additions and 197 deletions

View File

@ -138,8 +138,6 @@ group :test, :development do
gem 'rb-inotify', '~> 0.9', require: RUBY_PLATFORM =~ /linux/i ? 'rb-inotify' : false gem 'rb-inotify', '~> 0.9', require: RUBY_PLATFORM =~ /linux/i ? 'rb-inotify' : false
gem 'rspec-rails', require: false gem 'rspec-rails', require: false
gem 'shoulda', require: false gem 'shoulda', require: false
gem 'simplecov', require: false
gem 'rspec-given'
gem 'rspec-html-matchers' gem 'rspec-html-matchers'
gem 'spork-rails' gem 'spork-rails'
gem 'pry-nav' gem 'pry-nav'

View File

@ -77,7 +77,6 @@ GEM
discourse-qunit-rails (0.0.9) discourse-qunit-rails (0.0.9)
railties railties
discourse_fastimage (2.0.3) discourse_fastimage (2.0.3)
docile (1.1.5)
domain_name (0.5.25) domain_name (0.5.25)
unf (>= 0.0.5, < 1.0.0) unf (>= 0.0.5, < 1.0.0)
email_reply_trimmer (0.1.6) email_reply_trimmer (0.1.6)
@ -115,8 +114,6 @@ GEM
thor (~> 0.19.1) thor (~> 0.19.1)
fspath (2.1.1) fspath (2.1.1)
gctools (0.2.3) gctools (0.2.3)
given_core (3.7.1)
sorcerer (>= 0.3.7)
globalid (0.3.7) globalid (0.3.7)
activesupport (>= 4.1.0) activesupport (>= 4.1.0)
guess_html_encoding (0.0.11) guess_html_encoding (0.0.11)
@ -300,9 +297,6 @@ GEM
rspec-expectations (3.4.0) rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0) rspec-support (~> 3.4.0)
rspec-given (3.7.1)
given_core (= 3.7.1)
rspec (>= 2.14.0)
rspec-html-matchers (0.7.0) rspec-html-matchers (0.7.0)
nokogiri (~> 1) nokogiri (~> 1)
rspec (~> 3) rspec (~> 3)
@ -340,7 +334,7 @@ GEM
shoulda (3.5.0) shoulda (3.5.0)
shoulda-context (~> 1.0, >= 1.0.1) shoulda-context (~> 1.0, >= 1.0.1)
shoulda-matchers (>= 1.4.1, < 3.0) shoulda-matchers (>= 1.4.1, < 3.0)
shoulda-context (1.2.1) shoulda-context (1.2.2)
shoulda-matchers (2.8.0) shoulda-matchers (2.8.0)
activesupport (>= 3.0.0) activesupport (>= 3.0.0)
sidekiq (4.2.4) sidekiq (4.2.4)
@ -351,17 +345,11 @@ GEM
sidekiq-statistic (1.2.0) sidekiq-statistic (1.2.0)
sidekiq (>= 3.3.4, < 5) sidekiq (>= 3.3.4, < 5)
simple-rss (1.3.1) simple-rss (1.3.1)
simplecov (0.11.1)
docile (~> 1.1.0)
json (~> 1.8)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
sinatra (1.4.6) sinatra (1.4.6)
rack (~> 1.4) rack (~> 1.4)
rack-protection (~> 1.4) rack-protection (~> 1.4)
tilt (>= 1.3, < 3) tilt (>= 1.3, < 3)
slop (3.6.0) slop (3.6.0)
sorcerer (1.0.2)
spork (1.0.0rc4) spork (1.0.0rc4)
spork-rails (4.0.0) spork-rails (4.0.0)
rails (>= 3.0.0, < 5) rails (>= 3.0.0, < 5)
@ -475,7 +463,6 @@ DEPENDENCIES
rinku rinku
rmmseg-cpp rmmseg-cpp
rspec rspec
rspec-given
rspec-html-matchers rspec-html-matchers
rspec-rails rspec-rails
rtlit rtlit
@ -488,7 +475,6 @@ DEPENDENCIES
sidekiq sidekiq
sidekiq-statistic sidekiq-statistic
simple-rss simple-rss
simplecov
sinatra sinatra
spork-rails spork-rails
stackprof stackprof

View File

@ -80,19 +80,19 @@ describe Email::Sender do
context "doesn't add return_path when no plus addressing" do context "doesn't add return_path when no plus addressing" do
before { SiteSetting.reply_by_email_address = '%{reply_key}@test.com' } before { SiteSetting.reply_by_email_address = '%{reply_key}@test.com' }
When { email_sender.send } it 'should not set the return_path' do
Then { email_sender.send
expect(message.header[:return_path].to_s).to eq("") expect(message.header[:return_path].to_s).to eq("")
} end
end end
context "adds return_path with plus addressing" do context "adds return_path with plus addressing" do
before { SiteSetting.reply_by_email_address = 'replies+%{reply_key}@test.com' } before { SiteSetting.reply_by_email_address = 'replies+%{reply_key}@test.com' }
When { email_sender.send } it 'should set the return_path' do
Then { email_sender.send
expect(message.header[:return_path].to_s).to eq("replies+verp-#{EmailLog.last.bounce_key}@test.com") expect(message.header[:return_path].to_s).to eq("replies+verp-#{EmailLog.last.bounce_key}@test.com")
} end
end end
context "adds a List-ID header to identify the forum" do context "adds a List-ID header to identify the forum" do
@ -102,14 +102,21 @@ describe Email::Sender do
message.header['X-Discourse-Topic-Id'] = topic.id message.header['X-Discourse-Topic-Id'] = topic.id
end end
When { email_sender.send } it 'should add the right header' do
Then { expect(message.header['List-ID']).to be_present } email_sender.send
Then { expect(message.header['List-ID'].to_s).to match('name-with-space') }
expect(message.header['List-ID']).to be_present
expect(message.header['List-ID'].to_s).to match('name-with-space')
end
end end
context "adds a Message-ID header even when topic id is not present" do context "adds a Message-ID header even when topic id is not present" do
When { email_sender.send }
Then { expect(message.header['Message-ID']).to be_present } it 'should add the right header' do
email_sender.send
expect(message.header['Message-ID']).to be_present
end
end end
context "adds Precedence header" do context "adds Precedence header" do
@ -117,8 +124,10 @@ describe Email::Sender do
message.header['X-Discourse-Topic-Id'] = 5577 message.header['X-Discourse-Topic-Id'] = 5577
end end
When { email_sender.send } it 'should add the right header' do
Then { expect(message.header['Precedence']).to be_present } email_sender.send
expect(message.header['Precedence']).to be_present
end
end end
context "removes custom Discourse headers from topic notification mails" do context "removes custom Discourse headers from topic notification mails" do
@ -126,17 +135,21 @@ describe Email::Sender do
message.header['X-Discourse-Topic-Id'] = 5577 message.header['X-Discourse-Topic-Id'] = 5577
end end
When { email_sender.send } it 'should remove the right headers' do
Then { expect(message.header['X-Discourse-Topic-Id']).not_to be_present } email_sender.send
Then { expect(message.header['X-Discourse-Post-Id']).not_to be_present } expect(message.header['X-Discourse-Topic-Id']).not_to be_present
Then { expect(message.header['X-Discourse-Reply-Key']).not_to be_present } expect(message.header['X-Discourse-Post-Id']).not_to be_present
expect(message.header['X-Discourse-Reply-Key']).not_to be_present
end
end end
context "removes custom Discourse headers from digest/registration/other mails" do context "removes custom Discourse headers from digest/registration/other mails" do
When { email_sender.send } it 'should remove the right headers' do
Then { expect(message.header['X-Discourse-Topic-Id']).not_to be_present } email_sender.send
Then { expect(message.header['X-Discourse-Post-Id']).not_to be_present } expect(message.header['X-Discourse-Topic-Id']).not_to be_present
Then { expect(message.header['X-Discourse-Reply-Key']).not_to be_present } expect(message.header['X-Discourse-Post-Id']).not_to be_present
expect(message.header['X-Discourse-Reply-Key']).not_to be_present
end
end end
context "email threading" do context "email threading" do
@ -214,8 +227,10 @@ describe Email::Sender do
message.header['X-MC-Metadata'] = { foo: "bar" }.to_json message.header['X-MC-Metadata'] = { foo: "bar" }.to_json
end end
When { email_sender.send } it 'should set the right header' do
Then { expect(message.header['X-MC-Metadata'].to_s).to match(message.message_id) } email_sender.send
expect(message.header['X-MC-Metadata'].to_s).to match(message.message_id)
end
end end
context "merges custom sparkpost header" do context "merges custom sparkpost header" do
@ -224,19 +239,24 @@ describe Email::Sender do
message.header['X-MSYS-API'] = { foo: "bar" }.to_json message.header['X-MSYS-API'] = { foo: "bar" }.to_json
end end
When { email_sender.send } it 'should set the right header' do
Then { expect(message.header['X-MSYS-API'].to_s).to match(message.message_id) } email_sender.send
expect(message.header['X-MSYS-API'].to_s).to match(message.message_id)
end
end end
context 'email logs' do context 'email logs' do
let(:email_log) { EmailLog.last } let(:email_log) { EmailLog.last }
When { email_sender.send } it 'should create the right log' do
Then { expect(email_log).to be_present } email_sender.send
Then { expect(email_log.email_type).to eq('valid_type') }
Then { expect(email_log.to_address).to eq('eviltrout@test.domain') } expect(email_log).to be_present
Then { expect(email_log.reply_key).to be_blank } expect(email_log.email_type).to eq('valid_type')
Then { expect(email_log.user_id).to be_blank } expect(email_log.to_address).to eq('eviltrout@test.domain')
expect(email_log.reply_key).to be_blank
expect(email_log.user_id).to be_blank
end
end end
context "email log with a post id and topic id" do context "email log with a post id and topic id" do
@ -246,9 +266,12 @@ describe Email::Sender do
end end
let(:email_log) { EmailLog.last } let(:email_log) { EmailLog.last }
When { email_sender.send }
Then { expect(email_log.post_id).to eq(3344) } it 'should create the right log' do
Then { expect(email_log.topic_id).to eq(5577) } email_sender.send
expect(email_log.post_id).to eq(3344)
expect(email_log.topic_id).to eq(5577)
end
end end
context "email log with a reply key" do context "email log with a reply key" do
@ -257,17 +280,23 @@ describe Email::Sender do
end end
let(:email_log) { EmailLog.last } let(:email_log) { EmailLog.last }
When { email_sender.send }
Then { expect(email_log.reply_key).to eq(reply_key) } it 'should create the right log' do
email_sender.send
expect(email_log.reply_key).to eq(reply_key)
end
end end
context 'email parts' do context 'email parts' do
When { email_sender.send } it 'should contain the right message' do
Then { expect(message).to be_multipart } email_sender.send
Then { expect(message.text_part.content_type).to eq('text/plain; charset=UTF-8') }
Then { expect(message.html_part.content_type).to eq('text/html; charset=UTF-8') } expect(message).to be_multipart
Then { expect(message.html_part.body.to_s).to match("<p><strong>hello</strong></p>") } expect(message.text_part.content_type).to eq('text/plain; charset=UTF-8')
expect(message.html_part.content_type).to eq('text/html; charset=UTF-8')
expect(message.html_part.body.to_s).to match("<p><strong>hello</strong></p>")
end
end end
end end

View File

@ -4,53 +4,66 @@ require 'rails_helper'
describe SpamRulesEnforcer do describe SpamRulesEnforcer do
Given(:ip_address) { '182.189.119.174' } let(:ip_address) { '182.189.119.174' }
Given!(:spammer1) { Fabricate(:user, ip_address: ip_address) } let!(:spammer1) { Fabricate(:user, ip_address: ip_address) }
Given!(:spammer2) { Fabricate(:user, ip_address: ip_address) } let!(:spammer2) { Fabricate(:user, ip_address: ip_address) }
Given(:spammer3) { Fabricate(:user, ip_address: ip_address) } let(:spammer3) { Fabricate(:user, ip_address: ip_address) }
context 'flag_sockpuppets is disabled' do context 'flag_sockpuppets is disabled' do
Given { SiteSetting.stubs(:flag_sockpuppets).returns(false) } let!(:first_post) { create_post(user: spammer1) }
Given!(:first_post) { create_post(user: spammer1) } let!(:second_post) { create_post(user: spammer2, topic: first_post.topic) }
Given!(:second_post) { create_post(user: spammer2, topic: first_post.topic) }
Then { expect(first_post.reload.spam_count).to eq(0) } it 'should not increase spam count' do
And { expect(second_post.reload.spam_count).to eq(0) } expect(first_post.reload.spam_count).to eq(0)
expect(second_post.reload.spam_count).to eq(0)
end
end end
context 'flag_sockpuppets is enabled' do context 'flag_sockpuppets is enabled' do
Given { SiteSetting.stubs(:flag_sockpuppets).returns(true) } before do
SiteSetting.flag_sockpuppets = true
end
after do
SiteSetting.flag_sockpuppets = false
end
context 'first spammer starts a topic' do context 'first spammer starts a topic' do
Given!(:first_post) { create_post(user: spammer1) } let!(:first_post) { create_post(user: spammer1) }
context 'second spammer replies' do context 'second spammer replies' do
Given!(:second_post) { create_post(user: spammer2, topic: first_post.topic) } let!(:second_post) { create_post(user: spammer2, topic: first_post.topic) }
Then { expect(first_post.reload.spam_count).to eq(1) } it 'should increase spam count' do
And { expect(second_post.reload.spam_count).to eq(1) } expect(first_post.reload.spam_count).to eq(1)
expect(second_post.reload.spam_count).to eq(1)
end
context 'third spam post' do context 'third spam post' do
Given!(:third_post) { create_post(user: spammer3, topic: first_post.topic) } let!(:third_post) { create_post(user: spammer3, topic: first_post.topic) }
Then { expect(first_post.reload.spam_count).to eq(1) } it 'should increase spam count' do
And { expect(second_post.reload.spam_count).to eq(1) } expect(first_post.reload.spam_count).to eq(1)
And { expect(third_post.reload.spam_count).to eq(1) } expect(second_post.reload.spam_count).to eq(1)
expect(third_post.reload.spam_count).to eq(1)
end
end end
end end
end end
context 'first user is not new' do context 'first user is not new' do
Given!(:old_user) { Fabricate(:user, ip_address: ip_address, created_at: 2.days.ago, trust_level: TrustLevel[1]) } let!(:old_user) { Fabricate(:user, ip_address: ip_address, created_at: 2.days.ago, trust_level: TrustLevel[1]) }
context 'first user starts a topic' do context 'first user starts a topic' do
Given!(:first_post) { create_post(user: old_user) } let!(:first_post) { create_post(user: old_user) }
context 'a reply by a new user at the same IP address' do context 'a reply by a new user at the same IP address' do
Given!(:second_post) { create_post(user: spammer2, topic: first_post.topic) } let!(:second_post) { create_post(user: spammer2, topic: first_post.topic) }
Then { expect(first_post.reload.spam_count).to eq(0) } it 'should increase the spam count correctly' do
And { expect(second_post.reload.spam_count).to eq(1) } expect(first_post.reload.spam_count).to eq(0)
expect(second_post.reload.spam_count).to eq(1)
end
end end
end end
end end

View File

@ -5,102 +5,129 @@ require 'rails_helper'
describe SpamRulesEnforcer do describe SpamRulesEnforcer do
describe 'auto-blocking users based on flagging' do describe 'auto-blocking users based on flagging' do
before do site_setting(:flags_required_to_hide_post, 0)
SiteSetting.stubs(:flags_required_to_hide_post).returns(0) # never site_setting(:num_spam_flags_to_block_new_user, 2)
SiteSetting.stubs(:num_spam_flags_to_block_new_user).returns(2) site_setting(:num_users_to_block_new_user, 2)
SiteSetting.stubs(:num_users_to_block_new_user).returns(2)
end
Given!(:admin) { Fabricate(:admin) } # needed to send a system message let!(:admin) { Fabricate(:admin) } # needed to send a system message
Given!(:moderator) { Fabricate(:moderator) } let!(:moderator) { Fabricate(:moderator) }
Given(:user1) { Fabricate(:user) } let(:user1) { Fabricate(:user) }
Given(:user2) { Fabricate(:user) } let(:user2) { Fabricate(:user) }
context 'spammer is a new user' do context 'spammer is a new user' do
Given(:spammer) { Fabricate(:user, trust_level: TrustLevel[0]) } let(:spammer) { Fabricate(:user, trust_level: TrustLevel[0]) }
context 'spammer post is not flagged enough times' do context 'spammer post is not flagged enough times' do
Given!(:spam_post) { create_post(user: spammer) } let!(:spam_post) { create_post(user: spammer) }
Given!(:spam_post2) { create_post(user: spammer) } let!(:spam_post2) { create_post(user: spammer) }
When { PostAction.act(user1, spam_post, PostActionType.types[:spam]) }
Then { expect(spam_post.reload).to_not be_hidden } before do
PostAction.act(user1, spam_post, PostActionType.types[:spam])
end
it 'should not hide the post' do
expect(spam_post.reload).to_not be_hidden
end
context 'spam posts are flagged enough times, but not by enough users' do context 'spam posts are flagged enough times, but not by enough users' do
When { PostAction.act(user1, spam_post2, PostActionType.types[:spam]) } it 'should not hide the post' do
Then { expect(spam_post.reload).to_not be_hidden } PostAction.act(user1, spam_post2, PostActionType.types[:spam])
And { expect(spam_post2.reload).to_not be_hidden }
And { expect(spammer.reload).to_not be_blocked } expect(spam_post.reload).to_not be_hidden
expect(spam_post2.reload).to_not be_hidden
expect(spammer.reload).to_not be_blocked
end
end end
context 'one spam post is flagged enough times by enough users' do context 'one spam post is flagged enough times by enough users' do
Given!(:another_topic) { Fabricate(:topic) } let!(:another_topic) { Fabricate(:topic) }
Given!(:private_messages_count) { spammer.private_topics_count } let!(:private_messages_count) { spammer.private_topics_count }
Given!(:mod_pm_count) { moderator.private_topics_count } let!(:mod_pm_count) { moderator.private_topics_count }
When { PostAction.act(user2, spam_post, PostActionType.types[:spam]) } before do
PostAction.act(user2, spam_post, PostActionType.types[:spam])
Invariant { expect(Guardian.new(spammer).can_create_topic?(nil)).to be false } expect(Guardian.new(spammer).can_create_topic?(nil)).to be(false)
Invariant { expect{PostCreator.create(spammer, {title: 'limited time offer for you', raw: 'better buy this stuff ok', archetype_id: 1})}.to raise_error(Discourse::InvalidAccess) } expect{PostCreator.create(spammer, {title: 'limited time offer for you', raw: 'better buy this stuff ok', archetype_id: 1})}.to raise_error(Discourse::InvalidAccess)
Invariant { expect(PostCreator.create(spammer, {topic_id: another_topic.id, raw: 'my reply is spam in your topic', archetype_id: 1})).to eq(nil) } expect(PostCreator.create(spammer, {topic_id: another_topic.id, raw: 'my reply is spam in your topic', archetype_id: 1})).to eq(nil)
end
Then { expect(spammer.reload).to be_blocked }
And { expect(spam_post.reload).to be_hidden }
And { expect(spam_post2.reload).to be_hidden }
And { expect(spammer.reload.private_topics_count).to eq(private_messages_count + 1) }
it 'should hide the posts' do
expect(spammer.reload).to be_blocked
expect(spam_post.reload).to be_hidden
expect(spam_post2.reload).to be_hidden
expect(spammer.reload.private_topics_count).to eq(private_messages_count + 1)
end
# The following cases describe when a staff user takes some action, but the user # The following cases describe when a staff user takes some action, but the user
# still won't be able to make posts. # still won't be able to make posts.
# A staff user needs to clear the blocked flag from the user record. # A staff user needs to clear the blocked flag from the user record.
context "a post's flags are cleared" do context "a post's flags are cleared" do
When { PostAction.clear_flags!(spam_post, admin); spammer.reload } it 'should block the spammer' do
Then { expect(spammer.reload).to be_blocked } PostAction.clear_flags!(spam_post, admin); spammer.reload
expect(spammer.reload).to be_blocked
end
end end
context "a post is deleted" do context "a post is deleted" do
When { spam_post.trash!(moderator); spammer.reload } it 'should block the spammer' do
Then { expect(spammer.reload).to be_blocked } spam_post.trash!(moderator); spammer.reload
expect(spammer.reload).to be_blocked
end
end end
context "spammer becomes trust level 1" do context "spammer becomes trust level 1" do
When { spammer.change_trust_level!(TrustLevel[1]); spammer.reload } it 'should block the spammer' do
Then { expect(spammer.reload).to be_blocked } spammer.change_trust_level!(TrustLevel[1]); spammer.reload
expect(spammer.reload).to be_blocked
end
end end
end end
context 'flags_required_to_hide_post takes effect too' do context 'flags_required_to_hide_post takes effect too' do
Given { SiteSetting.stubs(:flags_required_to_hide_post).returns(2) } site_setting(:flags_required_to_hide_post, 2)
When { PostAction.act(user2, spam_post, PostActionType.types[:spam]) }
Then { expect(spammer.reload).to be_blocked } it 'should block the spammer' do
And { expect(Guardian.new(spammer).can_create_topic?(nil)).to be false } PostAction.act(user2, spam_post, PostActionType.types[:spam])
expect(spammer.reload).to be_blocked
expect(Guardian.new(spammer).can_create_topic?(nil)).to be false
end
end end
end end
end end
context "spammer has trust level basic" do context "spammer has trust level basic" do
Given(:spammer) { Fabricate(:user, trust_level: TrustLevel[1]) } let(:spammer) { Fabricate(:user, trust_level: TrustLevel[1]) }
context 'one spam post is flagged enough times by enough users' do context 'one spam post is flagged enough times by enough users' do
Given!(:spam_post) { Fabricate(:post, user: spammer) } let!(:spam_post) { Fabricate(:post, user: spammer) }
Given!(:private_messages_count) { spammer.private_topics_count } let!(:private_messages_count) { spammer.private_topics_count }
When { PostAction.act(user1, spam_post, PostActionType.types[:spam]) }
When { PostAction.act(user2, spam_post, PostActionType.types[:spam]) } it 'should not allow spammer to create new posts' do
Then { expect(spam_post.reload).to_not be_hidden } PostAction.act(user1, spam_post, PostActionType.types[:spam])
And { expect(Guardian.new(spammer).can_create_topic?(nil)).to be true } PostAction.act(user2, spam_post, PostActionType.types[:spam])
And { expect{PostCreator.create(spammer, {title: 'limited time offer for you', raw: 'better buy this stuff ok', archetype_id: 1})}.to_not raise_error }
And { expect(spammer.reload.private_topics_count).to eq(private_messages_count) } expect(spam_post.reload).to_not be_hidden
expect(Guardian.new(spammer).can_create_topic?(nil)).to be(true)
expect{PostCreator.create(spammer, {title: 'limited time offer for you', raw: 'better buy this stuff ok', archetype_id: 1})}.to_not raise_error
expect(spammer.reload.private_topics_count).to eq(private_messages_count)
end
end end
end end
[[:user, trust_level: TrustLevel[2]], [:admin], [:moderator]].each do |spammer_args| [[:user, trust_level: TrustLevel[2]], [:admin], [:moderator]].each do |spammer_args|
context "spammer is trusted #{spammer_args[0]}" do context "spammer is trusted #{spammer_args[0]}" do
Given!(:spammer) { Fabricate(*spammer_args) } let!(:spammer) { Fabricate(*spammer_args) }
Given!(:spam_post) { Fabricate(:post, user: spammer) } let!(:spam_post) { Fabricate(:post, user: spammer) }
Given!(:private_messages_count) { spammer.private_topics_count } let!(:private_messages_count) { spammer.private_topics_count }
When { PostAction.act(user1, spam_post, PostActionType.types[:spam]) }
When { PostAction.act(user2, spam_post, PostActionType.types[:spam]) } it 'should not hide the post' do
Then { expect(spam_post.reload).to_not be_hidden } PostAction.act(user1, spam_post, PostActionType.types[:spam])
PostAction.act(user2, spam_post, PostActionType.types[:spam])
expect(spam_post.reload).to_not be_hidden
end
end end
end end
end end

View File

@ -19,25 +19,35 @@ describe Topic do
end end
end end
before do
before { @original_value = SiteSetting.queue_jobs
SiteSetting.queue_jobs = true SiteSetting.queue_jobs = true
Jobs::CloseTopic.jobs.clear Jobs::CloseTopic.jobs.clear
} end
after do
SiteSetting.queue_jobs = @original_value
end
context 'creating a topic without auto-close' do context 'creating a topic without auto-close' do
Given(:topic) { Fabricate(:topic, category: category) } let(:topic) { Fabricate(:topic, category: category) }
context 'uncategorized' do context 'uncategorized' do
Given(:category) { nil } let(:category) { nil }
Then { expect(topic.auto_close_at).to eq(nil) }
And { expect(scheduled_jobs_for(:close_topic)).to be_empty } it 'should not schedule the topic to auto-close' do
expect(topic.auto_close_at).to eq(nil)
expect(scheduled_jobs_for(:close_topic)).to be_empty
end
end end
context 'category without default auto-close' do context 'category without default auto-close' do
Given(:category) { Fabricate(:category, auto_close_hours: nil) } let(:category) { Fabricate(:category, auto_close_hours: nil) }
Then { expect(topic.auto_close_at).to eq(nil) }
And { expect(scheduled_jobs_for(:close_topic)).to be_empty } it 'should not schedule the topic to auto-close' do
expect(topic.auto_close_at).to eq(nil)
expect(scheduled_jobs_for(:close_topic)).to be_empty
end
end end
context 'jobs may be queued' do context 'jobs may be queued' do
@ -51,50 +61,69 @@ describe Topic do
end end
context 'category has a default auto-close' do context 'category has a default auto-close' do
Given(:category) { Fabricate(:category, auto_close_hours: 2.0) } let(:category) { Fabricate(:category, auto_close_hours: 2.0) }
Then { expect(topic.auto_close_at).to be_within_one_second_of(2.hours.from_now) }
And { expect(topic.auto_close_started_at).to eq(Time.zone.now) } it 'should schedule the topic to auto-close' do
And { expect(scheduled_jobs_for(:close_topic, {topic_id: topic.id}).size).to eq(1) } expect(topic.auto_close_at).to be_within_one_second_of(2.hours.from_now)
And { expect(scheduled_jobs_for(:close_topic, {topic_id: category.topic.id})).to be_empty } expect(topic.auto_close_started_at).to eq(Time.zone.now)
expect(scheduled_jobs_for(:close_topic, {topic_id: topic.id}).size).to eq(1)
expect(scheduled_jobs_for(:close_topic, {topic_id: category.topic.id})).to be_empty
end
context 'topic was created by staff user' do context 'topic was created by staff user' do
Given(:admin) { Fabricate(:admin) } let(:admin) { Fabricate(:admin) }
Given(:staff_topic) { Fabricate(:topic, user: admin, category: category) } let(:staff_topic) { Fabricate(:topic, user: admin, category: category) }
Then { expect(scheduled_jobs_for(:close_topic, {topic_id: staff_topic.id, user_id: admin.id}).size).to eq(1) }
it 'should schedule the topic to auto-close' do
expect(scheduled_jobs_for(:close_topic, {topic_id: staff_topic.id, user_id: admin.id}).size).to eq(1)
end
context 'topic is closed manually' do context 'topic is closed manually' do
When { staff_topic.update_status('closed', true, admin) } it 'should remove the schedule to auto-close the topic' do
Then { expect(staff_topic.reload.auto_close_at).to eq(nil) } staff_topic.update_status('closed', true, admin)
And { expect(staff_topic.auto_close_started_at).to eq(nil) } expect(staff_topic.reload.auto_close_at).to eq(nil)
expect(staff_topic.auto_close_started_at).to eq(nil)
end
end end
end end
context 'topic was created by a non-staff user' do context 'topic was created by a non-staff user' do
Given!(:system_user) { Discourse.system_user } let(:regular_user) { Fabricate(:user) }
Given { Discourse.stubs(:system_user).returns(system_user) } let(:regular_user_topic) { Fabricate(:topic, user: regular_user, category: category) }
Given(:regular_user) { Fabricate(:user) }
Given(:regular_user_topic) { Fabricate(:topic, user: regular_user, category: category) } it 'should schedule the topic to auto-close' do
Then { expect(scheduled_jobs_for(:close_topic, {topic_id: regular_user_topic.id, user_id: system_user.id}).size).to eq(1) } expect(scheduled_jobs_for(:close_topic, {topic_id: regular_user_topic.id, user_id: Discourse.system_user.id}).size).to eq(1)
end
end end
context 'auto_close_hours of topic was set to 0' do context 'auto_close_hours of topic was set to 0' do
Given(:dont_close_topic) { Fabricate(:topic, auto_close_hours: 0, category: category) } let(:dont_close_topic) { Fabricate(:topic, auto_close_hours: 0, category: category) }
Then { expect(scheduled_jobs_for(:close_topic)).to be_empty }
it 'should not schedule the topic to auto-close' do
expect(scheduled_jobs_for(:close_topic)).to be_empty
end
end end
context 'two topics in the category' do context 'two topics in the category' do
Given!(:other_topic) { Fabricate(:topic, category: category) } let!(:other_topic) { Fabricate(:topic, category: category) }
When { topic } # create the second topic
Then { expect(scheduled_jobs_for(:close_topic).size).to eq(2) } it 'should schedule the topic to auto-close' do
topic
expect(scheduled_jobs_for(:close_topic).size).to eq(2)
end
end end
end end
context 'a topic that has been auto-closed' do context 'a topic that has been auto-closed' do
Given(:admin) { Fabricate(:admin) } let(:admin) { Fabricate(:admin) }
Given!(:auto_closed_topic) { Fabricate(:topic, user: admin, closed: true, auto_close_at: 1.day.ago, auto_close_user_id: admin.id, auto_close_started_at: 6.days.ago) } let!(:auto_closed_topic) { Fabricate(:topic, user: admin, closed: true, auto_close_at: 1.day.ago, auto_close_user_id: admin.id, auto_close_started_at: 6.days.ago) }
When { auto_closed_topic.update_status('closed', false, admin) }
Then { expect(auto_closed_topic.reload.auto_close_at).to eq(nil) } it 'should set the right attributes' do
And { expect(auto_closed_topic.auto_close_started_at).to eq(nil) } auto_closed_topic.update_status('closed', false, admin)
expect(auto_closed_topic.reload.auto_close_at).to eq(nil)
expect(auto_closed_topic.auto_close_started_at).to eq(nil)
end
end end
end end
end end

View File

@ -15,21 +15,32 @@ describe Jobs::EnqueueDigestEmails do
end end
context 'unapproved users' do context 'unapproved users' do
Given!(:unapproved_user) { Fabricate(:active_user, approved: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) } let!(:unapproved_user) { Fabricate(:active_user, approved: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) }
When { SiteSetting.stubs(:must_approve_users?).returns(true) }
Then { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(false) }
# As a moderator before do
And { unapproved_user.update_column(:moderator, true) } @original_value = SiteSetting.must_approve_users
And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) } SiteSetting.must_approve_users = true
end
# As an admin after do
And { unapproved_user.update_attributes(admin: true, moderator: false) } SiteSetting.must_approve_users = @original_value
And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) } end
# As an approved user it 'should enqueue the right digest emails' do
And { unapproved_user.update_attributes(admin: false, moderator: false, approved: true ) } expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(false)
And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) }
# As a moderator
unapproved_user.update_column(:moderator, true)
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true)
# As an admin
unapproved_user.update_attributes(admin: true, moderator: false)
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true)
# As an approved user
unapproved_user.update_attributes(admin: false, moderator: false, approved: true )
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true)
end
end end
context 'staged users' do context 'staged users' do

View File

@ -6,24 +6,33 @@ describe Jobs::EnqueueMailingListEmails do
describe '#target_users' do describe '#target_users' do
context 'unapproved users' do context 'unapproved users' do
Given!(:unapproved_user) { Fabricate(:active_user, approved: false, first_seen_at: 24.hours.ago) } let!(:unapproved_user) { Fabricate(:active_user, approved: false, first_seen_at: 24.hours.ago) }
When do
before do
@original_value = SiteSetting.must_approve_users
SiteSetting.must_approve_users = true SiteSetting.must_approve_users = true
unapproved_user.user_option.update(mailing_list_mode: true, mailing_list_mode_frequency: 0)
end end
Then { expect(Jobs::EnqueueMailingListEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(false) }
# As a moderator after do
And { unapproved_user.update_column(:moderator, true) } SiteSetting.must_approve_users = @original_value
And { expect(Jobs::EnqueueMailingListEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) } end
# As an admin it 'should enqueue the right emails' do
And { unapproved_user.update_attributes(admin: true, moderator: false) } unapproved_user.user_option.update(mailing_list_mode: true, mailing_list_mode_frequency: 0)
And { expect(Jobs::EnqueueMailingListEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) } expect(Jobs::EnqueueMailingListEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(false)
# As an approved user # As a moderator
And { unapproved_user.update_attributes(admin: false, moderator: false, approved: true ) } unapproved_user.update_column(:moderator, true)
And { expect(Jobs::EnqueueMailingListEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) } expect(Jobs::EnqueueMailingListEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true)
# As an admin
unapproved_user.update_attributes(admin: true, moderator: false)
expect(Jobs::EnqueueMailingListEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true)
# As an approved user
unapproved_user.update_attributes(admin: false, moderator: false, approved: true )
expect(Jobs::EnqueueMailingListEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true)
end
end end
context 'staged users' do context 'staged users' do

View File

@ -1,4 +1,20 @@
module Helpers module Helpers
extend ActiveSupport::Concern
class_methods do
def site_setting(setting_name, value)
original_value = SiteSetting.public_send(setting_name.to_sym)
self.before do
SiteSetting.public_send("#{setting_name}=", value)
end
self.after do
SiteSetting.public_send("#{setting_name}=", original_value)
end
end
end
def self.next_seq def self.next_seq
@next_seq = (@next_seq || 0) + 1 @next_seq = (@next_seq || 0) + 1
end end