diff --git a/.rspec b/.rspec index 65e0669b..931d72a0 100644 --- a/.rspec +++ b/.rspec @@ -2,4 +2,4 @@ --color --order rand --profile 5 ---require spec_helper +--require rails_helper diff --git a/app/classes/fnf/event_reporter.rb b/app/classes/fnf/event_reporter.rb index 036e9c91..d618eb91 100644 --- a/app/classes/fnf/event_reporter.rb +++ b/app/classes/fnf/event_reporter.rb @@ -2,6 +2,7 @@ require 'newrelic_rpm' require 'singleton' + module FnF class EventReporter class << self diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index f1609efd..b9aebc37 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -37,7 +37,7 @@ def create return redirect_to root_path unless @payment.can_view?(current_user) if @payment.save_and_charge! - PaymentMailer.payment_received(@payment).deliver_now + PaymentReceivedMailer.payment_received(@payment).deliver_now @payment.ticket_request.mark_complete redirect_to @payment, notice: 'Payment was successfully received.' else @@ -83,7 +83,7 @@ def mark_received if @payment @payment.mark_received @payment.ticket_request.mark_complete - PaymentMailer.payment_received(@payment).deliver_now + PaymentReceivedMailer.payment_received(@payment).deliver_now redirect_to :back end end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb new file mode 100644 index 00000000..630923de --- /dev/null +++ b/app/mailers/application_mailer.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class ApplicationMailer < ActionMailer::Base + DEFAULT_SENDER_EMAIL = 'tickets@fnf.org' + DEFAULT_REPLY_TO_EMAIL = 'tickets@fnf.org' + + layout 'email' + + abstract! +end diff --git a/app/mailers/eald_payment_mailer.rb b/app/mailers/eald_payment_mailer.rb index b18698ae..bddd9a8f 100644 --- a/app/mailers/eald_payment_mailer.rb +++ b/app/mailers/eald_payment_mailer.rb @@ -1,29 +1,12 @@ # frozen_string_literal: true -class EaldPaymentMailer < ActionMailer::Base - DEFAULT_SENDER_EMAIL = 'tickets@fnf.org' - DEFAULT_REPLY_TO_EMAIL = 'tickets@fnf.org' - - helper PaymentsHelper - - layout 'email' - - def eald_payment_received(eald_payment) - @eald_payment = eald_payment - @event = @eald_payment.event - mail to: "#{@eald_payment.name} <#{@eald_payment.email}>", +class EaldPaymentMailer < PaymentReceivedMailer + def eald_payment_received(payment) + @payment = payment + @event = @payment.event + mail to: "#{@payment.name} <#{@payment.email}>", from: from_email, reply_to: reply_to_email, subject: "Your payment for #{@event.name} Early Arrival/Late Departure passes has been received" end - - private - - def from_email - "#{@event.name} <#{DEFAULT_SENDER_EMAIL}>" - end - - def reply_to_email - "#{@event.name} Ticketing <#{DEFAULT_REPLY_TO_EMAIL}>" - end end diff --git a/app/mailers/payment_mailer.rb b/app/mailers/payment_received_mailer.rb similarity index 67% rename from app/mailers/payment_mailer.rb rename to app/mailers/payment_received_mailer.rb index 7a0e33a1..8fd4adc0 100644 --- a/app/mailers/payment_mailer.rb +++ b/app/mailers/payment_received_mailer.rb @@ -1,18 +1,9 @@ # frozen_string_literal: true -class PaymentMailer < ActionMailer::Base - DEFAULT_SENDER_EMAIL = 'tickets@fnf.org' - DEFAULT_REPLY_TO_EMAIL = 'tickets@fnf.org' - - helper PaymentsHelper - - layout 'email' - +class PaymentReceivedMailer < ApplicationMailer def payment_received(payment) - @payment = payment - @ticket_request = @payment.ticket_request - @event = @ticket_request.event - @user = @ticket_request.user + self.payment = payment + mail to: "#{@user.name} <#{@user.email}>", from: from_email, reply_to: reply_to_email, @@ -28,4 +19,11 @@ def from_email def reply_to_email "#{@event.name} Ticketing <#{DEFAULT_REPLY_TO_EMAIL}>" end + + def payment=(payment) + @payment = payment + @ticket_request = @payment.ticket_request + @event = @ticket_request&.event + @user = @ticket_request&.user + end end diff --git a/app/mailers/ticket_request_mailer.rb b/app/mailers/ticket_request_mailer.rb index 7ca777d9..fcfa649b 100644 --- a/app/mailers/ticket_request_mailer.rb +++ b/app/mailers/ticket_request_mailer.rb @@ -3,9 +3,6 @@ require 'awesome_print' class TicketRequestMailer < ActionMailer::Base - DEFAULT_SENDER_EMAIL = 'tickets@fnf.org' - DEFAULT_REPLY_TO_EMAIL = 'tickets@fnf.org' - layout 'email' def request_received(ticket_request) diff --git a/app/models/eald_payment.rb b/app/models/eald_payment.rb index e5702ed2..b13cd5a5 100644 --- a/app/models/eald_payment.rb +++ b/app/models/eald_payment.rb @@ -26,11 +26,13 @@ class EaldPayment < ApplicationRecord attr_accessible :event_id, :name, :email, :early_arrival_passes, :late_departure_passes, :stripe_card_token - validates :early_arrival_passes, presence: true, - numericality: { only_integer: true, greater_than_or_equal_to: 0 } + validates :early_arrival_passes, + presence: true, + numericality: { only_integer: true, greater_than_or_equal_to: 0 } - validates :late_departure_passes, presence: true, - numericality: { only_integer: true, greater_than_or_equal_to: 0 } + validates :late_departure_passes, + presence: true, + numericality: { only_integer: true, greater_than_or_equal_to: 0 } attr_accessor :stripe_card_token diff --git a/app/views/eald_payment_mailer/eald_payment_received.html.haml b/app/views/eald_payment_mailer/eald_payment_received.html.haml index e7891bc9..a16a1839 100644 --- a/app/views/eald_payment_mailer/eald_payment_received.html.haml +++ b/app/views/eald_payment_mailer/eald_payment_received.html.haml @@ -1,13 +1,13 @@ %p - Hi #{@eald_payment.name}, + Hi #{@payment.name}, %p Your payment of - %b= number_to_currency(@eald_payment.amount_charged_cents.to_f / 100) + %b= number_to_currency(@payment.amount_charged_cents.to_f / 100) for - %b= @eald_payment.early_arrival_passes + %b= @payment.early_arrival_passes early arrival passes and - %b= @eald_payment.late_departure_passes + %b= @payment.late_departure_passes late departure passes for %b= @event.name was received. Thank you! @@ -23,5 +23,4 @@ %p More information can be found at: - = link_to 'Early Arrival / Late Departure Policy', - 'https://fnf.page.link/eald-policy' + = link_to 'Early Arrival / Late Departure Policy', 'https://fnf.page.link/eald-policy' diff --git a/app/views/payment_mailer/payment_received.html.haml b/app/views/payment_received_mailer/payment_received.html.haml similarity index 100% rename from app/views/payment_mailer/payment_received.html.haml rename to app/views/payment_received_mailer/payment_received.html.haml diff --git a/spec/factories/eald_payment.rb b/spec/factories/eald_payment.rb new file mode 100644 index 00000000..2cedea1a --- /dev/null +++ b/spec/factories/eald_payment.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :eald_payment do + event + name { Faker::Name.name } + early_arrival_passes { 10 } + amount_charged_cents { 1000 } + late_departure_passes { 5 } + email { Faker::Internet.email } + stripe_charge_id { Faker::Alphanumeric.alphanumeric(number: 10) } + end +end diff --git a/spec/lib/fnf/event_reporter_spec.rb b/spec/lib/fnf/event_reporter_spec.rb index 1f7ec137..515eae3e 100644 --- a/spec/lib/fnf/event_reporter_spec.rb +++ b/spec/lib/fnf/event_reporter_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'fnf/csv_reader' +require 'rails_helper' module FnF RSpec.describe EventReporter do diff --git a/spec/mailers/eald_payment_mailer_spec.rb b/spec/mailers/eald_payment_mailer_spec.rb new file mode 100644 index 00000000..cd890d80 --- /dev/null +++ b/spec/mailers/eald_payment_mailer_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe EaldPaymentMailer do + let(:payment) { create(:eald_payment) } + let(:event) { payment.event } + + describe '#eald_payment_received' do + subject(:mail) { described_class.eald_payment_received(payment) } + + let(:expected_subject) { "Your payment for #{event.name} Early Arrival/Late Departure passes has been received" } + + its(:subject) { is_expected.to eql(expected_subject) } + + its(:to) { is_expected.to eql([payment.email]) } + + its('body.encoded') { is_expected.to match(event.name) } + end +end diff --git a/spec/mailers/payment_mailer_spec.rb b/spec/mailers/payment_mailer_spec.rb deleted file mode 100644 index 7d125df4..00000000 --- a/spec/mailers/payment_mailer_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -describe PaymentMailer do - let(:user) { User.make! } - let(:event) { Event.make! name: 'Test Event' } - - let(:ticket_request) do - TicketRequest.make! event:, - user: - end - - let(:payment) { Payment.make! ticket_request: } - - describe '#payment_received' do - let(:mail) { described_class.payment_received(payment) } - - it 'renders the subject' do - mail.subject.should == 'Your payment for Test Event has been received' - end - - it 'sends to the owner of the ticket request' do - mail.to.should == [user.email] - end - - it "includes the user's name" do - mail.body.encoded.should match(user.first_name) - end - - it "includes the event's name" do - mail.body.encoded.should match('Test Event') - end - end -end diff --git a/spec/mailers/payment_received_mailer_spec.rb b/spec/mailers/payment_received_mailer_spec.rb new file mode 100644 index 00000000..aaf262cc --- /dev/null +++ b/spec/mailers/payment_received_mailer_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe PaymentReceivedMailer do + let(:ticket_request) { create(:ticket_request) } + let(:user) { ticket_request.user } + let(:event) { ticket_request.event } + let(:payment) { create(:payment, ticket_request:) } + + describe '#payment_received' do + subject(:mail) { described_class.payment_received(payment) } + + let(:expected_subject) { "Your payment for #{event.name} has been received" } + + its(:subject) { is_expected.to eql(expected_subject) } + + its(:to) { is_expected.to eql([user.email]) } + + its('body.encoded') { is_expected.to match(user.first_name) } + + its('body.encoded') { is_expected.to match(event.name) } + end +end