This repository has been archived by the owner on Feb 27, 2021. It is now read-only.
/
store_credits_controller_spec.rb
120 lines (105 loc) · 4.47 KB
/
store_credits_controller_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require 'spec_helper'
describe Spree::Admin::StoreCreditsController do
stub_authorization!
before do
user = create(:admin_user)
controller.stub(:spree_current_user => user)
end
context '#index' do
context "with store credits" do
let!(:store_credits) { 3.times.map{ create(:store_credit) } }
it "should display the index page" do
spree_get :index
response.status.should eq(200)
response.should render_template(:index)
assigned_credits = assigns(:collection)
store_credits.each do |c|
assigned_credits.should include(c)
end
end
end
context "without store credits" do
it "should display an empty" do
spree_get :index
response.status.should eq(200)
response.should render_template(:index)
assigns(:collection).should be_empty
end
end
end
context '#new' do
it 'should render the correct template' do
spree_get :new
response.status.should eq(200)
response.should render_template(:new)
end
end
context '#create' do
let(:user) { create(:user) }
let(:reason) { SecureRandom.hex(5) }
let(:amount) { BigDecimal.new(rand()*100, 2).to_f }
it 'should create a store credit for the user when arguments are provided' do
lambda {
spree_post :create, store_credit: { amount: amount, reason: reason, user_id: user.id }
response.should redirect_to(spree.admin_store_credits_path)
}.should change(Spree::StoreCredit, :count).by(1)
user.reload
store_credit = user.store_credits.first
store_credit.should_not be_nil
store_credit.user.should eq(user)
store_credit.reason.should eq(reason)
store_credit.amount.should eq(amount)
store_credit.remaining_amount.should eq(amount)
end
end
context '#edit' do
let(:new_store_credit) { create(:store_credit, amount: 40.0, remaining_amount: 40.0) }
let(:used_store_credit) { create(:store_credit, amount: 40.0, remaining_amount: 20.0) }
it 'should render the correct template for a new store credit' do
spree_get :edit, id: new_store_credit
response.status.should eq(200)
response.should render_template(:edit)
end
it 'should redirect to spree.admin_store_credits_path for a used store credit' do
spree_get :edit, id: used_store_credit
response.should redirect_to(spree.admin_store_credits_path)
flash[:error].should eq("Cannot be edited because it has been used")
end
end
context '#update' do
let(:new_store_credit) { create(:store_credit, amount: 40.0, remaining_amount: 40.0) }
let(:used_store_credit) { create(:store_credit, amount: 40.0, remaining_amount: 20.0) }
it 'should update the value and redirect for a new store credit' do
new_reason = SecureRandom.hex(5)
spree_put :update, id: new_store_credit, store_credit: { amount: new_store_credit.amount,
remaining_amount: new_store_credit.remaining_amount,
user_id: new_store_credit.user,
reason: new_reason }
new_store_credit.reload
new_store_credit.reason.should eq(new_reason)
flash[:error].should be_nil
response.should redirect_to(spree.admin_store_credits_path)
end
it 'should redirect to spree.admin_store_credits_path for a used store credit' do
old_reason = used_store_credit.reason
new_reason = SecureRandom.hex(5)
spree_put :update, id: used_store_credit, store_credit: { amount: used_store_credit.amount,
remaining_amount: used_store_credit.remaining_amount,
user_id: used_store_credit.user,
reason: new_reason }
response.should redirect_to(spree.admin_store_credits_path)
flash[:error].should eq("Cannot be edited because it has been used")
used_store_credit.reload
used_store_credit.reason.should eq(old_reason)
end
end
context '#destroy' do
let!(:store_credit) { create(:store_credit) }
it 'should destroy the store credit' do
lambda {
spree_delete :destroy, id: store_credit.id
response.should redirect_to(spree.admin_store_credits_path)
}.should change(Spree::StoreCredit, :count).by(-1)
end
end
end