Skip to content

Commit

Permalink
Only Controller tests are remaining
Browse files Browse the repository at this point in the history
  • Loading branch information
kigster committed Mar 26, 2024
1 parent cdd0077 commit 90212a2
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 99 deletions.
13 changes: 13 additions & 0 deletions app/concerns/routing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Routing
extend ActiveSupport::Concern

included do
include Rails.application.routes.url_helpers
end

def default_url_options
Rails.application.config.action_mailer.default_url_options
end
end
2 changes: 1 addition & 1 deletion app/controllers/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def mark_received
if @payment
@payment.mark_received
@payment.ticket_request.mark_complete
PaymentReceivedMailer.payment_received(@payment).deliver_now
PaymentMailer.payment_received(@payment).deliver_now
redirect_to :back
end
end
Expand Down
10 changes: 10 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ class ApplicationMailer < ActionMailer::Base
layout 'email'

abstract!

protected

def from_email
"#{@event.name} <#{DEFAULT_SENDER_EMAIL}>" if defined?(:@event)
end

def reply_to_email
"#{@event.name} Ticketing <#{DEFAULT_REPLY_TO_EMAIL}>" if defined?(:@event)
end
end
10 changes: 5 additions & 5 deletions app/mailers/eald_payment_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

class EaldPaymentMailer < PaymentReceivedMailer
def eald_payment_received(payment)
@payment = payment
@event = @payment.event
mail to: "#{@payment.name} <#{@payment.email}>",
class EaldPaymentMailer < ApplicationMailer
def eald_payment_received(eald_payment)
@eald_payment = eald_payment
@event = @eald_payment.event
mail to: "#{@eald_payment.name} <#{@eald_payment.email}>",
from: from_email,
reply_to: reply_to_email,
subject: "Your payment for #{@event.name} Early Arrival/Late Departure passes has been received"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class PaymentReceivedMailer < ApplicationMailer
class PaymentMailer < ApplicationMailer
include PaymentsHelper

def payment_received(payment)
self.payment = payment

Expand All @@ -12,14 +14,6 @@ def payment_received(payment)

private

def from_email
"#{@event.name} <#{DEFAULT_SENDER_EMAIL}>"
end

def reply_to_email
"#{@event.name} Ticketing <#{DEFAULT_REPLY_TO_EMAIL}>"
end

def payment=(payment)
@payment = payment
@ticket_request = @payment.ticket_request
Expand Down
83 changes: 60 additions & 23 deletions app/mailers/ticket_request_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,77 @@

require 'awesome_print'

class TicketRequestMailer < ActionMailer::Base
layout 'email'
require_relative 'application_mailer'
require_relative '../concerns/routing'

class TicketRequestMailer < ApplicationMailer
include ::Routing

# The `request_received` method is used to send an email when a ticket request is received.
# It sets the ticket request and then sends an email to the user who made the request.
#
# @param ticket_request [TicketRequest] The ticket request that has been received.
# @return [Mail::Message] The email that has been prepared to be sent.
def request_received(ticket_request)
@ticket_request = ticket_request
@event = @ticket_request.event
mail to: to_email,
from: from_email,
reply_to: reply_to_email,
subject: "#{@event.name} ticket request confirmation"
# Set the ticket request
self.ticket_request = ticket_request

@ticket_request_url = event_ticket_request_url(event_id: @event.id, id: @ticket_request.id)

# Prepare the email to be sent
mail to: to_email, # The recipient of the email
from: from_email, # The sender of the email
reply_to: reply_to_email, # The email address that will receive replies
subject: "#{@event.name} ticket request confirmation" # The subject of the email
end

# The `request_approved` method is used to send an email when a ticket request is approved.
# It sets the ticket request, generates an authentication token for the user, and then sends an email to the user.
#
# @param ticket_request [TicketRequest] The ticket request that has been approved.
#
# @return [Mail::Message, nil] The email that has been prepared to be sent, or nil if the authentication token is blank.
def request_approved(ticket_request)
@auth_token = ticket_request.user.generate_auth_token!
@ticket_request = ticket_request
@event = @ticket_request.event
mail to: to_email,
from: from_email,
reply_to: reply_to_email,
subject: "Your #{@event.name} ticket request has been approved!"
end
# Set the ticket request
self.ticket_request = ticket_request

private
# Generate an authentication token for the user
@auth_token = ticket_request&.user&.generate_auth_token!

def to_email
"#{@ticket_request.user.name} <#{@ticket_request.user.email}>"
@payment_url = new_payment_url(ticket_request: @ticket_request,
user: @ticket_request.user,
user_token: @auth_token)

if @event.eald?
@extra_params = {}.tap do |params|
params[:early_arrival_passes] = @ticket_request.early_arrival_passes
params[:late_departure_passes] = @ticket_request.late_departure_passes
params[:email] = @ticket_request.user.email
params[:name] = @ticket_request.user.name
end

@eald_url = new_event_eald_payment_path(@event, @extra_params)
end

# Return if the authentication token is blank
return if @auth_token.blank?

# Prepare the email to be sent
mail to: to_email, # The recipient of the email
from: from_email, # The sender of the email
reply_to: reply_to_email, # The email address that will ¬receive replies
subject: "Your #{@event.name} ticket request has been approved!" # The subject of the email
end

def from_email
"#{@event.name} <#{DEFAULT_SENDER_EMAIL}>"
private

def ticket_request=(ticket_request)
@ticket_request = ticket_request
@event = @ticket_request&.event
end

def reply_to_email
"#{@event.name} Ticketing <#{DEFAULT_REPLY_TO_EMAIL}>"
def to_email
"#{@ticket_request.user.name} <#{@ticket_request.user.email}>"
end

