From f7729394b05a1fddcddbf3b52d7d30c50e963746 Mon Sep 17 00:00:00 2001 From: Celia Collins Date: Fri, 18 Oct 2024 11:02:59 +0100 Subject: [PATCH] Don't send bad link in welcome message --- app/jobs/send_welcome_message_job.rb | 18 +++++++++++++----- test/jobs/send_welcome_message_job_test.rb | 17 ++++++++++++++++- test/system/users_test.rb | 4 ++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/app/jobs/send_welcome_message_job.rb b/app/jobs/send_welcome_message_job.rb index 6380c3d..9af2c61 100644 --- a/app/jobs/send_welcome_message_job.rb +++ b/app/jobs/send_welcome_message_job.rb @@ -14,11 +14,19 @@ class SendWelcomeMessageJob < ApplicationJob } def perform(user) - message = Message.create do |m| - m.token = m.send(:generate_token) - m.link = WELCOME_VIDEOS[user.child_age_in_months_today] - m.user = user - m.body = "Welcome to our programme of weekly texts with fun activities! Here's a video to get you started: #{track_link_url(m.token)}" + message = if WELCOME_VIDEOS[user.child_age_in_months_today] + Message.create do |m| + m.token = m.send(:generate_token) + m.link = WELCOME_VIDEOS[user.child_age_in_months_today] + m.user = user + m.body = "Welcome to Tiny Happy People, a programme of weekly texts with fun activities! Here's a video to get you started: #{track_link_url(m.token)}" + end + else + Message.create do |m| + m.token = m.send(:generate_token) + m.user = user + m.body = "Welcome to Tiny Happy People, a programme of weekly texts with fun activities! You'll receive your first activity soon." + end end Twilio::Client.new.send_message(message) diff --git a/test/jobs/send_welcome_message_job_test.rb b/test/jobs/send_welcome_message_job_test.rb index 3137fe1..ccc3e67 100644 --- a/test/jobs/send_welcome_message_job_test.rb +++ b/test/jobs/send_welcome_message_job_test.rb @@ -9,7 +9,7 @@ class SendWelcomeMessageJobTest < ActiveSupport::TestCase Message.any_instance.stubs(:generate_token).returns("123") - message = "Welcome to our programme of weekly texts with fun activities! Here's a video to get you started: #{track_link_url(123)}" + message = "Welcome to Tiny Happy People, a programme of weekly texts with fun activities! Here's a video to get you started: #{track_link_url(123)}" stub_successful_twilio_call(message, user) @@ -19,4 +19,19 @@ class SendWelcomeMessageJobTest < ActiveSupport::TestCase assert_match(/m\/123/, Message.last.body) assert_equal "https://www.bbc.co.uk/tiny-happy-people/shopping-game-18-24/zbhyf4j", Message.last.link end + + test "#perform sends message with no link if there isn't appropriate content" do + user = create(:user, child_birthday: 25.months.ago) + + Message.any_instance.stubs(:generate_token).returns("123") + + message = "Welcome to Tiny Happy People, a programme of weekly texts with fun activities! You'll receive your first activity soon." + + stub_successful_twilio_call(message, user) + + SendWelcomeMessageJob.new.perform(user) + + assert_equal 1, Message.count + assert_nil Message.last.link + end end diff --git a/test/system/users_test.rb b/test/system/users_test.rb index af8a8bf..c27fe54 100644 --- a/test/system/users_test.rb +++ b/test/system/users_test.rb @@ -16,7 +16,7 @@ class UsersTest < ApplicationSystemTestCase check "I accept the terms of service and privacy policy" Message.any_instance.stubs(:generate_token).returns("123") - message = "Welcome to our programme of weekly texts with fun activities! Here's a video to get you started: http://localhost:3000/m/123" + message = "Welcome to Tiny Happy People, a programme of weekly texts with fun activities! Here's a video to get you started: http://localhost:3000/m/123" stub_successful_twilio_call(message, User.new(phone_number: "+447444930200")) within("#sign-up-form") do @@ -36,7 +36,7 @@ class UsersTest < ApplicationSystemTestCase assert_text "ABC123" assert_text "Does have family support" assert_text "On Slack" - assert_text "Welcome to our programme of weekly texts with fun activities!" + assert_text "Welcome to Tiny Happy People, a programme of weekly texts with fun activities!" end test "form shows errors" do