summaryrefslogtreecommitdiff
path: root/spec/controllers/spree/admin/subscriptions_controller_spec.rb
blob: d64d756e75781069986d2a77f05eca6470129279 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
require 'spec_helper'

RSpec.describe Spree::Admin::SubscriptionsController, type: :request do
  extend Spree::TestingSupport::AuthorizationHelpers::Request
  stub_authorization!

  before do
    ActionController::Base.allow_forgery_protection = false
  end

  after do
    ActionController::Base.allow_forgery_protection = true
  end

  describe 'get /admin/subscriptions' do
    subject do
      get spree.admin_subscriptions_path
      response
    end

    it { is_expected.to be_successful }
  end

  describe 'GET :new' do
    subject do
      get spree.new_admin_subscription_path
      response
    end

    it { is_expected.to be_successful }
  end

  describe 'GET :edit' do
    subject do
      get spree.edit_admin_subscription_path(subscription)
      response
    end

    let(:subscription) { create :subscription, :actionable }

    it { is_expected.to be_successful }
  end

  describe 'PUT :update' do
    it 'redirects to edit subscription page' do
      subscription = create :subscription
      subscription_params = { subscription: { interval_length: 1 } }

      expect(put(spree.admin_subscription_path(subscription), params: subscription_params)).
        to redirect_to spree.edit_admin_subscription_path(subscription)
    end

    it 'updates the subscription attributes', :aggregate_failures do
      expected_date = DateTime.parse('2001/11/12')
      subscription = create :subscription, :actionable
      subscription_params = { subscription: { actionable_date: expected_date } }

      expect { put spree.admin_subscription_path(subscription), params: subscription_params }.
        to change { subscription.reload.actionable_date }.
        to expected_date
    end

    it 'does not duplicate line items' do
      variant = create :variant, subscribable: true
      subscription = create :subscription
      subscription_params = {
        subscription: {
          line_items_attributes: [
            { subscribable_id: variant.id, quantity: 1 }
          ]
        }
      }

      expect { put spree.admin_subscription_path(subscription), params: subscription_params }.
        to change { subscription.reload.line_items.count }.
        by 1
    end

    context 'when updating the payment method' do
      it 'updates the subscription payment method' do
        check_payment_method = create :check_payment_method
        subscription = create :subscription
        subscription_params = { subscription: { payment_method_id: check_payment_method.id } }

        put spree.admin_subscription_path(subscription), params: subscription_params

        expect(subscription.reload).to have_attributes(
          payment_method: check_payment_method,
          payment_source: nil,
        )
      end

      it 'updates the subscription payment source if payment method requires source' do
        payment = create :credit_card_payment
        payment_source = payment.source
        payment_method = payment.payment_method

        subscription = create :subscription, user: payment.order.user
        subscription_params = {
          subscription: {
            payment_method_id: payment_method.id,
            payment_source_id: payment_source.id,
          }
        }

        put spree.admin_subscription_path(subscription), params: subscription_params

        expect(subscription.reload.payment_source).to eq(payment_source)
      end
    end
  end

  describe 'POST cancel' do
    subject { delete spree.cancel_admin_subscription_path(subscription) }

    context 'the subscription can be canceled' do
      let(:subscription) { create :subscription, :actionable }

      it { is_expected.to redirect_to spree.admin_subscriptions_path }

      it 'has a message' do
        subject
        expect(flash[:notice]).to be_present
      end

      it 'cancels the subscription' do
        expect { subject }.to change { subscription.reload.state }.from('active').to('canceled')
      end
    end

    context 'the subscription cannot be canceled' do
      let(:subscription) { create :subscription, :canceled }

      it { is_expected.to redirect_to spree.admin_subscriptions_path }

      it 'has a message' do
        subject
        expect(flash[:notice]).to be_present
      end

      it 'cancels the subscription' do
        expect { subject }.not_to change { subscription.reload.state }
      end
    end
  end

  describe 'POST activate' do
    subject { post spree.activate_admin_subscription_path(subscription) }

    context 'the subscription can be activated' do
      let(:subscription) { create :subscription, :canceled, :with_line_item }

      it { is_expected.to redirect_to spree.admin_subscriptions_path }

      it 'has a message' do
        subject
        expect(flash[:notice]).to be_present
      end

      it 'cancels the subscription' do
        expect { subject }.to change { subscription.reload.state }.from('canceled').to('active')
      end
    end

    context 'the subscription cannot be activated' do
      let(:subscription) { create :subscription, :actionable, :with_line_item }

      it { is_expected.to redirect_to spree.admin_subscriptions_path }

      it 'has a message' do
        subject
        expect(flash[:notice]).to be_present
      end

      it 'cancels the subscription' do
        expect { subject }.not_to change { subscription.reload.state }
      end
    end
  end

  describe 'POST skip' do
    subject { post spree.skip_admin_subscription_path(subscription) }

    let(:subscription) { create :subscription, :actionable, :with_line_item }
    let!(:expected_date) { subscription.next_actionable_date }

    it { is_expected.to redirect_to spree.admin_subscriptions_path }

    it 'has a message' do
      subject
      expect(flash[:notice]).to be_present
    end

    it 'advances the actioanble_date' do
      expect { subject }.
        to change { subscription.reload.actionable_date }.
        from(subscription.actionable_date).to(expected_date)
    end
  end
end