activity_notification 2.1.2 → 2.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +10 -16
  3. data/CHANGELOG.md +52 -0
  4. data/Gemfile +2 -3
  5. data/README.md +2 -2
  6. data/activity_notification.gemspec +1 -1
  7. data/app/channels/activity_notification/notification_api_with_devise_channel.rb +1 -1
  8. data/app/channels/activity_notification/notification_channel.rb +1 -1
  9. data/app/channels/activity_notification/notification_with_devise_channel.rb +1 -1
  10. data/app/controllers/activity_notification/notifications_controller.rb +0 -20
  11. data/app/controllers/activity_notification/subscriptions_api_controller.rb +1 -1
  12. data/app/controllers/activity_notification/subscriptions_controller.rb +2 -2
  13. data/app/views/activity_notification/subscriptions/default/_form.html.erb +1 -1
  14. data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +2 -2
  15. data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +2 -2
  16. data/app/views/activity_notification/subscriptions/default/index.html.erb +2 -2
  17. data/app/views/activity_notification/subscriptions/default/show.html.erb +2 -2
  18. data/bin/bundle_update.sh +0 -1
  19. data/docs/Functions.md +19 -3
  20. data/docs/Setup.md +190 -63
  21. data/gemfiles/Gemfile.rails-5.0 +3 -1
  22. data/gemfiles/Gemfile.rails-5.1 +3 -1
  23. data/gemfiles/Gemfile.rails-5.2 +3 -1
  24. data/gemfiles/Gemfile.rails-6.0 +2 -3
  25. data/gemfiles/{Gemfile.rails-4.2 → Gemfile.rails-6.1} +4 -5
  26. data/lib/activity_notification/apis/notification_api.rb +7 -0
  27. data/lib/activity_notification/apis/subscription_api.rb +5 -5
  28. data/lib/activity_notification/common.rb +15 -4
  29. data/lib/activity_notification/config.rb +53 -23
  30. data/lib/activity_notification/controllers/common_controller.rb +2 -18
  31. data/lib/activity_notification/models/concerns/notifiable.rb +12 -12
  32. data/lib/activity_notification/models/concerns/subscriber.rb +6 -4
  33. data/lib/activity_notification/models/concerns/swagger/notification_schema.rb +34 -34
  34. data/lib/activity_notification/models/concerns/swagger/subscription_schema.rb +17 -17
  35. data/lib/activity_notification/models/concerns/target.rb +8 -12
  36. data/lib/activity_notification/optional_targets/action_cable_api_channel.rb +1 -1
  37. data/lib/activity_notification/optional_targets/action_cable_channel.rb +1 -1
  38. data/lib/activity_notification/orm/active_record.rb +1 -1
  39. data/lib/activity_notification/orm/active_record/notification.rb +3 -3
  40. data/lib/activity_notification/orm/dynamoid.rb +10 -3
  41. data/lib/activity_notification/orm/dynamoid/notification.rb +49 -14
  42. data/lib/activity_notification/orm/dynamoid/subscription.rb +2 -2
  43. data/lib/activity_notification/orm/mongoid.rb +10 -3
  44. data/lib/activity_notification/orm/mongoid/notification.rb +8 -6
  45. data/lib/activity_notification/orm/mongoid/subscription.rb +1 -1
  46. data/lib/activity_notification/renderable.rb +2 -2
  47. data/lib/activity_notification/roles/acts_as_notifiable.rb +13 -16
  48. data/lib/activity_notification/version.rb +1 -1
  49. data/lib/generators/templates/activity_notification.rb +11 -1
  50. data/lib/generators/templates/migrations/migration.rb +1 -1
  51. data/spec/channels/notification_api_channel_spec.rb +42 -44
  52. data/spec/channels/notification_api_with_devise_channel_spec.rb +57 -59
  53. data/spec/channels/notification_channel_spec.rb +41 -43
  54. data/spec/channels/notification_with_devise_channel_spec.rb +75 -77
  55. data/spec/concerns/apis/subscription_api_spec.rb +144 -2
  56. data/spec/concerns/common_spec.rb +25 -3
  57. data/spec/concerns/models/notifiable_spec.rb +35 -35
  58. data/spec/concerns/models/subscriber_spec.rb +179 -6
  59. data/spec/concerns/models/target_spec.rb +10 -12
  60. data/spec/concerns/renderable_spec.rb +5 -5
  61. data/spec/config_spec.rb +26 -15
  62. data/spec/controllers/controller_spec_utility.rb +15 -51
  63. data/spec/generators/migration/migration_generator_spec.rb +2 -10
  64. data/spec/helpers/view_helpers_spec.rb +1 -1
  65. data/spec/optional_targets/action_cable_api_channel_spec.rb +21 -24
  66. data/spec/optional_targets/action_cable_channel_spec.rb +26 -29
  67. data/spec/rails_app/app/controllers/users_controller.rb +5 -0
  68. data/spec/rails_app/app/javascript/App.vue +8 -72
  69. data/spec/rails_app/app/javascript/components/DeviseTokenAuth.vue +3 -4
  70. data/spec/rails_app/app/javascript/components/Top.vue +2 -3
  71. data/spec/rails_app/app/javascript/packs/spa.js +6 -3
  72. data/spec/rails_app/app/javascript/router/index.js +73 -0
  73. data/spec/rails_app/app/javascript/store/{auth.js → index.js} +0 -0
  74. data/spec/rails_app/app/models/dummy/dummy_group.rb +8 -0
  75. data/spec/rails_app/app/models/dummy/dummy_notifiable_target.rb +8 -0
  76. data/spec/rails_app/app/models/user.rb +2 -1
  77. data/spec/rails_app/config/application.rb +6 -7
  78. data/spec/rails_app/config/dynamoid.rb +11 -3
  79. data/spec/rails_app/config/environments/production.rb +3 -0
  80. data/spec/rails_app/config/environments/test.rb +2 -11
  81. data/spec/rails_app/config/initializers/activity_notification.rb +13 -3
  82. data/spec/rails_app/config/routes.rb +5 -1
  83. data/spec/rails_app/db/seeds.rb +9 -2
  84. data/spec/rails_app/package.json +14 -14
  85. data/spec/roles/acts_as_notifiable_spec.rb +5 -5
  86. data/spec/spec_helper.rb +1 -5
  87. metadata +12 -12
  88. data/spec/support/patch_rails_42_action_controller_test_response.rb +0 -11
