Maintenance: Improve logging of failures when sending SMS via Massenversand.de.

This commit is contained in:
Thorsten Eckel 2020-08-26 13:46:08 +02:00
parent 9dd2b59037
commit 4696a85cd4
2 changed files with 53 additions and 58 deletions

View file

@ -12,13 +12,20 @@ class Channel::Driver::Sms::Massenversand
if Setting.get('developer_mode') != true if Setting.get('developer_mode') != true
response = Faraday.get(url).body response = Faraday.get(url).body
raise response if !response.match?('OK')
if !response.match?('OK')
message = "Received non-OK response from gateway URL '#{url}'"
Rails.logger.error "#{message}: #{response.inspect}"
raise message
end
end end
true true
rescue => e rescue => e
Rails.logger.debug "Massenversand error: #{e.inspect}" message = "Error while performing request to gateway URL '#{url}'"
raise e Rails.logger.error message
Rails.logger.error e
raise message
end end
end end

View file

@ -1,30 +1,24 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Channel::Driver::Sms::Massenversand do RSpec.describe Channel::Driver::Sms::Massenversand do
it 'passes' do let(:gateway) { 'https://gate1.goyyamobile.com/sms/sendsms.asp' }
channel = create_channel let(:message_body) { 'Test' }
let(:receiver_number) { '+37060010000' }
stub_request(:get, url_to_mock) let(:sender_number) { '+491000000000' }
.to_return(body: 'OK') let(:token) { '00q1234123423r5rwefdfsfsfef' }
let(:url) { "#{gateway}?#{params}" }
api = channel.driver_instance.new let(:params) do
expect(api.send(channel.options, { recipient: receiver_number, message: message_body })).to be true URI.encode_www_form(
authToken: token,
getID: 1,
msg: message_body,
msgtype: 'c',
receiver: receiver_number,
sender: sender_number
)
end end
let(:channel) do
it 'fails' do create(:channel,
channel = create_channel
stub_request(:get, url_to_mock)
.to_return(body: 'blocked receiver ()')
api = channel.driver_instance.new
expect { api.send(channel.options, { recipient: receiver_number, message: message_body }) }.to raise_exception(RuntimeError)
end
private
def create_channel
FactoryBot.create(:channel,
options: { options: {
adapter: 'sms/massenversand', adapter: 'sms/massenversand',
gateway: gateway, gateway: gateway,
@ -34,39 +28,33 @@ RSpec.describe Channel::Driver::Sms::Massenversand do
created_by_id: 1, created_by_id: 1,
updated_by_id: 1) updated_by_id: 1)
end end
let(:instance) { described_class.new }
def url_to_mock context 'when gateway returns OK' do
params = {
authToken: token, before do
getID: 1, stub_request(:get, url).to_return(body: 'OK')
msg: message_body, end
msgtype: 'c',
receiver: receiver_number, it 'passes' do
sender: sender_number expect(instance.send(channel.options, { recipient: receiver_number, message: message_body })).to be true
end
end
context 'when gateway response is invalid' do
before do
stub_request(:get, url).to_return(body: body)
end
context 'when receiver is blocked' do
let(:body) { 'blocked receiver ()' }
it 'raises RuntimeError' do # rubocop:disable RSpec/MultipleExpectations
expect { instance.send(channel.options, { recipient: receiver_number, message: message_body }) }.to raise_error { |error|
expect(error.message).not_to include(body)
} }
end
gateway + '?' + URI.encode_www_form(params) end
end
# api parameters
def gateway
'https://gate1.goyyamobile.com/sms/sendsms.asp'
end
def message_body
'Test'
end
def receiver_number
'+37060010000'
end
def sender_number
'+491000000000'
end
def token
'00q1234123423r5rwefdfsfsfef'
end end
end end