activity_notification 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +33 -0
  3. data/.rubocop.yml +1157 -0
  4. data/.yardopts +3 -0
  5. data/CHANGELOG.md +25 -0
  6. data/Gemfile.lock +15 -17
  7. data/README.md +154 -27
  8. data/activity_notification.gemspec +1 -1
  9. data/app/controllers/activity_notification/notifications_controller.rb +30 -104
  10. data/app/controllers/activity_notification/notifications_with_devise_controller.rb +1 -33
  11. data/app/controllers/activity_notification/subscriptions_controller.rb +184 -0
  12. data/app/controllers/activity_notification/subscriptions_with_devise_controller.rb +6 -0
  13. data/app/mailers/activity_notification/mailer.rb +3 -3
  14. data/app/views/activity_notification/notifications/default/_index.html.erb +3 -0
  15. data/app/views/activity_notification/notifications/default/index.html.erb +8 -10
  16. data/app/views/activity_notification/notifications/default/show.html.erb +24 -2
  17. data/app/views/activity_notification/subscriptions/default/_form.html.erb +52 -0
  18. data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +89 -0
  19. data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +73 -0
  20. data/app/views/activity_notification/subscriptions/default/_subscriptions.html.erb +13 -0
  21. data/app/views/activity_notification/subscriptions/default/create.js.erb +5 -0
  22. data/app/views/activity_notification/subscriptions/default/destroy.js.erb +5 -0
  23. data/app/views/activity_notification/subscriptions/default/index.html.erb +197 -0
  24. data/app/views/activity_notification/subscriptions/default/show.html.erb +177 -0
  25. data/app/views/activity_notification/subscriptions/default/subscribe.js.erb +8 -0
  26. data/app/views/activity_notification/subscriptions/default/subscribe_to_email.js.erb +6 -0
  27. data/app/views/activity_notification/subscriptions/default/unsubscribe.js.erb +8 -0
  28. data/app/views/activity_notification/subscriptions/default/unsubscribe_to_email.js.erb +6 -0
  29. data/gemfiles/Gemfile.rails-4.2.lock +18 -20
  30. data/gemfiles/Gemfile.rails-5.0.lock +18 -20
  31. data/lib/activity_notification.rb +7 -3
  32. data/lib/activity_notification/apis/notification_api.rb +95 -61
  33. data/lib/activity_notification/apis/subscription_api.rb +51 -0
  34. data/lib/activity_notification/common.rb +1 -1
  35. data/lib/activity_notification/config.rb +65 -17
  36. data/lib/activity_notification/controllers/common_controller.rb +118 -0
  37. data/lib/activity_notification/controllers/devise_authentication_controller.rb +41 -0
  38. data/lib/activity_notification/helpers/view_helpers.rb +131 -3
  39. data/lib/activity_notification/mailers/helpers.rb +6 -8
  40. data/lib/activity_notification/models/concerns/notifiable.rb +45 -27
  41. data/lib/activity_notification/models/concerns/subscriber.rb +149 -0
  42. data/lib/activity_notification/models/concerns/target.rb +100 -66
  43. data/lib/activity_notification/models/notification.rb +7 -5
  44. data/lib/activity_notification/models/subscription.rb +93 -0
  45. data/lib/activity_notification/rails/routes.rb +148 -33
  46. data/lib/activity_notification/renderable.rb +3 -4
  47. data/lib/activity_notification/roles/acts_as_notifiable.rb +14 -1
  48. data/lib/activity_notification/roles/acts_as_target.rb +11 -8
  49. data/lib/activity_notification/version.rb +1 -1
  50. data/lib/generators/activity_notification/controllers_generator.rb +2 -2
  51. data/lib/generators/activity_notification/install_generator.rb +0 -1
  52. data/lib/generators/activity_notification/migration/migration_generator.rb +8 -2
  53. data/lib/generators/activity_notification/models_generator.rb +53 -0
  54. data/lib/generators/activity_notification/views_generator.rb +7 -7
  55. data/lib/generators/templates/activity_notification.rb +17 -3
  56. data/lib/generators/templates/controllers/notifications_controller.rb +18 -17
  57. data/lib/generators/templates/controllers/notifications_with_devise_controller.rb +18 -17
  58. data/lib/generators/templates/controllers/subscriptions_controller.rb +79 -0
  59. data/lib/generators/templates/controllers/subscriptions_with_devise_controller.rb +87 -0
  60. data/lib/generators/templates/migrations/migration.rb +57 -0
  61. data/lib/generators/templates/models/README +10 -0
  62. data/lib/generators/templates/{notification → models}/notification.rb +1 -3
  63. data/lib/generators/templates/models/subscription.rb +4 -0
  64. data/spec/concerns/apis/notification_api_spec.rb +48 -11
  65. data/spec/concerns/apis/subscription_api_spec.rb +167 -0
  66. data/spec/concerns/models/notifiable_spec.rb +60 -0
  67. data/spec/concerns/models/subscriber_spec.rb +525 -0
  68. data/spec/concerns/models/target_spec.rb +271 -42
  69. data/spec/controllers/common_controller_spec.rb +25 -0
  70. data/spec/controllers/dummy_common_controller.rb +5 -0
  71. data/spec/controllers/notifications_controller_shared_examples.rb +2 -6
  72. data/spec/controllers/subscriptions_controller_shared_examples.rb +735 -0
  73. data/spec/controllers/subscriptions_controller_spec.rb +12 -0
  74. data/spec/controllers/subscriptions_with_devise_controller_spec.rb +91 -0
  75. data/spec/factories/dummy/dummy_subscriber.rb +4 -0
  76. data/spec/factories/subscriptions.rb +8 -0
  77. data/spec/generators/controllers_generator_spec.rb +25 -2
  78. data/spec/generators/migration/migration_generator_spec.rb +3 -3
  79. data/spec/generators/models_generator_spec.rb +96 -0
  80. data/spec/generators/views_generator_spec.rb +84 -0
  81. data/spec/helpers/view_helpers_spec.rb +143 -0
  82. data/spec/mailers/mailer_spec.rb +5 -4
  83. data/spec/models/dummy/dummy_subscriber_spec.rb +5 -0
  84. data/spec/models/notification_spec.rb +7 -7
  85. data/spec/models/subscription_spec.rb +158 -0
  86. data/spec/rails_app/app/controllers/users/notifications_controller.rb +67 -0
  87. data/spec/rails_app/app/controllers/users/notifications_with_devise_controller.rb +75 -0
  88. data/spec/rails_app/app/controllers/users/subscriptions_controller.rb +79 -0
  89. data/spec/rails_app/app/controllers/users/subscriptions_with_devise_controller.rb +87 -0
  90. data/spec/rails_app/app/models/admin.rb +1 -0
  91. data/spec/rails_app/app/models/dummy/dummy_subscriber.rb +4 -0
  92. data/spec/rails_app/app/models/user.rb +2 -1
  93. data/spec/rails_app/app/views/activity_notification/mailer/dummy_subscribers/test_key.text.erb +1 -0
  94. data/spec/rails_app/app/views/articles/index.html.erb +6 -0
  95. data/spec/rails_app/config/initializers/activity_notification.rb +17 -3
  96. data/spec/rails_app/config/routes.rb +2 -2
  97. data/spec/rails_app/db/migrate/20160715050420_create_activity_notification_tables.rb +33 -0
  98. data/spec/rails_app/db/schema.rb +18 -0
  99. data/spec/roles/acts_as_notifiable_spec.rb +1 -1
  100. data/spec/roles/acts_as_target_spec.rb +1 -1
  101. metadata +70 -11
  102. data/lib/generators/activity_notification/notification/notification_generator.rb +0 -20
  103. data/lib/generators/templates/active_record/migration.rb +0 -18
  104. data/spec/generators/notification/notification_generator_spec.rb +0 -41
  105. data/spec/rails_app/db/migrate/20160715050420_create_notifications.rb +0 -18