@@ -1,3 +1,3 @@
1
1
  module ActivityNotification
2
- VERSION = "2.1.2"
2
+ VERSION = "2.2.2"
3
3
  end
@@ -31,6 +31,16 @@ ActivityNotification.configure do |config|
31
31
  # Set false when you want to unsubscribe to any notifications as default.
32
32
  config.subscribe_as_default = true
33
33
 
34
+ # Configure default email subscription value to use when the subscription record does not configured.
35
+ # Note that you can configure them for each method calling as default argument.
36
+ # Set false when you want to unsubscribe to email notifications as default.
37
+ # config.subscribe_to_email_as_default = true
38
+
39
+ # Configure default optional target subscription value to use when the subscription record does not configured.
40
+ # Note that you can configure them for each method calling as default argument.
41
+ # Set false when you want to unsubscribe to optinal target notifications as default.
42
+ # config.subscribe_to_optional_targets_as_default = true
43
+
34
44
  # Configure the e-mail address which will be shown in ActivityNotification::Mailer,
35
45
  # note that it will be overwritten if you use your own mailer class with default "from" parameter.
36
46
  config.mailer_sender = 'please-change-me-at-config-initializers-activity_notification@example.com'
@@ -76,7 +86,7 @@ ActivityNotification.configure do |config|
76
86
  # Set true when you want to turn on WebSocket API subscription using ActionCable as default.
77
87
  config.action_cable_api_enabled = false
78
88
 
