Cleaned up TopicUserSpec, introduces clearing of pinned topics

This commit is contained in:
Robin Ward
2013-03-06 15:17:07 -05:00
parent 3af2ab9022
commit f8d8272406
27 changed files with 2663 additions and 342 deletions

View File

@ -5,155 +5,165 @@ describe TopicUser do
it { should belong_to :user }
it { should belong_to :topic }
let!(:yesterday) { DateTime.now.yesterday }
before do
#mock time so we can test dates
@now = DateTime.now.yesterday
DateTime.expects(:now).at_least_once.returns(@now)
@topic = Fabricate(:topic)
@user = Fabricate(:coding_horror)
DateTime.expects(:now).at_least_once.returns(yesterday)
end
let!(:topic) { Fabricate(:topic) }
let!(:user) { Fabricate(:coding_horror) }
let(:topic_user) { TopicUser.get(topic,user) }
let(:topic_creator_user) { TopicUser.get(topic, topic.user) }
let(:post) { Fabricate(:post, topic: topic, user: user) }
let(:new_user) { Fabricate(:user, auto_track_topics_after_msecs: 1000) }
let(:topic_new_user) { TopicUser.get(topic, new_user)}
describe "unpinned" do
before do
TopicUser.change(user, topic, {:starred_at => yesterday})
end
it "defaults to blank" do
topic_user.cleared_pinned_at.should be_blank
end
end
describe 'notifications' do
it 'should be set to tracking if auto_track_topics is enabled' do
@user.auto_track_topics_after_msecs = 0
@user.save
TopicUser.change(@user, @topic, {:starred_at => DateTime.now})
TopicUser.get(@topic,@user).notification_level.should == TopicUser::NotificationLevel::TRACKING
user.update_column(:auto_track_topics_after_msecs, 0)
TopicUser.change(user, topic, {:starred_at => yesterday})
TopicUser.get(topic, user).notification_level.should == TopicUser.notification_levels[:tracking]
end
it 'should reset regular topics to tracking topics if auto track is changed' do
TopicUser.change(@user, @topic, {:starred_at => DateTime.now})
@user.auto_track_topics_after_msecs = 0
@user.save
TopicUser.get(@topic,@user).notification_level.should == TopicUser::NotificationLevel::TRACKING
TopicUser.change(user, topic, {:starred_at => yesterday})
user.auto_track_topics_after_msecs = 0
user.save
topic_user.notification_level.should == TopicUser.notification_levels[:tracking]
end
it 'should be set to "regular" notifications, by default on non creators' do
TopicUser.change(@user, @topic, {:starred_at => DateTime.now})
TopicUser.get(@topic,@user).notification_level.should == TopicUser::NotificationLevel::REGULAR
TopicUser.change(user, topic, {:starred_at => yesterday})
TopicUser.get(topic,user).notification_level.should == TopicUser.notification_levels[:regular]
end
it 'reason should reset when changed' do
@topic.notify_muted!(@topic.user)
TopicUser.get(@topic,@topic.user).notifications_reason_id.should == TopicUser::NotificationReasons::USER_CHANGED
topic.notify_muted!(topic.user)
TopicUser.get(topic,topic.user).notifications_reason_id.should == TopicUser.notification_reasons[:user_changed]
end
it 'should have the correct reason for a user change when watched' do
@topic.notify_watch!(@user)
tu = TopicUser.get(@topic,@user)
tu.notification_level.should == TopicUser::NotificationLevel::WATCHING
tu.notifications_reason_id.should == TopicUser::NotificationReasons::USER_CHANGED
tu.notifications_changed_at.should_not be_nil
topic.notify_watch!(user)
topic_user.notification_level.should == TopicUser.notification_levels[:watching]
topic_user.notifications_reason_id.should == TopicUser.notification_reasons[:user_changed]
topic_user.notifications_changed_at.should_not be_nil
end
it 'should have the correct reason for a user change when set to regular' do
@topic.notify_regular!(@user)
tu = TopicUser.get(@topic,@user)
tu.notification_level.should == TopicUser::NotificationLevel::REGULAR
tu.notifications_reason_id.should == TopicUser::NotificationReasons::USER_CHANGED
tu.notifications_changed_at.should_not be_nil
topic.notify_regular!(user)
topic_user.notification_level.should == TopicUser.notification_levels[:regular]
topic_user.notifications_reason_id.should == TopicUser.notification_reasons[:user_changed]
topic_user.notifications_changed_at.should_not be_nil
end
it 'should have the correct reason for a user change when set to regular' do
@topic.notify_muted!(@user)
tu = TopicUser.get(@topic,@user)
tu.notification_level.should == TopicUser::NotificationLevel::MUTED
tu.notifications_reason_id.should == TopicUser::NotificationReasons::USER_CHANGED
tu.notifications_changed_at.should_not be_nil
topic.notify_muted!(user)
topic_user.notification_level.should == TopicUser.notification_levels[:muted]
topic_user.notifications_reason_id.should == TopicUser.notification_reasons[:user_changed]
topic_user.notifications_changed_at.should_not be_nil
end
it 'should watch topics a user created' do
tu = TopicUser.get(@topic,@topic.user)
tu.notification_level.should == TopicUser::NotificationLevel::WATCHING
tu.notifications_reason_id.should == TopicUser::NotificationReasons::CREATED_TOPIC
topic_creator_user.notification_level.should == TopicUser.notification_levels[:watching]
topic_creator_user.notifications_reason_id.should == TopicUser.notification_reasons[:created_topic]
end
end
describe 'visited at' do
before do
TopicUser.track_visit!(@topic, @user)
@topic_user = TopicUser.get(@topic,@user)
before do
TopicUser.track_visit!(topic, user)
end
it 'set upon initial visit' do
@topic_user.first_visited_at.to_i.should == @now.to_i
@topic_user.last_visited_at.to_i.should == @now.to_i
topic_user.first_visited_at.to_i.should == yesterday.to_i
topic_user.last_visited_at.to_i.should == yesterday.to_i
end
it 'updates upon repeat visit' do
tomorrow = @now.tomorrow
DateTime.expects(:now).returns(tomorrow)
today = yesterday.tomorrow
DateTime.expects(:now).returns(today)
TopicUser.track_visit!(@topic,@user)
TopicUser.track_visit!(topic,user)
# reload is a no go
@topic_user = TopicUser.get(@topic,@user)
@topic_user.first_visited_at.to_i.should == @now.to_i
@topic_user.last_visited_at.to_i.should == tomorrow.to_i
topic_user = TopicUser.get(topic,user)
topic_user.first_visited_at.to_i.should == yesterday.to_i
topic_user.last_visited_at.to_i.should == today.to_i
end
end
describe 'read tracking' do
before do
@post = Fabricate(:post, topic: @topic, user: @topic.user)
TopicUser.update_last_read(@user, @topic.id, 1, 0)
@topic_user = TopicUser.get(@topic,@user)
end
it 'should create a new record for a visit' do
@topic_user.last_read_post_number.should == 1
@topic_user.last_visited_at.to_i.should == @now.to_i
@topic_user.first_visited_at.to_i.should == @now.to_i
end
context "without auto tracking" do
it 'should update the record for repeat visit' do
Fabricate(:post, topic: @topic, user: @user)
TopicUser.update_last_read(@user, @topic.id, 2, 0)
@topic_user = TopicUser.get(@topic,@user)
@topic_user.last_read_post_number.should == 2
@topic_user.last_visited_at.to_i.should == @now.to_i
@topic_user.first_visited_at.to_i.should == @now.to_i
before do
TopicUser.update_last_read(user, topic.id, 1, 0)
end
let(:topic_user) { TopicUser.get(topic,user) }
it 'should create a new record for a visit' do
topic_user.last_read_post_number.should == 1
topic_user.last_visited_at.to_i.should == yesterday.to_i
topic_user.first_visited_at.to_i.should == yesterday.to_i
end
it 'should update the record for repeat visit' do
Fabricate(:post, topic: topic, user: user)
TopicUser.update_last_read(user, topic.id, 2, 0)
topic_user = TopicUser.get(topic,user)
topic_user.last_read_post_number.should == 2
topic_user.last_visited_at.to_i.should == yesterday.to_i
topic_user.first_visited_at.to_i.should == yesterday.to_i
end
end
context 'auto tracking' do
before do
Fabricate(:post, topic: @topic, user: @user)
@new_user = Fabricate(:user, auto_track_topics_after_msecs: 1000)
TopicUser.update_last_read(@new_user, @topic.id, 2, 0)
@topic_user = TopicUser.get(@topic,@new_user)
TopicUser.update_last_read(new_user, topic.id, 2, 0)
end
it 'should automatically track topics you reply to' do
post = Fabricate(:post, topic: @topic, user: @new_user)
@topic_user = TopicUser.get(@topic,@new_user)
@topic_user.notification_level.should == TopicUser::NotificationLevel::TRACKING
@topic_user.notifications_reason_id.should == TopicUser::NotificationReasons::CREATED_POST
post = Fabricate(:post, topic: topic, user: new_user)
topic_new_user.notification_level.should == TopicUser.notification_levels[:tracking]
topic_new_user.notifications_reason_id.should == TopicUser.notification_reasons[:created_post]
end
it 'should not automatically track topics you reply to and have set state manually' do
Fabricate(:post, topic: @topic, user: @new_user)
TopicUser.change(@new_user, @topic, notification_level: TopicUser::NotificationLevel::REGULAR)
@topic_user = TopicUser.get(@topic,@new_user)
@topic_user.notification_level.should == TopicUser::NotificationLevel::REGULAR
@topic_user.notifications_reason_id.should == TopicUser::NotificationReasons::USER_CHANGED
Fabricate(:post, topic: topic, user: new_user)
TopicUser.change(new_user, topic, notification_level: TopicUser.notification_levels[:regular])
topic_new_user.notification_level.should == TopicUser.notification_levels[:regular]
topic_new_user.notifications_reason_id.should == TopicUser.notification_reasons[:user_changed]
end
it 'should automatically track topics after they are read for long enough' do
@topic_user.notification_level.should == TopicUser::NotificationLevel::REGULAR
TopicUser.update_last_read(@new_user, @topic.id, 2, 1001)
@topic_user = TopicUser.get(@topic,@new_user)
@topic_user.notification_level.should == TopicUser::NotificationLevel::TRACKING
topic_new_user.notification_level.should ==TopicUser.notification_levels[:regular]
TopicUser.update_last_read(new_user, topic.id, 2, 1001)
TopicUser.get(topic, new_user).notification_level.should == TopicUser.notification_levels[:tracking]
end
it 'should not automatically track topics after they are read for long enough if changed manually' do
TopicUser.change(@new_user, @topic, notification_level: TopicUser::NotificationLevel::REGULAR)
@topic_user = TopicUser.get(@topic,@new_user)
TopicUser.update_last_read(@new_user, @topic, 2, 1001)
@topic_user = TopicUser.get(@topic,@new_user)
@topic_user.notification_level.should == TopicUser::NotificationLevel::REGULAR
TopicUser.change(new_user, topic, notification_level: TopicUser.notification_levels[:regular])
TopicUser.update_last_read(new_user, topic, 2, 1001)
topic_new_user.notification_level.should == TopicUser.notification_levels[:regular]
end
end
end
@ -162,34 +172,33 @@ describe TopicUser do
it 'creates a forum topic user record' do
lambda {
TopicUser.change(@user, @topic.id, starred: true)
TopicUser.change(user, topic.id, starred: true)
}.should change(TopicUser, :count).by(1)
end
it "only inserts a row once, even on repeated calls" do
lambda {
TopicUser.change(@user, @topic.id, starred: true)
TopicUser.change(@user, @topic.id, starred: false)
TopicUser.change(@user, @topic.id, starred: true)
TopicUser.change(user, topic.id, starred: true)
TopicUser.change(user, topic.id, starred: false)
TopicUser.change(user, topic.id, starred: true)
}.should change(TopicUser, :count).by(1)
end
describe 'after creating a row' do
before do
TopicUser.change(@user, @topic.id, starred: true)
@topic_user = TopicUser.where(user_id: @user.id, topic_id: @topic.id).first
TopicUser.change(user, topic.id, starred: true)
end
it 'has the correct starred value' do
@topic_user.should be_starred
TopicUser.get(topic, user).should be_starred
end
it 'has a lookup' do
TopicUser.lookup_for(@user, [@topic]).should be_present
TopicUser.lookup_for(user, [topic]).should be_present
end
it 'has a key in the lookup for this forum topic' do
TopicUser.lookup_for(@user, [@topic]).has_key?(@topic.id).should be_true
TopicUser.lookup_for(user, [topic]).has_key?(topic.id).should be_true
end
end