14.3 Iteration I3: Integration Tests 14.1 Iteration I1: Email Notifications
edit lib/pago.rb
require 'ostruct'
class Pago
def self.make_payment(order_id:,
payment_method:,
payment_details:)
case payment_method
when :check
Rails.logger.info "Processing check: " +
payment_details.fetch(:routing).to_s +
"/" +
payment_details.fetch(:account).to_s
when :credit_card
Rails.logger.info "Processing credit_card: " +
payment_details.fetch(:cc_num).to_s +
"/" +
payment_details.fetch(:expiration_month).to_s +
"/" +
payment_details.fetch(:expiration_year).to_s
when :po
Rails.logger.info "Processing purchase order: " +
payment_details.fetch(:po_num).to_s
else
raise "Dont' know what to do with payment_method #{payment_method}"
end
unless Rails.env.test?
sleep 3
end
Rails.logger.info "Done"
OpenStruct.new(succeeded?: true)
end
end
edit app/models/order.rb
require 'active_model/serializers/xml'
require 'pago'
class Order < ApplicationRecord
include ActiveModel::Serializers::Xml
enum pay_type: {
"Check" => 0,
"Credit card" => 1,
"Purchase order" => 2
}
has_many :line_items, dependent: :destroy
# ...
validates :name, :address, :email, presence: true
validates :pay_type, inclusion: pay_types.keys
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
end
end
def charge!(pay_type_params)
payment_details = {}
payment_method = nil
case pay_type
when "Check"
payment_method = :check
payment_details[:routing] = pay_type_params[:routing_number]
payment_details[:account] = pay_type_params[:account_number]
when "Credit card"
payment_method = :credit_card
month,year = pay_type_params[:expiration_date].split(//)
payment_details[:cc_num] = pay_type_params[:credit_card_number]
payment_details[:expiration_month] = month
payment_details[:expiration_year] = year
when "Purchase order"
payment_method = :po
payment_details[:po_num] = pay_type_params[:po_number]
end
payment_result = Pago.make_payment(
order_id: id,
payment_method: payment_method,
payment_details: payment_details
)
if payment_result.succeeded?
OrderMailer.received(self).deliver_later
else
raise payment_result.error
end
end
end
rails generate job charge_order
invoke test_unit
create test/jobs/charge_order_job_test.rb
create app/jobs/charge_order_job.rb
edit app/jobs/charge_order_job.rb
class ChargeOrderJob < ApplicationJob
queue_as :default
def perform(order,pay_type_params)
order.charge!(pay_type_params)
end
end
edit app/controllers/orders_controller.rb
class OrdersController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:new, :create]
before_action :ensure_cart_isnt_empty, only: :new
before_action :set_order, only: [:show, :edit, :update, :destroy]
# GET /orders
# GET /orders.json
def index
@orders = Order.all
end
# GET /orders/1
# GET /orders/1.json
def show
end
# GET /orders/new
def new
@order = Order.new
end
# GET /orders/1/edit
def edit
end
# POST /orders
# POST /orders.json
def create
@order = Order.new(order_params)
@order.add_line_items_from_cart(@cart)
respond_to do |format|
if @order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
session[:order_id] = @order.id
ChargeOrderJob.perform_later(@order,pay_type_params.to_h)
format.html { redirect_to store_index_url, notice:
'Thank you for your order.' }
format.json { render :show, status: :created,
location: @order }
else
format.html { render :new }
format.json { render json: @order.errors,
status: :unprocessable_entity }
end
end
end
# PATCH/PUT /orders/1
# PATCH/PUT /orders/1.json
def update
respond_to do |format|
if @order.update(order_params)
format.html { redirect_to @order, notice: 'Order was successfully updated.' }
format.json { render :show, status: :ok, location: @order }
else
format.html { render :edit }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1
# DELETE /orders/1.json
def destroy
@order.destroy
respond_to do |format|
format.html { redirect_to orders_url, notice: 'Order was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
@order = Order.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:name, :address, :email, :pay_type)
end
#...
private
def ensure_cart_isnt_empty
if @cart.line_items.empty?
redirect_to store_index_url, notice: 'Your cart is empty'
end
end
# START: pay_type_params
def pay_type_params
if order_params[:pay_type] == "Credit Card"
params.require(:order).permit(:credit_card_number, :expiration_date)
elsif order_params[:pay_type] == "Check"
params.require(:order).permit(:routing_number, :account_number)
elsif order_params[:pay_type] == "Purchase Order"
params.require(:order).permit(:po_number)
else
{}
end
end
# END: pay_type_params
end
edit test/system/orders_test.rb
require "application_system_test_case"
class OrdersTest < ApplicationSystemTestCase
include ActiveJob::TestHelper
test "check routing number" do
LineItem.delete_all
Order.delete_all
visit store_index_url
visit store_index_url
first('.entry').click_on 'Add to Cart'
click_on 'Checkout'
fill_in 'order_name', with: 'Dave Thomas'
fill_in 'order_address', with: '123 Main Street'
fill_in 'order_email', with: 'dave@example.com'
assert_no_selector "#order_routing_number"
select 'Check', from: 'pay_type'
assert_selector "#order_routing_number"
fill_in "Routing #", with: "123456"
fill_in "Account #", with: "987654"
perform_enqueued_jobs do
click_button "Place Order"
end
orders = Order.all
assert_equal 1, orders.size
order = orders.first
assert_equal "Dave Thomas", order.name
assert_equal "123 Main Street", order.address
assert_equal "dave@example.com", order.email
assert_equal "Check", order.pay_type
assert_equal 1, order.line_items.size
mail = ActionMailer::Base.deliveries.last
assert_equal ["dave@example.com"], mail.to
assert_equal 'Sam Ruby <depot@example.com>', mail[:from].value
assert_equal "Pragmatic Store Order Confirmation", mail.subject
end
end
14.3 Iteration I3: Integration Tests 14.1 Iteration I1: Email Notifications