79
- # Configure if ctivity_notification publishes WebSocket notifications using ActionCable only to authenticated target with Devise.
89
+ # Configure if activity_notification publishes WebSocket notifications using ActionCable only to authenticated target with Devise.
80
90
  # Note that you can configure them for each model by acts_as roles.
81
91
  # Set true when you want to use Device integration with WebSocket subscription using ActionCable as default.
82
92
  config.action_cable_with_devise = false
@@ -1,5 +1,5 @@
1
1
  # Migration responsible for creating a table with notifications
2
- class <%= @migration_name %> < ActiveRecord::Migration<%= Rails::VERSION::MAJOR >= 5 ? "[#{Rails.version.to_f}]" : "" %>
2
+ class <%= @migration_name %> < ActiveRecord::Migration<%= "[#{Rails.version.to_f}]" %>
3
3
  # Create tables
4
4
  def change
5
5
  <% if @migration_tables.include?('notifications') %>create_table :notifications do |t|
@@ -1,51 +1,49 @@
1
- if Rails::VERSION::MAJOR >= 5
2
- require 'channels/notification_api_channel_shared_examples'
3
-
4
- # @See https://github.com/palkan/action-cable-testing
5
- describe ActivityNotification::NotificationApiChannel, type: :channel do
6
- let(:test_target) { create(:user) }
7
- let(:target_type) { "User" }
8
- let(:typed_target_param) { "user_id" }
9
- let(:extra_params) { {} }
10
-
11
- context "when target.notification_action_cable_with_devise? returns true" do
12
- before do
13
- @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
14
- User._notification_action_cable_with_devise = true
15
- end
16
-
17
- after do
18
- User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
19
- end
20
-
21
- it "rejects subscription even if target_type and target_id parameters are passed" do
22
- subscribe({ target_type: target_type, target_id: test_target.id })
23
- expect(subscription).to be_rejected
24
- expect {
25
- expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_api_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
26
- }.to raise_error(/Must be subscribed!/)
27
- end
1
+ require 'channels/notification_api_channel_shared_examples'
2
+
3
+ # @See https://github.com/palkan/action-cable-testing
4
+ describe ActivityNotification::NotificationApiChannel, type: :channel do
5
+ let(:test_target) { create(:user) }
6
+ let(:target_type) { "User" }
7
+ let(:typed_target_param) { "user_id" }
8
+ let(:extra_params) { {} }
9
+
10
+ context "when target.notification_action_cable_with_devise? returns true" do
11
+ before do
12
+ @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
13
+ User._notification_action_cable_with_devise = true
28
14
  end
29
15
 
30
- context "when target.notification_action_cable_with_devise? returns false" do
31
- before do
32
- @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
33
- User._notification_action_cable_with_devise = false
34
- @auth_headers = {}
35
- end
36
-
37
- after do
38
- User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
39
- end
16
+ after do
17
+ User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
18
+ end
40
19
 
41
- it "successfully subscribes with target_type and target_id parameters" do
42
- subscribe({ target_type: target_type, target_id: test_target.id })
43
- expect(subscription).to be_confirmed
20
+ it "rejects subscription even if target_type and target_id parameters are passed" do
21
+ subscribe({ target_type: target_type, target_id: test_target.id })
22
+ expect(subscription).to be_rejected
23
+ expect {
44
24
  expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_api_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
45
- expect(subscription).to have_stream_from("activity_notification_api_channel_User##{test_target.id}")
46
- end
25
+ }.to raise_error(/Must be subscribed!/)
26
+ end
27
+ end
47
28
 
48
- it_behaves_like :notification_api_channel
29
+ context "when target.notification_action_cable_with_devise? returns false" do
30
+ before do
31
+ @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
32
+ User._notification_action_cable_with_devise = false
33
+ @auth_headers = {}
49
34
  end
