Content of bounced emails in tests for Action Mailbox - ruby-on-rails

How can I check the content of a bounced email?
require "rails_helper"
RSpec.describe EventsMailbox do
it "requires a current user" do
expect(mail_processed).to have_bounced
# here I would like to check the content of the bounced email
end
def mail
Mail.new(
from: "dorian#dorianmarie.fr",
subject: "Some subject",
body: "Some body"
)
end
def mail_processed
#mail_processed ||= process(mail)
end
end

Here is how I did it, the key was that perform_enqueued_jobs that allows emails to be retrieved from ActionMailer::Base.deliveries:
require "rails_helper"
RSpec.describe EventsMailbox do
include ActiveJob::TestHelper
around(:each) { |example| perform_enqueued_jobs { example.run } }
it "requires a current user", focus: true do
expect(mail_processed).to have_bounced
expect(last_delivery.from).to eq(["contact#socializus.app"])
expect(last_delivery.to).to eq(["dorian#dorianmarie.fr"])
expect(last_delivery_text).to(
include("You need to be registered on Socializus")
)
end
def mail
Mail.new(
from: "dorian#dorianmarie.fr",
to: "party#events.socializus.app",
subject: "Some subject",
body: "Some body"
)
end
def mail_processed
#mail_processed ||= process(mail)
end
def last_delivery
ActionMailer::Base.deliveries.last
end
def last_delivery_text
return unless last_delivery
text_part = last_delivery.parts.detect { _1.mime_type == "text/plain" }
return unless text_part
text_part.body.to_s
end
end

Related

How make stub on request with VCR?

How can I do stub on request with VCR?
The problem is that real request is made in the test, which I want to stub.
RSpec.describe CreditRegisterLoader do
describe ".call" do
it "should create credit institutions", :vcr do
Timecop.freeze(2020, 3, 25, 13, 0, 0) do
expect { described_class.new.call }.to change { CreditInstitution.count }.by(4965)
end
end
end
end
Also here is code of my class CreditRegisterLoader which I want to test:
class CreditRegisterLoader < ApplicationService
def initialize
#timestamp = (Time.now - 1.minute).to_i.to_s
end
def call
sai_response = get_credit_institutions
unless sai_response
Airbrake.notify("invalid_sai_response")
return
end
begin
CreditInstitutionUpdater.new(JSON.parse(sai_response.body)).create
rescue => error
Airbrake.notify(error)
end
end
private
def get_credit_institutions
RestClient::Request.execute(
method: :post,
url: "https://sai.dpl.europa.eu/register/api/search/entities?t=#{#timestamp}",
headers: {
"Content-Type" => "application/json",
"Accept" => "application/json",
},
payload: JSON.generate({"$and": [{"_messagetype": "SAIDPL"}]})
)
end
end
I would suggest the following solution
RSpec.describe CreditRegisterLoader do
describe ".call" do
let(:response) { OpenStruct.new(body: File.read("yours fixtures in json format")) }
context "Failure flow" do
it "should notify Airbrake with error" do
error = StandardError.new("Bad Request")
expect(RestClient::Request).to receive(:execute).and_return(response)
expect_any_instance_of(CreditInstitutionUpdater).to receive(:create).and_raise(error)
expect(Airbrake).to receive(:notify).with(error)
subject.call
end
it "should notify Airbrake with invalid_sai_response" do
expect(subject).to receive(:get_credit_institutions).and_return(nil)
expect(Airbrake).to receive(:notify).with("invalid_sai_response")
subject.call
end
end
context "Successfully flow" do
it "should creates credit institutions" do
expect(RestClient::Request).to receive(:execute).and_return(response)
expect { subject.call }.to change { CreditInstitution.count }.by(2)
fixtures_response = JSON.parse(response.body)
end
end
end
end

Rails RSpec use mock instead of stub

I've got a lib which deals with some external API. The details are irrelevant, everything works well:
module SampleApi
extend self
def fetch_mm_recommender(email:)
handle_response(client.get("#{Sample.test_uri}/api/v1/mm_recommender", email: email))
end
private
def handle_response(response)
return parse_body(response) if response.success?
Rails.logger.error "Request failed with code: #{response.status}, message: #{response.reason_phrase}"
response.reason_phrase
end
def parse_body(response)
JSON.parse(response.body)
end
def client
#client ||= HttpHelper.new(with_cert: true)
end
end
I've made RSpec test for that:
describe '#fetch_mm_recommender' do
subject(:fetch_physician) { described_class.fetch_mm_recommender(email: email) }
let(:email) { 'michael_scott#physician.com' }
context 'when email exists in SAMPLE' do
before do
stub_request(:get, %r{/api/v1/mm_recommender})
.to_return(status: 200,
body: sample_response,
headers: {})
end
it 'fetch user data' do
expect(fetch_physician).to be_present
expect(fetch_physician).to eq(JSON.parse(sample_response))
end
end
Shouldn't I use mock rather than stub? Would there be any benefit to this? if so, what should such a test look like?
[EDIT]
context 'when email does not exist' do
let(:email) { 'test#test.com' }
let(:sample_response) { 'Not found'.to_json }
before do
stub_request(:get, %r{/api/v1/mm_recommender})
.to_return(status: 404,
body: sample_response,
headers: {})
allow(Rails.logger).to receive(:error)
end
it 'does not retrieve data' do
expect(fetch_physician).to eq('')
expect(Rails.logger).to have_received(:error).with('Request failed with code: 404, message: ')
end
end

