Skip to content

Commit

Permalink
Mailgun tests (#236)
Browse files Browse the repository at this point in the history
* unitclient replaced with fake_web

* obsolete option removed
  • Loading branch information
alexey-voronenko committed Jul 31, 2017
1 parent 34e7729 commit be42768
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 94 deletions.
68 changes: 0 additions & 68 deletions spec/support/mailgun_unit_client.rb

This file was deleted.

25 changes: 8 additions & 17 deletions spec/unit/lib/mail_adapters/mailgun_spec.rb
Expand Up @@ -31,8 +31,14 @@
end

describe '.find' do
let(:mailgun_message) { double(to_h: message) }
let(:events) { double(to_h: { 'items' => [event] }) }
let(:mailgun_message) { JSON.generate(message) }
let(:events) { JSON.generate('items' => [event]) }
before do
FakeWeb.register_uri(:any, 'https://api:mailgun_account_private_key@api.mailgun.net/v3/'\
'mailgun@test.domain/events?event=stored', body: events.to_s)
FakeWeb.register_uri(:any, 'https://api:mailgun_account_private_key@si.api.mailgun.net/v3/'\
'domains/mg.strongqa.com/messages/1234567890', body: mailgun_message.to_s)
end
subject { Howitzer::MailAdapters::Mailgun.find(recipient, message_subject, wait: 0.01) }

context 'when message is found' do
Expand All @@ -50,15 +56,6 @@
}
}
end
before do
allow(Howitzer::MailgunApi::Connector.instance.client).to receive(:get).with(
'mailgun@test.domain/events',
params: { event: 'stored' }
) { events }
allow(Howitzer::MailgunApi::Connector.instance.client).to receive(:get_url).with(
'https://si.api.mailgun.net/v3/domains/mg.strongqa.com/messages/1234567890'
) { mailgun_message }
end
it do
expect(Howitzer::Email.adapter).to receive(:new).with(message).once
subject
Expand All @@ -80,12 +77,6 @@
}
}
end
before do
allow(Howitzer::MailgunApi::Connector.instance.client).to receive(:get).with(
'mailgun@test.domain/events',
params: { event: 'stored' }
) { events }
end
it do
expect { subject }.to raise_error(
Howitzer::EmailNotFoundError,
Expand Down
40 changes: 31 additions & 9 deletions spec/unit/lib/mailgun_api/client_spec.rb
Expand Up @@ -3,6 +3,30 @@

RSpec.describe Howitzer::MailgunApi::Client do
let(:mg_obj) { described_class.new(api_key: 'Fake-API-Key') }
let(:bounce_msg) do
JSON.generate(
'total_count' => 1,
'items' => {
'created_at' => 'Fri, 21 Oct 2011 11:02:55 GMT',
'code' => 550,
'address' => 'baz@example.com',
'error' => 'Message was not accepted -- invalid mailbox. Local mailbox baz@example.com is" +
" unavailable: user not found'
}
)
end
let(:message) do
{
'body-plain' => 'test body footer',
'stripped-html' => '<p> test body </p> <p> footer </p>',
'stripped-text' => 'test body',
'From' => 'Strong Tester <tester@gmail.com>',
'To' => 'baz@example.com',
'Received' => 'by 10.216.46.75 with HTTP; Sat, 5 Apr 2014 05:10:42 -0700 (PDT)',
'sender' => 'tester@gmail.com',
'attachments' => []
}
end
describe '.new' do
subject { mg_obj }
it { expect { subject }.not_to raise_error }
Expand All @@ -13,8 +37,8 @@
subject { mg_obj.get('test.com/bounces', params: query_string) }
context 'when simulation of client' do
before do
expect(RestClient::Resource).to receive(:new)
.once.and_return(Howitzer::MailgunApi::UnitClient.new('Fake-API-Key', 'api.mailgun.net', 'v2'))
FakeWeb.register_uri(:get, 'https://api:Fake-API-Key@api.mailgun.net/v3/test.com/'\
'bounces?skip=10&limit=5', body: bounce_msg.to_s)
end
it do
expect(subject.body).to include('total_count')
Expand All @@ -36,15 +60,13 @@

describe '#get_url' do
let(:response_raw) { double }
before do
FakeWeb.register_uri(:any, 'https://api:Fake-API-Key@ci.api.mailgan.com/'\
'domains/test_domain/messages/asdfasdf', body: JSON.generate(message))
end
subject { mg_obj.get_url('https://ci.api.mailgan.com/domains/test_domain/messages/asdfasdf') }
context 'when success request' do
let(:response_raw) { double }
let(:response_real) { double }
before do
allow(RestClient::Request).to receive(:execute).with(any_args).and_return(response_raw)
allow(Howitzer::MailgunApi::Response).to receive(:new).with(response_raw).and_return(response_real)
end
it { is_expected.to eq(response_real) }
it { expect(subject.to_h).to eq(message) }
end
context 'when error happens' do
before do
Expand Down

0 comments on commit be42768

Please sign in to comment.