35
+
36
+ after do
37
+ User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
38
+ end
39
+
40
+ it "successfully subscribes with target_type and target_id parameters" do
41
+ subscribe({ target_type: target_type, target_id: test_target.id })
42
+ expect(subscription).to be_confirmed
43
+ expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_api_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
44
+ expect(subscription).to have_stream_from("activity_notification_api_channel_User##{test_target.id}")
45
+ end
46
+
47
+ it_behaves_like :notification_api_channel
50
48
  end
51
- end
49
+ end
@@ -1,78 +1,76 @@
1
- if Rails::VERSION::MAJOR >= 5
2
- require 'channels/notification_api_channel_shared_examples'
1
+ require 'channels/notification_api_channel_shared_examples'
3
2
 
4
- # @See https://github.com/palkan/action-cable-testing
5
- describe ActivityNotification::NotificationApiWithDeviseChannel, type: :channel do
6
- let(:test_user) { create(:confirmed_user) }
7
- let(:unauthenticated_user) { create(:confirmed_user) }
8
- let(:test_target) { create(:admin, user: test_user) }
9
- let(:target_type) { "Admin" }
10
- let(:typed_target_param) { "admin_id" }
11
- let(:extra_params) { { devise_type: :users } }
12
- let(:valid_session) {}
3
+ # @See https://github.com/palkan/action-cable-testing
4
+ describe ActivityNotification::NotificationApiWithDeviseChannel, type: :channel do
5
+ let(:test_user) { create(:confirmed_user) }
6
+ let(:unauthenticated_user) { create(:confirmed_user) }
7
+ let(:test_target) { create(:admin, user: test_user) }
8
+ let(:target_type) { "Admin" }
9
+ let(:typed_target_param) { "admin_id" }
10
+ let(:extra_params) { { devise_type: :users } }
11
+ let(:valid_session) {}
13
12
 
14
- # @See https://github.com/lynndylanhurley/devise_token_auth
15
- def sign_in(current_target)
16
- @auth_headers = current_target.create_new_auth_token
17
- end
13
+ # @See https://github.com/lynndylanhurley/devise_token_auth
14
+ def sign_in(current_target)
15
+ @auth_headers = current_target.create_new_auth_token
16
+ end
18
17
 
19
- before do
20
- @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
21
- User._notification_action_cable_with_devise = true
22
- end
18
+ before do
19
+ @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
20
+ User._notification_action_cable_with_devise = true
21
+ end
23
22
 
24
- after do
25
- User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
26
- end
23
+ after do
24
+ User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
25
+ end
27
26
 
28
- context "signed in with devise as authenticated user" do
29
- before do
30
- sign_in test_user
31
- end
32
-
33
- it_behaves_like :notification_api_channel
27
+ context "signed in with devise as authenticated user" do
28
+ before do
29
+ sign_in test_user
34
30
  end
31
+
32
+ it_behaves_like :notification_api_channel
33
+ end
35
34
 
36
- context "signed in with devise as unauthenticated user" do
37
- let(:target_params) { { target_type: target_type, devise_type: :users } }
35
+ context "signed in with devise as unauthenticated user" do
36
+ let(:target_params) { { target_type: target_type, devise_type: :users } }
38
37
 
39
- before do
40
- sign_in unauthenticated_user
41
- end
38
+ before do
39
+ sign_in unauthenticated_user
40
+ end
42
41
 
43
- it "rejects subscription" do
44
- subscribe(target_params.merge({ typed_target_param => test_target }).merge(@auth_headers))
45
- expect(subscription).to be_rejected
46
- expect {
47
- expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_api_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
48
- }.to raise_error(/Must be subscribed!/)
49
- end
42
+ it "rejects subscription" do
43
+ subscribe(target_params.merge({ typed_target_param => test_target }).merge(@auth_headers))
44
+ expect(subscription).to be_rejected
45
+ expect {
46
+ expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_api_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
47
+ }.to raise_error(/Must be subscribed!/)
50
48
  end
49
+ end
51
50
 
52
- context "unsigned in with devise" do
53
- let(:target_params) { { target_type: target_type, devise_type: :users } }
51
+ context "unsigned in with devise" do
52
+ let(:target_params) { { target_type: target_type, devise_type: :users } }
54
53
 
