mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Improve flaky time-sensitive specs (#9141)
This commit is contained in:
@@ -535,15 +535,15 @@ describe Category do
|
||||
|
||||
describe 'latest' do
|
||||
it 'should be updated correctly' do
|
||||
category = Fabricate(:category_with_definition)
|
||||
post = create_post(category: category.id)
|
||||
category = freeze_time(1.minute.ago) { Fabricate(:category_with_definition) }
|
||||
post = create_post(category: category.id, created_at: 15.seconds.ago)
|
||||
|
||||
category.reload
|
||||
expect(category.latest_post_id).to eq(post.id)
|
||||
expect(category.latest_topic_id).to eq(post.topic_id)
|
||||
|
||||
post2 = create_post(category: category.id)
|
||||
post3 = create_post(topic_id: post.topic_id, category: category.id)
|
||||
post2 = create_post(category: category.id, created_at: 10.seconds.ago)
|
||||
post3 = create_post(topic_id: post.topic_id, category: category.id, created_at: 5.seconds.ago)
|
||||
|
||||
category.reload
|
||||
expect(category.latest_post_id).to eq(post3.id)
|
||||
@@ -858,22 +858,18 @@ describe Category do
|
||||
end
|
||||
|
||||
it 'should correctly automatically bump topics' do
|
||||
freeze_time 1.second.ago
|
||||
category = Fabricate(:category_with_definition)
|
||||
freeze_time
|
||||
category = Fabricate(:category_with_definition, created_at: 1.minute.ago)
|
||||
category.clear_auto_bump_cache!
|
||||
|
||||
freeze_time 1.second.from_now
|
||||
post1 = create_post(category: category)
|
||||
freeze_time 1.second.from_now
|
||||
_post2 = create_post(category: category)
|
||||
freeze_time 1.second.from_now
|
||||
_post3 = create_post(category: category)
|
||||
post1 = create_post(category: category, created_at: 15.seconds.ago)
|
||||
_post2 = create_post(category: category, created_at: 10.seconds.ago)
|
||||
_post3 = create_post(category: category, created_at: 5.seconds.ago)
|
||||
|
||||
# no limits on post creation or category creation please
|
||||
RateLimiter.enable
|
||||
|
||||
time = 1.month.from_now
|
||||
freeze_time time
|
||||
time = freeze_time 1.month.from_now
|
||||
|
||||
expect(category.auto_bump_topic!).to eq(false)
|
||||
expect(Topic.where(bumped_at: time).count).to eq(0)
|
||||
@@ -886,8 +882,7 @@ describe Category do
|
||||
# our extra bump message
|
||||
expect(post1.topic.reload.posts_count).to eq(2)
|
||||
|
||||
time = time + 13.hours
|
||||
freeze_time time
|
||||
time = freeze_time 13.hours.from_now
|
||||
|
||||
expect(category.auto_bump_topic!).to eq(true)
|
||||
expect(Topic.where(bumped_at: time).count).to eq(1)
|
||||
@@ -895,8 +890,7 @@ describe Category do
|
||||
expect(category.auto_bump_topic!).to eq(false)
|
||||
expect(Topic.where(bumped_at: time).count).to eq(1)
|
||||
|
||||
time = 1.month.from_now
|
||||
freeze_time time
|
||||
time = freeze_time 1.month.from_now
|
||||
|
||||
category.auto_bump_limiter.clear!
|
||||
expect(Category.auto_bump_topic!).to eq(true)
|
||||
@@ -909,18 +903,16 @@ describe Category do
|
||||
end
|
||||
|
||||
it 'should not automatically bump topics with a bump scheduled' do
|
||||
freeze_time 1.second.ago
|
||||
category = Fabricate(:category_with_definition)
|
||||
freeze_time
|
||||
category = Fabricate(:category_with_definition, created_at: 1.second.ago)
|
||||
category.clear_auto_bump_cache!
|
||||
|
||||
freeze_time 1.second.from_now
|
||||
post1 = create_post(category: category)
|
||||
|
||||
# no limits on post creation or category creation please
|
||||
RateLimiter.enable
|
||||
|
||||
time = 1.month.from_now
|
||||
freeze_time time
|
||||
time = freeze_time 1.month.from_now
|
||||
|
||||
expect(category.auto_bump_topic!).to eq(false)
|
||||
expect(Topic.where(bumped_at: time).count).to eq(0)
|
||||
|
||||
@@ -125,13 +125,14 @@ describe Invite do
|
||||
end
|
||||
|
||||
it 'updates timestamp of existing invite' do
|
||||
freeze_time
|
||||
invite.update!(created_at: 10.days.ago)
|
||||
|
||||
resend_invite = Invite.invite_by_email(
|
||||
'iceking@adventuretime.ooo', inviter, topic
|
||||
)
|
||||
|
||||
expect(resend_invite.created_at).to be_within(1.minute).of(Time.zone.now)
|
||||
expect(resend_invite.created_at).to eq_time(Time.zone.now)
|
||||
end
|
||||
|
||||
it 'returns a new invite if the other has expired' do
|
||||
|
||||
@@ -729,7 +729,7 @@ describe PostAction do
|
||||
topic_status_update = TopicTimer.last
|
||||
|
||||
expect(topic_status_update.topic).to eq(topic)
|
||||
expect(topic_status_update.execute_at).to be_within(1.second).of(1.hour.from_now)
|
||||
expect(topic_status_update.execute_at).to eq_time(1.hour.from_now)
|
||||
expect(topic_status_update.status_type).to eq(TopicTimer.types[:open])
|
||||
end
|
||||
|
||||
@@ -772,7 +772,7 @@ describe PostAction do
|
||||
Jobs::ToggleTopicClosed.new.execute(topic_timer_id: timer.id, state: false)
|
||||
|
||||
expect(topic.reload.closed).to eq(true)
|
||||
expect(timer.reload.execute_at).to eq(1.hour.from_now)
|
||||
expect(timer.reload.execute_at).to eq_time(1.hour.from_now)
|
||||
|
||||
freeze_time timer.execute_at
|
||||
SiteSetting.num_flaggers_to_close_topic = 10
|
||||
|
||||
@@ -24,10 +24,12 @@ describe PostMover do
|
||||
|
||||
describe 'move_posts' do
|
||||
context 'topics' do
|
||||
before { freeze_time }
|
||||
|
||||
fab!(:user) { Fabricate(:user, admin: true) }
|
||||
fab!(:another_user) { evil_trout }
|
||||
fab!(:category) { Fabricate(:category, user: user) }
|
||||
fab!(:topic) { Fabricate(:topic, user: user) }
|
||||
fab!(:topic) { Fabricate(:topic, user: user, created_at: 4.hours.ago) }
|
||||
fab!(:p1) { Fabricate(:post, topic: topic, user: user, created_at: 3.hours.ago, reply_count: 2) }
|
||||
|
||||
fab!(:p2) do
|
||||
@@ -37,14 +39,15 @@ describe PostMover do
|
||||
user: another_user,
|
||||
raw: "Has a link to [evil trout](http://eviltrout.com) which is a cool site.",
|
||||
reply_to_post_number: p1.post_number,
|
||||
reply_count: 1
|
||||
reply_count: 1,
|
||||
created_at: 2.hours.ago
|
||||
)
|
||||
end
|
||||
|
||||
fab!(:p3) { Fabricate(:post, topic: topic, reply_to_post_number: p1.post_number, user: user) }
|
||||
fab!(:p4) { Fabricate(:post, topic: topic, reply_to_post_number: p2.post_number, user: user) }
|
||||
fab!(:p5) { Fabricate(:post) }
|
||||
let(:p6) { Fabricate(:post, topic: topic) }
|
||||
fab!(:p3) { Fabricate(:post, topic: topic, reply_to_post_number: p1.post_number, user: user, created_at: 1.hour.ago) }
|
||||
fab!(:p4) { Fabricate(:post, topic: topic, reply_to_post_number: p2.post_number, user: user, created_at: 45.minutes.ago) }
|
||||
fab!(:p5) { Fabricate(:post, created_at: 30.minutes.ago) }
|
||||
let(:p6) { Fabricate(:post, topic: topic, created_at: 15.minutes.ago) }
|
||||
|
||||
before do
|
||||
SiteSetting.tagging_enabled = true
|
||||
@@ -135,8 +138,6 @@ describe PostMover do
|
||||
before do
|
||||
TopicUser.update_last_read(user, topic.id, p4.post_number, p4.post_number, 0)
|
||||
TopicLink.extract_from(p2)
|
||||
|
||||
freeze_time Time.now
|
||||
end
|
||||
|
||||
def create_post_timing(post, user, msecs)
|
||||
@@ -216,7 +217,6 @@ describe PostMover do
|
||||
end
|
||||
|
||||
context "to a new topic" do
|
||||
|
||||
it "works correctly" do
|
||||
topic.expects(:add_moderator_post).once
|
||||
new_topic = topic.move_posts(user, [p2.id, p4.id], title: "new testing topic name", category_id: category.id, tags: ["tag1", "tag2"])
|
||||
@@ -238,8 +238,8 @@ describe PostMover do
|
||||
|
||||
p4.reload
|
||||
expect(new_topic.last_post_user_id).to eq(p4.user_id)
|
||||
expect(new_topic.last_posted_at).to eq(p4.created_at)
|
||||
expect(new_topic.bumped_at).to be_within(1.second).of(Time.now)
|
||||
expect(new_topic.last_posted_at).to eq_time(p4.created_at)
|
||||
expect(new_topic.bumped_at).to eq_time(Time.zone.now)
|
||||
|
||||
p2.reload
|
||||
expect(p2.sort_order).to eq(1)
|
||||
@@ -511,8 +511,8 @@ describe PostMover do
|
||||
expect(moved_to.category_id).to eq(SiteSetting.uncategorized_category_id)
|
||||
p4.reload
|
||||
expect(moved_to.last_post_user_id).to eq(p4.user_id)
|
||||
expect(moved_to.last_posted_at).to eq(p4.created_at)
|
||||
expect(moved_to.bumped_at).to be_within(1.second).of(Time.now)
|
||||
expect(moved_to.last_posted_at).to eq_time(p4.created_at)
|
||||
expect(moved_to.bumped_at).to eq_time(Time.zone.now)
|
||||
|
||||
# Posts should be re-ordered
|
||||
p2.reload
|
||||
@@ -822,8 +822,8 @@ describe PostMover do
|
||||
|
||||
p4.reload
|
||||
expect(new_topic.last_post_user_id).to eq(p4.user_id)
|
||||
expect(new_topic.last_posted_at).to eq(p4.created_at)
|
||||
expect(new_topic.bumped_at).to be_within(1.second).of(Time.now)
|
||||
expect(new_topic.last_posted_at).to eq_time(p4.created_at)
|
||||
expect(new_topic.bumped_at).to eq_time(Time.zone.now)
|
||||
|
||||
p2.reload
|
||||
expect(p2.sort_order).to eq(1)
|
||||
@@ -920,7 +920,6 @@ describe PostMover do
|
||||
end
|
||||
|
||||
context "moving the first post" do
|
||||
|
||||
it "copies the OP, doesn't delete it" do
|
||||
topic.expects(:add_moderator_post).once
|
||||
new_topic = topic.move_posts(user, [p1.id, p2.id], title: "new testing topic name")
|
||||
@@ -940,7 +939,7 @@ describe PostMover do
|
||||
# New first post
|
||||
new_first = new_topic.posts.where(post_number: 1).first
|
||||
expect(new_first.reply_count).to eq(1)
|
||||
expect(new_first.created_at).to be_within(1.second).of(p1.created_at)
|
||||
expect(new_first.created_at).to eq_time(p1.created_at)
|
||||
|
||||
# Second post is in a new topic
|
||||
p2.reload
|
||||
@@ -1074,13 +1073,12 @@ describe PostMover do
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
fab!(:another_user) { Fabricate(:user) }
|
||||
fab!(:regular_user) { Fabricate(:trust_level_4) }
|
||||
fab!(:topic) { Fabricate(:topic) }
|
||||
fab!(:personal_message) { Fabricate(:private_message_topic, user: evil_trout) }
|
||||
fab!(:p1) { Fabricate(:post, topic: personal_message, user: user) }
|
||||
fab!(:p2) { Fabricate(:post, topic: personal_message, reply_to_post_number: p1.post_number, user: another_user) }
|
||||
fab!(:p3) { Fabricate(:post, topic: personal_message, reply_to_post_number: p1.post_number, user: user) }
|
||||
fab!(:p4) { Fabricate(:post, topic: personal_message, reply_to_post_number: p2.post_number, user: user) }
|
||||
fab!(:p5) { Fabricate(:post, topic: personal_message, user: evil_trout) }
|
||||
fab!(:p1) { Fabricate(:post, topic: personal_message, user: user, created_at: 4.hours.ago) }
|
||||
fab!(:p2) { Fabricate(:post, topic: personal_message, reply_to_post_number: p1.post_number, user: another_user, created_at: 3.hours.ago) }
|
||||
fab!(:p3) { Fabricate(:post, topic: personal_message, reply_to_post_number: p1.post_number, user: user, created_at: 2.hours.ago) }
|
||||
fab!(:p4) { Fabricate(:post, topic: personal_message, reply_to_post_number: p2.post_number, user: user, created_at: 1.hour.ago) }
|
||||
fab!(:p5) { Fabricate(:post, topic: personal_message, user: evil_trout, created_at: 30.minutes.ago) }
|
||||
let(:another_personal_message) do
|
||||
Fabricate(:private_message_topic, user: user, topic_allowed_users: [
|
||||
Fabricate.build(:topic_allowed_user, user: admin)
|
||||
@@ -1181,6 +1179,8 @@ describe PostMover do
|
||||
end
|
||||
|
||||
it "does not allow moving regular topic posts in personal message" do
|
||||
topic = Fabricate(:topic, created_at: 4.hours.ago)
|
||||
|
||||
expect {
|
||||
personal_message.move_posts(admin, [p2.id, p5.id], destination_topic_id: topic.id)
|
||||
}.to raise_error(Discourse::InvalidParameters)
|
||||
|
||||
@@ -1132,7 +1132,7 @@ describe Post do
|
||||
SiteSetting.newuser_spam_host_threshold = 1
|
||||
SiteSetting.newuser_max_links = 3
|
||||
user = Fabricate(:user, staged: true, trust_level: 0)
|
||||
user.created_at = 1.day.ago
|
||||
user.created_at = 2.days.ago
|
||||
user.unstage
|
||||
post = Fabricate(:post, raw: raw, user: user)
|
||||
expect(post.has_host_spam?).to eq(false)
|
||||
@@ -1224,7 +1224,7 @@ describe Post do
|
||||
baked = post.baked_at
|
||||
Post.rebake_old(100)
|
||||
post.reload
|
||||
expect(post.baked_at).to eq(baked)
|
||||
expect(post.baked_at).to eq_time(baked)
|
||||
end
|
||||
|
||||
it "will rate limit globally" do
|
||||
@@ -1475,10 +1475,7 @@ describe Post do
|
||||
end
|
||||
|
||||
def updates_topic_updated_at
|
||||
|
||||
freeze_time 1.day.from_now
|
||||
time = Time.now
|
||||
|
||||
time = freeze_time 1.day.from_now
|
||||
result = yield
|
||||
|
||||
topic.reload
|
||||
|
||||
@@ -84,7 +84,6 @@ describe QuotedPost do
|
||||
quote.reload
|
||||
|
||||
expect(original_date).to eq_time(quote.created_at)
|
||||
|
||||
expect(post2.reply_quoted).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -103,7 +103,7 @@ describe RemoteTheme do
|
||||
expect(@theme.settings.length).to eq(1)
|
||||
expect(@theme.settings.first.value).to eq(true)
|
||||
|
||||
expect(remote.remote_updated_at).to eq(time)
|
||||
expect(remote.remote_updated_at).to eq_time(time)
|
||||
|
||||
scheme = ColorScheme.find_by(theme_id: @theme.id)
|
||||
expect(scheme.name).to eq("Amazing")
|
||||
@@ -150,7 +150,7 @@ describe RemoteTheme do
|
||||
expect(@theme.settings.length).to eq(1)
|
||||
expect(@theme.settings.first.value).to eq(32)
|
||||
|
||||
expect(remote.remote_updated_at).to eq(time)
|
||||
expect(remote.remote_updated_at).to eq_time(time)
|
||||
expect(remote.about_url).to eq("https://newsite.com/about")
|
||||
|
||||
# It should be able to remove old colors as well
|
||||
|
||||
@@ -80,9 +80,9 @@ describe ScreenedEmail do
|
||||
|
||||
shared_examples "when a ScreenedEmail record matches" do
|
||||
it "updates statistics" do
|
||||
freeze_time(Time.zone.now) do
|
||||
freeze_time do
|
||||
expect { subject }.to change { screened_email.reload.match_count }.by(1)
|
||||
expect(screened_email.last_match_at).to be_within_one_second_of(Time.zone.now)
|
||||
expect(screened_email.last_match_at).to eq_time(Time.zone.now)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -988,7 +988,7 @@ describe Topic do
|
||||
fab!(:topic) { Fabricate(:topic, bumped_at: 1.hour.ago) }
|
||||
|
||||
before do
|
||||
@original_bumped_at = topic.bumped_at.to_f
|
||||
@original_bumped_at = topic.bumped_at
|
||||
@user = topic.user
|
||||
@user.admin = true
|
||||
end
|
||||
@@ -1000,7 +1000,7 @@ describe Topic do
|
||||
topic.reload
|
||||
expect(topic).not_to be_visible
|
||||
expect(topic.moderator_posts_count).to eq(1)
|
||||
expect(topic.bumped_at.to_f).to be_within(1e-4).of(@original_bumped_at)
|
||||
expect(topic.bumped_at).to eq_time(@original_bumped_at)
|
||||
end
|
||||
|
||||
it 'removes itself as featured topic on user profiles' do
|
||||
@@ -1022,7 +1022,7 @@ describe Topic do
|
||||
it 'should be visible with correct counts' do
|
||||
expect(topic).to be_visible
|
||||
expect(topic.moderator_posts_count).to eq(1)
|
||||
expect(topic.bumped_at.to_f).to be_within(1e-4).of(@original_bumped_at)
|
||||
expect(topic.bumped_at).to eq_time(@original_bumped_at)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1037,7 +1037,7 @@ describe Topic do
|
||||
it "doesn't have a pinned_at but has correct dates" do
|
||||
expect(topic.pinned_at).to be_blank
|
||||
expect(topic.moderator_posts_count).to eq(1)
|
||||
expect(topic.bumped_at.to_f).to be_within(1e-4).of(@original_bumped_at)
|
||||
expect(topic.bumped_at).to eq_time(@original_bumped_at)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1050,7 +1050,7 @@ describe Topic do
|
||||
|
||||
it 'should enable correctly' do
|
||||
expect(topic.pinned_at).to be_present
|
||||
expect(topic.bumped_at.to_f).to be_within(1e-4).of(@original_bumped_at)
|
||||
expect(topic.bumped_at).to eq_time(@original_bumped_at)
|
||||
expect(topic.moderator_posts_count).to eq(1)
|
||||
end
|
||||
|
||||
@@ -1061,14 +1061,14 @@ describe Topic do
|
||||
context 'disable' do
|
||||
before do
|
||||
@archived_topic = Fabricate(:topic, archived: true, bumped_at: 1.hour.ago)
|
||||
@original_bumped_at = @archived_topic.bumped_at.to_f
|
||||
@original_bumped_at = @archived_topic.bumped_at
|
||||
@archived_topic.update_status('archived', false, @user)
|
||||
@archived_topic.reload
|
||||
end
|
||||
|
||||
it 'should archive correctly' do
|
||||
expect(@archived_topic).not_to be_archived
|
||||
expect(@archived_topic.bumped_at.to_f).to be_within(1e-4).of(@original_bumped_at)
|
||||
expect(@archived_topic.bumped_at).to eq_time(@original_bumped_at)
|
||||
expect(@archived_topic.moderator_posts_count).to eq(1)
|
||||
end
|
||||
end
|
||||
@@ -1083,7 +1083,7 @@ describe Topic do
|
||||
it 'should be archived' do
|
||||
expect(topic).to be_archived
|
||||
expect(topic.moderator_posts_count).to eq(1)
|
||||
expect(topic.bumped_at.to_f).to be_within(1e-4).of(@original_bumped_at)
|
||||
expect(topic.bumped_at).to eq_time(@original_bumped_at)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1092,7 +1092,7 @@ describe Topic do
|
||||
context 'disable' do
|
||||
before do
|
||||
@closed_topic = Fabricate(:topic, closed: true, bumped_at: 1.hour.ago)
|
||||
@original_bumped_at = @closed_topic.bumped_at.to_f
|
||||
@original_bumped_at = @closed_topic.bumped_at
|
||||
@closed_topic.update_status(status, false, @user)
|
||||
@closed_topic.reload
|
||||
end
|
||||
@@ -1100,7 +1100,7 @@ describe Topic do
|
||||
it 'should not be pinned' do
|
||||
expect(@closed_topic).not_to be_closed
|
||||
expect(@closed_topic.moderator_posts_count).to eq(1)
|
||||
expect(@closed_topic.bumped_at.to_f).not_to be_within(1e-4).of(@original_bumped_at)
|
||||
expect(@closed_topic.bumped_at).not_to eq_time(@original_bumped_at)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1113,7 +1113,7 @@ describe Topic do
|
||||
|
||||
it 'should be closed' do
|
||||
expect(topic).to be_closed
|
||||
expect(topic.bumped_at.to_f).to be_within(1e-4).of(@original_bumped_at)
|
||||
expect(topic.bumped_at).to eq_time(@original_bumped_at)
|
||||
expect(topic.moderator_posts_count).to eq(1)
|
||||
expect(topic.topic_timers.first).to eq(nil)
|
||||
end
|
||||
@@ -1473,6 +1473,8 @@ describe Topic do
|
||||
end
|
||||
|
||||
it 'should set a topic timer' do
|
||||
freeze_time
|
||||
|
||||
expect { topic.change_category_to_id(new_category.id) }
|
||||
.to change { TopicTimer.count }.by(1)
|
||||
|
||||
@@ -1481,7 +1483,7 @@ describe Topic do
|
||||
topic_timer = TopicTimer.last
|
||||
|
||||
expect(topic_timer.topic).to eq(topic)
|
||||
expect(topic_timer.execute_at).to be_within(1.second).of(Time.zone.now + 5.hours)
|
||||
expect(topic_timer.execute_at).to eq_time(5.hours.from_now)
|
||||
end
|
||||
|
||||
describe 'when topic is already closed' do
|
||||
@@ -1506,11 +1508,8 @@ describe Topic do
|
||||
topic.change_category_to_id(new_category.id)
|
||||
|
||||
expect(topic.reload.category).to eq(new_category)
|
||||
|
||||
expect(topic.public_topic_timer).to eq(topic_timer)
|
||||
|
||||
expect(topic.public_topic_timer.execute_at)
|
||||
.to be_within(1.second).of(topic_timer.execute_at)
|
||||
expect(topic.public_topic_timer.execute_at).to eq_time(topic_timer.execute_at)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1628,32 +1627,32 @@ describe Topic do
|
||||
freeze_time now
|
||||
|
||||
topic.set_or_create_timer(TopicTimer.types[:close], 72, by_user: admin)
|
||||
expect(topic.topic_timers.first.execute_at).to eq(3.days.from_now)
|
||||
expect(topic.topic_timers.first.execute_at).to eq_time(3.days.from_now)
|
||||
end
|
||||
|
||||
it 'can take a number of hours as a string' do
|
||||
freeze_time now
|
||||
topic.set_or_create_timer(TopicTimer.types[:close], '18', by_user: admin)
|
||||
expect(topic.topic_timers.first.execute_at).to eq(18.hours.from_now)
|
||||
expect(topic.topic_timers.first.execute_at).to eq_time(18.hours.from_now)
|
||||
end
|
||||
|
||||
it 'can take a number of hours as a string and can handle based on last post' do
|
||||
freeze_time now
|
||||
topic.set_or_create_timer(TopicTimer.types[:close], '18', by_user: admin, based_on_last_post: true)
|
||||
expect(topic.topic_timers.first.execute_at).to eq(18.hours.from_now)
|
||||
expect(topic.topic_timers.first.execute_at).to eq_time(18.hours.from_now)
|
||||
end
|
||||
|
||||
it "can take a timestamp for a future time" do
|
||||
freeze_time now
|
||||
topic.set_or_create_timer(TopicTimer.types[:close], '2013-11-22 5:00', by_user: admin)
|
||||
expect(topic.topic_timers.first.execute_at).to eq(Time.zone.local(2013, 11, 22, 5, 0))
|
||||
expect(topic.topic_timers.first.execute_at).to eq_time(Time.zone.local(2013, 11, 22, 5, 0))
|
||||
end
|
||||
|
||||
it "sets a validation error when given a timestamp in the past" do
|
||||
freeze_time now
|
||||
topic.set_or_create_timer(TopicTimer.types[:close], '2013-11-19 5:00', by_user: admin)
|
||||
|
||||
expect(topic.topic_timers.first.execute_at).to eq(Time.zone.local(2013, 11, 19, 5, 0))
|
||||
expect(topic.topic_timers.first.execute_at).to eq_time(Time.zone.local(2013, 11, 19, 5, 0))
|
||||
expect(topic.topic_timers.first.errors[:execute_at]).to be_present
|
||||
end
|
||||
|
||||
@@ -1672,7 +1671,7 @@ describe Topic do
|
||||
it "can take a timestamp with timezone" do
|
||||
freeze_time now
|
||||
topic.set_or_create_timer(TopicTimer.types[:close], '2013-11-25T01:35:00-08:00', by_user: admin)
|
||||
expect(topic.topic_timers.first.execute_at).to eq(Time.utc(2013, 11, 25, 9, 35))
|
||||
expect(topic.topic_timers.first.execute_at).to eq_time(Time.utc(2013, 11, 25, 9, 35))
|
||||
end
|
||||
|
||||
it 'sets topic status update user to given user if it is a staff or TL4 user' do
|
||||
@@ -1716,7 +1715,7 @@ describe Topic do
|
||||
it 'updates topic status update execute_at if it was already set to close' do
|
||||
freeze_time now
|
||||
closing_topic.set_or_create_timer(TopicTimer.types[:close], 48)
|
||||
expect(closing_topic.reload.public_topic_timer.execute_at).to eq(2.days.from_now)
|
||||
expect(closing_topic.reload.public_topic_timer.execute_at).to eq_time(2.days.from_now)
|
||||
end
|
||||
|
||||
it 'should not delete topic_timer of another status_type' do
|
||||
@@ -1735,7 +1734,7 @@ describe Topic do
|
||||
TopicTimer.types[:publish_to_category], 72, by_user: admin
|
||||
)
|
||||
|
||||
expect(topic_timer.execute_at).to eq(3.days.from_now)
|
||||
expect(topic_timer.execute_at).to eq_time(3.days.from_now)
|
||||
end
|
||||
|
||||
it "does not update topic's topic status created_at it was already set to close" do
|
||||
@@ -1792,7 +1791,7 @@ describe Topic do
|
||||
topic.set_or_create_timer(TopicTimer.types[:reminder], 11, by_user: admin)
|
||||
}.to_not change { TopicTimer.count }
|
||||
reminder.reload
|
||||
expect(reminder.execute_at).to eq(11.hours.from_now)
|
||||
expect(reminder.execute_at).to eq_time(11.hours.from_now)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1810,19 +1809,19 @@ describe Topic do
|
||||
end
|
||||
|
||||
it "doesn't return category topics" do
|
||||
Fabricate(:category_with_definition)
|
||||
Fabricate(:category_with_definition, created_at: 1.minute.ago)
|
||||
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to be_blank
|
||||
end
|
||||
|
||||
it "returns regular topics" do
|
||||
topic = Fabricate(:topic)
|
||||
topic = Fabricate(:topic, created_at: 1.minute.ago)
|
||||
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to eq([topic])
|
||||
end
|
||||
|
||||
it "doesn't return topics from muted categories" do
|
||||
user = Fabricate(:user)
|
||||
category = Fabricate(:category_with_definition)
|
||||
Fabricate(:topic, category: category)
|
||||
category = Fabricate(:category_with_definition, created_at: 2.minutes.ago)
|
||||
Fabricate(:topic, category: category, created_at: 1.minute.ago)
|
||||
|
||||
CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[:muted], category.id)
|
||||
|
||||
@@ -1830,7 +1829,7 @@ describe Topic do
|
||||
end
|
||||
|
||||
it "doesn't return topics that a user has muted" do
|
||||
topic = Fabricate(:topic)
|
||||
topic = Fabricate(:topic, created_at: 1.minute.ago)
|
||||
user = Fabricate(:user)
|
||||
|
||||
Fabricate(:topic_user,
|
||||
@@ -1844,8 +1843,8 @@ describe Topic do
|
||||
|
||||
it "doesn't return topics from suppressed categories" do
|
||||
user = Fabricate(:user)
|
||||
category = Fabricate(:category_with_definition)
|
||||
Fabricate(:topic, category: category)
|
||||
category = Fabricate(:category_with_definition, created_at: 2.minutes.ago)
|
||||
Fabricate(:topic, category: category, created_at: 1.minute.ago)
|
||||
|
||||
SiteSetting.digest_suppress_categories = "#{category.id}"
|
||||
|
||||
@@ -1854,20 +1853,20 @@ describe Topic do
|
||||
|
||||
it "doesn't return topics from TL0 users" do
|
||||
new_user = Fabricate(:user, trust_level: 0)
|
||||
Fabricate(:topic, user: new_user)
|
||||
Fabricate(:topic, user: new_user, created_at: 1.minute.ago)
|
||||
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to be_blank
|
||||
end
|
||||
|
||||
it "returns topics from TL0 users if given include_tl0" do
|
||||
new_user = Fabricate(:user, trust_level: 0)
|
||||
topic = Fabricate(:topic, user_id: new_user.id)
|
||||
topic = Fabricate(:topic, user_id: new_user.id, created_at: 1.minute.ago)
|
||||
|
||||
expect(Topic.for_digest(user, 1.year.ago, top_order: true, include_tl0: true)).to eq([topic])
|
||||
end
|
||||
|
||||
it "returns topics from TL0 users if enabled in preferences" do
|
||||
new_user = Fabricate(:user, trust_level: 0)
|
||||
topic = Fabricate(:topic, user: new_user)
|
||||
topic = Fabricate(:topic, user: new_user, created_at: 1.minute.ago)
|
||||
|
||||
u = Fabricate(:user)
|
||||
u.user_option.include_tl0_in_digests = true
|
||||
@@ -1879,7 +1878,7 @@ describe Topic do
|
||||
user = Fabricate(:user)
|
||||
tag = Fabricate(:tag)
|
||||
TagUser.change(user.id, tag.id, TagUser.notification_levels[:muted])
|
||||
Fabricate(:topic, tags: [tag])
|
||||
Fabricate(:topic, tags: [tag], created_at: 1.minute.ago)
|
||||
|
||||
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to be_blank
|
||||
end
|
||||
@@ -1888,7 +1887,7 @@ describe Topic do
|
||||
user = Fabricate(:user)
|
||||
muted_tag, other_tag = Fabricate(:tag), Fabricate(:tag)
|
||||
TagUser.change(user.id, muted_tag.id, TagUser.notification_levels[:muted])
|
||||
topic = Fabricate(:topic, tags: [muted_tag, other_tag])
|
||||
topic = Fabricate(:topic, tags: [muted_tag, other_tag], created_at: 1.minute.ago)
|
||||
|
||||
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to eq([topic])
|
||||
end
|
||||
@@ -1897,32 +1896,34 @@ describe Topic do
|
||||
user = Fabricate(:user)
|
||||
muted_tag = Fabricate(:tag)
|
||||
TagUser.change(user.id, muted_tag.id, TagUser.notification_levels[:muted])
|
||||
_topic1 = Fabricate(:topic, tags: [muted_tag])
|
||||
topic2 = Fabricate(:topic, tags: [Fabricate(:tag), Fabricate(:tag)])
|
||||
topic3 = Fabricate(:topic)
|
||||
|
||||
_topic1 = Fabricate(:topic, tags: [muted_tag], created_at: 1.minute.ago)
|
||||
topic2 = Fabricate(:topic, tags: [Fabricate(:tag), Fabricate(:tag)], created_at: 1.minute.ago)
|
||||
topic3 = Fabricate(:topic, created_at: 1.minute.ago)
|
||||
topics = Topic.for_digest(user, 1.year.ago, top_order: true)
|
||||
|
||||
expect(topics.size).to eq(2)
|
||||
expect(topics).to contain_exactly(topic2, topic3)
|
||||
end
|
||||
|
||||
it "sorts by category notification levels" do
|
||||
category1, category2 = Fabricate(:category_with_definition), Fabricate(:category_with_definition)
|
||||
2.times { |i| Fabricate(:topic, category: category1) }
|
||||
topic1 = Fabricate(:topic, category: category2)
|
||||
2.times { |i| Fabricate(:topic, category: category1) }
|
||||
category1, category2 = Fabricate(:category_with_definition), Fabricate(:category_with_definition, created_at: 2.minutes.ago)
|
||||
2.times { |i| Fabricate(:topic, category: category1, created_at: 1.minute.ago) }
|
||||
topic1 = Fabricate(:topic, category: category2, created_at: 1.minute.ago)
|
||||
2.times { |i| Fabricate(:topic, category: category1, created_at: 1.minute.ago) }
|
||||
CategoryUser.create(user: user, category: category2, notification_level: CategoryUser.notification_levels[:watching])
|
||||
for_digest = Topic.for_digest(user, 1.year.ago, top_order: true)
|
||||
|
||||
expect(for_digest.first).to eq(topic1)
|
||||
end
|
||||
|
||||
it "sorts by topic notification levels" do
|
||||
topics = []
|
||||
3.times { |i| topics << Fabricate(:topic) }
|
||||
3.times { |i| topics << Fabricate(:topic, created_at: 1.minute.ago) }
|
||||
user = Fabricate(:user)
|
||||
TopicUser.create(user_id: user.id, topic_id: topics[0].id, notification_level: TopicUser.notification_levels[:tracking])
|
||||
TopicUser.create(user_id: user.id, topic_id: topics[2].id, notification_level: TopicUser.notification_levels[:watching])
|
||||
for_digest = Topic.for_digest(user, 1.year.ago, top_order: true).pluck(:id)
|
||||
|
||||
expect(for_digest).to eq([topics[2].id, topics[0].id, topics[1].id])
|
||||
end
|
||||
end
|
||||
@@ -2090,7 +2091,7 @@ describe Topic do
|
||||
topic_embed = TopicEmbed.create!(topic_id: topic.id, embed_url: "https://blog.codinghorror.com/password-rules-are-bullshit", post_id: post.id, deleted_at: 1.day.ago)
|
||||
topic.recover!
|
||||
topic_embed.reload
|
||||
expect(topic_embed.deleted_at).to eq(nil)
|
||||
expect(topic_embed.deleted_at).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2256,7 +2257,6 @@ describe Topic do
|
||||
end
|
||||
|
||||
it 'allows users to normalize counts' do
|
||||
|
||||
topic = Fabricate(:topic, last_posted_at: 1.year.ago)
|
||||
post1 = Fabricate(:post, topic: topic, post_number: 1)
|
||||
post2 = Fabricate(:post, topic: topic, post_type: Post.types[:whisper], post_number: 2)
|
||||
@@ -2267,7 +2267,7 @@ describe Topic do
|
||||
expect(topic.posts_count).to eq(1)
|
||||
expect(topic.highest_post_number).to eq(post1.post_number)
|
||||
expect(topic.highest_staff_post_number).to eq(post2.post_number)
|
||||
expect(topic.last_posted_at).to be_within(1.second).of (post1.created_at)
|
||||
expect(topic.last_posted_at).to eq_time(post1.created_at)
|
||||
end
|
||||
|
||||
context 'featured link' do
|
||||
|
||||
@@ -458,6 +458,7 @@ describe TopicTrackingState do
|
||||
end
|
||||
|
||||
it "correctly handles seen categories" do
|
||||
freeze_time 1.minute.ago
|
||||
user = Fabricate(:user)
|
||||
post
|
||||
|
||||
@@ -473,6 +474,7 @@ describe TopicTrackingState do
|
||||
report = TopicTrackingState.report(user)
|
||||
expect(report.length).to eq(0)
|
||||
|
||||
unfreeze_time
|
||||
post.topic.touch(:created_at)
|
||||
|
||||
report = TopicTrackingState.report(user)
|
||||
|
||||
@@ -226,22 +226,19 @@ describe UserAction do
|
||||
end
|
||||
|
||||
describe 'when a user posts a new topic' do
|
||||
def process_alerts(post)
|
||||
PostAlerter.post_created(post)
|
||||
end
|
||||
|
||||
before do
|
||||
@post = create_post(created_at: DateTime.now - 100)
|
||||
process_alerts(@post)
|
||||
freeze_time(100.days.ago) do
|
||||
@post = create_post
|
||||
PostAlerter.post_created(@post)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'topic action' do
|
||||
before do
|
||||
@action = @post.user.user_actions.find_by(action_type: UserAction::NEW_TOPIC)
|
||||
end
|
||||
it 'should exist' do
|
||||
@action = @post.user.user_actions.find_by(action_type: UserAction::NEW_TOPIC)
|
||||
|
||||
expect(@action).not_to eq(nil)
|
||||
expect(@action.created_at).to be_within(1).of(@post.topic.created_at)
|
||||
expect(@action.created_at).to eq_time(@post.topic.created_at)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -256,7 +253,7 @@ describe UserAction do
|
||||
|
||||
@response = PostCreator.new(@other_user, reply_to_post_number: 1, topic_id: @post.topic_id, raw: "perhaps @#{@mentioned.username} knows how this works?").create
|
||||
|
||||
process_alerts(@response)
|
||||
PostAlerter.post_created(@response)
|
||||
end
|
||||
|
||||
it 'should log user actions correctly' do
|
||||
|
||||
@@ -83,23 +83,21 @@ describe UserAuthToken do
|
||||
end
|
||||
|
||||
it "expires correctly" do
|
||||
|
||||
freeze_time Time.zone.now
|
||||
user = Fabricate(:user)
|
||||
|
||||
user_token = UserAuthToken.generate!(user_id: user.id,
|
||||
user_agent: "some user agent 2",
|
||||
client_ip: "1.1.2.3")
|
||||
|
||||
UserAuthToken.lookup(user_token.unhashed_auth_token, seen: true)
|
||||
|
||||
freeze_time (SiteSetting.maximum_session_age.hours - 1).from_now
|
||||
freeze_time SiteSetting.maximum_session_age.hours.from_now - 1.second
|
||||
|
||||
user_token.reload
|
||||
|
||||
user_token.rotate!
|
||||
UserAuthToken.lookup(user_token.unhashed_auth_token, seen: true)
|
||||
|
||||
freeze_time (SiteSetting.maximum_session_age.hours - 1).from_now
|
||||
freeze_time SiteSetting.maximum_session_age.hours.from_now - 1.second
|
||||
|
||||
still_good = UserAuthToken.lookup(user_token.unhashed_auth_token, seen: true)
|
||||
expect(still_good).not_to eq(nil)
|
||||
@@ -111,7 +109,7 @@ describe UserAuthToken do
|
||||
end
|
||||
|
||||
it "can properly rotate tokens" do
|
||||
|
||||
freeze_time 3.days.ago
|
||||
user = Fabricate(:user)
|
||||
|
||||
user_token = UserAuthToken.generate!(user_id: user.id,
|
||||
@@ -126,12 +124,14 @@ describe UserAuthToken do
|
||||
|
||||
user_token.update_columns(auth_token_seen: true)
|
||||
|
||||
rotation_time = freeze_time 1.day.from_now
|
||||
|
||||
rotated = user_token.rotate!(user_agent: "a new user agent", client_ip: "1.1.2.4")
|
||||
expect(rotated).to eq(true)
|
||||
|
||||
user_token.reload
|
||||
|
||||
expect(user_token.rotated_at).to be_within(5.second).of(Time.zone.now)
|
||||
expect(user_token.rotated_at).to eq_time(rotation_time)
|
||||
expect(user_token.client_ip).to eq("1.1.2.4")
|
||||
expect(user_token.user_agent).to eq("a new user agent")
|
||||
expect(user_token.auth_token_seen).to eq(false)
|
||||
@@ -139,17 +139,17 @@ describe UserAuthToken do
|
||||
expect(user_token.prev_auth_token).to eq(prev_auth_token)
|
||||
|
||||
# ability to auth using an old token
|
||||
freeze_time
|
||||
seen_at = freeze_time 1.day.from_now
|
||||
|
||||
looked_up = UserAuthToken.lookup(user_token.unhashed_auth_token, seen: true)
|
||||
expect(looked_up.id).to eq(user_token.id)
|
||||
expect(looked_up.auth_token_seen).to eq(true)
|
||||
expect(looked_up.seen_at).to be_within(1.second).of(Time.zone.now)
|
||||
expect(looked_up.seen_at).to eq_time(seen_at)
|
||||
|
||||
looked_up = UserAuthToken.lookup(unhashed_prev, seen: true)
|
||||
expect(looked_up.id).to eq(user_token.id)
|
||||
|
||||
freeze_time(2.minute.from_now)
|
||||
freeze_time 2.minutes.from_now
|
||||
|
||||
looked_up = UserAuthToken.lookup(unhashed_prev)
|
||||
expect(looked_up).not_to eq(nil)
|
||||
|
||||
@@ -863,7 +863,6 @@ describe User do
|
||||
end
|
||||
|
||||
describe "previous_visit_at" do
|
||||
|
||||
let(:user) { Fabricate(:user) }
|
||||
let!(:first_visit_date) { Time.zone.now }
|
||||
let!(:second_visit_date) { 2.hours.from_now }
|
||||
@@ -889,36 +888,36 @@ describe User do
|
||||
# second visit
|
||||
user.update_last_seen!(second_visit_date)
|
||||
user.reload
|
||||
expect(user.previous_visit_at).to be_within_one_second_of(first_visit_date)
|
||||
expect(user.previous_visit_at).to eq_time(first_visit_date)
|
||||
|
||||
# third visit
|
||||
user.update_last_seen!(third_visit_date)
|
||||
user.reload
|
||||
expect(user.previous_visit_at).to be_within_one_second_of(second_visit_date)
|
||||
expect(user.previous_visit_at).to eq_time(second_visit_date)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "update_last_seen!" do
|
||||
let (:user) { Fabricate(:user) }
|
||||
let(:user) { Fabricate(:user) }
|
||||
let!(:first_visit_date) { Time.zone.now }
|
||||
let!(:second_visit_date) { 2.hours.from_now }
|
||||
|
||||
it "should update the last seen value" do
|
||||
expect(user.last_seen_at).to eq nil
|
||||
user.update_last_seen!(first_visit_date)
|
||||
expect(user.reload.last_seen_at).to be_within_one_second_of(first_visit_date)
|
||||
expect(user.reload.last_seen_at).to eq_time(first_visit_date)
|
||||
end
|
||||
|
||||
it "should update the first seen value if it doesn't exist" do
|
||||
user.update_last_seen!(first_visit_date)
|
||||
expect(user.reload.first_seen_at).to be_within_one_second_of(first_visit_date)
|
||||
expect(user.reload.first_seen_at).to eq_time(first_visit_date)
|
||||
end
|
||||
|
||||
it "should not update the first seen value if it doesn't exist" do
|
||||
user.update_last_seen!(first_visit_date)
|
||||
user.update_last_seen!(second_visit_date)
|
||||
expect(user.reload.first_seen_at).to be_within_one_second_of(first_visit_date)
|
||||
expect(user.reload.first_seen_at).to eq_time(first_visit_date)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -976,58 +975,51 @@ describe User do
|
||||
end
|
||||
|
||||
describe 'with no previous values' do
|
||||
let!(:date) { Time.zone.now }
|
||||
|
||||
before do
|
||||
freeze_time date
|
||||
user.update_last_seen!
|
||||
end
|
||||
|
||||
after do
|
||||
Discourse.redis.flushall
|
||||
end
|
||||
|
||||
it "updates last_seen_at" do
|
||||
expect(user.last_seen_at).to be_within_one_second_of(date)
|
||||
date = freeze_time
|
||||
user.update_last_seen!
|
||||
|
||||
expect(user.last_seen_at).to eq_time(date)
|
||||
end
|
||||
|
||||
it "should have 0 for days_visited" do
|
||||
user.update_last_seen!
|
||||
user.reload
|
||||
|
||||
expect(user.user_stat.days_visited).to eq(1)
|
||||
end
|
||||
|
||||
it "should log a user_visit with the date" do
|
||||
expect(user.user_visits.first.visited_at).to eq(date.to_date)
|
||||
date = freeze_time
|
||||
user.update_last_seen!
|
||||
|
||||
expect(user.user_visits.first.visited_at).to eq_time(date.to_date)
|
||||
end
|
||||
|
||||
context "called twice" do
|
||||
|
||||
before do
|
||||
freeze_time date
|
||||
it "doesn't increase days_visited twice" do
|
||||
freeze_time
|
||||
user.update_last_seen!
|
||||
user.update_last_seen!
|
||||
user.reload
|
||||
end
|
||||
|
||||
it "doesn't increase days_visited twice" do
|
||||
expect(user.user_stat.days_visited).to eq(1)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "after 3 days" do
|
||||
let!(:future_date) { 3.days.from_now }
|
||||
|
||||
before do
|
||||
freeze_time future_date
|
||||
user.update_last_seen!
|
||||
end
|
||||
|
||||
it "should log a second visited_at record when we log an update later" do
|
||||
user.update_last_seen!
|
||||
future_date = freeze_time(3.days.from_now)
|
||||
user.update_last_seen!
|
||||
|
||||
expect(user.user_visits.count).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ describe UserStat do
|
||||
UserStat.ensure_consistency!
|
||||
|
||||
post.user.user_stat.reload
|
||||
expect(post.user.user_stat.first_unread_at).to eq_time(Time.now)
|
||||
expect(post.user.user_stat.first_unread_at).to eq_time(Time.zone.now)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ describe WatchedWord do
|
||||
expect {
|
||||
w = described_class.create_or_update_word(word: existing.word, action_key: :flag)
|
||||
expect(w.id).to eq(existing.id)
|
||||
expect(w.updated_at).to eq(w.updated_at)
|
||||
expect(w.updated_at).to eq_time(w.updated_at)
|
||||
}.to_not change { described_class.count }
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user