Skip to content

Commit

Permalink
Add specs for subscriptions_controller for both user and admin side
Browse files Browse the repository at this point in the history
  • Loading branch information
sachin1mittal committed May 20, 2016
1 parent c6f5df7 commit 6954fe3
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/spree/admin/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SubscriptionsController < Spree::Admin::ResourceController

before_action :ensure_not_cancelled, only: [:update, :cancel, :cancellation, :pause, :unpause]
before_action :update_params, only: :update

def cancellation
end

Expand Down
90 changes: 89 additions & 1 deletion spec/controllers/spree/admin/subscriptions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@

stub_authorization!

let(:active_subscription) { mock_model(Spree::Subscription, id: 1, enabled: true, next_occurrence_at: Time.current) }
let(:active_subscription) { mock_model(Spree::Subscription, id: 1, enabled: true, next_occurrence_at: Time.current, save: true) }
let(:cancelled_subscription) { mock_model(Spree::Subscription, id: 2, cancelled_at: Time.current, cancellation_reasons: "Test") }
let(:subscriptions) { double(ActiveRecord::Relation) }
let(:user) { mock_model(Spree.user_class) }
let(:credit_card1) { mock_model(Spree::CreditCard, id: 1, name: 'Sachin Mittal', number: '4111111111111111', expiry: '02/21', verification_value: '127', cc_type: 'visa', payment_method_id: 1) }
let(:credit_card2) { mock_model(Spree::CreditCard, id: 2, name: 'Sachin Mittal', number: '4222222222222', expiry: '02/31', verification_value: '1212', cc_type: 'visa', payment_method_id: 1) }
let(:credit_cards) { [credit_card1, credit_card2] }
let(:credit_cards_excluding_source) { [credit_card1] }
let(:order) { create(:completed_order_with_pending_payment) }

before do
order.user_id = user.id
end

describe "#cancellation" do
def do_cancellation params
Expand All @@ -32,6 +43,83 @@ def do_cancellation params
end
end

describe "edit" do
def do_edit params
spree_get :edit, params
end

before do
allow(Spree::Subscription).to receive(:find).and_return(active_subscription)
allow(active_subscription).to receive(:reload).and_return(active_subscription)
allow(user).to receive(:credit_cards).and_return(credit_cards)
allow(active_subscription).to receive(:source).and_return(credit_card2)
allow(active_subscription).to receive(:parent_order).and_return(order)
allow(order).to receive(:user).and_return(user)
end

describe "expects to receive" do
after { do_edit({ id: active_subscription.id }) }
it { expect(Spree::Subscription).to receive(:find).and_return(active_subscription) }
it { expect(active_subscription).to receive(:reload).and_return(active_subscription) }
it { expect(user).to receive(:credit_cards).and_return(credit_cards) }
it { expect(active_subscription).to receive(:source).and_return(credit_card2) }
it { expect(active_subscription).to receive(:parent_order).and_return(order) }
it { expect(order).to receive(:user).and_return(user) }
end

describe 'Assignment' do
before { do_edit({ id: active_subscription.id }) }

it { expect(assigns[:credit_cards]).to eq(credit_cards_excluding_source) }
it { expect(assigns[:order]).to eq(order) }
end
end

describe "update" do
def do_update params
spree_put :update, params
end

let(:params) { { id: active_subscription.id, subscription: { quantity: 2 } } }

describe "when subscription is successfully updated" do
describe "expects to receive" do
context 'when payment method changed' do
before do
allow(Spree::Subscription).to receive(:find).and_return(active_subscription)
end
before do
allow(active_subscription).to receive(:cancelled?).and_return(false)
allow(active_subscription).to receive(:update_attributes).and_return(true)
end
context 'when existing card selected' do
before { params.merge!(use_another_card: 1, use_existing_card: 'yes', order: { existing_card: 1 }) }
after { do_update(params) }
it { expect(active_subscription).to receive(:update_attributes).with('quantity' => '2', 'source_id' => '1').and_return(true) }
end
context 'when new card created' do
let(:credit_card_params) { { name: 'Sachin Mittal',
number: '4012888888881881',
cc_type: 'visa',
verification_value: '1212',
expiry: '05/33',
payment_method_id: 1 } }