@@ -0,0 +1,735 @@
1
+ shared_examples_for :subscription_controller do
2
+ let(:target_params) { { target_type: target_type }.merge(extra_params || {}) }
3
+
4
+ describe "GET #index" do
5
+ context "with target_type and target_id parameters" do
6
+ before do
7
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
8
+ @notification = create(:notification, target: test_target, key: 'test_notification_key')
9
+ get_with_compatibility :index, target_params.merge({ target_id: test_target, typed_target_param => 'dummy' }), valid_session
10
+ end
11
+
12
+ it "returns 200 as http status code" do
13
+ expect(response.status).to eq(200)
14
+ end
15
+
16
+ it "assigns configured subscription index as @subscriptions" do
17
+ expect(assigns(:subscriptions)).to eq([@subscription])
18
+ end
19
+
20
+ it "assigns unconfigured notification keys as @notification_keys" do
21
+ expect(assigns(:notification_keys)).to eq([@notification.key])
22
+ end
23
+
24
+ it "renders the :index template" do
25
+ expect(response).to render_template :index
26
+ end
27
+ end
28
+
29
+ context "with target_type and (typed_target)_id parameters" do
30
+ before do
31
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
32
+ @notification = create(:notification, target: test_target, key: 'test_notification_key')
33
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target }), valid_session
34
+ end
35
+
36
+ it "returns 200 as http status code" do
37
+ expect(response.status).to eq(200)
38
+ end
39
+
40
+ it "assigns subscription index as @subscriptions" do
41
+ expect(assigns(:subscriptions)).to eq([@subscription])
42
+ end
43
+
44
+ it "assigns unconfigured notification keys as @notification_keys" do
45
+ expect(assigns(:notification_keys)).to eq([@notification.key])
46
+ end
47
+
48
+ it "renders the :index template" do
49
+ expect(response).to render_template :index
50
+ end
51
+ end
52
+
53
+ context "without target_type parameters" do
54
+ before do
55
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
56
+ @notification = create(:notification, target: test_target, key: 'test_notification_key')
57
+ get_with_compatibility :index, { typed_target_param => test_target }, valid_session
58
+ end
59
+
60
+ it "returns 400 as http status code" do
61
+ expect(response.status).to eq(400)
62
+ end
63
+ end
64
+
65
+ context "with not found (typed_target)_id parameter" do
66
+ before do
67
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
68
+ @notification = create(:notification, target: test_target, key: 'test_notification_key')
69
+ end
70
+
71
+ it "raises ActiveRecord::RecordNotFound" do
72
+ expect {
73
+ get_with_compatibility :index, target_params.merge({ typed_target_param => 0 }), valid_session
74
+ }.to raise_error(ActiveRecord::RecordNotFound)
75
+ end
76
+ end
77
+
78
+ context "with json as format parameter" do
79
+ before do
80
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
81
+ @notification = create(:notification, target: test_target, key: 'test_notification_key')
82
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, format: :json }), valid_session
83
+ end
84
+
85
+ it "returns 200 as http status code" do
86
+ expect(response.status).to eq(200)
87
+ end
88
+
89
+ it "returns json format" do
90
+ expect(JSON.parse(response.body)["subscriptions"].first)
91
+ .to include("target_id" => test_target.id, "target_type" => test_target.to_class_name)
92
+ expect(JSON.parse(response.body)["unconfigured_notification_keys"].first)
93
+ .to eq('test_notification_key')
94
+ end
95
+ end
96
+
97
+ context "with filter parameter" do
98
+ context "with configured as filter" do
99
+ before do
100
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
101
+ @notification = create(:notification, target: test_target, key: 'test_notification_key')
102
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, filter: 'configured' }), valid_session
103
+ end
104
+
105
+ it "assigns configured subscription index as @subscriptions" do
106
+ expect(assigns(:subscriptions)).to eq([@subscription])
107
+ end
108
+
109
+ it "does not assign unconfigured notification keys as @notification_keys" do
110
+ expect(assigns(:notification_keys)).to be_nil
111
+ end
112
+ end
113
+
114
+ context "with unconfigured as filter" do
115
+ before do
116
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
117
+ @notification = create(:notification, target: test_target, key: 'test_notification_key')
118
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, filter: 'unconfigured' }), valid_session
119
+ end
120
+
121
+ it "does not assign configured subscription index as @subscriptions" do
122
+ expect(assigns(:subscriptions)).to be_nil
123
+ end
124
+
125
+ it "assigns unconfigured notification keys as @notification_keys" do
126
+ expect(assigns(:notification_keys)).to eq([@notification.key])
127
+ end
128
+ end
129
+ end
130
+
131
+ context "with limit parameter" do
132
+ before do
133
+ create(:subscription, target: test_target, key: 'test_subscription_key_1')
134
+ create(:subscription, target: test_target, key: 'test_subscription_key_2')
135
+ create(:notification, target: test_target, key: 'test_notification_key_1')
136
+ create(:notification, target: test_target, key: 'test_notification_key_2')
137
+ end
138
+ context "with 2 as limit" do
139
+ before do
140
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, limit: 2 }), valid_session
141
+ end
142
+
143
+ it "assigns subscription index of size 2 as @subscriptions" do
144
+ expect(assigns(:subscriptions).size).to eq(2)
145
+ end
146
+
147
+ it "assigns notification key index of size 2 as @notification_keys" do
148
+ expect(assigns(:notification_keys).size).to eq(2)
149
+ end
150
+ end
151
+
152
+ context "with 1 as limit" do
153
+ before do
154
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, limit: 1 }), valid_session
155
+ end
156
+
157
+ it "assigns subscription index of size 1 as @subscriptions" do
158
+ expect(assigns(:subscriptions).size).to eq(1)
159
+ end
160
+
161
+ it "assigns notification key index of size 1 as @notification_keys" do
162
+ expect(assigns(:notification_keys).size).to eq(1)
163
+ end
164
+ end
165
+ end
166
+
167
+ context "with reload parameter" do
168
+ context "with false as reload" do
169
+ before do
170
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
171
+ @notification = create(:notification, target: test_target, key: 'test_notification_key')
172
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, reload: false }), valid_session
173
+ end
174
+
175
+ it "returns 200 as http status code" do
176
+ expect(response.status).to eq(200)
177
+ end
178
+
179
+ it "does not assign subscription index as @subscriptions" do
180
+ expect(assigns(:subscriptions)).to be_nil
181
+ end
182
+
183
+ it "does not assign unconfigured notification keys as @notification_keys" do
184
+ expect(assigns(:notification_keys)).to be_nil
185
+ end
186
+
187
+ it "renders the :index template" do
188
+ expect(response).to render_template :index
189
+ end
190
+ end
191
+ end
192
+
193
+ context "with options filter parameters" do
194
+ before do
195
+ @subscription1 = create(:subscription, target: test_target, key: 'test_subscription_key_1')
196
+ @subscription2 = create(:subscription, target: test_target, key: 'test_subscription_key_2')
197
+ @notification1 = create(:notification, target: test_target, key: 'test_notification_key_1')
198
+ @notification2 = create(:notification, target: test_target, key: 'test_notification_key_2')
199
+ end
200
+
201
+ context 'with filtered_by_key parameter' do
202
+ it "returns filtered subscriptions only" do
203
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, filtered_by_key: 'test_subscription_key_2' }), valid_session
204
+ expect(assigns(:subscriptions)[0]).to eq(@subscription2)
205
+ expect(assigns(:subscriptions).size).to eq(1)
206
+ end
207
+
208
+ it "returns filtered notification keys only" do
209
+ get_with_compatibility :index, target_params.merge({ typed_target_param => test_target, filtered_by_key: 'test_notification_key_2' }), valid_session
210
+ expect(assigns(:notification_keys)[0]).to eq(@notification2.key)
211
+ expect(assigns(:notification_keys).size).to eq(1)
212
+ end
213
+ end
214
+ end
215
+ end
216
+
217
+ describe "POST #create" do
218
+ before do
219
+ test_target.subscriptions.delete_all
220
+ expect(test_target.subscriptions.size).to eq(0)
221
+ end
222
+
223
+ context "http direct POST request" do
224
+ before do
225
+ post_with_compatibility :create, target_params.merge({
226
+ typed_target_param => test_target,
227
+ "subscription" => { "key" => "new_subscription_key",
228
+ "subscribing"=> "true",
229
+ "subscribing_to_email"=>"true"
230
+ }
231
+ }), valid_session
232
+ end
233
+
234
+ it "returns 302 as http status code" do
235
+ expect(response.status).to eq(302)
236
+ end
237
+
238
+ it "creates new subscription of the target" do
239
+ expect(test_target.subscriptions.reload.size).to eq(1)
240
+ expect(test_target.subscriptions.reload.first.key).to eq("new_subscription_key")
241
+ end
242
+
243
+ it "redirects to :index" do
244
+ expect(response).to redirect_to action: :index
245
+ end
246
+ end
247
+
248
+ context "http POST request from root_path" do
249
+ before do
250
+ request.env["HTTP_REFERER"] = root_path
251
+ post_with_compatibility :create, target_params.merge({
252
+ typed_target_param => test_target,
253
+ "subscription" => { "key" => "new_subscription_key",
254
+ "subscribing"=> "true",
255
+ "subscribing_to_email"=>"true"
256
+ }
257
+ }), valid_session
258
+ end
259
+
260
+ it "returns 302 as http status code" do
261
+ expect(response.status).to eq(302)
262
+ end
263
+
264
+ it "creates new subscription of the target" do
265
+ expect(test_target.subscriptions.reload.size).to eq(1)
266
+ expect(test_target.subscriptions.reload.first.key).to eq("new_subscription_key")
267
+ end
268
+
269
+ it "redirects to root_path as request.referer" do
270
+ expect(response).to redirect_to root_path
271
+ end
272
+ end
273
+
274
+ context "Ajax POST request" do
275
+ before do
276
+ request.env["HTTP_REFERER"] = root_path
277
+ xhr_with_compatibility :post, :create, target_params.merge({
278
+ typed_target_param => test_target,
279
+ "subscription" => { "key" => "new_subscription_key",
280
+ "subscribing"=> "true",
281
+ "subscribing_to_email"=>"true"
282
+ }
283
+ }), valid_session
284
+ end
285
+
286
+ it "returns 200 as http status code" do
287
+ expect(response.status).to eq(200)
288
+ end
289
+
290
+ it "assigns subscription index as @subscriptions" do
291
+ expect(assigns(:subscriptions)).to eq([test_target.subscriptions.reload.first])
292
+ end
293
+
294
+ it "creates new subscription of the target" do
295
+ expect(test_target.subscriptions.reload.size).to eq(1)
296
+ expect(test_target.subscriptions.reload.first.key).to eq("new_subscription_key")
297
+ end
298
+
299
+ it "renders the :create template as format js" do
300
+ expect(response).to render_template :create, format: :js
301
+ end
302
+ end
303
+ end
304
+
305
+ describe "GET #show" do
306
+ context "with id, target_type and (typed_target)_id parameters" do
307
+ before do
308
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
309
+ get_with_compatibility :show, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
310
+ end
311
+
312
+ it "returns 200 as http status code" do
313
+ expect(response.status).to eq(200)
314
+ end
315
+
316
+ it "assigns the requested subscription as @subscription" do
317
+ expect(assigns(:subscription)).to eq(@subscription)
318
+ end
319
+
320
+ it "renders the :index template" do
321
+ expect(response).to render_template :show
322
+ end
323
+ end
324
+
325
+ context "with wrong id and (typed_target)_id parameters" do
326
+ before do
327
+ @subscription = create(:subscription, target: create(:user))
328
+ get_with_compatibility :show, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
329
+ end
330
+
331
+ it "returns 403 as http status code" do
332
+ expect(response.status).to eq(403)
333
+ end
334
+ end
335
+ end
336
+
337
+ describe "DELETE #destroy" do
338
+ context "http direct DELETE request" do
339
+ before do
340
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
341
+ delete_with_compatibility :destroy, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
342
+ end
343
+
344
+ it "returns 302 as http status code" do
345
+ expect(response.status).to eq(302)
346
+ end
347
+
348
+ it "deletes the subscription" do
349
+ expect(assigns(test_target.subscriptions.where(id: @subscription.id).exists?)).to be_falsey
350
+ end
351
+
352
+ it "redirects to :index" do
353
+ expect(response).to redirect_to action: :index
354
+ end
355
+ end
356
+
357
+ context "http DELETE request from root_path" do
358
+ before do
359
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
360
+ request.env["HTTP_REFERER"] = root_path
361
+ delete_with_compatibility :destroy, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
362
+ end
363
+
364
+ it "returns 302 as http status code" do
365
+ expect(response.status).to eq(302)
366
+ end
367
+
368
+ it "deletes the subscription" do
369
+ expect(assigns(test_target.subscriptions.where(id: @subscription.id).exists?)).to be_falsey
370
+ end
371
+
372
+ it "redirects to root_path as request.referer" do
373
+ expect(response).to redirect_to root_path
374
+ end
375
+ end
376
+
377
+ context "Ajax DELETE request" do
378
+ before do
379
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
380
+ xhr_with_compatibility :delete, :destroy, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
381
+ end
382
+
383
+ it "returns 200 as http status code" do
384
+ expect(response.status).to eq(200)
385
+ end
386
+
387
+ it "assigns subscription index as @subscriptions" do
388
+ expect(assigns(:subscriptions)).to eq([])
389
+ end
390
+
391
+ it "deletes the subscription" do
392
+ expect(assigns(test_target.subscriptions.where(id: @subscription.id).exists?)).to be_falsey
393
+ end
394
+
395
+ it "renders the :destroy template as format js" do
396
+ expect(response).to render_template :destroy, format: :js
397
+ end
398
+ end
399
+ end
400
+
401
+ describe "POST #subscribe" do
402
+ context "http direct POST request" do
403
+ before do
404
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
405
+ @subscription.unsubscribe
406
+ expect(@subscription.subscribing).to be_falsey
407
+ post_with_compatibility :subscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
408
+ end
409
+
410
+ it "returns 302 as http status code" do
411
+ expect(response.status).to eq(302)
412
+ end
413
+
414
+ it "updates subscribing to true" do
415
+ expect(@subscription.reload.subscribing).to be_truthy
416
+ end
417
+
418
+ it "redirects to :index" do
419
+ expect(response).to redirect_to action: :index
420
+ end
421
+ end
422
+
423
+ context "http POST request from root_path" do
424
+ before do
425
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
426
+ @subscription.unsubscribe
427
+ expect(@subscription.subscribing).to be_falsey
428
+ request.env["HTTP_REFERER"] = root_path
429
+ post_with_compatibility :subscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
430
+ end
431
+
432
+ it "returns 302 as http status code" do
433
+ expect(response.status).to eq(302)
434
+ end
435
+
436
+ it "updates subscribing to true" do
437
+ expect(@subscription.reload.subscribing).to be_truthy
438
+ end
439
+
440
+ it "redirects to root_path as request.referer" do
441
+ expect(response).to redirect_to root_path
442
+ end
443
+ end
444
+
445
+ context "Ajax POST request" do
446
+ before do
447
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
448
+ @subscription.unsubscribe
449
+ expect(@subscription.subscribing).to be_falsey
450
+ request.env["HTTP_REFERER"] = root_path
451
+ xhr_with_compatibility :post, :subscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
452
+ end
453
+
454
+ it "returns 200 as http status code" do
455
+ expect(response.status).to eq(200)
456
+ end
457
+
458
+ it "assigns subscription index as @subscriptions" do
459
+ expect(assigns(:subscriptions)).to eq([@subscription])
460
+ end
461
+
462
+ it "updates subscribing to true" do
463
+ expect(@subscription.reload.subscribing).to be_truthy
464
+ end
465
+
466
+ it "renders the :open template as format js" do
467
+ expect(response).to render_template :subscribe, format: :js
468
+ end
469
+ end
470
+ end
471
+
472
+ describe "POST #unsubscribe" do
473
+ context "http direct POST request" do
474
+ before do
475
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
476
+ expect(@subscription.subscribing).to be_truthy
477
+ post_with_compatibility :unsubscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
478
+ end
479
+
480
+ it "returns 302 as http status code" do
481
+ expect(response.status).to eq(302)
482
+ end
483
+
484
+ it "updates subscribing to false" do
485
+ expect(@subscription.reload.subscribing).to be_falsey
486
+ end
487
+
488
+ it "redirects to :index" do
489
+ expect(response).to redirect_to action: :index
490
+ end
491
+ end
492
+
493
+ context "http POST request from root_path" do
494
+ before do
495
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
496
+ expect(@subscription.subscribing).to be_truthy
497
+ request.env["HTTP_REFERER"] = root_path
498
+ post_with_compatibility :unsubscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
499
+ end
500
+
501
+ it "returns 302 as http status code" do
502
+ expect(response.status).to eq(302)
503
+ end
504
+
505
+ it "updates subscribing to false" do
506
+ expect(@subscription.reload.subscribing).to be_falsey
507
+ end
508
+
509
+ it "redirects to root_path as request.referer" do
510
+ expect(response).to redirect_to root_path
511
+ end
512
+ end
513
+
514
+ context "Ajax POST request" do
515
+ before do
516
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
517
+ expect(@subscription.subscribing).to be_truthy
518
+ request.env["HTTP_REFERER"] = root_path
519
+ xhr_with_compatibility :post, :unsubscribe, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
520
+ end
521
+
522
+ it "returns 200 as http status code" do
523
+ expect(response.status).to eq(200)
524
+ end
525
+
526
+ it "assigns subscription index as @subscriptions" do
527
+ expect(assigns(:subscriptions)).to eq([@subscription])
528
+ end
529
+
530
+ it "updates subscribing to false" do
531
+ expect(@subscription.reload.subscribing).to be_falsey
532
+ end
533
+
534
+ it "renders the :open template as format js" do
535
+ expect(response).to render_template :unsubscribe, format: :js
536
+ end
537
+ end
538
+ end
539
+
540
+ describe "POST #subscribe_to_email" do
541
+ context "http direct POST request" do
542
+ before do
543
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
544
+ @subscription.unsubscribe_to_email
545
+ expect(@subscription.subscribing_to_email).to be_falsey
546
+ post_with_compatibility :subscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
547
+ end
548
+
549
+ it "returns 302 as http status code" do
550
+ expect(response.status).to eq(302)
551
+ end
552
+
553
+ it "updates subscribing_to_email to true" do
554
+ expect(@subscription.reload.subscribing_to_email).to be_truthy
555
+ end
556
+
557
+ it "redirects to :index" do
558
+ expect(response).to redirect_to action: :index
559
+ end
560
+ end
561
+
562
+ context "http POST request from root_path" do
563
+ before do
564
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
565
+ @subscription.unsubscribe_to_email
566
+ expect(@subscription.subscribing_to_email).to be_falsey
567
+ request.env["HTTP_REFERER"] = root_path
568
+ post_with_compatibility :subscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
569
+ end
570
+
571
+ it "returns 302 as http status code" do
572
+ expect(response.status).to eq(302)
573
+ end
574
+
575
+ it "updates subscribing_to_email to true" do
576
+ expect(@subscription.reload.subscribing_to_email).to be_truthy
577
+ end
578
+
579
+ it "redirects to root_path as request.referer" do
580
+ expect(response).to redirect_to root_path
581
+ end
582
+ end
583
+
584
+ context "Ajax POST request" do
585
+ before do
586
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
587
+ @subscription.unsubscribe_to_email
588
+ expect(@subscription.subscribing_to_email).to be_falsey
589
+ request.env["HTTP_REFERER"] = root_path
590
+ xhr_with_compatibility :post, :subscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
591
+ end
592
+
593
+ it "returns 200 as http status code" do
594
+ expect(response.status).to eq(200)
595
+ end
596
+
597
+ it "assigns subscription index as @subscriptions" do
598
+ expect(assigns(:subscriptions)).to eq([@subscription])
599
+ end
600
+
601
+ it "updates subscribing_to_email to true" do
602
+ expect(@subscription.reload.subscribing_to_email).to be_truthy
603
+ end
604
+
605
+ it "renders the :open template as format js" do
606
+ expect(response).to render_template :subscribe_to_email, format: :js
607
+ end
608
+ end
609
+
610
+ context "with unsubscribed target" do
611
+ before do
612
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
613
+ @subscription.unsubscribe
614
+ expect(@subscription.subscribing).to be_falsey
615
+ expect(@subscription.subscribing_to_email).to be_falsey
616
+ post_with_compatibility :subscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
617
+ end
618
+
619
+ it "returns 302 as http status code" do
620
+ expect(response.status).to eq(302)
621
+ end
622
+
623
+ it "cannot update subscribing_to_email to true" do
624
+ expect(@subscription.reload.subscribing_to_email).to be_falsey
625
+ end
626
+
627
+ it "redirects to :index" do
628
+ expect(response).to redirect_to action: :index
629
+ end
630
+ end
631
+ end
632
+
633
+ describe "POST #unsubscribe_to_email" do
634
+ context "http direct POST request" do
635
+ before do
636
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
637
+ expect(@subscription.subscribing_to_email).to be_truthy
638
+ post_with_compatibility :unsubscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
639
+ end
640
+
641
+ it "returns 302 as http status code" do
642
+ expect(response.status).to eq(302)
643
+ end
644
+
645
+ it "updates subscribing_to_email to false" do
646
+ expect(@subscription.reload.subscribing_to_email).to be_falsey
647
+ end
648
+
649
+ it "redirects to :index" do
650
+ expect(response).to redirect_to action: :index
651
+ end
652
+ end
653
+
654
+ context "http POST request from root_path" do
655
+ before do
656
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
657
+ expect(@subscription.subscribing_to_email).to be_truthy
658
+ request.env["HTTP_REFERER"] = root_path
659
+ post_with_compatibility :unsubscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
660
+ end
661
+
662
+ it "returns 302 as http status code" do
663
+ expect(response.status).to eq(302)
664
+ end
665
+
666
+ it "updates subscribing_to_email to false" do
667
+ expect(@subscription.reload.subscribing_to_email).to be_falsey
668
+ end
669
+
670
+ it "redirects to root_path as request.referer" do
671
+ expect(response).to redirect_to root_path
672
+ end
673
+ end
674
+
675
+ context "Ajax POST request" do
676
+ before do
677
+ @subscription = create(:subscription, target: test_target, key: 'test_subscription_key')
678
+ expect(@subscription.subscribing_to_email).to be_truthy
679
+ request.env["HTTP_REFERER"] = root_path
680
+ xhr_with_compatibility :post, :unsubscribe_to_email, target_params.merge({ id: @subscription, typed_target_param => test_target }), valid_session
681
+ end
682
+
683
+ it "returns 200 as http status code" do
684
+ expect(response.status).to eq(200)
685
+ end
686
+
687
+ it "assigns subscription index as @subscriptions" do
688
+ expect(assigns(:subscriptions)).to eq([@subscription])
689
+ end
690
+
691
+ it "updates subscribing_to_email to false" do
692
+ expect(@subscription.reload.subscribing_to_email).to be_falsey
693
+ end
694
+
695
+ it "renders the :open template as format js" do
696
+ expect(response).to render_template :unsubscribe_to_email, format: :js
697
+ end
698
+ end
699
+ end
700
+
701
+
702
+ private
703
+
704
+ def get_with_compatibility action, params, session
705
+ if Rails::VERSION::MAJOR <= 4
706
+ get action, params, session
707
+ else
708
+ get action, params: params, session: session
709
+ end
710
+ end
711
+
712
+ def post_with_compatibility action, params, session
713
+ if Rails::VERSION::MAJOR <= 4
714
+ post action, params, session
715
+ else
716
+ post action, params: params, session: session
717
+ end
718
+ end
719
+
720
+ def delete_with_compatibility action, params, session
721
+ if Rails::VERSION::MAJOR <= 4
722
+ delete action, params, session
723
+ else
724
+ delete action, params: params, session: session
725
+ end
726
+ end
727
+
728
+ def xhr_with_compatibility method, action, params, session
729
+ if Rails::VERSION::MAJOR <= 4
730
+ xhr method, action, params, session
731
+ else
732
+ send method.to_s, action, xhr: true, params: params, session: session
733
+ end
734
+ end
735
+ end