Skip to content

Commit

Permalink
Bring test coverage of support.rake to ~100%
Browse files Browse the repository at this point in the history
  • Loading branch information
deborahchua committed Jan 24, 2025
1 parent 59c08ce commit f609a05
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/tasks/support.rake
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace :support do
active_subscription = Subscription.active.find_by(subscriber_list:, subscriber:)
if active_subscription
active_subscription.end(reason: :unsubscribed)
puts "Unsubscribing from #{email_address} from #{subscriber_list_slug}"
puts "Unsubscribing #{email_address} from #{subscriber_list_slug}"
else
puts "Subscriber #{email_address} already unsubscribed from #{subscriber_list_slug}"
end
Expand Down
139 changes: 135 additions & 4 deletions spec/lib/tasks/support_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
RSpec.describe "support" do
after(:each) do
Rake::Task["support:emails:stats_for_content_id"].reenable
end

describe "stats_for_content_id" do
after(:each) do
Rake::Task["support:emails:stats_for_content_id"].reenable
end

context "with invalid dates" do
it "outputs all subscriptions for a subscriber" do
expect { Rake::Task["support:emails:stats_for_content_id"].invoke(SecureRandom.uuid, "bad_date", "bad_date") }
Expand Down Expand Up @@ -141,4 +141,135 @@
.to output(/Subscriber unsubscribed@test.com already unsubscribed from another-list/).to_stdout
end
end

describe "unsubscribe_single_subscription" do
before do
@subscriber_list = create(:subscriber_list, slug: "my-list", title: "My List")
end

after(:each) do
Rake::Task["support:unsubscribe_single_subscription"].reenable
end

it "displays error message if subscriber is not found" do
expect { Rake::Task["support:unsubscribe_single_subscription"].invoke("test@example.com", "my-list") }
.to output(/Subscriber test@example.com not found/).to_stdout
end

it "displays error message if subscriber list is not found but subscriber exists" do
create(:subscriber, address: "test-1@example.com")

expect { Rake::Task["support:unsubscribe_single_subscription"].invoke("test-1@example.com", "invalid-subscription-list") }
.to output(/Subscriber list invalid-subscription-list not found/).to_stdout
end

it "displays error message if subscriber is not subscribed to subscriber list" do
create(:subscriber, address: "notsubscribed@example.com")

expect { Rake::Task["support:unsubscribe_single_subscription"].invoke("notsubscribed@example.com", "my-list") }
.to output(/Subscriber notsubscribed@example.com does not appear to be signed up for my-list/).to_stdout
end

it "successfully unsubscribes an active subscriber" do
subscriber = create(:subscriber, address: "subscribed@example.com")
active_subscription = create(:subscription, subscriber_list: @subscriber_list, subscriber:)

expect { Rake::Task["support:unsubscribe_single_subscription"].invoke("subscribed@example.com", "my-list") }
.to output(/Unsubscribing subscribed@example.com from my-list/).to_stdout
.and change { active_subscription.reload.ended_reason }
.from(nil)
.to("unsubscribed")
end

it "displays message if subscriber has already been unsubscribed" do
subscriber = create(:subscriber, address: "unsubscribed@example.com")
create(:subscription, :ended, subscriber_list: @subscriber_list, subscriber:)

expect { Rake::Task["support:unsubscribe_single_subscription"].invoke("unsubscribed@example.com", "my-list") }
.to output(/Subscriber unsubscribed@example.com already unsubscribed from my-list/).to_stdout
end
end

describe "unsubscribe_all_subscriptions" do
after(:each) do
Rake::Task["support:unsubscribe_all_subscriptions"].reenable
end

it "displays error essage if the subscriber is not found" do
expect { Rake::Task["support:unsubscribe_all_subscriptions"].invoke("test@example.com") }
.to output(/Subscriber test@example.com not found/).to_stdout
end

it "displays message if user has been unsubscribed" do
subscriber_list1 = create(:subscriber_list, slug: "my-list", title: "My List")
subscriber_list2 = create(:subscriber_list, slug: "another-list", title: "another List")
subscriber = create(:subscriber, address: "subscribed@example.com")
active_subscription1 = create(:subscription, subscriber_list: subscriber_list1, subscriber:)
active_subscription2 = create(:subscription, subscriber_list: subscriber_list2, subscriber:)

expect { Rake::Task["support:unsubscribe_all_subscriptions"].invoke("subscribed@example.com") }
.to output(/Unsubscribing subscribed@example.com/).to_stdout
.and change { active_subscription1.reload.ended_reason }
.from(nil)
.to("unsubscribed")
.and change { active_subscription2.reload.ended_reason }
.from(nil)
.to("unsubscribed")
end
end

describe "change_email_address" do
after(:each) do
Rake::Task["support:change_email_address"].reenable
end

it "displays error message and aborts if email address is not found" do
expect { Rake::Task["support:change_email_address"].invoke("old@example.com", "new@example.com") }
.to raise_error(SystemExit, /Cannot find any subscriber with email address old@example.com/)
end

it "displays message when email address for subsciber has been changed" do
subscriber = create(:subscriber, address: "old@example.com")

expect { Rake::Task["support:change_email_address"].invoke("old@example.com", "new@example.com") }
.to output(/Changed email address for old@example.com to new@example.com/).to_stdout
.and change { subscriber.reload.address }
.from("old@example.com")
.to("new@example.com")
end
end

describe "view_subscriptions" do
after(:each) do
Rake::Task["support:view_subscriptions"].reenable
end

it "displays error message and aborts if email address is not found" do
expect { Rake::Task["support:view_subscriptions"].invoke("test@example.com") }
.to raise_error(SystemExit, /Cannot find any subscriber with email address test@example.com/)
end

it "displays all subscriptions for a subscriber" do
subscriber = create(:subscriber, address: "test@example.com")
subscriber_list1 = create(:subscriber_list, slug: "my-list", title: "My List")
subscriber_list2 = create(:subscriber_list, slug: "another-list", title: "Another List")
subscription1 = create(:subscription, subscriber_list: subscriber_list1, subscriber:)
subscription2 = create(:subscription, :ended, subscriber_list: subscriber_list2, subscriber:)

report = <<~TEXT
[{:status=>"Active",
:subscriber_list=>"#{subscriber_list1.title} (slug: #{subscriber_list1.slug})",
:frequency=>"#{subscription1.frequency}",
:timeline=>"Subscribed #{subscription1.created_at}"},
{:status=>"Inactive (#{subscription2.ended_reason})",
:subscriber_list=>"#{subscriber_list2.title} (slug: #{subscriber_list2.slug})",
:frequency=>"#{subscription2.frequency}",
:timeline=>
"Subscribed #{subscription2.created_at}, Ended #{subscription2.ended_at}"}]
TEXT

expect { Rake::Task["support:view_subscriptions"].invoke("test@example.com") }
.to output(report).to_stdout
end
end
end

0 comments on commit f609a05

Please sign in to comment.