class << self
Expand Down
8 changes: 4 additions & 4 deletions app/views/eald_payment_mailer/eald_payment_received.html.haml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
%p
Hi #{@payment.name},
Hi #{@eald_payment.name},

%p
Your payment of
%b= number_to_currency(@payment.amount_charged_cents.to_f / 100)
%b= number_to_currency(@eald_payment.amount_charged_cents.to_f / 100)
for
%b= @payment.early_arrival_passes
%b= @eald_payment.early_arrival_passes
early arrival passes and
%b= @payment.late_departure_passes
%b= @eald_payment.late_departure_passes
late departure passes for
%b= @event.name
was received. Thank you!
Expand Down
2 changes: 1 addition & 1 deletion app/views/events/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
%span.help-inline
Share this link so people can create ticket requests
%code
= new_event_ticket_request_url(@event)
= new_event_ticket_request_path(@event)

.well
%h2 Event Admins
Expand Down
13 changes: 2 additions & 11 deletions app/views/ticket_request_mailer/request_approved.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,15 @@
- else
You can now
= succeed '.' do
= link_to new_payment_url(ticket_request_id: @ticket_request,
user_id: @ticket_request.user_id,
user_token: @auth_token) do
= link_to @payment_url do
purchase your
= 'ticket'.pluralize(@ticket_request.total_tickets)

- if @event.eald?
%p
%b Planning to arrive early or leave late?
%br
:ruby
extra_params = {}
extra_params[:early_arrival_passes] = @ticket_request.early_arrival_passes
extra_params[:late_departure_passes] = @ticket_request.late_departure_passes
extra_params[:email] = @ticket_request.user.email
extra_params[:name] = @ticket_request.user.name
= link_to 'Early Arrival / Late Departure Passes',
new_event_eald_payment_url(@event, extra_params)
= link_to 'Early Arrival / Late Departure Passes', @eald_url
must be purchased separately!

%p
Expand Down
5 changes: 2 additions & 3 deletions app/views/ticket_request_mailer/request_received.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%p
We have received your ticket request for #{@event.name}.

-# XXX: This is temporary until tickets stop selling
-# TODO: This is temporary until tickets stop selling
-#- if @ticket_request.role == 'volunteer'
-# %p
-# At this point, all available tickets have been requested. However,
Expand All @@ -28,5 +28,4 @@
P.S. You can see the status of your request at any time by
visiting
= succeed '.' do
= link_to 'this page',
event_ticket_request_url(@event, @ticket_request)
= link_to 'this page', @ticket_request_url
3 changes: 2 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
module TicketBooth
class Application < ::Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.0
config.load_defaults 6.1

config.time_zone = 'Pacific Time (US & Canada)'

Expand Down Expand Up @@ -42,6 +42,7 @@ class Application < ::Rails::Application
config.assets.initialize_on_precompile = false

config.eager_load_paths << Rails.root.join('app/classes')
config.eager_load_paths << Rails.root.join('app/concerns')
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'rails_helper'

describe PaymentReceivedMailer do
describe PaymentMailer do
let(:ticket_request) { create(:ticket_request) }
let(:user) { ticket_request.user }
let(:event) { ticket_request.event }
Expand Down
56 changes: 16 additions & 40 deletions spec/mailers/ticket_request_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,46 @@
require 'rails_helper'

describe TicketRequestMailer do
let(:user) { User.make! }
let(:event) { Event.make! name: 'Test Event' }
let(:user) { ticket_request.user }
let(:event) { ticket_request.event }
let(:ticket_request) { create(:ticket_request, special_price: price) }
let(:price) { nil }

let(:ticket_request) do
TicketRequest.make! event:,
user:,
special_price: price
end

describe '#request_received' do
let(:mail) { described_class.request_received(ticket_request) }
subject(:mail) { described_class.request_received(ticket_request) }

it 'renders the subject' do
mail.subject.should == 'Test Event ticket request confirmation'
end
its(:subject) { is_expected.to eql "#{event.name} ticket request confirmation" }

it 'renders the receiver email' do
mail.to.should == [user.email]
end
its(:to) { is_expected.to eql [user.email] }

it "includes the user's name" do
mail.body.encoded.should match(user.first_name)
end
its('body.encoded') { is_expected.to match(user.first_name) }

it "includes the event's name" do
mail.body.encoded.should match('Test Event')
end
its('body.encoded') { is_expected.to match(event.name) }
end

describe '#request_approved' do
let(:mail) { described_class.request_approved(ticket_request) }
subject(:mail) { described_class.request_approved(ticket_request) }

let(:body) { mail.body.encoded }

it 'renders the subject' do
mail.subject.should == 'Your Test Event ticket request has been approved!'
end
its(:subject) { is_expected.to eq "Your #{event.name} ticket request has been approved!" }

it 'renders the receiver email' do
mail.to.should == [user.email]
end
its(:to) { is_expected.to eq [user.email] }

it "includes the user's name" do
body.should match(user.first_name)
end
its(:body) { is_expected.to match(user.first_name) }

it "includes the event's name" do
body.should match('Test Event')
end
its('body.decoded') { is_expected.to match(event.name) }

context 'when the ticket request is free' do
let(:price) { 0 }

it 'includes the correct phrase' do
body.should match("You're good to go!")
end
its(:body) { is_expected.to match("You're good to go!") }
end

context 'when the ticket request is not free' do
let(:price) { 10 }

it 'includes the word "purchase"' do
body.should match('purchase')
end
its(:body) { is_expected.to match('purchase') }
end
end
end

0 comments on commit 90212a2

Please sign in to comment.