|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Griddler::Adapters::MandrillAdapter, '.normalize_params' do |
| 4 | + it 'normalizes parameters' do |
| 5 | + params = default_params |
| 6 | + |
| 7 | + normalized_params = Griddler::Adapters::MandrillAdapter.normalize_params(params) |
| 8 | + normalized_params.each do |params| |
| 9 | + params[:to].should eq ['The Token <[email protected]>'] |
| 10 | + params[:from].should eq '[email protected]' |
| 11 | + params[:subject].should eq 'hello' |
| 12 | + params[:text].should include('Dear bob') |
| 13 | + params[:html].should include('<p>Dear bob</p>') |
| 14 | + params[:raw_body].should include('raw') |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + it 'passes the received array of files' do |
| 19 | + params = params_with_attachments |
| 20 | + |
| 21 | + normalized_params = Griddler::Adapters::MandrillAdapter.normalize_params(params) |
| 22 | + |
| 23 | + first, second = *normalized_params[0][:attachments] |
| 24 | + |
| 25 | + first.original_filename.should eq('photo1.jpg') |
| 26 | + first.size.should eq(upload_1_params[:length]) |
| 27 | + |
| 28 | + second.original_filename.should eq('photo2.jpg') |
| 29 | + second.size.should eq(upload_2_params[:length]) |
| 30 | + end |
| 31 | + |
| 32 | + it 'has no attachments' do |
| 33 | + params = default_params |
| 34 | + |
| 35 | + normalized_params = Griddler::Adapters::MandrillAdapter.normalize_params(params) |
| 36 | + |
| 37 | + normalized_params[0][:attachments].should be_empty |
| 38 | + end |
| 39 | + |
| 40 | + def default_params |
| 41 | + mandrill_events (params_hash*2).to_json |
| 42 | + end |
| 43 | + |
| 44 | + def params_hash |
| 45 | + [{ |
| 46 | + event: "inbound", |
| 47 | + ts: 1364601140, |
| 48 | + msg: |
| 49 | + { |
| 50 | + raw_msg: "raw", |
| 51 | + headers: {}, |
| 52 | + text: text_body, |
| 53 | + html: text_html, |
| 54 | + |
| 55 | + from_name: "Hernan Example", |
| 56 | + to: [["[email protected]", "The Token"]], |
| 57 | + subject: "hello", |
| 58 | + spam_report: { |
| 59 | + score: -0.8, |
| 60 | + matched_rules: "..." |
| 61 | + }, |
| 62 | + dkim: {signed: true, valid: true}, |
| 63 | + spf: {result: "pass", detail: "sender SPF authorized"}, |
| 64 | + |
| 65 | + tags: [], |
| 66 | + sender: nil |
| 67 | + } |
| 68 | + }] |
| 69 | + end |
| 70 | + |
| 71 | + def params_with_attachments |
| 72 | + params = params_hash |
| 73 | + params[0][:msg][:attachments] = { |
| 74 | + 'photo1.jpg' => upload_1_params, |
| 75 | + 'photo2.jpg' => upload_2_params |
| 76 | + } |
| 77 | + mandrill_events params.to_json |
| 78 | + end |
| 79 | + |
| 80 | + def mandrill_events(json) |
| 81 | + { mandrill_events: json } |
| 82 | + end |
| 83 | + |
| 84 | + def text_body |
| 85 | + <<-EOS.strip_heredoc.strip |
| 86 | + Dear bob |
| 87 | +
|
| 88 | + Reply ABOVE THIS LINE |
| 89 | +
|
| 90 | + hey sup |
| 91 | + EOS |
| 92 | + end |
| 93 | + |
| 94 | + def text_html |
| 95 | + <<-EOS.strip_heredoc.strip |
| 96 | + <p>Dear bob</p> |
| 97 | +
|
| 98 | + Reply ABOVE THIS LINE |
| 99 | +
|
| 100 | + hey sup |
| 101 | + EOS |
| 102 | + end |
| 103 | + |
| 104 | + def cwd |
| 105 | + File.expand_path File.dirname(__FILE__) |
| 106 | + end |
| 107 | + |
| 108 | + def upload_1_params |
| 109 | + @upload_1_params ||= begin |
| 110 | + file = File.new("#{cwd}/../../../spec/fixtures/photo1.jpg") |
| 111 | + size = file.size |
| 112 | + { |
| 113 | + name: 'photo1.jpg', |
| 114 | + content: Base64.encode64(file.read), |
| 115 | + type: 'image/jpeg', |
| 116 | + length: file.size |
| 117 | + } |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + def upload_2_params |
| 122 | + @upload_2_params ||= begin |
| 123 | + file = File.new("#{cwd}/../../../spec/fixtures/photo2.jpg") |
| 124 | + size = file.size |
| 125 | + { |
| 126 | + name: 'photo2.jpg', |
| 127 | + content: Base64.encode64(file.read), |
| 128 | + type: 'image/jpeg', |
| 129 | + length: file.size |
| 130 | + } |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + def upload_1 |
| 135 | + @upload_1 ||= ActionDispatch::Http::UploadedFile.new({ |
| 136 | + filename: 'photo1.jpg', |
| 137 | + type: 'image/jpeg', |
| 138 | + tempfile: File.new("#{cwd}/../../../spec/fixtures/photo1.jpg") |
| 139 | + }) |
| 140 | + end |
| 141 | + |
| 142 | + def upload_2 |
| 143 | + @upload_2 ||= ActionDispatch::Http::UploadedFile.new({ |
| 144 | + filename: 'photo2.jpg', |
| 145 | + type: 'image/jpeg', |
| 146 | + tempfile: File.new("#{cwd}/../../../spec/fixtures/photo2.jpg") |
| 147 | + }) |
| 148 | + end |
| 149 | +end |
0 commit comments