From 641411af3dd738226612b7dd4f3616db2af52410 Mon Sep 17 00:00:00 2001 From: Petro Smachylo Date: Tue, 11 May 2021 10:53:35 +0300 Subject: [PATCH] De 87 (#238) * 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 * DE-87: railgun tests Co-authored-by: Lukas Barry --- spec/integration/mailer_spec.rb | 67 +++++++++++ spec/unit/railgun/mailer_spec.rb | 54 +++++++++ vcr_cassettes/mailer_invalid_domain.yml | 109 +++++++++++++++++ vcr_cassettes/message_deliver.yml | 149 ++++++++++++++++++++++++ 4 files changed, 379 insertions(+) create mode 100644 spec/integration/mailer_spec.rb create mode 100644 vcr_cassettes/mailer_invalid_domain.yml create mode 100644 vcr_cassettes/message_deliver.yml diff --git a/spec/integration/mailer_spec.rb b/spec/integration/mailer_spec.rb new file mode 100644 index 0000000..fda6dc3 --- /dev/null +++ b/spec/integration/mailer_spec.rb @@ -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: '

Test!

'.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 diff --git a/spec/unit/railgun/mailer_spec.rb b/spec/unit/railgun/mailer_spec.rb index af61722..6b55268 100644 --- a/spec/unit/railgun/mailer_spec.rb +++ b/spec/unit/railgun/mailer_spec.rb @@ -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) @@ -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 diff --git a/vcr_cassettes/mailer_invalid_domain.yml b/vcr_cassettes/mailer_invalid_domain.yml new file mode 100644 index 0000000..6df08ed --- /dev/null +++ b/vcr_cassettes/mailer_invalid_domain.yml @@ -0,0 +1,109 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.mailgun.net/v3/not-our-doma.in/messages + body: + encoding: US-ASCII + string: from[]=sally%40not-our-doma.in&subject[]=subject&html[]=%3Cp%3ETest%21%3C%2Fp%3E&text[]=Test%21&to[]=bob%40DOMAIN.TEST&h%3Acontent-type=multipart%2Falternative%3B+boundary%3D%22--%3D%3D_mimepart_6098fb5a12edf_78633ff4e00521889696a%22%3B+charset%3DUTF-8 + headers: + Accept: + - "*/*" + User-Agent: + - rest-client/2.1.0 (darwin18.7.0 x86_64) ruby/2.2.2p95 + Content-Length: + - '262' + Content-Type: + - application/x-www-form-urlencoded + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Host: + - api.mailgun.net + Authorization: + - Basic YXBpOmQ5MTViNWNkYjlhNTgzNjg1ZDhmM2ZiMWJlYzBmMjBmLTA3YmM3YjA1LWZhNDgxNmEx + response: + status: + code: 401 + message: UNAUTHORIZED + headers: + Access-Control-Allow-Headers: + - Content-Type, x-requested-with, Authorization + Access-Control-Allow-Methods: + - GET, POST, PUT, DELETE, OPTIONS + Access-Control-Allow-Origin: + - "*" + Access-Control-Max-Age: + - '600' + Cache-Control: + - no-store + Content-Type: + - text/html; charset=utf-8 + Date: + - Mon, 10 May 2021 09:22:34 GMT + Server: + - nginx + Www-Authenticate: + - Basic realm="MG API" + Content-Length: + - '9' + Connection: + - keep-alive + body: + encoding: UTF-8 + string: Forbidden + http_version: + recorded_at: Mon, 10 May 2021 09:22:34 GMT +- request: + method: post + uri: https://api.mailgun.net/v3/not-our-doma.in/messages + body: + encoding: US-ASCII + string: from[]=sally%40not-our-doma.in&subject[]=subject&html[]=%3Cp%3ETest%21%3C%2Fp%3E&text[]=Test%21&to[]=bob%40DOMAIN.TEST&h%3Acontent-type=multipart%2Falternative%3B+boundary%3D%22--%3D%3D_mimepart_6098fb5a12edf_78633ff4e00521889696a%22%3B+charset%3DUTF-8 + headers: + Accept: + - "*/*" + User-Agent: + - rest-client/2.1.0 (darwin18.7.0 x86_64) ruby/2.2.2p95 + Content-Length: + - '262' + Content-Type: + - application/x-www-form-urlencoded + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Host: + - api.mailgun.net + Authorization: + - Basic YXBpOmQ5MTViNWNkYjlhNTgzNjg1ZDhmM2ZiMWJlYzBmMjBmLTA3YmM3YjA1LWZhNDgxNmEx + response: + status: + code: 401 + message: UNAUTHORIZED + headers: + Access-Control-Allow-Headers: + - Content-Type, x-requested-with, Authorization + Access-Control-Allow-Methods: + - GET, POST, PUT, DELETE, OPTIONS + Access-Control-Allow-Origin: + - "*" + Access-Control-Max-Age: + - '600' + Cache-Control: + - no-store + Content-Type: + - text/html; charset=utf-8 + Date: + - Mon, 10 May 2021 09:22:40 GMT + Server: + - nginx + Www-Authenticate: + - Basic realm="MG API" + Content-Length: + - '9' + Connection: + - keep-alive + body: + encoding: UTF-8 + string: Forbidden + http_version: + recorded_at: Mon, 10 May 2021 09:22:40 GMT +recorded_with: VCR 3.0.3 diff --git a/vcr_cassettes/message_deliver.yml b/vcr_cassettes/message_deliver.yml new file mode 100644 index 0000000..7fc19d9 --- /dev/null +++ b/vcr_cassettes/message_deliver.yml @@ -0,0 +1,149 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.mailgun.net/v3/DOMAIN.TEST/messages + body: + encoding: US-ASCII + string: from[]=bob%40DOMAIN.TEST&subject[]=subject&html[]=%3Cp%3ETest%21%3C%2Fp%3E&text[]=Test%21&to[]=bob%40DOMAIN.TEST&h%3Acontent-type=multipart%2Falternative%3B+boundary%3D%22--%3D%3D_mimepart_6098f913d64ca_62833fd4d74521802775%22%3B+charset%3DUTF-8 + headers: + Accept: + - "*/*" + User-Agent: + - rest-client/2.1.0 (darwin18.7.0 x86_64) ruby/2.2.2p95 + Content-Length: + - '261' + Content-Type: + - application/x-www-form-urlencoded + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Host: + - api.mailgun.net + Authorization: + - Basic YXBpOmQ5MTViNWNkYjlhNTgzNjg1ZDhmM2ZiMWJlYzBmMjBmLTA3YmM3YjA1LWZhNDgxNmEx + response: + status: + code: 200 + message: OK + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Headers: + - Content-Type, x-requested-with, Authorization + Access-Control-Allow-Methods: + - GET, POST, PUT, DELETE, OPTIONS + Access-Control-Allow-Origin: + - "*" + Access-Control-Max-Age: + - '600' + Cache-Control: + - no-store + Content-Disposition: + - inline + Content-Type: + - application/json + Date: + - Mon, 10 May 2021 09:12:52 GMT + Server: + - nginx + Strict-Transport-Security: + - max-age=63072000; includeSubDomains + X-Ratelimit-Limit: + - '2000000' + X-Ratelimit-Remaining: + - '1999999' + X-Ratelimit-Reset: + - '1620637982656' + X-Recipient-Limit: + - '1000000' + X-Recipient-Remaining: + - '999999' + X-Recipient-Reset: + - '1620637982657' + Content-Length: + - '136' + Connection: + - keep-alive + body: + encoding: UTF-8 + string: |- + { + "id": "<20210510091252.1.D6347B73DA43962E@DOMAIN.TEST>", + "message": "Queued. Thank you." + } + http_version: + recorded_at: Mon, 10 May 2021 09:12:52 GMT +- request: + method: post + uri: https://api.mailgun.net/v3/DOMAIN.TEST/messages + body: + encoding: US-ASCII + string: from[]=bob%40DOMAIN.TEST&subject[]=subject&html[]=%3Cp%3ETest%21%3C%2Fp%3E&text[]=Test%21&to[]=bob%40DOMAIN.TEST&h%3Amessage-id=%3C20210510091252.1.D6347B73DA43962E%40DOMAIN.TEST%3E&h%3Acontent-type=multipart%2Falternative%3B+boundary%3D%22--%3D%3D_mimepart_6098f913d64ca_62833fd4d74521802775%22%3B+charset%3DUTF-8 + headers: + Accept: + - "*/*" + User-Agent: + - rest-client/2.1.0 (darwin18.7.0 x86_64) ruby/2.2.2p95 + Content-Length: + - '370' + Content-Type: + - application/x-www-form-urlencoded + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Host: + - api.mailgun.net + Authorization: + - Basic YXBpOmQ5MTViNWNkYjlhNTgzNjg1ZDhmM2ZiMWJlYzBmMjBmLTA3YmM3YjA1LWZhNDgxNmEx + response: + status: + code: 200 + message: OK + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Headers: + - Content-Type, x-requested-with, Authorization + Access-Control-Allow-Methods: + - GET, POST, PUT, DELETE, OPTIONS + Access-Control-Allow-Origin: + - "*" + Access-Control-Max-Age: + - '600' + Cache-Control: + - no-store + Content-Disposition: + - inline + Content-Type: + - application/json + Date: + - Mon, 10 May 2021 09:12:56 GMT + Server: + - nginx + Strict-Transport-Security: + - max-age=63072000; includeSubDomains + X-Ratelimit-Limit: + - '2000000' + X-Ratelimit-Remaining: + - '1999998' + X-Ratelimit-Reset: + - '1620637982656' + X-Recipient-Limit: + - '1000000' + X-Recipient-Remaining: + - '999998' + X-Recipient-Reset: + - '1620637982657' + Content-Length: + - '136' + Connection: + - keep-alive + body: + encoding: UTF-8 + string: |- + { + "id": "<20210510091252.1.D6347B73DA43962E@DOMAIN.TEST>", + "message": "Queued. Thank you." + } + http_version: + recorded_at: Mon, 10 May 2021 09:12:56 GMT +recorded_with: VCR 3.0.3