Skip to content

Commit

Permalink
custom matcher moved to appropriate matchers location
Browse files Browse the repository at this point in the history
  • Loading branch information
ibalosh committed Sep 30, 2019
1 parent 6d1f200 commit f4938b4
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 194 deletions.
64 changes: 29 additions & 35 deletions spec/integration/api_client_hashes_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
require 'spec_helper'

describe "Sending messages as Ruby hashes with Postmark::ApiClient" do
let(:message_id_format) { /<.+@.+>/ }
let(:postmark_message_id_format) { /\w{8}\-\w{4}-\w{4}-\w{4}-\w{12}/ }
let(:api_client) { Postmark::ApiClient.new('POSTMARK_API_TEST', :http_open_timeout => 15, :http_read_timeout => 15) }
let(:message_id_format) {/<.+@.+>/}
let(:postmark_message_id_format) {/\w{8}\-\w{4}-\w{4}-\w{4}-\w{12}/}
let(:api_client) {Postmark::ApiClient.new('POSTMARK_API_TEST', :http_open_timeout => 15, :http_read_timeout => 15)}

let(:message) {
{
:from => "[email protected]",
:to => "[email protected]",
:subject => "Mail::Message object",
:text_body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " \
:from => "[email protected]",
:to => "[email protected]",
:subject => "Mail::Message object",
:text_body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " \
"sed do eiusmod tempor incididunt ut labore et dolore " \
"magna aliqua."
}
}

let(:message_with_no_body) {
{
:from => "[email protected]",
:to => "[email protected]",
:from => "[email protected]",
:to => "[email protected]",
}
}

Expand All @@ -29,66 +29,60 @@
end
}

let(:message_with_invalid_to) {
{
:from => "[email protected]",
:to => "@postmarkapp.com"
}
}
let(:message_with_invalid_to) {{:from => "[email protected]", :to => "@postmarkapp.com"}}
let(:valid_messages) {[message, message.dup]}
let(:partially_valid_messages) {[message, message.dup, message_with_no_body]}
let(:invalid_messages) {[message_with_no_body, message_with_no_body.dup]}

let(:valid_messages) { [message, message.dup] }
let(:partially_valid_messages) { [message, message.dup, message_with_no_body] }
let(:invalid_messages) { [message_with_no_body, message_with_no_body.dup] }

context "message by message" do
it 'delivers a plain text message' do
context "single message" do
it 'plain text message' do
expect(api_client.deliver(message)).to have_key(:message_id)
end

it 'updates a message object with Message-ID' do
expect(api_client.deliver(message)[:message_id]).to be =~ postmark_message_id_format
it 'message with attachment' do
expect(api_client.deliver(message_with_attachment)).to have_key(:message_id)
end

it 'returns full Postmark response' do
expect(api_client.deliver(message)).to be_a Hash
it 'response Message-ID' do
expect(api_client.deliver(message)[:message_id]).to be =~ postmark_message_id_format
end

it 'delivers a message with attachment' do
expect(api_client.deliver(message_with_attachment)).to have_key(:message_id)
it 'response is Hash' do
expect(api_client.deliver(message)).to be_a Hash
end

it 'fails to deliver a message without body' do
expect { api_client.deliver(message_with_no_body) }.to raise_error(Postmark::InvalidMessageError)
expect {api_client.deliver(message_with_no_body)}.to raise_error(Postmark::InvalidMessageError)
end

it 'fails to deliver a message with invalid To address' do
expect { api_client.deliver(message_with_invalid_to) }.to raise_error(Postmark::InvalidMessageError)
expect {api_client.deliver(message_with_invalid_to)}.to raise_error(Postmark::InvalidMessageError)
end
end

context "in batches" do
it 'returns as many responses as many messages were sent' do
context "batch message" do
it 'response messages count' do
expect(api_client.deliver_in_batches(valid_messages).count).to eq valid_messages.count
end

context "given custom max_batch_size" do
context "custom max_batch_size" do
before do
api_client.max_batch_size = 1
end

it 'returns as many responses as many messages were sent' do
it 'response message count' do
expect(api_client.deliver_in_batches(valid_messages).count).to eq valid_messages.count
end
end

it 'partially delivers a batch of partially valid Mail::Message objects' do
response = api_client.deliver_in_batches(partially_valid_messages)
expect(response).to satisfy { |r| r.count { |mr| mr[:error_code].to_i.zero? } == 2 }
expect(response).to satisfy {|r| r.count {|mr| mr[:error_code].to_i.zero?} == 2}
end

it "doesn't deliver a batch of invalid Mail::Message objects" do
response = api_client.deliver_in_batches(invalid_messages)
expect(response).to satisfy { |r| r.all? { |mr| !!mr[:error_code] } }
expect(response).to satisfy {|r| r.all? {|mr| !!mr[:error_code]}}
end
end
end
32 changes: 16 additions & 16 deletions spec/integration/api_client_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@
end
end

context "message by message" do
it 'delivers a plain text message' do
context "single message" do
it 'plain text message' do
expect(api_client.deliver_message(message)).to have_key(:message_id)
end

it 'updates a message object with Message-ID' do
expect(api_client.deliver_message(message)[:message_id]).to be =~ postmark_message_id_format
it 'message with attachment' do
expect(api_client.deliver_message(message_with_attachment)).to have_key(:message_id)
end

it 'returns full Postmark response' do
expect(api_client.deliver_message(message)).to be_a Hash
it 'response Message-ID' do
expect(api_client.deliver_message(message)[:message_id]).to be =~ postmark_message_id_format
end

it 'delivers a message with attachment' do
expect(api_client.deliver_message(message_with_attachment)).to have_key(:message_id)
it 'response is Hash' do
expect(api_client.deliver_message(message)).to be_a Hash
end

