Skip to content

Commit

Permalink
Fix rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelMartini committed Mar 12, 2021
1 parent 545f267 commit 99de74d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 63 deletions.
77 changes: 38 additions & 39 deletions spec/requests/spree/checkout_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe Spree::CheckoutController, type: :request, with_signed_in_user: true do
Expand All @@ -9,7 +10,7 @@
address.attributes.except("created_at", "updated_at")
end
let(:order) { create(:order_with_line_items) }

context "#edit" do
let!(:order) { create(:order_with_line_items) }

Expand All @@ -28,7 +29,7 @@
end

it "redirects to the cart path if current_order is nil" do
order.destroy
order.destroy
get spree.checkout_path, params: { state: "delivery" }

expect(response).to redirect_to(spree.cart_path)
Expand All @@ -37,14 +38,14 @@
it "redirects to cart if order is completed" do
order.touch(:completed_at)
get spree.checkout_path, params: { state: "address" }

expect(response).to redirect_to(spree.cart_path)
end

# Regression test for https://github.com/spree/spree/issues/2280
it "redirects to current step trying to access a future step" do
order.update_column(:state, "address")

get spree.checkout_path, params: { state: "delivery" }
expect(response).to redirect_to spree.checkout_state_path("address")
end
Expand Down Expand Up @@ -81,16 +82,15 @@
context "save successful" do
def post_address
patch spree.update_checkout_path(state: "address",
order: {
bill_address_attributes: address_params,
use_billing: true
}
)
order: {
bill_address_attributes: address_params,
use_billing: true
})
end

let!(:payment_method) { create(:payment_method) }

context "with the order in the cart state", partial_double_verification: false do
context "when the order in the cart state", partial_double_verification: false do
let!(:order) { create(:order_with_line_items, state: 'cart') }

it "assigns order" do
Expand All @@ -103,7 +103,7 @@ def post_address
expect(order.reload.state).to eq("delivery")
end

it "should redirect the next state" do
it "redirects the next state" do
post_address
expect(response).to redirect_to spree.checkout_state_path("delivery")
end
Expand All @@ -113,25 +113,24 @@ def post_address

def post_persist_address
patch spree.update_checkout_path(state: "address",
order: {
bill_address_attributes: address_params,
use_billing: true
},
save_user_address: "1"
)
order: {
bill_address_attributes: address_params,
use_billing: true
},
save_user_address: "1")
end

it "calls persist order address on user" do
user.user_addresses.destroy

expect { post_persist_address }.to change { user.user_addresses.count }.from(0).to(1)
expect { post_persist_address }.to change { user.user_addresses.count }.from(0).to(1)
end
end
end

context "when the order in the address state" do
context 'when landing to address page' do
let!(:order) do
let!(:order) do
create(:order_with_line_items, state: 'address', user_id: user.id, bill_address: nil, ship_address: nil)
end
let(:user) { create(:user_with_addresses) }
Expand All @@ -144,7 +143,7 @@ def post_persist_address
end
end

context "with a billing and shipping address" do
context "when a billing and shipping address" do
subject do
patch spree.update_checkout_path(
state: 'address',
Expand All @@ -157,15 +156,15 @@ def post_persist_address
end

it "doesn't change bill address" do
expect {
expect do
subject
}.not_to change { order.reload.ship_address.id }
end.not_to(change { order.reload.ship_address.id })
end

it "doesn't change ship address" do
expect {
expect do
subject
}.not_to change { order.reload.bill_address.id }
end.not_to(change { order.reload.bill_address.id })
end
end
end
Expand Down Expand Up @@ -232,7 +231,7 @@ def post_persist_address
}
end

context 'with a permitted payment method' do
context 'when a permitted payment method' do
it 'sets the payment amount' do
patch spree.update_checkout_path(params)
order.reload
Expand All @@ -242,13 +241,13 @@ def post_persist_address
end
end

context 'with an unpermitted payment method' do
context 'when an unpermitted payment method' do
before { payment_method.update!(available_to_users: false) }

it 'sets the payment amount' do
expect {
expect do
patch spree.update_checkout_path(params)
}.to raise_error(ActiveRecord::RecordNotFound)
end.to raise_error(ActiveRecord::RecordNotFound)

expect(order.state).to eq('payment')
expect(order.payments).to be_empty
Expand Down Expand Up @@ -282,7 +281,7 @@ def post_persist_address

context "when in the confirm state" do
let(:order) { create(:order_with_line_items, state: 'confirm') }

before do
# An order requires a payment to reach the complete state
# This is because payment_required? is true on the order
Expand All @@ -306,20 +305,20 @@ def post_persist_address
it "removes completed order from current_order" do
patch spree.update_checkout_path(state: "confirm")
expect(assigns(:current_order)).to be_nil
expect(assigns(:order)).to eql order
expect(assigns(:order)).to eql order
end
end
end

context "save unsuccessful" do
it "does not assign order" do
patch spree.update_checkout_path(state: "address", order: { bill_address_attributes: address_params } )
patch spree.update_checkout_path(state: "address", order: { bill_address_attributes: address_params })
expect(assigns[:order]).not_to be_nil
end