55
- it "rejects subscription" do
56
- subscribe(target_params.merge({ typed_target_param => test_target }))
57
- expect(subscription).to be_rejected
58
- expect {
59
- expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_api_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
60
- }.to raise_error(/Must be subscribed!/)
61
- end
54
+ it "rejects subscription" do
55
+ subscribe(target_params.merge({ typed_target_param => test_target }))
56
+ expect(subscription).to be_rejected
57
+ expect {
58
+ expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_api_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
59
+ }.to raise_error(/Must be subscribed!/)
62
60
  end
61
+ end
63
62
 
64
- context "without target_id and (typed_target)_id parameters for devise integrated channel with devise_type option" do
65
- let(:target_params) { { target_type: target_type, devise_type: :users } }
63
+ context "without target_id and (typed_target)_id parameters for devise integrated channel with devise_type option" do
64
+ let(:target_params) { { target_type: target_type, devise_type: :users } }
66
65
 
67
- before do
68
- sign_in test_target.user
69
- end
66
+ before do
67
+ sign_in test_target.user
68
+ end
70
69
 
71
- it "successfully subscribes" do
72
- subscribe(target_params.merge(@auth_headers))
73
- expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_api_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
74
- expect(subscription).to have_stream_from("activity_notification_api_channel_Admin##{test_target.id}")
75
- end
70
+ it "successfully subscribes" do
71
+ subscribe(target_params.merge(@auth_headers))
72
+ expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_api_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
73
+ expect(subscription).to have_stream_from("activity_notification_api_channel_Admin##{test_target.id}")
76
74
  end
77
75
  end
78
76
  end
@@ -1,50 +1,48 @@
1
- if Rails::VERSION::MAJOR >= 5
2
- require 'channels/notification_channel_shared_examples'
3
-
4
- # @See https://github.com/palkan/action-cable-testing
5
- describe ActivityNotification::NotificationChannel, type: :channel do
6
- let(:test_target) { create(:user) }
7
- let(:target_type) { "User" }
8
- let(:typed_target_param) { "user_id" }
9
- let(:extra_params) { {} }
10
-
11
- context "when target.notification_action_cable_with_devise? returns true" do
12
- before do
13
- @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
14
- User._notification_action_cable_with_devise = true
15
- end
16
-
17
- after do
18
- User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
19
- end
20
-
21
- it "rejects subscription even if target_type and target_id parameters are passed" do
22
- subscribe({ target_type: target_type, target_id: test_target.id })
23
- expect(subscription).to be_rejected
24
- expect {
25
- expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
26
- }.to raise_error(/Must be subscribed!/)
27
- end
1
+ require 'channels/notification_channel_shared_examples'
2
+
3
+ # @See https://github.com/palkan/action-cable-testing
4
+ describe ActivityNotification::NotificationChannel, type: :channel do
5
+ let(:test_target) { create(:user) }
6
+ let(:target_type) { "User" }
7
+ let(:typed_target_param) { "user_id" }
8
+ let(:extra_params) { {} }
9
+
10
+ context "when target.notification_action_cable_with_devise? returns true" do
11
+ before do
12
+ @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
13
+ User._notification_action_cable_with_devise = true
28
14
  end
29
15
 
30
- context "when target.notification_action_cable_with_devise? returns false" do
31
- before do
32
- @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
33
- User._notification_action_cable_with_devise = false
34
- end
35
-
36
- after do
37
- User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
38
- end
16
+ after do
17
+ User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
18
+ end
39
19
 
40
- it "successfully subscribes with target_type and target_id parameters" do
41
- subscribe({ target_type: target_type, target_id: test_target.id })
42
- expect(subscription).to be_confirmed
20
+ it "rejects subscription even if target_type and target_id parameters are passed" do
21
+ subscribe({ target_type: target_type, target_id: test_target.id })
22
+ expect(subscription).to be_rejected
23
+ expect {
43
24
  expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
44
- expect(subscription).to have_stream_from("activity_notification_channel_User##{test_target.id}")
45
- end
25
+ }.to raise_error(/Must be subscribed!/)
26
+ end
27
+ end
46
28
 
