FIX: When checking policy acceptance looking at wrong date

FIX: When checking policy acceptance looking at wrong date

I mistakenly looked at policy creation date in the delete query instead of the policy acceptance date.

diff --git a/jobs/scheduled/check_policy.rb b/jobs/scheduled/check_policy.rb
index 0707b71..4c1dab7 100644
--- a/jobs/scheduled/check_policy.rb
+++ b/jobs/scheduled/check_policy.rb
@@ -61,12 +61,12 @@ module Jobs
       end
 
       sql = <<~SQL
-      DELETE FROM post_custom_fields p
+      DELETE FROM post_custom_fields f
       USING post_policies pp
-      WHERE p.post_id = pp.post_id AND
+      WHERE f.post_id = pp.post_id AND
         pp.renew_start IS NULL AND
-        p.name = :accepted_by AND
-        pp.created_at < :now::timestamp - ( INTERVAL '1 day' *  pp.renew_days )
+        f.name = :accepted_by AND
+        f.created_at < :now::timestamp - ( INTERVAL '1 day' *  pp.renew_days )
       SQL
 
       DB.exec(
diff --git a/spec/lib/check_policy_spec.rb b/spec/lib/check_policy_spec.rb
index f755260..c1a6f96 100644
--- a/spec/lib/check_policy_spec.rb
+++ b/spec/lib/check_policy_spec.rb
@@ -23,6 +23,43 @@ describe DiscoursePolicy::CheckPolicy do
     group
   end
 
+  def accept_policy(post)
+    [user1, user2].each do |u|
+      PostCustomField.create!(
+        post_id: post.id,
+        name: DiscoursePolicy::AcceptedBy,
+        value: u.id
+      )
+    end
+  end
+
+  it "correctly renews policise with no renew-start" do
+    freeze_time Time.utc(2019)
+
+    raw = <<~MD
+     [policy group=#{group.name} renew=400]
+     I always open **doors**!
+     [/policy]
+    MD
+
+    post = create_post(raw: raw, user: Fabricate(:admin))
+
+    freeze_time Time.utc(2021)
+    accept_policy(post)
+
+    freeze_time Time.utc(2022)
+    DiscoursePolicy::CheckPolicy.new.execute
+
+    post.reload
+    expect(post.custom_fields[DiscoursePolicy::AcceptedBy].length).to eq(2)
+
+    freeze_time Time.utc(2023)
+    DiscoursePolicy::CheckPolicy.new.execute
+
+    post.reload
+    expect(post.custom_fields[DiscoursePolicy::AcceptedBy]).to eq(nil)
+  end
+
   it "correctly renews policies" do
 
     freeze_time Time.utc(2019)
@@ -35,13 +72,7 @@ describe DiscoursePolicy::CheckPolicy do
 
     post = create_post(raw: raw, user: Fabricate(:admin))
 
-    [user1, user2].each do |u|
-      PostCustomField.create!(
-        post_id: post.id,
-        name: DiscoursePolicy::AcceptedBy,
-        value: u.id
-      )
-    end
+    accept_policy(post)
 
     freeze_time Time.utc(2020)
     DiscoursePolicy::CheckPolicy.new.execute
@@ -57,13 +88,7 @@ describe DiscoursePolicy::CheckPolicy do
     post.reload
     expect(post.custom_fields[DiscoursePolicy::AcceptedBy]).to eq(nil)
 
-    [user1, user2].each do |u|
-      PostCustomField.create!(
-        post_id: post.id,
-        name: DiscoursePolicy::AcceptedBy,
-        value: u.id
-      )
-    end
+    accept_policy(post)
 
     freeze_time (Time.utc(2020, 10, 17) + 101.days)

GitHub sha: a472ed6d

1 Like