it "renders the edit template" do
order.line_items.destroy_all
patch spree.update_checkout_path(state: "address", order: { bill_address_attributes: address_params } )
patch spree.update_checkout_path(state: "address", order: { bill_address_attributes: address_params })
expect(response).to redirect_to(spree.cart_path)
end
end
Expand Down Expand Up @@ -418,7 +417,7 @@ def post_persist_address
before do
allow(Spree::OrderUpdateAttributes).to receive(:new).and_raise(Spree::Order::InsufficientStock)
allow(Spree::Stock::AvailabilityValidator).to receive(:new) { availability_validator }
allow(availability_validator).to receive(:validate).and_return(false)
allow(availability_validator).to receive(:validate).and_return(false)
end

it "redirects the customer to the address checkout page with an error message" do
Expand Down Expand Up @@ -451,7 +450,7 @@ def post_persist_address
expect(response).to redirect_to spree.cart_path
end

it "should set flash message for no inventory" do
it "redirects set flash message for no inventory" do
expect(flash[:error]).to eq("#{order.line_items.first.name} became unavailable.")
end
end
Expand All @@ -477,9 +476,9 @@ def post_persist_address
end

it "doesn't remove unshippable items before payment" do
expect {
expect do
patch spree.update_checkout_path(state: "payment")
}.to_not change { order.line_items }
end.to_not(change { order.line_items })
end
end

Expand All @@ -492,9 +491,9 @@ def post_persist_address
end

it "removes unshippable items before payment" do
expect {
expect do
patch spree.update_checkout_path(state: "payment", order: { email: "johndoe@example.com" })
}.to change { order.line_items.reload.to_a.size }.from(1).to(0)
end.to change { order.line_items.reload.to_a.size }.from(1).to(0)
end
end
end
7 changes: 3 additions & 4 deletions spec/requests/spree/current_order_tracking_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def not_create_order
head :ok
end
end

before do
Rails.application.routes.draw do
get '/test', to: 'test#create_order'
Expand All @@ -27,7 +27,6 @@ def not_create_order
Rails.application.reload_routes!
end


it 'automatically tracks who the order was created by & IP address' do
get '/test'

Expand All @@ -37,9 +36,9 @@ def not_create_order

context "current order creation" do
it "doesn't create a new order out of the blue" do
expect {
expect do
get '/test2'
}.not_to change { Spree::Order.count }
end.not_to(change { Spree::Order.count })
end
end
end
24 changes: 12 additions & 12 deletions spec/requests/spree/orders_controller_ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@ module Spree
let!(:store) { create(:store) }
let(:variant) { create(:variant) }

it 'should understand order routes with token' do
it 'understands order routes with token' do
expect(spree.token_order_path('R123456', 'ABCDEF')).to eq('/orders/R123456/token/ABCDEF')
end

context 'when an order exists in the cookies.signed', with_guest_session: true do
before { order.update(guest_token: nil) }

context '#populate' do
it 'should check if user is authorized for :update' do
it 'checks if user is authorized for :update' do
post spree.populate_orders_path, params: { variant_id: variant.id }
expect(response).to redirect_to :unauthorized
end
end

context '#edit' do
it 'should check if user is authorized for :read' do
it 'checks if user is authorized for :read' do
get spree.cart_path
expect(response).to redirect_to :unauthorized
end
end

context '#update' do
it 'should check if user is authorized for :update' do
it 'checks if user is authorized for :update' do
put spree.order_path(order.number), params: { order: { email: "foo@bar.com" } }
expect(response).to redirect_to :unauthorized
end
end

context '#empty' do
it 'should check if user is authorized for :update' do
it 'checks if user is authorized for :update' do
put spree.empty_cart_path
expect(response).to redirect_to :unauthorized
end
end

context "#show" do
it "should check against the specified order" do
it "checks against the specified order" do
get spree.order_path(id: order.number)
expect(response).to redirect_to :unauthorized
end
Expand All @@ -54,28 +54,28 @@ module Spree
context 'when no authenticated user' do
let(:order) { create(:order, number: 'R123') }
let(:another_order) { create(:order) }

context '#show' do
context 'when token parameter present' do
it 'always ooverride existing token when passing a new one' do
allow(ActionDispatch::Cookies::PermanentCookieJar).to receive(:signed) { { guest_token: order.guest_token } }
get spree.order_path(id: another_order.number, token: another_order.guest_token)
jar = ActionDispatch::Cookies::CookieJar.build(request, cookies.to_hash)
expect(jar.signed[:guest_token]).to eq another_order.guest_token
end
end

it 'should store as guest_token in session' do
it 'stores as guest_token in session' do
get spree.order_path(id: order.number, token: order.guest_token)
jar = ActionDispatch::Cookies::CookieJar.build(request, cookies.to_hash)
expect(jar.signed[:guest_token]).to eq order.guest_token
end
end

context 'when no token present' do
it 'should respond with 404' do
expect {
it 'responds with 404' do
expect do
get spree.order_path(id: 'R123')
}.to raise_error(ActiveRecord::RecordNotFound)
end.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
Expand Down
Loading

0 comments on commit 99de74d

Please sign in to comment.