47
- it_behaves_like :notification_channel
29
+ context "when target.notification_action_cable_with_devise? returns false" do
30
+ before do
31
+ @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
32
+ User._notification_action_cable_with_devise = false
48
33
  end
34
+
35
+ after do
36
+ User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
37
+ end
38
+
39
+ it "successfully subscribes with target_type and target_id parameters" do
40
+ subscribe({ target_type: target_type, target_id: test_target.id })
41
+ expect(subscription).to be_confirmed
42
+ expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
43
+ expect(subscription).to have_stream_from("activity_notification_channel_User##{test_target.id}")
44
+ end
45
+
46
+ it_behaves_like :notification_channel
49
47
  end
50
- end
48
+ end
@@ -1,99 +1,97 @@
1
- if Rails::VERSION::MAJOR >= 5
2
- require 'channels/notification_channel_shared_examples'
1
+ require 'channels/notification_channel_shared_examples'
3
2
 
4
- #TODO Make it more smart test method
5
- module ActivityNotification
6
- module Test
7
- class NotificationWithDeviseChannel < ::ActivityNotification::NotificationWithDeviseChannel
8
- @@custom_current_target = nil
9
-
10
- def set_custom_current_target(custom_current_target)
11
- @@custom_current_target = custom_current_target
12
- end
13
-
14
- def find_current_target(devise_type = nil)
15
- super(devise_type)
16
- rescue NoMethodError
17
- devise_type = (devise_type || @target.notification_devise_resource.class.name).to_s
18
- @@custom_current_target.is_a?(devise_type.to_model_class) ? @@custom_current_target : nil
19
- end
3
+ #TODO Make it more smart test method
4
+ module ActivityNotification
5
+ module Test
6
+ class NotificationWithDeviseChannel < ::ActivityNotification::NotificationWithDeviseChannel
7
+ @@custom_current_target = nil
8
+
9
+ def set_custom_current_target(custom_current_target)
10
+ @@custom_current_target = custom_current_target
11
+ end
12
+
13
+ def find_current_target(devise_type = nil)
14
+ super(devise_type)
15
+ rescue NoMethodError
16
+ devise_type = (devise_type || @target.notification_devise_resource.class.name).to_s
17
+ @@custom_current_target.is_a?(devise_type.to_model_class) ? @@custom_current_target : nil
20
18
  end
21
19
  end
22
20
  end
21
+ end
23
22
 
24
- # @See https://github.com/palkan/action-cable-testing
25
- describe ActivityNotification::Test::NotificationWithDeviseChannel, type: :channel do
26
- let(:test_user) { create(:confirmed_user) }
27
- let(:unauthenticated_user) { create(:confirmed_user) }
28
- let(:test_target) { create(:admin, user: test_user) }
29
- let(:target_type) { "Admin" }
30
- let(:typed_target_param) { "admin_id" }
31
- let(:extra_params) { { devise_type: :users } }
32
- let(:valid_session) {}
33
-
34
- #TODO Make it more smart test method
35
- #include Devise::Test::IntegrationHelpers
36
- def sign_in(current_target)
37
- described_class.new(ActionCable::Channel::ConnectionStub.new, {}).set_custom_current_target(current_target)
38
- end
23
+ # @See https://github.com/palkan/action-cable-testing
24
+ describe ActivityNotification::Test::NotificationWithDeviseChannel, type: :channel do
25
+ let(:test_user) { create(:confirmed_user) }
26
+ let(:unauthenticated_user) { create(:confirmed_user) }
27
+ let(:test_target) { create(:admin, user: test_user) }
28
+ let(:target_type) { "Admin" }
29
+ let(:typed_target_param) { "admin_id" }
30
+ let(:extra_params) { { devise_type: :users } }
31
+ let(:valid_session) {}
39
32
 
