File: postback_spec.rb

package info (click to toggle)
ruby-mail-room 0.10.0%2Breally0.0.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 376 kB
  • sloc: ruby: 1,749; makefile: 5
file content (91 lines) | stat: -rw-r--r-- 2,710 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'spec_helper'
require 'mail_room/delivery/postback'

describe MailRoom::Delivery::Postback do
  describe '#deliver' do
    context 'with token auth delivery' do
      let(:mailbox) {build_mailbox({
        :delivery_url => 'http://localhost/inbox',
        :delivery_token => 'abcdefg'
      })}

      let(:delivery_options) {
        MailRoom::Delivery::Postback::Options.new(mailbox)
      }

      it 'posts the message with faraday' do
        connection = stub
        request = stub
        Faraday.stubs(:new).returns(connection)

        connection.expects(:token_auth).with('abcdefg')
        connection.expects(:post).yields(request)

        request.expects(:url).with('http://localhost/inbox')
        request.expects(:body=).with('a message')

        MailRoom::Delivery::Postback.new(delivery_options).deliver('a message')
      end
    end

    context 'with basic auth delivery options' do
      let(:mailbox) {build_mailbox({
        :delivery_options => {
          :url => 'http://localhost/inbox',
          :username => 'user1',
          :password => 'password123abc'
        }
      })}

      let(:delivery_options) {
        MailRoom::Delivery::Postback::Options.new(mailbox)
      }

      it 'posts the message with faraday' do
        connection = stub
        request = stub
        Faraday.stubs(:new).returns(connection)

        connection.expects(:basic_auth).with('user1', 'password123abc')
        connection.expects(:post).yields(request)

        request.expects(:url).with('http://localhost/inbox')
        request.expects(:body=).with('a message')

        MailRoom::Delivery::Postback.new(delivery_options).deliver('a message')
      end

      context 'with content type in the delivery options' do
        let(:mailbox) {build_mailbox({
          :delivery_options => {
            :url => 'http://localhost/inbox',
            :username => 'user1',
            :password => 'password123abc',
            :content_type => 'text/plain'
          }
        })}

  
        let(:delivery_options) {
          MailRoom::Delivery::Postback::Options.new(mailbox)
        }
  
        it 'posts the message with faraday' do
          connection = stub
          request = stub
          Faraday.stubs(:new).returns(connection)

          connection.expects(:post).yields(request)
          request.stubs(:url)
          request.stubs(:body=)
          request.stubs(:headers).returns({})
          connection.expects(:basic_auth).with('user1', 'password123abc')

          MailRoom::Delivery::Postback.new(delivery_options).deliver('a message')
          
          expect(request.headers['Content-Type']).to eq('text/plain')
        end
      end
    end
  end
end