Skip to content

Commit

Permalink
De 87 (#238)
Browse files Browse the repository at this point in the history
* mailgun 1.2.4 (#229)

* DE-73: default content-type (#216)

* DE-73: default content-type

* DE-73: refactor

* DE-62: reset recipient_variables (#218)

* DE-64: set single track opens-clicks (#219)

* DE-67: accept frozen mailgun options to message body (#221)

* DE-71: fix parse addres when full_name is nil (#222)

* update gem version

* fixes batch add unsubscribes and adds test (#187)

* update version to 1.2.3

* DE-56: ignore mime-version header (#225)

* DE-53: allow string values in custom variables (#226)

* DE-86: update mailgun rails load (#227)

* DE-81: fail if domain is missing (#228)

* update version to 1.2.4

Co-authored-by: Lukas Barry <LukasBBarry@gmail.com>

* DE-87: railgun tests

Co-authored-by: Lukas Barry <LukasBBarry@gmail.com>
  • Loading branch information
Petro Smachylo and LukasBarry authored May 11, 2021
1 parent 30479e9 commit 641411a
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 0 deletions.
67 changes: 67 additions & 0 deletions spec/integration/mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'spec_helper'
require 'json'
require 'logger'
require 'railgun'
require 'mailgun'
require 'mailgun/exceptions/exceptions'

ActionMailer::Base.raise_delivery_errors = true
Rails.logger = Logger.new('/dev/null')
Rails.logger.level = Logger::DEBUG

class UnitTestMailer < ActionMailer::Base
default from: 'unittest@example.org'

def plain_message(address, from, subject, headers)
headers(headers)
mail(to: address, from: from, subject: subject) do |format|
format.text { render plain: 'Test!' }
format.html { render html: '<p>Test!</p>'.html_safe }
end
end
end

vcr_opts = { :cassette_name => 'message_deliver' }

describe 'Message deliver', vcr: vcr_opts do
let(:domain) { TESTDOMAIN }
let(:config) do
{
api_key: APIKEY,
domain: domain
}
end
let(:mail) { UnitTestMailer.plain_message("bob@#{domain}", "bob@#{domain}", 'subject', {}) }

it 'successfully delivers message' do
result = Railgun::Mailer.new(config).deliver!(mail)
result.to_h!

expect(result.body['message']).to eq('Queued. Thank you.')
expect(result.body).to include('id')
expect(result.code).to eq(200)
end
end

vcr_opts = { :cassette_name => 'mailer_invalid_domain' }

describe 'Invalid domain', vcr: vcr_opts do
let(:domain) { 'not-our-doma.in' }
let(:config) do
{
api_key: APIKEY,
domain: domain
}
end
let(:mail) { UnitTestMailer.plain_message("bob@#{domain}", 'sally@not-our-doma.in' 'subject', {}) }

it 'raises expected error' do

Railgun::Mailer.new(config).deliver!(mail)
rescue Mailgun::CommunicationError => err
expect(err.message).to eq('401 Unauthorized: Forbidden - Invalid Domain or API key')
else
fail

end
end
54 changes: 54 additions & 0 deletions spec/unit/railgun/mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ def message_with_template(address, subject, template_name)
expect(@mailer_obj.mailgun_client).to be_a(Mailgun::Client)
end

context 'when config does not have api_key or domain' do
it 'raises configuration error' do
config = {
api_key: {}
}

expect { Railgun::Mailer.new(config) }.to raise_error(Railgun::ConfigurationError)
end
end

context 'when fake_message_send is present in config' do
it 'enables test mode' do
config = {
api_key: {},
domain: {},
fake_message_send: true
}
client_double = double(Mailgun::Client)
allow(Mailgun::Client).to receive(:new).and_return(client_double)
expect(client_double).to receive(:enable_test_mode!)

Railgun::Mailer.new(config)
end
end

it 'properly creates a message body' do
message = UnitTestMailer.plain_message('test@example.org', 'Test!', {})
body = Railgun.transform_for_mailgun(message)
Expand Down Expand Up @@ -312,4 +337,33 @@ def message_with_template(address, subject, template_name)
end
end
end

describe 'deliver!' do
let(:config) do
{
api_key: 'api_key',
domain: 'domain'
}
end
let(:mail) { UnitTestMailer.plain_message('test@example.org', '', {}) }
let(:response) do
response = Struct.new(:code, :id)
response.new(200, rand(50..100))
end

it 'initiates client message send' do
result = { from: 'test@example.org' }
allow(Railgun).to receive(:transform_for_mailgun).and_return(result)

expect_any_instance_of(Mailgun::Client).to receive(:send_message)
.with(config[:domain], result)
.and_return(response)
Railgun::Mailer.new(config).deliver!(mail)
end

it 'returns response' do
expect_any_instance_of(Mailgun::Client).to receive(:send_message).and_return(response)
expect(Railgun::Mailer.new(config).deliver!(mail)).to eq(response)
end
end
end
109 changes: 109 additions & 0 deletions vcr_cassettes/mailer_invalid_domain.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

149 changes: 149 additions & 0 deletions vcr_cassettes/message_deliver.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 641411a

Please sign in to comment.