How to set header_cookies in rspec mailers

In my mailers I generate pdf file for attachments, using header_cookies (pass it from controller). How do I generate header_cookies in the articles_spec.rb test ?
Article Mailer:
class ArticlesMailer < ActionMailer::Base
def send_article(article, header_cookies)
#article = article
#user = #article.user
attachments['article.pdf'] = prepare_pdf_file
mail(
subject: "Article №#{#article.id}",
to: #user.email
)
end
def prepare_pdf_file(header_cookies)
Grover.new(pdf_view_article_url(#article), cookies: header_cookies).to_pdf
end
end
Article Controller
def create
#articele = Article.new(article_params)
if #article.save
ArticlesMailer.send_article(#article, header_cookies).deliver_now
else
render :new
end
end
artilces_spec.rb
RSpec.describe ArticlesMailer, type: :mailer do
describe "send_article" do
let!(:admin) { create(:user, :superadmin) }
let!(:article) { create(:article, user: admin) }
let!(:mail) { ArticlesMailer.send_article(article, header_cookies) }
let(:article_preview_page) { preview_article_path(user_id: admin.id, id: article.id) }
it "renders the headers" do
expect(mail.subject).to eq("Article №#{article.id}")
expect(mail.to).to eq admin.email
end
it "renders the body" do
expect(mail.body.encoded).to match("Hi #{admin.name}")
end
end
end
Did you try creating the header_cookies object and pass it to the send_article call?
let!(:header_cookies) do
[
{ name: 'sign_username', value: 'any#any.com', domain: 'mydomain' },
{ name: '_session_id', value: '9c014df0b699d8dc08d1c472f8cc594c', domain: 'mydomain' }
]
end

How to assert that a method call was not made, without any_instance?

I have a class, that in one situation should call :my_method, but in another situation must not call method :my_method. I would like to test both cases. Also, I would like the test to document the cases when :my_method should not be called.
Using any_instance is generally discouraged, so I would be happy to learn a nice way to replace it.
This code snippet is a reduced example on what I kind of test I would like to write.
class TestSubject
def call
call_me
end
def call_me; end
def never_mind; end
end
require 'rspec'
spec = RSpec.describe 'TestSubject' do
describe '#call' do
it 'calls #call_me' do
expect_any_instance_of(TestSubject).to receive(:call_me)
TestSubject.new.call
end
it 'does not call #never_mind' do
expect_any_instance_of(TestSubject).not_to receive(:never_mind)
TestSubject.new.call
end
end
end
spec.run # => true
It works, but uses expect_any_instance_of method, which is not recommended.
How to replace it?
I'll do somehting like that
describe TestSubject do
describe '#call' do
it 'does not call #something' do
subject = TestSubject.new
allow(subject).to receive(:something)
subject.call
expect(subject).not_to have_received(:something)
end
end
end
Hope this helped !
This is how I normally unit-test. I updated the code to support other possible questions you (or other readers) may have in the future.
class TestSubject
def call
some_call_me_value = call_me
call_you(some_call_me_value)
end
def call_me; end
def call_you(x); end
def never_mind; end
class << self
def some_class_method_a; end
def some_class_method_b(x, y); end
end
end
require 'rspec'
spec = RSpec.describe TestSubject do
context 'instance methods' do
let(:test_subject) { TestSubject.new }
describe '#call' do
let(:args) { nil }
let(:mocked_call_me_return_value) { 'somecallmevalue' }
subject { test_subject.call(*args) }
before do
allow(test_subject).to receive(:call_me) do
mocked_call_me_return_value
end
end
it 'calls #call_me' do
expect(test_subject).to receive(:call_me).once
subject
end
it 'calls #call_you with call_me value as the argument' do
expect(test_subject).to receive(:call_you).once.with(mocked_call_me_return_value)
subject
end
it 'does not call #never_mind' do
expect(test_subject).to_not receive(:never_mind)
subject
end
it 'calls in order' do
expect(test_subject).to receive(:call_me).once.ordered
expect(test_subject).to receive(:call_you).once.ordered
subject
end
end
describe '#call_me' do
let(:args) { nil }
subject { test_subject.call_me(*args) }
# it ...
end
describe '#call_you' do
let(:args) { nil }
subject { test_subject.call_you(*args) }
shared_examples_for 'shared #call_you behaviours' do
it 'calls your phone number'
it 'creates a Conversation record'
end
# just an example of argument-dependent behaviour spec
context 'when argument is true' do
let(:args) { [true] }
it 'does something magical'
it_behaves_like 'shared #call_you behaviours'
end
# just an example of argument-dependent behaviour spec
context 'when argument is false' do
let(:args) { [false] }
it 'does something explosive'
it_behaves_like 'shared #call_you behaviours'
end
end
end
context 'class methods' do
let(:args) { nil }
describe '#some_class_method_a' do
let(:args) { nil }
subject { TestSubject.some_class_method_a(*args) }
# it ...
end
describe '#some_class_method_b' do
let(:args) { [1, 2] }
subject { TestSubject.some_class_method_b(*args) }
# it ...
end
end
end
spec.run # => true
Do not test if some method was called or wasn't.
This will tight your tests to the implementation details and will force you to change tests every time you refactor(change implementation details without changing the behaviour) your class under test.
Instead test against return value or changed application state.
It is difficult come up with the example, you didn't provide enough context about the class under the test.
class CreateEntity
def initialize(name)
#name = name
end
def call
if company_name?(#name)
create_company
else
create_person
end
end
def create_person
Person.create!(:name => #name)
end
def create_company
Company.create!(:name => #name)
end
end
# tests
RSpec.describe CreateEntity do
let(:create) { CreateEntity.new(name).call }
describe '#call' do
context 'when person name is given' do
let(:name) { 'Firstname Lastname' }
it 'creates a person' do
expect { create }.to change { Person.count }.by(1)
end
it 'do not create a company' do
expect { create }.not_to change { Company.count }
end
end
context 'when company name is given' do
let(:name) { 'Name & Sons Ltd' }
it 'creates a company' do
expect { create }.to change { Company.count }.by(1)
end
it 'do not create a person' do
expect { create }.not_to change { Person.count }
end
end
end
end
With tests above I would be able to change how CreateEntity.call method implemented without changing tests as far as behaviour remain same.

rspec 3.4 test controller concern with response.body.read

I have the following controller concern that is used for authentication:
module ValidateEventRequest
extend ActiveSupport::Concern
def event_request_verified?(request)
sha256 = OpenSSL::Digest::SHA256.new
secret = app_client_id
body = request.body.read
signature = OpenSSL::HMAC.hexdigest(sha256, secret, body)
([signature] & [request.headers['X-Webhook-Signature'], request.headers['X-Api-Signature']]).present?
end
private
def app_client_id
ENV['APP_CLIENT_ID']
end
end
So far I have the following Rspec Test setup to hit this:
RSpec.describe ValidateEventRequest, type: :concern do
let!(:current_secret) { SecureRandom.hex }
describe '#event_request_verified?' do
it 'validates X-Webhook-Signature' do
# TBD
end
it 'validates X-Api-Signature' do
# TBD
end
end
end
I started out with stubbing the request, then mocking and stubbing, and now I am down to scrapping what I have and seeking assistance. 100% coverage is important to me and I am looking for some pointers on how to structure tests that cover this 100%.
object_double is handy for testing concerns:
require 'rails_helper'
describe MyClass do
subject { object_double(Class.new).tap {|c| c.extend MyClass} }
it "extends the subject" do
expect(subject.respond_to?(:some_method_in_my_class)).to be true
# ...
Then you can test subject like any other class. Of course you need to pass in the appropriate arguments when testing methods, which may mean creating additional mocks -- in your case a request object.
Here is how I solved this issue, and I am open to ideas:
RSpec.describe ValidateApiRequest, type: :concern do
let!(:auth_secret) { ENV['APP_CLIENT_ID'] }
let!(:auth_sha256) { OpenSSL::Digest::SHA256.new }
let!(:auth_body) { 'TESTME' }
let(:object) { FakeController.new }
before(:each) do
allow(described_class).to receive(:secret).and_return(auth_secret)
class FakeController < ApplicationController
include ValidateApiRequest
end
end
after(:each) do
Object.send :remove_const, :FakeController
end
describe '#event_request_verified?' do
context 'X-Api-Signature' do
it 'pass' do
request = OpenStruct.new(headers: { 'X-Api-Signature' => OpenSSL::HMAC.hexdigest(auth_sha256, auth_secret, auth_body) }, raw_post: auth_body)
expect(object.event_request_verified?(request)).to be_truthy
end
it 'fail' do
request = OpenStruct.new(headers: { 'X-Api-Signature' => OpenSSL::HMAC.hexdigest(auth_sha256, 'not-the-same', auth_body) }, raw_post: auth_body)
expect(object.event_request_verified?(request)).to be_falsey
end
end
context 'X-Webhook-Signature' do
it 'pass' do
request = OpenStruct.new(headers: { 'X-Webhook-Signature' => OpenSSL::HMAC.hexdigest(auth_sha256, auth_secret, auth_body) }, raw_post: auth_body)
expect(object.event_request_verified?(request)).to be_truthy
end
it 'fail' do
request = OpenStruct.new(headers: { 'X-Webhook-Signature' => OpenSSL::HMAC.hexdigest(auth_sha256, 'not-the-same', auth_body) }, raw_post: auth_body)
expect(object.event_request_verified?(request)).to be_falsey
end
end
end
end

Resources