2014-06-27 12:26:03 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
describe TrustLevel3Requirements do
|
2014-06-27 12:26:03 -04:00
|
|
|
|
|
|
|
let(:user) { Fabricate.build(:user) }
|
2014-09-05 01:20:39 -04:00
|
|
|
subject(:tl3_requirements) { described_class.new(user) }
|
2014-06-27 12:26:03 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
described_class.clear_cache
|
|
|
|
end
|
|
|
|
|
2014-07-01 13:34:48 -04:00
|
|
|
def make_view(id, at, user_id)
|
2014-09-05 01:20:39 -04:00
|
|
|
TopicViewItem.add(id, '11.22.33.44', user_id, at, _skip_redis=true)
|
2014-07-01 13:34:48 -04:00
|
|
|
end
|
|
|
|
|
2014-06-27 12:26:03 -04:00
|
|
|
describe "requirements" do
|
|
|
|
it "min_days_visited uses site setting" do
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.stubs(:tl3_requires_days_visited).returns(66)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.min_days_visited.should == 66
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min_topics_replied_to uses site setting" do
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.stubs(:tl3_requires_topics_replied_to).returns(12)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.min_topics_replied_to.should == 12
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min_topics_viewed depends on site setting and number of topics created" do
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.stubs(:tl3_requires_topics_viewed).returns(75)
|
2014-06-27 12:26:03 -04:00
|
|
|
described_class.stubs(:num_topics_in_time_period).returns(31)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.min_topics_viewed.should == 23
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min_posts_read depends on site setting and number of posts created" do
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.stubs(:tl3_requires_posts_read).returns(66)
|
2014-06-27 12:26:03 -04:00
|
|
|
described_class.stubs(:num_posts_in_time_period).returns(1234)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.min_posts_read.should == 814
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
2014-07-01 13:34:48 -04:00
|
|
|
it "min_topics_viewed_all_time depends on site setting" do
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.stubs(:tl3_requires_topics_viewed_all_time).returns(75)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.min_topics_viewed_all_time.should == 75
|
2014-07-01 13:34:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min_posts_read_all_time depends on site setting" do
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.stubs(:tl3_requires_posts_read_all_time).returns(1001)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.min_posts_read_all_time.should == 1001
|
2014-07-01 13:34:48 -04:00
|
|
|
end
|
|
|
|
|
2014-06-27 12:26:03 -04:00
|
|
|
it "max_flagged_posts depends on site setting" do
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.stubs(:tl3_requires_max_flagged).returns(3)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.max_flagged_posts.should == 3
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
2014-08-22 17:33:05 -04:00
|
|
|
|
|
|
|
it "min_likes_given depends on site setting" do
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.stubs(:tl3_requires_likes_given).returns(30)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.min_likes_given.should == 30
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min_likes_received depends on site setting" do
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.stubs(:tl3_requires_likes_received).returns(20)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.min_likes_received.should == 20
|
2014-10-07 15:52:54 -04:00
|
|
|
tl3_requirements.min_likes_received_days.should == 7
|
|
|
|
tl3_requirements.min_likes_received_users.should == 5
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "days_visited" do
|
|
|
|
it "counts visits when posts were read no further back than 100 days ago" do
|
|
|
|
user.save
|
|
|
|
user.update_posts_read!(1, 2.days.ago)
|
|
|
|
user.update_posts_read!(1, 3.days.ago)
|
|
|
|
user.update_posts_read!(0, 4.days.ago)
|
|
|
|
user.update_posts_read!(3, 101.days.ago)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.days_visited.should == 2
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "num_topics_replied_to" do
|
|
|
|
it "counts topics in which user replied in last 100 days" do
|
|
|
|
user.save
|
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
_not_a_reply = create_post(user: user) # user created the topic, so it doesn't count
|
2014-06-27 12:26:03 -04:00
|
|
|
|
|
|
|
topic1 = create_post.topic
|
2014-09-05 01:20:39 -04:00
|
|
|
_reply1 = create_post(topic: topic1, user: user)
|
|
|
|
_reply_again = create_post(topic: topic1, user: user) # two replies in one topic
|
2014-06-27 12:26:03 -04:00
|
|
|
|
|
|
|
topic2 = create_post(created_at: 101.days.ago).topic
|
2014-09-05 01:20:39 -04:00
|
|
|
_reply2 = create_post(topic: topic2, user: user, created_at: 101.days.ago) # topic is over 100 days old
|
2014-06-27 12:26:03 -04:00
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.num_topics_replied_to.should == 1
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "topics_viewed" do
|
|
|
|
it "counts topics views within last 100 days, not counting a topic more than once" do
|
|
|
|
user.save
|
|
|
|
make_view(9, 1.day.ago, user.id)
|
|
|
|
make_view(9, 3.days.ago, user.id) # same topic, different day
|
|
|
|
make_view(3, 4.days.ago, user.id)
|
|
|
|
make_view(2, 101.days.ago, user.id) # too long ago
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.topics_viewed.should == 2
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "posts_read" do
|
|
|
|
it "counts posts read within the last 100 days" do
|
|
|
|
user.save
|
|
|
|
user.update_posts_read!(3, 2.days.ago)
|
|
|
|
user.update_posts_read!(1, 3.days.ago)
|
|
|
|
user.update_posts_read!(0, 4.days.ago)
|
|
|
|
user.update_posts_read!(5, 101.days.ago)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.posts_read.should == 4
|
2014-07-01 13:34:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "topics_viewed_all_time" do
|
|
|
|
it "counts topics viewed at any time" do
|
|
|
|
user.save
|
|
|
|
make_view(10, 1.day.ago, user.id)
|
|
|
|
make_view(9, 100.days.ago, user.id)
|
|
|
|
make_view(8, 101.days.ago, user.id)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.topics_viewed_all_time.should == 3
|
2014-07-01 13:34:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "posts_read_all_time" do
|
|
|
|
it "counts posts read at any time" do
|
|
|
|
user.save
|
|
|
|
user.update_posts_read!(3, 2.days.ago)
|
|
|
|
user.update_posts_read!(1, 101.days.ago)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.posts_read_all_time.should == 4
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with flagged posts" do
|
|
|
|
before do
|
|
|
|
user.save
|
|
|
|
flags = [:off_topic, :inappropriate, :notify_user, :notify_moderators, :spam].map do |t|
|
2014-07-31 10:43:13 -04:00
|
|
|
Fabricate(:flag, post: Fabricate(:post, user: user), post_action_type_id: PostActionType.types[t], agreed_at: 1.minute.ago)
|
|
|
|
end
|
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
_deferred_flags = [:off_topic, :inappropriate, :notify_user, :notify_moderators, :spam].map do |t|
|
2014-08-11 05:56:54 -04:00
|
|
|
Fabricate(:flag, post: Fabricate(:post, user: user), post_action_type_id: PostActionType.types[t], deferred_at: 1.minute.ago)
|
2014-07-31 10:43:13 -04:00
|
|
|
end
|
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
_deleted_flags = [:off_topic, :inappropriate, :notify_user, :notify_moderators, :spam].map do |t|
|
2014-07-31 10:43:13 -04:00
|
|
|
Fabricate(:flag, post: Fabricate(:post, user: user), post_action_type_id: PostActionType.types[t], deleted_at: 1.minute.ago)
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Same post, different user:
|
2014-07-31 10:43:13 -04:00
|
|
|
Fabricate(:flag, post: flags[1].post, post_action_type_id: PostActionType.types[:spam], agreed_at: 1.minute.ago)
|
2014-06-27 12:26:03 -04:00
|
|
|
|
|
|
|
# Flagged their own post:
|
2014-07-31 10:43:13 -04:00
|
|
|
Fabricate(:flag, user: user, post: Fabricate(:post, user: user), post_action_type_id: PostActionType.types[:spam], agreed_at: 1.minute.ago)
|
2014-06-27 12:26:03 -04:00
|
|
|
|
|
|
|
# More than 100 days ago:
|
2014-07-31 10:43:13 -04:00
|
|
|
Fabricate(:flag, post: Fabricate(:post, user: user, created_at: 101.days.ago), post_action_type_id: PostActionType.types[:spam], created_at: 101.days.ago, agreed_at: 1.day.ago)
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
2014-07-31 10:43:13 -04:00
|
|
|
it "num_flagged_posts and num_flagged_by_users count spam and inappropriate agreed flags in the last 100 days" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.num_flagged_posts.should == 2
|
|
|
|
tl3_requirements.num_flagged_by_users.should == 3
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-22 17:33:05 -04:00
|
|
|
describe "num_likes_given" do
|
|
|
|
it "counts likes given in the last 100 days" do
|
|
|
|
ActiveRecord::Base.observers.enable :user_action_observer
|
|
|
|
|
|
|
|
recent_post1 = create_post(created_at: 1.hour.ago)
|
|
|
|
recent_post2 = create_post(created_at: 10.days.ago)
|
|
|
|
old_post = create_post(created_at: 102.days.ago)
|
|
|
|
|
|
|
|
Fabricate(:like, user: user, post: recent_post1, created_at: 2.hours.ago)
|
|
|
|
Fabricate(:like, user: user, post: recent_post2, created_at: 5.days.ago)
|
|
|
|
Fabricate(:like, user: user, post: old_post, created_at: 101.days.ago)
|
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.num_likes_given.should == 2
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "num_likes_received" do
|
|
|
|
it "counts likes received in the last 100 days" do
|
|
|
|
ActiveRecord::Base.observers.enable :user_action_observer
|
|
|
|
|
|
|
|
t = Fabricate(:topic, user: user, created_at: 102.days.ago)
|
|
|
|
old_post = create_post(topic: t, user: user, created_at: 102.days.ago)
|
|
|
|
recent_post2 = create_post(topic: t, user: user, created_at: 10.days.ago)
|
|
|
|
recent_post1 = create_post(topic: t, user: user, created_at: 1.hour.ago)
|
|
|
|
|
|
|
|
liker = Fabricate(:user)
|
2014-10-07 15:52:54 -04:00
|
|
|
liker2 = Fabricate(:user)
|
|
|
|
Fabricate(:like, user: liker, post: recent_post1, created_at: 2.hours.ago)
|
|
|
|
Fabricate(:like, user: liker2, post: recent_post1, created_at: 2.hours.ago)
|
|
|
|
Fabricate(:like, user: liker, post: recent_post2, created_at: 5.days.ago)
|
|
|
|
Fabricate(:like, user: liker, post: old_post, created_at: 101.days.ago)
|
2014-08-22 17:33:05 -04:00
|
|
|
|
2014-10-07 15:52:54 -04:00
|
|
|
tl3_requirements.num_likes_received.should == 3
|
|
|
|
tl3_requirements.num_likes_received_days.should == 2
|
|
|
|
tl3_requirements.num_likes_received_users.should == 2
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-28 02:42:38 -04:00
|
|
|
describe "requirements" do
|
|
|
|
|
|
|
|
before do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:min_days_visited).returns(50)
|
|
|
|
tl3_requirements.stubs(:min_topics_replied_to).returns(10)
|
|
|
|
tl3_requirements.stubs(:min_topics_viewed).returns(25)
|
|
|
|
tl3_requirements.stubs(:min_posts_read).returns(25)
|
|
|
|
tl3_requirements.stubs(:min_topics_viewed_all_time).returns(200)
|
|
|
|
tl3_requirements.stubs(:min_posts_read_all_time).returns(500)
|
|
|
|
tl3_requirements.stubs(:max_flagged_posts).returns(5)
|
|
|
|
tl3_requirements.stubs(:max_flagged_by_users).returns(5)
|
|
|
|
tl3_requirements.stubs(:min_likes_given).returns(30)
|
|
|
|
tl3_requirements.stubs(:min_likes_received).returns(20)
|
2014-10-07 15:52:54 -04:00
|
|
|
tl3_requirements.stubs(:min_likes_received_days).returns(7)
|
|
|
|
tl3_requirements.stubs(:min_likes_received_users).returns(5)
|
2014-09-05 01:20:39 -04:00
|
|
|
|
|
|
|
tl3_requirements.stubs(:days_visited).returns(50)
|
|
|
|
tl3_requirements.stubs(:num_topics_replied_to).returns(10)
|
|
|
|
tl3_requirements.stubs(:topics_viewed).returns(25)
|
|
|
|
tl3_requirements.stubs(:posts_read).returns(25)
|
|
|
|
tl3_requirements.stubs(:topics_viewed_all_time).returns(200)
|
|
|
|
tl3_requirements.stubs(:posts_read_all_time).returns(500)
|
|
|
|
tl3_requirements.stubs(:num_flagged_posts).returns(0)
|
|
|
|
tl3_requirements.stubs(:num_flagged_by_users).returns(0)
|
|
|
|
tl3_requirements.stubs(:num_likes_given).returns(30)
|
|
|
|
tl3_requirements.stubs(:num_likes_received).returns(20)
|
2014-10-07 15:52:54 -04:00
|
|
|
tl3_requirements.stubs(:num_likes_received_days).returns(7)
|
|
|
|
tl3_requirements.stubs(:num_likes_received_users).returns(5)
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are met when all requirements are met" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.requirements_met?.should == true
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are not met if too few days visited" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:days_visited).returns(49)
|
|
|
|
tl3_requirements.requirements_met?.should == false
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are not lost if requirements are close" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:days_visited).returns(45)
|
|
|
|
tl3_requirements.stubs(:num_topics_replied_to).returns(9)
|
|
|
|
tl3_requirements.stubs(:topics_viewed).returns(23)
|
|
|
|
tl3_requirements.stubs(:posts_read).returns(23)
|
|
|
|
tl3_requirements.stubs(:num_likes_given).returns(29)
|
|
|
|
tl3_requirements.stubs(:num_likes_received).returns(19)
|
|
|
|
tl3_requirements.requirements_lost?.should == false
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough visited" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:days_visited).returns(44)
|
|
|
|
tl3_requirements.requirements_lost?.should == true
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough topics replied to" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:num_topics_replied_to).returns(8)
|
|
|
|
tl3_requirements.requirements_lost?.should == true
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough topics viewed" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:topics_viewed).returns(22)
|
|
|
|
tl3_requirements.requirements_lost?.should == true
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough posts read" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:posts_read).returns(22)
|
|
|
|
tl3_requirements.requirements_lost?.should == true
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
2014-08-22 17:33:05 -04:00
|
|
|
it "are not met if not enough likes given" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:num_likes_given).returns(29)
|
|
|
|
tl3_requirements.requirements_met?.should == false
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are not met if not enough likes received" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:num_likes_received).returns(19)
|
|
|
|
tl3_requirements.requirements_met?.should == false
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
|
|
|
|
2014-10-07 15:52:54 -04:00
|
|
|
it "are not met if not enough likes received on different days" do
|
|
|
|
tl3_requirements.stubs(:num_likes_received_days).returns(6)
|
|
|
|
tl3_requirements.requirements_met?.should == false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "are not met if not enough likes received by different users" do
|
|
|
|
tl3_requirements.stubs(:num_likes_received_users).returns(4)
|
|
|
|
tl3_requirements.requirements_met?.should == false
|
|
|
|
end
|
|
|
|
|
2014-08-22 17:33:05 -04:00
|
|
|
it "are lost if not enough likes given" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:num_likes_given).returns(26)
|
|
|
|
tl3_requirements.requirements_lost?.should == true
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough likes received" do
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.stubs(:num_likes_received).returns(17)
|
|
|
|
tl3_requirements.requirements_lost?.should == true
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
2014-08-29 12:33:34 -04:00
|
|
|
|
|
|
|
it "are not met if suspended" do
|
|
|
|
user.stubs(:suspended?).returns(true)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.requirements_met?.should == false
|
2014-08-29 12:33:34 -04:00
|
|
|
end
|
|
|
|
|
2014-10-07 15:52:54 -04:00
|
|
|
it "are lost if not enough likes received on different days" do
|
|
|
|
tl3_requirements.stubs(:num_likes_received_days).returns(4)
|
|
|
|
tl3_requirements.requirements_lost?.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough likes received by different users" do
|
|
|
|
tl3_requirements.stubs(:num_likes_received_users).returns(3)
|
|
|
|
tl3_requirements.requirements_lost?.should == true
|
|
|
|
end
|
|
|
|
|
2014-08-29 12:33:34 -04:00
|
|
|
it "are lost if suspended" do
|
|
|
|
user.stubs(:suspended?).returns(true)
|
2014-09-05 01:20:39 -04:00
|
|
|
tl3_requirements.requirements_lost?.should == true
|
2014-08-29 12:33:34 -04:00
|
|
|
end
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|