Sha256: 9fcc30860c3f4db5e6c379a278c52255c3bc322b008b2e437b275c9b6f6126fe

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

require "spec_helper"

describe Mailstro::Delivery do
  let(:template)  { "name" }
  let(:recipient) { { :email => "foo@bar.com" } }
  let(:payload)   { { :data => "here" } }

  describe '.delivery' do
    let(:delivery) { double }

    it "creates an instance of the Delivery and delivers it" do
      Mailstro::Delivery.should_receive(:new).with(template, :recipient => recipient, :payload => payload).and_return(delivery)
      delivery.should_receive(:deliver)

      Mailstro::Delivery.deliver(template, :recipient => recipient, :payload => payload)
    end
  end

  describe "#delivery" do
    let(:delivery)      { Mailstro::Delivery.new(template, :recipient => recipient, :payload => payload) }
    let(:request)       { double.as_null_object }
    let(:response)      { double(:body => '{ "status": "OK" }').as_null_object }
    let(:http)          { double(:request => response).as_null_object }
    let(:configuration) { double(:use_ssl => true, :endpoint => 'foo.com', :api_version => '2', :api_key => '1234') }

    before do
      Mailstro.stub(:configuration => configuration)
      Net::HTTP.should_receive(:new).with('foo.com', 443).and_return(http)
      Net::HTTP::Post.should_receive(:new).with('/v2/postman', { "Content-Type"=>"application/json" }).and_return(request)
    end

    it "sends a json payload to the mailstro server" do
      http.should_receive(:request).with(request).and_return(response)

      delivery.deliver
    end

    it "generates the correct payload" do
      expected_form_data = {
        'template'  => 'name',
        'recipient' => { 'email' => 'foo@bar.com' },
        'payload'   => { 'data' => 'here' },
        'api_key'   => '1234'
      }

      request.should_receive(:body=) do |body|
        parsed_body = JSON.parse(body)
        parsed_body.should == expected_form_data
      end

      delivery.deliver
    end

    it "parses the JSON response" do
      delivery.deliver['status'].should == 'OK'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mailstro-0.0.2 spec/mailstro/delivery_spec.rb
mailstro-0.0.1 spec/mailstro/delivery_spec.rb