2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'promotion'
|
|
|
|
|
|
|
|
describe Promotion do
|
|
|
|
|
2014-06-28 00:26:03 +08:00
|
|
|
describe "review" do
|
|
|
|
it "skips regular users" do
|
|
|
|
# Reviewing users at higher trust levels is expensive, so trigger those reviews in a background job.
|
2014-09-05 13:20:39 +08:00
|
|
|
regular = Fabricate.build(:user, trust_level: TrustLevel[2])
|
2014-06-28 00:26:03 +08:00
|
|
|
promotion = described_class.new(regular)
|
2014-09-05 13:20:39 +08:00
|
|
|
promotion.expects(:review_tl2).never
|
2014-06-28 00:26:03 +08:00
|
|
|
promotion.review
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-18 07:11:13 +08:00
|
|
|
context "newuser" do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
let(:user) { Fabricate(:user, trust_level: TrustLevel[0])}
|
2013-02-06 03:16:51 +08:00
|
|
|
let(:promotion) { Promotion.new(user) }
|
|
|
|
|
|
|
|
it "doesn't raise an error with a nil user" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect { Promotion.new(nil).review }.not_to raise_error
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'that has done nothing' do
|
|
|
|
let!(:result) { promotion.review }
|
|
|
|
|
|
|
|
it "returns false" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(result).to eq(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has not changed the user's trust level" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(user.trust_level).to eq(TrustLevel[0])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "that has done the requisite things" do
|
|
|
|
|
|
|
|
before do
|
2013-10-04 11:28:49 +08:00
|
|
|
stat = user.user_stat
|
2014-09-05 04:16:46 +08:00
|
|
|
stat.topics_entered = SiteSetting.tl1_requires_topics_entered
|
|
|
|
stat.posts_read_count = SiteSetting.tl1_requires_read_posts
|
|
|
|
stat.time_read = SiteSetting.tl1_requires_time_spent_mins * 60
|
2013-02-06 03:16:51 +08:00
|
|
|
@result = promotion.review
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(@result).to eq(true)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has upgraded the user to basic" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(user.trust_level).to eq(TrustLevel[1])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-04-05 12:29:46 +08:00
|
|
|
context "basic" do
|
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
let(:user) { Fabricate(:user, trust_level: TrustLevel[1])}
|
2013-04-05 12:29:46 +08:00
|
|
|
let(:promotion) { Promotion.new(user) }
|
|
|
|
|
|
|
|
context 'that has done nothing' do
|
|
|
|
let!(:result) { promotion.review }
|
|
|
|
|
|
|
|
it "returns false" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(result).to eq(false)
|
2013-04-05 12:29:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has not changed the user's trust level" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(user.trust_level).to eq(TrustLevel[1])
|
2013-04-05 12:29:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "that has done the requisite things" do
|
|
|
|
|
|
|
|
before do
|
2013-10-04 11:28:49 +08:00
|
|
|
stat = user.user_stat
|
2014-09-05 04:16:46 +08:00
|
|
|
stat.topics_entered = SiteSetting.tl2_requires_topics_entered
|
|
|
|
stat.posts_read_count = SiteSetting.tl2_requires_read_posts
|
|
|
|
stat.time_read = SiteSetting.tl2_requires_time_spent_mins * 60
|
|
|
|
stat.days_visited = SiteSetting.tl2_requires_days_visited * 60
|
|
|
|
stat.likes_received = SiteSetting.tl2_requires_likes_received
|
|
|
|
stat.likes_given = SiteSetting.tl2_requires_likes_given
|
|
|
|
stat.topic_reply_count = SiteSetting.tl2_requires_topic_reply_count
|
2013-04-05 12:29:46 +08:00
|
|
|
|
|
|
|
@result = promotion.review
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(@result).to eq(true)
|
2013-04-05 12:29:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has upgraded the user to regular" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(user.trust_level).to eq(TrustLevel[2])
|
2013-04-05 12:29:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-06-28 00:26:03 +08:00
|
|
|
context "regular" do
|
2014-09-05 13:20:39 +08:00
|
|
|
let(:user) { Fabricate(:user, trust_level: TrustLevel[2])}
|
2014-06-28 00:26:03 +08:00
|
|
|
let(:promotion) { Promotion.new(user) }
|
|
|
|
|
|
|
|
context "doesn't qualify for promotion" do
|
|
|
|
before do
|
2014-09-05 13:20:39 +08:00
|
|
|
TrustLevel3Requirements.any_instance.expects(:requirements_met?).at_least_once.returns(false)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
it "review_tl2 returns false" do
|
2014-06-28 00:26:03 +08:00
|
|
|
expect {
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(promotion.review_tl2).to eq(false)
|
2014-06-28 00:26:03 +08:00
|
|
|
}.to_not change { user.reload.trust_level }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't promote" do
|
|
|
|
expect {
|
2014-09-05 13:20:39 +08:00
|
|
|
promotion.review_tl2
|
2014-06-28 00:26:03 +08:00
|
|
|
}.to_not change { user.reload.trust_level }
|
|
|
|
end
|
2014-07-09 05:39:36 +08:00
|
|
|
|
|
|
|
it "doesn't log a trust level change" do
|
|
|
|
expect {
|
2014-09-05 13:20:39 +08:00
|
|
|
promotion.review_tl2
|
2014-07-09 05:39:36 +08:00
|
|
|
}.to_not change { UserHistory.count }
|
|
|
|
end
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "qualifies for promotion" do
|
|
|
|
before do
|
2014-09-05 13:20:39 +08:00
|
|
|
TrustLevel3Requirements.any_instance.expects(:requirements_met?).at_least_once.returns(true)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
it "review_tl2 returns true" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(promotion.review_tl2).to eq(true)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
it "promotes to tl3" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(promotion.review_tl2).to eq(true)
|
|
|
|
expect(user.reload.trust_level).to eq(TrustLevel[3])
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
2014-07-09 05:39:36 +08:00
|
|
|
|
|
|
|
it "logs a trust level change" do
|
|
|
|
expect {
|
2014-09-05 13:20:39 +08:00
|
|
|
promotion.review_tl2
|
2014-07-09 05:39:36 +08:00
|
|
|
}.to change { UserHistory.where(action: UserHistory.actions[:auto_trust_level_change]).count }.by(1)
|
|
|
|
end
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|