From e8b533243e8dde2e2d3faf79127f24154d6f35af Mon Sep 17 00:00:00 2001 From: Augusto Xavier Date: Fri, 12 Jun 2026 11:59:00 -0300 Subject: [PATCH] test: cover DeliveryMethods::Sms and CustomOrgLinkPolicy::Scope (#6833) --- app/notifications/delivery_methods/sms.rb | 2 +- .../delivery_methods/sms_spec.rb | 67 ++++++++++++++++++- spec/policies/custom_org_link_policy_spec.rb | 32 +++++++++ 3 files changed, 98 insertions(+), 3 deletions(-) diff --git a/app/notifications/delivery_methods/sms.rb b/app/notifications/delivery_methods/sms.rb index d1fefb7150..4c4732ba58 100644 --- a/app/notifications/delivery_methods/sms.rb +++ b/app/notifications/delivery_methods/sms.rb @@ -17,7 +17,7 @@ def case_contact_url private def sender - User.find(params[:followup][:creator_id]) + @sender ||= User.find(params[:followup][:creator_id]) end def case_contact_id diff --git a/spec/notifications/delivery_methods/sms_spec.rb b/spec/notifications/delivery_methods/sms_spec.rb index d7d09a0b3c..9e69de2963 100644 --- a/spec/notifications/delivery_methods/sms_spec.rb +++ b/spec/notifications/delivery_methods/sms_spec.rb @@ -1,7 +1,70 @@ require "rails_helper" +require "support/stubbed_requests/webmock_helper" RSpec.describe DeliveryMethods::Sms do - # TODO: Add tests for DeliveryMethods::Sms + let(:casa_org) { create(:casa_org, twilio_enabled: true) } + let(:recipient) { create(:volunteer, casa_org: casa_org, phone_number: "+12222222222") } + let(:case_contact) { create(:case_contact) } + let(:followup) { create(:followup, creator: sender, case_contact: case_contact) } + let(:event) { create(:followup_notifier, params: {followup: followup, created_by: sender}, record: followup) } + let(:notification) { create(:notification, event: event, recipient: recipient) } + let(:case_contact_edit_url) do + "#{Rails.application.credentials[:BASE_URL]}/case_contacts/#{case_contact.id}/edit?notification_id=#{followup.id}" + end - pending "add some tests for DeliveryMethods::Sms" + before do + allow(event).to receive(:delivery_methods).and_return({sms: Noticed::Deliverable::DeliverBy.new(:sms, {})}) + WebMockHelper.short_io_stub_sms + WebMockHelper.twilio_activation_success_stub + end + + def deliver_notification + described_class.new.perform(:sms, notification) + end + + context "when the sender is a casa admin" do + let(:sender) { create(:casa_admin, casa_org: casa_org) } + + it "sends an sms with the flagged case contact message and shortened url" do + deliver_notification + + expect(a_request(:post, "https://api.twilio.com/2010-04-01/Accounts/articuno34/Messages.json") + .with(body: { + From: casa_org.twilio_phone_number, + Body: "#{sender.display_name} has flagged a Case Contact that needs follow up. Click to see more: https://42ni.short.gy/jzTwdF", + To: recipient.phone_number + })).to have_been_made.once + end + + it "shortens the case contact edit url" do + deliver_notification + + expect(a_request(:post, "https://api.short.io/links") + .with(body: {originalURL: case_contact_edit_url, domain: "42ni.short.gy"}.to_json)) + .to have_been_made.once + end + end + + context "when the sender is a supervisor" do + let(:sender) { create(:supervisor, casa_org: casa_org) } + + it "sends an sms" do + deliver_notification + + expect(a_request(:post, "https://api.twilio.com/2010-04-01/Accounts/articuno34/Messages.json")) + .to have_been_made.once + end + end + + context "when the sender is a volunteer" do + let(:sender) { create(:volunteer, casa_org: casa_org) } + + it "does not send an sms" do + deliver_notification + + expect(a_request(:post, "https://api.twilio.com/2010-04-01/Accounts/articuno34/Messages.json")) + .not_to have_been_made + expect(a_request(:post, "https://api.short.io/links")).not_to have_been_made + end + end end diff --git a/spec/policies/custom_org_link_policy_spec.rb b/spec/policies/custom_org_link_policy_spec.rb index 2879a5ffb3..1e8871049f 100644 --- a/spec/policies/custom_org_link_policy_spec.rb +++ b/spec/policies/custom_org_link_policy_spec.rb @@ -18,4 +18,36 @@ expect(described_class).not_to permit(volunteer) end end + + describe "Scope#resolve" do + subject(:resolved_scope) { described_class::Scope.new(user, CustomOrgLink.all).resolve } + + let(:casa_org) { create(:casa_org) } + let!(:custom_org_link) { create(:custom_org_link, casa_org: casa_org) } + let!(:other_org_custom_org_link) { create(:custom_org_link) } + + context "when user is a casa admin" do + let(:user) { create(:casa_admin, casa_org: casa_org) } + + it "resolves only links from the admin's organization" do + expect(resolved_scope).to contain_exactly(custom_org_link) + end + end + + context "when user is a supervisor" do + let(:user) { create(:supervisor, casa_org: casa_org) } + + it "resolves no links" do + expect(resolved_scope).to be_empty + end + end + + context "when user is a volunteer" do + let(:user) { create(:volunteer, casa_org: casa_org) } + + it "resolves no links" do + expect(resolved_scope).to be_empty + end + end + end end