forked from glossier/solidus_subscriptions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriptions_controller_spec.rb
More file actions
123 lines (92 loc) · 4.35 KB
/
Copy pathsubscriptions_controller_spec.rb
File metadata and controls
123 lines (92 loc) · 4.35 KB
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
121
122
123
require 'spec_helper'
include SubscriptionMacros
module Spree
describe Api::SubscriptionsController, type: :controller do
render_views
let(:current_api_user) do
user = Spree.user_class.new(email: "spree@example.com", password: 'dynamo1234')
user.generate_spree_api_key!
user
end
before do
setup_subscriptions_for current_api_user
@subscription = current_api_user.subscriptions.last
stub_authentication!
end
it "restricts access to subscriptions" do
@subscription.update_column(:user_id, 999)
api_get :show, id: @subscription.id
expect(json_response[:id]).to be nil
end
it "should allow the owner of the subscription to access it" do
api_get :show, id: @subscription.id
expect(json_response[:id]).to be @subscription.id
end
context "skipping" do
it "should skip next order" do
expect(@subscription.next_shipment_date.to_date).to eql 4.weeks.from_now.to_date
api_get :skip_next_order, id: @subscription.id
expect(@subscription.next_shipment_date.to_date).to eql 8.weeks.from_now.to_date
end
it "should be able to undo a skip next order" do
@subscription.skip_next_order
expect(@subscription.next_shipment_date.to_date).to eql 8.weeks.from_now.to_date
api_get :undo_skip_next_order, id: @subscription.id
expect(@subscription.next_shipment_date.to_date).to eql 4.weeks.from_now.to_date
end
end
it "should be able to cancel a subscription" do
api_put :cancel, id: @subscription.id
@subscription.reload
expect(@subscription.state).to eq 'cancelled'
expect(@subscription.can_renew?).to be false
end
it "should be able to pause a subscription" do
api_put :pause, id: @subscription.id
@subscription.reload
expect(@subscription.state).to eq 'paused'
expect(@subscription.can_renew?).to be false
end
it "should be able to resume after pausing" do
@subscription.pause
expect(@subscription.state).to eq 'paused'
api_put :resume, id: @subscription.id
@subscription.reload
expect(@subscription.state).to eq 'active'
expect(@subscription.can_renew?).to be true
end
it "should create a billing address" do
billing_address = FactoryGirl.create(:subscription_address)
api_post :create_address, id: @subscription.id, address: billing_address.attributes, attribute: 'billing_address'
expect(@subscription.reload.billing_address.id).to be json_response[:id]
end
it "should create a shipping address" do
shipping_address = FactoryGirl.create(:subscription_address)
api_post :create_address, id: @subscription.id, address: shipping_address.attributes, attribute: 'shipping_address'
expect(@subscription.reload.shipping_address.id).to be json_response[:id]
end
it "should update an address" do
updated_shipping_address = @subscription.shipping_address
updated_shipping_address.firstname = 'new'
updated_shipping_address.lastname = 'new'
updated_shipping_address.address1 = '1 new address'
updated_shipping_address.zipcode = '90000'
api_post :update_address, id: @subscription.id, address: updated_shipping_address.attributes, attribute: 'shipping_address'
@subscription.reload
expect(@subscription.shipping_address.firstname).to eq json_response[:firstname]
expect(@subscription.shipping_address.lastname).to eq json_response[:lastname]
expect(@subscription.shipping_address.address1).to eq json_response[:address1]
expect(@subscription.shipping_address.zipcode).to eq json_response[:zipcode]
end
it "should select an existing address" do
# create a new shipping address for the subscription
new_shipping_address = FactoryGirl.create(:subscription_address)
existing_shipping_address = @subscription.shipping_address
api_post :create_address, id: @subscription.id, address: new_shipping_address.attributes, attribute: 'shipping_address'
expect(@subscription.reload.shipping_address.id).to be json_response[:id]
# now select the previous shipping address
api_post :select_address, id: @subscription.id, address_id: existing_shipping_address.id, attribute: 'shipping_address'
expect(@subscription.reload.shipping_address.id).to be existing_shipping_address.id
end
end
end