beeper_agent_spec.rb 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. require 'rails_helper'
  2. describe Agents::BeeperAgent do
  3. let(:base_params) {
  4. {
  5. 'type' => 'message',
  6. 'app_id' => 'some-app-id',
  7. 'api_key' => 'some-api-key',
  8. 'sender_id' => 'sender-id',
  9. 'phone' => '+111111111111',
  10. 'text' => 'Some text'
  11. }
  12. }
  13. subject {
  14. agent = described_class.new(name: 'beeper-agent', options: base_params)
  15. agent.user = users(:jane)
  16. agent.save! and return agent
  17. }
  18. context 'validation' do
  19. it 'valid' do
  20. expect(subject).to be_valid
  21. end
  22. [:type, :app_id, :api_key, :sender_id].each do |attr|
  23. it "invalid without #{attr}" do
  24. subject.options[attr] = nil
  25. expect(subject).not_to be_valid
  26. end
  27. end
  28. it 'invalid with group_id and phone' do
  29. subject.options['group_id'] ='some-group-id'
  30. expect(subject).not_to be_valid
  31. end
  32. context '#message' do
  33. it 'requires text' do
  34. subject.options[:text] = nil
  35. expect(subject).not_to be_valid
  36. end
  37. end
  38. context '#image' do
  39. before(:each) do
  40. subject.options[:type] = 'image'
  41. end
  42. it 'invalid without image' do
  43. expect(subject).not_to be_valid
  44. end
  45. it 'valid with image' do
  46. subject.options[:image] = 'some-url'
  47. expect(subject).to be_valid
  48. end
  49. end
  50. context '#event' do
  51. before(:each) do
  52. subject.options[:type] = 'event'
  53. end
  54. it 'invalid without start_time' do
  55. expect(subject).not_to be_valid
  56. end
  57. it 'valid with start_time' do
  58. subject.options[:start_time] = Time.now
  59. expect(subject).to be_valid
  60. end
  61. end
  62. context '#location' do
  63. before(:each) do
  64. subject.options[:type] = 'location'
  65. end
  66. it 'invalid without latitude and longitude' do
  67. expect(subject).not_to be_valid
  68. end
  69. it 'valid with latitude and longitude' do
  70. subject.options[:latitude] = 15.0
  71. subject.options[:longitude] = 16.0
  72. expect(subject).to be_valid
  73. end
  74. end
  75. context '#task' do
  76. before(:each) do
  77. subject.options[:type] = 'task'
  78. end
  79. it 'valid with text' do
  80. expect(subject).to be_valid
  81. end
  82. end
  83. end
  84. context 'payload_for' do
  85. it 'removes unwanted attributes' do
  86. result = subject.send(:payload_for, {'type' => 'message', 'text' => 'text',
  87. 'sender_id' => 'sender', 'phone' => '+1', 'random_attribute' => 'unwanted'})
  88. expect(result).to eq('{"text":"text","sender_id":"sender","phone":"+1"}')
  89. end
  90. end
  91. context 'headers' do
  92. it 'sets X-Beeper-Application-Id header with app_id' do
  93. expect(subject.send(:headers)['X-Beeper-Application-Id']).to eq(base_params['app_id'])
  94. end
  95. it 'sets X-Beeper-REST-API-Key header with api_key' do
  96. expect(subject.send(:headers)['X-Beeper-REST-API-Key']).to eq(base_params['api_key'])
  97. end
  98. it 'sets Content-Type' do
  99. expect(subject.send(:headers)['Content-Type']).to eq('application/json')
  100. end
  101. end
  102. context 'endpoint_for' do
  103. it 'returns valid URL for message' do
  104. expect(subject.send(:endpoint_for, 'message')).to eq('https://api.beeper.io/api/messages.json')
  105. end
  106. it 'returns valid URL for image' do
  107. expect(subject.send(:endpoint_for, 'image')).to eq('https://api.beeper.io/api/images.json')
  108. end
  109. it 'returns valid URL for event' do
  110. expect(subject.send(:endpoint_for, 'event')).to eq('https://api.beeper.io/api/events.json')
  111. end
  112. it 'returns valid URL for location' do
  113. expect(subject.send(:endpoint_for, 'location')).to eq('https://api.beeper.io/api/locations.json')
  114. end
  115. it 'returns valid URL for task' do
  116. expect(subject.send(:endpoint_for, 'task')).to eq('https://api.beeper.io/api/tasks.json')
  117. end
  118. end
  119. end