it 'fails to deliver a message without body' do
Expand All @@ -68,37 +68,37 @@
end
end

context "in batches" do
it 'delivers a batch of valid Mail::Message objects' do
context "batch message" do
it 'response - valid Mail::Message objects' do
expect { api_client.deliver_messages(valid_messages) }.
to change{valid_messages.all? { |m| m.delivered? }}.to true
end

it 'updates delivered messages with X-PM-Message-Ids' do
it 'response - valid X-PM-Message-Ids' do
api_client.deliver_messages(valid_messages)
expect(valid_messages.all? { |m| m['X-PM-Message-Id'].to_s =~ postmark_message_id_format }).to be true
end

it 'updates delivered messages with related Postmark responses' do
it 'response - valid response objects' do
api_client.deliver_messages(valid_messages)
expect(valid_messages.all? { |m| m.postmark_response["To"] == m.to[0] }).to be true
end

it 'returns as many responses as many messages were sent' do
it 'response - message responses count' do
expect(api_client.deliver_messages(valid_messages).count).to eq valid_messages.count
end

context "given custom max_batch_size" do
context "custom max_batch_size" do
before do
api_client.max_batch_size = 1
end

it 'updates delivered messages with related Postmark responses' do
it 'response - valid response objects' do
api_client.deliver_messages(valid_messages)
expect(valid_messages.all? { |m| m.postmark_response["To"] == m.to[0] }).to be true
end

it 'returns as many responses as many messages were sent' do
it 'response - message responses count' do
expect(api_client.deliver_messages(valid_messages).count).to eq valid_messages.count
end
end
Expand Down
46 changes: 15 additions & 31 deletions spec/integration/api_client_resources_spec.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
require 'spec_helper'

describe 'Accessing server resources using the API' do

let(:api_client) {
Postmark::ApiClient.new(ENV['POSTMARK_API_KEY'], :http_open_timeout => 15)
}
let(:recipient) { ENV['POSTMARK_CI_RECIPIENT'] }
let(:api_client) {Postmark::ApiClient.new(ENV['POSTMARK_API_KEY'], :http_open_timeout => 15)}
let(:recipient) {ENV['POSTMARK_CI_RECIPIENT']}
let(:message) {
{
:from => ENV['POSTMARK_CI_SENDER'],
:to => recipient,
:subject => "Mail::Message object",
:text_body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " \
:from => ENV['POSTMARK_CI_SENDER'],
:to => recipient,
:subject => "Mail::Message object",
:text_body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " \
"sed do eiusmod tempor incididunt ut labore et dolore " \
"magna aliqua."
}
}

context 'Triggers API' do

let(:unique_token) { rand(36**32).to_s(36) }
let(:unique_token) {rand(36 ** 32).to_s(36)}

it 'can be used to manage tag triggers via the API' do
trigger = api_client.create_trigger(:tags,
:match_name => "gemtest_#{unique_token}",
:track_opens => true)
api_client.update_trigger(:tags,
trigger[:id],
:match_name => "pre_#{trigger[:match_name]}")
trigger = api_client.create_trigger(:tags, :match_name => "gemtest_#{unique_token}", :track_opens => true)
api_client.update_trigger(:tags, trigger[:id], :match_name => "pre_#{trigger[:match_name]}")
updated = api_client.get_trigger(:tags, trigger[:id])

expect(updated[:id]).to eq(trigger[:id])
expect(updated[:match_name]).not_to eq(trigger[:id])
expect(api_client.triggers(:tags).map { |t| t[:id] }).to include(trigger[:id])
expect(api_client.triggers(:tags).map {|t| t[:id]}).to include(trigger[:id])

api_client.delete_trigger(:tags, trigger[:id])
end

end

context 'Messages API' do

def with_retries(max_retries = 20, wait_seconds = 3)
yield
rescue => e
Expand All @@ -55,17 +45,13 @@ def with_retries(max_retries = 20, wait_seconds = 3)

it 'is possible to send a message and access its details via the Messages API' do
response = api_client.deliver(message)
message = with_retries {
api_client.get_message(response[:message_id])
}
message = with_retries {api_client.get_message(response[:message_id])}
expect(message[:recipients]).to include(recipient)
end

it 'is possible to send a message and dump it via the Messages API' do
response = api_client.deliver(message)
dump = with_retries {
api_client.dump_message(response[:message_id])
}
dump = with_retries {api_client.dump_message(response[:message_id])}
expect(dump[:body]).to include('Mail::Message object')
end

Expand All @@ -74,15 +60,13 @@ def with_retries(max_retries = 20, wait_seconds = 3)
expect {
with_retries {
messages = api_client.get_messages(:recipient => recipient,
:fromemail => message[:from],
:subject => message[:subject])
unless messages.map { |m| m[:message_id] }.include?(response[:message_id])
:fromemail => message[:from],
:subject => message[:subject])
unless messages.map {|m| m[:message_id]}.include?(response[:message_id])
raise 'Message not found'
end
}
}.not_to raise_error
end

end

end
7 changes: 0 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,3 @@
Postmark.response_parser_class = nil
end
end

# define custom matchers
RSpec::Matchers.define :be_serialized_to do |json|
match do |mail_message|
Postmark.convert_message_to_options_hash(mail_message) == JSON.parse(json)
end
end
6 changes: 6 additions & 0 deletions spec/support/custom_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ def postmark_json?(str)
RSpec::Matchers.define :json_representation_of do |x|
match { |actual| Postmark::Json.decode(actual) == x }
end

RSpec::Matchers.define :be_serialized_to do |json|
match do |mail_message|
Postmark.convert_message_to_options_hash(mail_message) == JSON.parse(json)
end
end
Loading

0 comments on commit f4938b4

Please sign in to comment.