Allow published_at to be within 15 minutes, not 15 hours (#18345)

* Allow published_at to be within 15 minutes, not 15 hours

* Fixed creating past articles in the specs

* Fixed one more spec with a past article

* Use ActiveSupport's helper instead of computation

Co-authored-by: Fernando Valverde <fernando@fdo.cr>

Co-authored-by: Fernando Valverde <fernando@fdo.cr>
This commit is contained in:
Anna Buianova 2022-08-22 12:48:40 +03:00 committed by GitHub
parent 2ad02cf31c
commit a4ffb7cac3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 8 deletions

View file

@ -817,7 +817,7 @@ class Article < ApplicationRecord
def future_or_current_published_at
# allow published_at in the future or within 15 minutes in the past
return if !published || published_at > Time.current - (15 * 60 * 60)
return if !published || published_at > 15.minutes.ago
errors.add(:published_at, I18n.t("models.article.future_or_current_published_at"))
end

View file

@ -500,6 +500,18 @@ RSpec.describe Article, type: :model do
.to include("only future or current published_at allowed when publishing an article")
end
it "doesn't allow recent published_at when publishing on create" do
article2 = build(:article, published_at: 1.hour.ago, published: true)
expect(article2.valid?).to be false
expect(article2.errors[:published_at])
.to include("only future or current published_at allowed when publishing an article")
end
it "allows recent published_at when publishing on create" do
article2 = build(:article, published_at: 5.minutes.ago, published: true)
expect(article2.valid?).to be true
end
it "doesn't allow updating published_at if an article has already been published" do
article.published_at = (Date.current + 10.days).strftime("%d/%m/%Y %H:%M")
expect(article.valid?).to be false
@ -789,15 +801,15 @@ RSpec.describe Article, type: :model do
describe ".active_help" do
it "returns properly filtered articles under the 'help' tag" do
filtered_article = create(:article, user: user, tags: "help",
published_at: 13.hours.ago, comments_count: 5, score: -3)
filtered_article = create(:article, :past, user: user, tags: "help",
past_published_at: 13.hours.ago, comments_count: 5, score: -3)
articles = described_class.active_help
expect(articles).to include(filtered_article)
end
it "returns any published articles tagged with 'help' when there are no articles that fit the criteria" do
unfiltered_article = create(:article, user: user, tags: "help",
published_at: 10.hours.ago, comments_count: 8, score: -5)
unfiltered_article = create(:article, :past, user: user, tags: "help",
past_published_at: 10.hours.ago, comments_count: 8, score: -5)
articles = described_class.active_help
expect(articles).to include(unfiltered_article)
end

View file

@ -146,7 +146,7 @@ RSpec.describe Articles::Feeds::LargeForemExperimental, type: :service do
end
describe ".globally_hot_articles" do
let!(:recently_published_article) { create(:article, published_at: 3.hours.ago) }
let!(:recently_published_article) { create(:article, :past, past_published_at: 3.hours.ago) }
let(:globally_hot_articles) { feed.globally_hot_articles(true).second }
it "returns hot recent stories" do
@ -157,7 +157,7 @@ RSpec.describe Articles::Feeds::LargeForemExperimental, type: :service do
context "when low number of hot stories and no recently published articles" do
before do
Article.delete_all
create(:article, hotness_score: 1000, score: 1000, published_at: 3.hours.ago)
create(:article, :past, hotness_score: 1000, score: 1000, past_published_at: 3.hours.ago)
end
# This test handles a situation in which there are a low number of hot or new stories, and the user is logged in.

View file

@ -4,7 +4,7 @@ RSpec.describe Articles::Feeds::LargeForemExperimental, type: :system, js: true
let(:user) { create(:user) }
let(:second_user) { create(:user) }
let!(:hot_story) do
create(:article, hotness_score: 1000, score: 1000, published_at: 3.hours.ago, user_id: second_user.id)
create(:article, :past, hotness_score: 1000, score: 1000, past_published_at: 3.hours.ago, user_id: second_user.id)
end
before do