summaryrefslogtreecommitdiff
path: root/spec/requests/api/v1/subscriptions_spec.rb
blob: 6807c67e2739663eb9b318ae83ad30b84ad11c30 (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
# frozen_string_literal: true

RSpec.describe '/api/v1/subscriptions' do
  include SolidusSubscriptions::Engine.routes.url_helpers

  describe 'PATCH /:id' do
    context 'when the subscription belongs to the user' do
      context 'with valid params' do
        it 'responds with 200 OK' do
          user = create(:user, &:generate_spree_api_key!)
          subscription = create(:subscription, user: user)

          patch(
            api_v1_subscription_path(subscription),
            params: { subscription: { interval_length: 11 } },
            headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
          )

          expect(response.status).to eq(200)
        end

        it 'updates the subscription' do
          user = create(:user, &:generate_spree_api_key!)
          subscription = create(:subscription, user: user)

          patch(
            api_v1_subscription_path(subscription),
            params: { subscription: { interval_length: 11 } },
            headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
          )

          expect(subscription.reload.interval_length).to eq(11)
        end
      end

      context 'with invalid params' do
        it 'responds with 422 Unprocessable Entity' do
          user = create(:user, &:generate_spree_api_key!)
          subscription = create(:subscription, user: user)

          patch(
            api_v1_subscription_path(subscription),
            params: { subscription: { interval_length: -1 } },
            headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
          )

          expect(response.status).to eq(422)
        end
      end
    end

    context 'when the subscription does not belong to the user' do
      it 'responds with 401 Unauthorized' do
        user = create(:user, &:generate_spree_api_key!)
        subscription = create(:subscription)

        patch(
          api_v1_subscription_path(subscription),
          params: { subscription: { interval_length: 11 } },
          headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
        )

        expect(response.status).to eq(401)
      end
    end
  end

  describe 'POST /:id/skip' do
    context 'when the subscription belongs to the user' do
      it 'responds with 200 OK' do
        user = create(:user, &:generate_spree_api_key!)
        subscription = create(:subscription, user: user)

        post(
          skip_api_v1_subscription_path(subscription),
          headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
        )

        expect(response.status).to eq(200)
      end

      it 'skips the subscription' do
        user = create(:user, &:generate_spree_api_key!)
        subscription = create(
          :subscription,
          user: user,
          interval_length: 1,
          interval_units: 'week',
          actionable_date: Time.zone.today,
        )

        post(
          skip_api_v1_subscription_path(subscription),
          headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
        )

        expect(subscription.reload.actionable_date).to eq(Time.zone.today + 1.week)
      end
    end

    context 'when the subscription does not belong to the user' do
      it 'responds with 401 Unauthorized' do
        user = create(:user, &:generate_spree_api_key!)
        subscription = create(:subscription)

        post(
          skip_api_v1_subscription_path(subscription),
          headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
        )

        expect(response.status).to eq(401)
      end
    end
  end

  describe 'POST /:id/cancel' do
    context 'when the subscription belongs to the user' do
      it 'responds with 200 OK' do
        user = create(:user, &:generate_spree_api_key!)
        subscription = create(:subscription, user: user)

        post(
          cancel_api_v1_subscription_path(subscription),
          headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
        )

        expect(response.status).to eq(200)
      end

      it 'cancels the subscription' do
        user = create(:user, &:generate_spree_api_key!)
        subscription = create(:subscription, user: user)

        post(
          cancel_api_v1_subscription_path(subscription),
          headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
        )

        expect(subscription.reload.state).to eq('canceled')
      end
    end

    context 'when the subscription does not belong to the user' do
      it 'responds with 401 Unauthorized' do
        user = create(:user, &:generate_spree_api_key!)
        subscription = create(:subscription)

        post(
          cancel_api_v1_subscription_path(subscription),
          headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
        )

        expect(response.status).to eq(401)
      end
    end
  end
end