before do
allow(controller).to receive(:subscription_user).and_return(user)
allow(user).to receive(:credit_cards).and_return(credit_cards)
allow(credit_cards).to receive(:create)
params.merge!(use_another_card: 1, use_existing_card: 'no', payment_source: { '1' => credit_card_params })
end

after { do_update(params) }
it { expect(params[:payment_source]['1'][:payment_method_id]).not_to be_nil }
end
end
end
end
end

describe "pause" do
def do_pause
spree_post :pause, format: :json, id: active_subscription.id
Expand Down
95 changes: 84 additions & 11 deletions spec/controllers/spree/subscriptions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

stub_authorization!

let(:active_subscription) { mock_model(Spree::Subscription, id: 1, enabled: true, next_occurrence_at: Time.current) }
let(:active_subscription) { mock_model(Spree::Subscription, id: 1, enabled: true, next_occurrence_at: Time.current, source_id: 2) }
let(:cancelled_subscription) { mock_model(Spree::Subscription, id: 2, cancelled_at: Time.current, cancellation_reasons: "Test", enabled: true) }
let(:subscriptions) { double(ActiveRecord::Relation) }
let(:user) { mock_model(Spree.user_class) }
let(:credit_card1) { mock_model(Spree::CreditCard, id: 1, name: 'Sachin Mittal', number: '4111111111111111', expiry: '02/21', verification_value: '127', cc_type: 'visa', payment_method_id: 1) }
let(:credit_card2) { mock_model(Spree::CreditCard, id: 2, name: 'Sachin Mittal', number: '4222222222222', expiry: '02/31', verification_value: '1212', cc_type: 'visa', payment_method_id: 1) }
let(:credit_cards) { [credit_card1, credit_card2] }
let(:credit_cards_excluding_source) { [credit_card1] }
let(:order) { create(:completed_order_with_pending_payment) }

describe "Callbacks" do
def do_cancel params
Expand Down Expand Up @@ -227,20 +233,36 @@ def do_edit params
before do
allow(Spree::Subscription).to receive(:active).and_return(subscriptions)
allow(subscriptions).to receive(:find_by).and_return(active_subscription)
allow(controller).to receive(:load_variables)
allow(active_subscription).to receive(:reload).and_return(active_subscription)
allow(controller).to receive(:subscription_user).and_return(user)
allow(user).to receive(:credit_cards).and_return(credit_cards)
allow(active_subscription).to receive(:source).and_return(credit_card2)
allow(active_subscription).to receive(:parent_order).and_return(order)
end

describe "expects to receive" do
after { do_edit({ id: active_subscription.id }) }
it { expect(Spree::Subscription).to receive(:active).and_return(subscriptions) }
it { expect(controller).to receive(:load_variables) }
it { expect(subscriptions).to receive(:find_by).and_return(active_subscription) }
it { expect(active_subscription).to receive(:reload).and_return(active_subscription) }
it { expect(controller).to receive(:subscription_user).and_return(user) }
it { expect(user).to receive(:credit_cards).and_return(credit_cards) }
it { expect(active_subscription).to receive(:source).and_return(credit_card2) }
it { expect(active_subscription).to receive(:parent_order).and_return(order) }
end

describe "response" do
before { do_edit({ id: active_subscription.id }) }
it { expect(response).to have_http_status 200 }
it { expect(response).to render_template :edit }
end

describe 'Assignment' do
before { do_edit({ id: active_subscription.id }) }

it { expect(assigns[:credit_cards]).to eq(credit_cards_excluding_source) }
it { expect(assigns[:order]).to eq(order) }
end
end

describe "when subscription is not found" do
Expand Down Expand Up @@ -283,12 +305,48 @@ def do_update params
end

