๐Ÿ“ฆ obafemitayor / newsletter-subscription-application

๐Ÿ“„ subscriptions_controller_spec.rb ยท 329 lines
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329# frozen_string_literal: true

require 'rails_helper'

RSpec.describe V1::SubscriptionsController, type: :controller do
  describe 'Create subscription' do
    let!(:category) { create(:category) }
    let(:valid_params) do
      {
        first_name: 'John',
        last_name: 'Doe',
        work_email: 'john@example.com',
        category_guids: [category.guid]
      }
    end

    context 'with valid parameters' do
      it 'creates a new subscription' do
        post :create, params: valid_params
        expect(response).to have_http_status(:created)
      end

      it 'creates a subscription for an existing customer without subscriptions' do
        customer = create(:customer, work_email: valid_params[:work_email])
        expect do
          post :create, params: valid_params
        end.not_to change(Customer, :count)
        expect(response).to have_http_status(:created)
        expect(customer.subscriptions.count).to eq(1)
      end

      it 'creates a subscription for an existing customer when request contains categories the customer is already subscribed to' do
        another_category = create(:category)
        customer = create(:customer, work_email: valid_params[:work_email])
        create(:subscription, customer: customer, category: category)

        params_with_both_categories = valid_params.merge(
          category_guids: [category.guid, another_category.guid]
        )

        post :create, params: params_with_both_categories
        expect(response).to have_http_status(:created)
        expect(customer.subscriptions.count).to eq(2)
        expect(customer.subscriptions.pluck(:category_id)).to match_array([category.id, another_category.id])
      end
    end

    context 'with invalid parameters' do
      it 'returns bad request when first_name is missing' do
        post :create, params: valid_params.except(:first_name)
        expect(response).to have_http_status(:bad_request)
      end

      it 'returns bad request when last_name is missing' do
        post :create, params: valid_params.except(:last_name)
        expect(response).to have_http_status(:bad_request)
      end

      it 'returns bad request when work_email is missing' do
        post :create, params: valid_params.except(:work_email)
        expect(response).to have_http_status(:bad_request)
      end

      it 'returns bad request when category_guids is empty' do
        post :create, params: valid_params.merge(category_guids: [])
        expect(response).to have_http_status(:bad_request)
      end

      it 'returns bad request when work_email is invalid' do
        post :create, params: valid_params.merge(work_email: 'not-an-email')
        expect(response).to have_http_status(:bad_request)
        json_response = JSON.parse(response.body)
        expect(json_response['error']).to eq('work_email must be a valid email address')
      end
    end
  end

  describe 'Get subscriptions' do
    let!(:category1) { create(:category) }
    let!(:category2) { create(:category) }
    let!(:customer) { create(:customer) }

    context 'when no query parameters exist in the request' do
      before do
        15.times do |i|
          create(:subscription,
                 customer: create(:customer),
                 category: i.even? ? category1 : category2)
        end
      end

      it 'returns paginated results with default limit' do
        # Get records of first page
        get :index
        expect(response).to have_http_status(:ok)
        first_page = JSON.parse(response.body)
        expect(first_page['subscriptions'].length).to eq(10) # Default limit
        expect(first_page['has_more']).to be_truthy
        expect(first_page['previous_cursor']).to be_present
        expect(first_page['next_cursor']).to be_present

        # Get records of second page
        get :index, params: { pagination_id: first_page['next_cursor'] }
        expect(response).to have_http_status(:ok)
        second_page = JSON.parse(response.body)
        expect(second_page['subscriptions'].length).to eq(5)
        expect(second_page['subscriptions']).not_to eq(first_page['subscriptions'])
        expect(second_page['next_cursor']).to be_present
        expect(second_page['has_more']).to be_falsey
        expect(second_page['previous_cursor']).to be_present

        [first_page, second_page].each do |page|
          page['subscriptions'].each do |subscription|
            expect(subscription).to include(
              'work_email',
              'first_name',
              'last_name',
              'category_name'
            )
          end
        end
      end
    end

    context 'when invalid query parameters exist in the request' do
      it 'validates pagination_id is numeric' do
        get :index, params: { pagination_id: 'abc' }

        expect(response).to have_http_status(:bad_request)
        json_response = JSON.parse(response.body)
        expect(json_response['error']).to eq('pagination_id must be numeric')
      end

      it 'validates limit is numeric' do
        get :index, params: { limit: 'abc' }

        expect(response).to have_http_status(:bad_request)
        json_response = JSON.parse(response.body)
        expect(json_response['error']).to eq('limit must be numeric')
      end

      it 'validates category_guids is not an empty string' do
        get :index, params: { category_guids: '' }

        expect(response).to have_http_status(:bad_request)
        json_response = JSON.parse(response.body)
        expect(json_response['error']).to eq('category_guids must be a list of non-empty strings')
      end

      it 'validates that pagination_direction is either forward or backward' do
        get :index, params: { pagination_direction: 'abc' }

        expect(response).to have_http_status(:bad_request)
        json_response = JSON.parse(response.body)
        expect(json_response['error']).to eq('pagination_direction must either be forward or backward')
      end
    end

    context 'when valid query parameters exist in the request' do
      before do
        15.times do |i|
          create(:subscription,
                 customer: create(:customer),
                 category: i.even? ? category1 : category2)
        end
      end

      it 'returns all subscriptions when only limit exists in the query parameters and limit is greater than total records' do
        get :index, params: { limit: 20 }
        expect(response).to have_http_status(:ok)
        result = JSON.parse(response.body)
        expect(result['subscriptions'].length).to eq(15)
        expect(result['next_cursor']).to be_present
        expect(result['previous_cursor']).to be_present
        expect(result['has_more']).to be_falsey

        result['subscriptions'].each do |subscription|
          expect(subscription).to include(
            'work_email',
            'first_name',
            'last_name',
            'category_name'
          )
        end
      end

      it 'returns paginated results when limit and one category_guid exist in the query parameters' do
        # Get records of first page
        get :index, params: { limit: 5, category_guids: category1.guid }
        expect(response).to have_http_status(:ok)
        first_page = JSON.parse(response.body)
        expect(first_page['subscriptions'].length).to eq(5)
        expect(first_page['next_cursor']).to be_present
        expect(first_page['previous_cursor']).to be_present
        expect(first_page['has_more']).to be_truthy

        # Get records of second page
        get :index, params: { limit: 5, category_guids: category1.guid, pagination_id: first_page['next_cursor'] }
        expect(response).to have_http_status(:ok)
        second_page = JSON.parse(response.body)
        expect(second_page['subscriptions'].length).to eq(3)
        expect(second_page['subscriptions']).not_to eq(first_page['subscriptions'])
        expect(second_page['next_cursor']).to be_present
        expect(second_page['previous_cursor']).to be_present
        expect(second_page['has_more']).to be_falsey

        [first_page, second_page].each do |page|
          page['subscriptions'].each do |subscription|
            expect(subscription).to include(
              'work_email',
              'first_name',
              'last_name',
              'category_name'
            )
          end
        end
      end

      it 'returns subscriptions when limit and multiple category_guid exist in the query parameters' do
        category3 = create(:category)
        create(:subscription, customer: customer, category: category3)

        get :index, params: {
          category_guid: [category1.guid, category2.guid, category3.guid],
          limit: 20
        }
        expect(response).to have_http_status(:ok)
        result = JSON.parse(response.body)
        expect(result['subscriptions'].length).to eq(16)
        expect(result['next_cursor']).to be_present
        expect(result['previous_cursor']).to be_present
        expect(result['has_more']).to be_falsey

        result['subscriptions'].each do |subscription|
          expect(subscription).to include(
            'work_email',
            'first_name',
            'last_name',
            'category_name'
          )
        end
      end

      it 'returns paginated results when only category_guid exists in the query parameters' do
        get :index, params: { category_guids: category1.guid }
        expect(response).to have_http_status(:ok)
        result = JSON.parse(response.body)
        expect(result['subscriptions'].length).to eq(8)
        expect(result['next_cursor']).to be_present
        expect(result['previous_cursor']).to be_present
        expect(result['has_more']).to be_falsey

        result['subscriptions'].each do |subscription|
          expect(subscription).to include(
            'work_email',
            'first_name',
            'last_name',
            'category_name'
          )
        end
      end

      it 'returns paginated results when there are multiple limit parameters in the query string' do
        get :index, params: { limit: 5 }, params: { limit: 3 }
        expect(response).to have_http_status(:ok)
        result = JSON.parse(response.body)
        expect(result['subscriptions'].length).to eq(3)
      end

      it 'returns paginated results when there are multiple pagination_id parameters in the query string' do
        first_subscription = create(:subscription, customer: customer, category: category1)
        second_subscription = create(:subscription, customer: customer, category: category2)
        third_subscription = create(:subscription, customer: customer, category: create(:category))

        get :index, params: { pagination_id: first_subscription.id, pagination_id: second_subscription.id }
        expect(response).to have_http_status(:ok)
        result = JSON.parse(response.body)

        expect(result['subscriptions'].length).to eq(1)
        expect(result['subscriptions'].first['work_email']).to eq(third_subscription.customer.work_email)
        expect(result['next_cursor']).to eq(third_subscription.id)
        expect(result['previous_cursor']).to eq(third_subscription.id)
        expect(result['has_more']).to be_falsey
      end
    end

    context 'when paginating' do
      it 'should be able to paginate in both directions' do
        subscription_one = create(:subscription, customer: create(:customer), category: category1)
        create(:subscription, customer: create(:customer), category: category1)
        subscription_three = create(:subscription, customer: create(:customer), category: category1)
        subscription_four = create(:subscription, customer: create(:customer), category: category1)
        create(:subscription, customer: create(:customer), category: category1)
        subscription_six = create(:subscription, customer: create(:customer), category: category1)

        # Forward pagination
        get :index, params: { limit: 3 }
        expect(response).to have_http_status(:ok)
        result = JSON.parse(response.body)

        expect(result['subscriptions'].length).to eq(3)
        expect(result['previous_cursor']).to eq(subscription_one.id)
        expect(result['next_cursor']).to eq(subscription_three.id)
        expect(result['has_more']).to be_truthy

        # Forward pagination
        get :index, params: { limit: 3, pagination_id: result['next_cursor'] }
        expect(response).to have_http_status(:ok)
        result_two = JSON.parse(response.body)

        expect(result_two['subscriptions'].length).to eq(3)
        expect(result_two['previous_cursor']).to eq(subscription_four.id)
        expect(result_two['next_cursor']).to eq(subscription_six.id)
        expect(result_two['has_more']).to be_falsey

        # Backward pagination
        get :index, params: { limit: 3, pagination_id: result_two['previous_cursor'], pagination_direction: 'backward' }
        expect(response).to have_http_status(:ok)
        result_three = JSON.parse(response.body)

        expect(result_three['subscriptions'].length).to eq(3)
        expect(result_three['previous_cursor']).to eq(subscription_one.id)
        expect(result_three['next_cursor']).to eq(subscription_three.id)
        expect(result_three['has_more']).to be_falsey
      end
    end
  end
end