message_bird_spec.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'messagebird'
  4. RSpec.describe Channel::Driver::Sms::MessageBird do
  5. it 'passes' do
  6. channel = create_channel
  7. stub_request(:post, url_to_mock)
  8. .to_return(body: mocked_response_success)
  9. api = channel.driver_instance.new
  10. expect(api.deliver(channel.options, { recipient: '+37060010000', message: message_body })).to be true
  11. end
  12. it 'fails' do
  13. channel = create_channel
  14. stub_request(:post, url_to_mock)
  15. .to_return(status: 400, body: mocked_response_failure)
  16. api = channel.driver_instance.new
  17. expect { api.deliver(channel.options, { recipient: 'asd', message: message_body }) }.to raise_exception(MessageBird::ServerException)
  18. end
  19. private
  20. def create_channel
  21. create(:channel,
  22. options: {
  23. adapter: 'sms/message_bird',
  24. sender: sender_number,
  25. token: token
  26. },
  27. created_by_id: 1,
  28. updated_by_id: 1)
  29. end
  30. # api parameters
  31. def url_to_mock
  32. 'https://rest.messagebird.com/messages'
  33. end
  34. def message_body
  35. 'Test'
  36. end
  37. def sender_number
  38. '+15005550006'
  39. end
  40. def token
  41. '2345r4erfdvc4wedxv3efds'
  42. end
  43. # mocked responses
  44. def mocked_response_success
  45. '{"id":"1e8cc35873d14fe4ab18bd97a412121","href":"https://rest.messagebird.com/messages/1e8cc35873d14fe4ab18bd121212f971a","direction":"mt","type":"sms","originator":"Zammad GmbH","body":"This is a test messageNEW","reference":"Foobar","validity":null,"gateway":10,"typeDetails":{},"datacoding":"plain","mclass":1,"scheduledDatetime":null,"createdDatetime":"2021-07-22T13:25:03+00:00","recipients":{"totalCount":1,"totalSentCount":1,"totalDeliveredCount":0,"totalDeliveryFailedCount":0,"items":[{"recipient":491234,"status":"sent","statusDatetime":"2021-07-22T13:25:03+00:00","messagePartCount":1}]}}'
  46. end
  47. def mocked_response_failure
  48. '{"errors":[{"code":9,"description":"no (correct) recipients found","parameter":"recipient"}]}'
  49. end
  50. end