describe "expects to receive" do
after { do_update(params) }
it { expect(Spree::Subscription).to receive(:active).and_return(subscriptions) }
it { expect(subscriptions).to receive(:find_by).and_return(active_subscription) }
it { expect(active_subscription).to receive(:not_changeable?).and_return(false) }
it { expect(controller).to receive(:subscription_attributes).and_call_original }
it { expect(active_subscription).to receive(:update).with(controller.send :subscription_attributes).and_return(true) }
context 'when payment method remains same' do
after { do_update(params) }
it { expect(Spree::Subscription).to receive(:active).and_return(subscriptions) }
it { expect(subscriptions).to receive(:find_by).and_return(active_subscription) }
it { expect(active_subscription).to receive(:not_changeable?).and_return(false) }
it { expect(controller).to receive(:subscription_attributes).and_call_original }
it { expect(active_subscription).to receive(:update).with(controller.send :subscription_attributes).and_return(true) }
end
context 'when payment method changed' do
context 'when existing card selected' do
before { params.merge!(use_another_card: 1, use_existing_card: 'yes', order: { existing_card: 1 }) }
after { do_update(params) }
it { expect(Spree::Subscription).to receive(:active).and_return(subscriptions) }
it { expect(subscriptions).to receive(:find_by).and_return(active_subscription) }
it { expect(active_subscription).to receive(:not_changeable?).and_return(false) }
it { expect(controller).to receive(:subscription_attributes).and_call_original }
it { expect(active_subscription).to receive(:update).with(controller.send :subscription_attributes).and_return(true) }
end
context 'when new card created' do
let(:credit_card_params) { { name: 'Sachin Mittal',
number: '4012888888881881',
cc_type: 'visa',
verification_value: '1212',
expiry: '05/33',
payment_method_id: 1 } }

before do
allow(controller).to receive(:subscription_user).and_return(user)
allow(user).to receive(:credit_cards).and_return(credit_cards)
allow(credit_cards).to receive(:create)
params.merge!(use_another_card: 1, use_existing_card: 'no', payment_source: { '1' => credit_card_params })
end

after { do_update(params) }
it { expect(Spree::Subscription).to receive(:active).and_return(subscriptions) }
it { expect(subscriptions).to receive(:find_by).and_return(active_subscription) }
it { expect(active_subscription).to receive(:not_changeable?).and_return(false) }
it { expect(controller).to receive(:subscription_attributes).and_call_original }
it { expect(active_subscription).to receive(:update).with(controller.send :subscription_attributes).and_return(true) }
it { expect(params[:payment_source]['1'][:payment_method_id]).not_to be_nil }
end
end
end

describe "response" do
Expand All @@ -313,7 +371,11 @@ def do_update params
allow(active_subscription).to receive(:not_changeable?).and_return(false)
allow(controller).to receive(:subscription_attributes).and_return(params[:subscription])
allow(active_subscription).to receive(:update).and_return(false)
allow(controller).to receive(:load_variables)
allow(active_subscription).to receive(:reload).and_return(active_subscription)
allow(controller).to receive(:subscription_user).and_return(user)
allow(user).to receive(:credit_cards).and_return(credit_cards)
allow(active_subscription).to receive(:parent_order).and_return(order)
allow(active_subscription).to receive(:source).and_return(credit_card2)
end

describe "expects to receive" do
Expand All @@ -323,7 +385,18 @@ def do_update params
it { expect(active_subscription).to receive(:not_changeable?).and_return(false) }
it { expect(controller).to receive(:subscription_attributes).and_call_original }
it { expect(active_subscription).to receive(:update).with(controller.send :subscription_attributes).and_return(false) }
it { expect(controller).to receive(:load_variables) }
it { expect(active_subscription).to receive(:reload).and_return(active_subscription) }
it { expect(controller).to receive(:subscription_user).and_return(user) }
it { expect(user).to receive(:credit_cards).and_return(credit_cards) }
it { expect(active_subscription).to receive(:source).and_return(credit_card2) }
it { expect(active_subscription).to receive(:parent_order).and_return(order) }
end

describe 'Assignment' do
before { do_update(params) }

it { expect(assigns[:credit_cards]).to eq(credit_cards_excluding_source) }
it { expect(assigns[:order]).to eq(order) }
end

describe "response" do
Expand Down

0 comments on commit 6954fe3

Please sign in to comment.