Files
growstuff/spec/tasks/members_spec.rb
Daniel O'Connor 8e7dd25e98 Add rake task to cleanup inactive members (#4574)
* Add members:cleanup_inactive rake task

This task identifies and deletes members who have not logged in for over
24 months and have no gardens, plantings, or other activity (posts,
comments, seeds, harvests, etc).

Includes support for DRY_RUN=true to preview deletions.
Added tests in spec/tasks/members_spec.rb.

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>

* Refactor activity check to Member#has_activity? and update rake task

- Added `Member#has_activity?` to encapsulate the check for gardens, plantings, and other activity.
- Updated `members:cleanup_inactive` rake task to use `Member#has_activity?`.
- Maintained `DRY_RUN` support and existing tests.

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>

* Apply suggestion from @CloCkWeRX

* Apply suggestions from code review

Co-authored-by: Daniel O'Connor <daniel.oconnor@gmail.com>

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-04-27 01:40:54 +09:30

69 lines
1.9 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
require 'rake'
describe 'members:cleanup_inactive' do
before :all do
Rails.application.load_tasks
end
let(:cleanup_task) { Rake::Task['members:cleanup_inactive'] }
before do
cleanup_task.reenable
end
it "deletes inactive members with no gardens and no other activity" do
inactive_no_activity = create(:member, last_sign_in_at: 25.months.ago)
# We must explicitly remove the default garden to test the "no gardens" condition
inactive_no_activity.gardens.destroy_all
expect(inactive_no_activity.gardens.count).to eq(0)
cleanup_task.invoke
expect(Member.exists?(inactive_no_activity.id)).to be_falsey
end
it "does not delete inactive members with a garden (even if empty)" do
inactive_with_garden = create(:member, last_sign_in_at: 25.months.ago)
# They have 1 default garden
expect(inactive_with_garden.gardens.count).to eq(1)
cleanup_task.invoke
expect(Member.exists?(inactive_with_garden.id)).to be_truthy
end
it "does not delete members with recent login" do
recent_member = create(:member, last_sign_in_at: 1.month.ago)
recent_member.gardens.destroy_all
cleanup_task.invoke
expect(Member.exists?(recent_member.id)).to be_truthy
end
it "does not delete inactive members with activity (posts)" do
inactive_with_post = create(:member, last_sign_in_at: 25.months.ago)
inactive_with_post.gardens.destroy_all
create(:post, author: inactive_with_post)
cleanup_task.invoke
expect(Member.exists?(inactive_with_post.id)).to be_truthy
end
it "honors DRY_RUN environment variable" do
inactive_no_activity = create(:member, last_sign_in_at: 25.months.ago)
inactive_no_activity.gardens.destroy_all
ENV['DRY_RUN'] = 'true'
cleanup_task.invoke
ENV['DRY_RUN'] = nil
expect(Member.exists?(inactive_no_activity.id)).to be_truthy
end
end