40
- before do
41
- @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
42
- User._notification_action_cable_with_devise = true
43
- end
33
+ #TODO Make it more smart test method
34
+ #include Devise::Test::IntegrationHelpers
35
+ def sign_in(current_target)
36
+ described_class.new(ActionCable::Channel::ConnectionStub.new, {}).set_custom_current_target(current_target)
37
+ end
44
38
 
45
- after do
46
- User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
47
- end
39
+ before do
40
+ @user_notification_action_cable_with_devise = User._notification_action_cable_with_devise
41
+ User._notification_action_cable_with_devise = true
42
+ end
48
43
 
49
- context "signed in with devise as authenticated user" do
50
- before do
51
- sign_in test_user
52
- end
53
-
54
- it_behaves_like :notification_channel
44
+ after do
45
+ User._notification_action_cable_with_devise = @user_notification_action_cable_with_devise
46
+ end
47
+
48
+ context "signed in with devise as authenticated user" do
49
+ before do
50
+ sign_in test_user
55
51
  end
52
+
53
+ it_behaves_like :notification_channel
54
+ end
56
55
 
57
- context "signed in with devise as unauthenticated user" do
58
- let(:target_params) { { target_type: target_type, devise_type: :users } }
56
+ context "signed in with devise as unauthenticated user" do
57
+ let(:target_params) { { target_type: target_type, devise_type: :users } }
59
58
 
60
- before do
61
- sign_in unauthenticated_user
62
- end
59
+ before do
60
+ sign_in unauthenticated_user
61
+ end
63
62
 
64
- it "rejects subscription" do
65
- subscribe(target_params.merge({ typed_target_param => test_target }))
66
- expect(subscription).to be_rejected
67
- expect {
68
- expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
69
- }.to raise_error(/Must be subscribed!/)
70
- end
63
+ it "rejects subscription" do
64
+ subscribe(target_params.merge({ typed_target_param => test_target }))
65
+ expect(subscription).to be_rejected
66
+ expect {
67
+ expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
68
+ }.to raise_error(/Must be subscribed!/)
71
69
  end
70
+ end
72
71
 
73
- context "unsigned in with devise" do
74
- let(:target_params) { { target_type: target_type, devise_type: :users } }
72
+ context "unsigned in with devise" do
73
+ let(:target_params) { { target_type: target_type, devise_type: :users } }
75
74
 
76
- it "rejects subscription" do
77
- subscribe(target_params.merge({ typed_target_param => test_target }))
78
- expect(subscription).to be_rejected
79
- expect {
80
- expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
81
- }.to raise_error(/Must be subscribed!/)
82
- end
75
+ it "rejects subscription" do
76
+ subscribe(target_params.merge({ typed_target_param => test_target }))
77
+ expect(subscription).to be_rejected
78
+ expect {
79
+ expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
80
+ }.to raise_error(/Must be subscribed!/)
83
81
  end
82
+ end
84
83
 
85
- context "without target_id and (typed_target)_id parameters for devise integrated channel with devise_type option" do
86
- let(:target_params) { { target_type: target_type, devise_type: :users } }
84
+ context "without target_id and (typed_target)_id parameters for devise integrated channel with devise_type option" do
85
+ let(:target_params) { { target_type: target_type, devise_type: :users } }
87
86
 
88
- before do
89
- sign_in test_target.user
90
- end
87
+ before do
88
+ sign_in test_target.user
89
+ end
91
90
 
92
- it "successfully subscribes" do
93
- subscribe(target_params)
94
- expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
95
- expect(subscription).to have_stream_from("activity_notification_channel_Admin##{test_target.id}")
96
- end
91
+ it "successfully subscribes" do
92
+ subscribe(target_params)
93
+ expect(subscription).to have_stream_from("#{ActivityNotification.config.notification_channel_prefix}_#{test_target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{test_target.id}")
94
+ expect(subscription).to have_stream_from("activity_notification_channel_Admin##{test_target.id}")
97
95
  end
98
96
  end
99
97
  end