summaryrefslogtreecommitdiff
path: root/spec/controllers/solidus_subscriptions/api/v1/subscriptions_controller_spec.rb
blob: 2c9007fdb6ed2183bacf22183a30487c8ed8503f (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
require 'spec_helper'

RSpec.describe SolidusSubscriptions::Api::V1::SubscriptionsController do
  routes { SolidusSubscriptions::Engine.routes }

  let!(:user) { create :user }

  before { user.generate_spree_api_key! }

  shared_examples "an authenticated subscription" do
    context "when the subscription belongs to user" do
      let!(:subscription) do
        create(
          :subscription,
          :with_line_item,
          actionable_date: (Date.current + 1.month ),
          user: user
        )
      end

      it { is_expected.to be_successful }
    end

    context "when the subscription belongs to someone else" do
      let!(:subscription) { create :subscription, user: create(:user) }

      it { is_expected.to be_unauthorized }
    end

    context 'when the subscription is canceled' do
      let!(:subscription) { create :subscription, user: user, state: 'canceled' }

      it { is_expected.to be_unprocessable }
    end
  end

  describe 'PATCH :update' do
    subject { patch :update, params: params }

    let(:params) do
      {
        id: subscription.id,
        token: user.spree_api_key,
        subscription: subscription_params
      }
    end

    let(:address_country) { create(:country) }
    let(:address_state) { create(:state, country: address_country) }

    let(:subscription_params) do
      {
        line_items_attributes: [{
          id: subscription.line_items.first.id,
          quantity: 6
        }],
        shipping_address_attributes: {
          firstname: 'Ash',
          lastname: 'Ketchum',
          address1: '1 Rainbow Road',
          city: 'Palette Town',
          country_id: address_country.id,
          state_id: address_state.id,
          phone: '999-999-999',
          zipcode: '10001'
        },
        billing_address_attributes: {
          firstname: 'Ash',
          lastname: 'Ketchum',
          address1: '1 Rainbow Road',
          city: 'Palette Town',
          country_id: address_country.id,
          state_id: address_state.id,
          phone: '999-999-999',
          zipcode: '10001'
        }
      }
    end

    context 'when the subscription belongs to the user' do
      let!(:subscription) { create :subscription, :with_line_item, user: user }

      it { is_expected.to be_successful }

      context 'when the params are not valid' do
        let(:subscription_params) do
          {
            line_items_attributes: [{
              id: subscription.line_items.first.id,
              quantity: -6
            }]
          }
        end

        it { is_expected.to have_http_status(:unprocessable_entity) }
      end
    end

    context 'when the subscription belongs to someone else' do
      let!(:subscription) { create :subscription, :with_line_item, user: create(:user) }

      it { is_expected.to be_unauthorized }
    end
  end

  describe "POST :skip" do
    subject { post :skip, params: params }

    let(:params) { { id: subscription.id, token: user.spree_api_key } }

    it_behaves_like "an authenticated subscription"
  end

  describe "POST :cancel" do
    subject { post :cancel, params: params }

    let(:params) { { id: subscription.id, token: user.spree_api_key } }

    it_behaves_like "an authenticated subscription"
  end
end