activity_notification 2.4.1 → 2.5.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 (250) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -2
  3. data/app/jobs/activity_notification/cascading_notification_job.rb +123 -0
  4. data/docs/Functions.md +197 -1
  5. data/lib/activity_notification/apis/cascading_notification_api.rb +208 -0
  6. data/lib/activity_notification/apis/notification_api.rb +3 -0
  7. data/lib/activity_notification/config.rb +10 -0
  8. data/lib/activity_notification/mailers/helpers.rb +27 -1
  9. data/lib/activity_notification/version.rb +1 -1
  10. data/lib/generators/templates/activity_notification.rb +8 -0
  11. metadata +5 -441
  12. data/.codeclimate.yml +0 -33
  13. data/.coveralls.yml +0 -1
  14. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -22
  15. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -17
  16. data/.github/pull_request_template.md +0 -13
  17. data/.github/workflows/build.yml +0 -100
  18. data/.gitignore +0 -74
  19. data/.rspec +0 -3
  20. data/.rubocop.yml +0 -1157
  21. data/.yardopts +0 -6
  22. data/CHANGELOG.md +0 -452
  23. data/Gemfile +0 -31
  24. data/Procfile +0 -2
  25. data/Rakefile +0 -28
  26. data/activity_notification.gemspec +0 -44
  27. data/ai-curated-specs/issues/172/design.md +0 -220
  28. data/ai-curated-specs/issues/172/tasks.md +0 -326
  29. data/ai-curated-specs/issues/188/design.md +0 -227
  30. data/ai-curated-specs/issues/188/requirements.md +0 -78
  31. data/ai-curated-specs/issues/188/tasks.md +0 -203
  32. data/ai-curated-specs/issues/188/upstream-contributions.md +0 -592
  33. data/ai-curated-specs/issues/50/design.md +0 -235
  34. data/ai-curated-specs/issues/50/requirements.md +0 -49
  35. data/ai-curated-specs/issues/50/tasks.md +0 -232
  36. data/bin/_dynamodblocal +0 -4
  37. data/bin/bundle_update.sh +0 -7
  38. data/bin/deploy_on_heroku.sh +0 -16
  39. data/bin/install_dynamodblocal.sh +0 -5
  40. data/bin/start_dynamodblocal.sh +0 -47
  41. data/bin/stop_dynamodblocal.sh +0 -34
  42. data/gemfiles/Gemfile.rails-5.0 +0 -25
  43. data/gemfiles/Gemfile.rails-5.1 +0 -25
  44. data/gemfiles/Gemfile.rails-5.2 +0 -24
  45. data/gemfiles/Gemfile.rails-6.0 +0 -23
  46. data/gemfiles/Gemfile.rails-6.1 +0 -22
  47. data/gemfiles/Gemfile.rails-7.0 +0 -25
  48. data/gemfiles/Gemfile.rails-7.1 +0 -23
  49. data/gemfiles/Gemfile.rails-7.2 +0 -23
  50. data/gemfiles/Gemfile.rails-8.0 +0 -24
  51. data/package.json +0 -8
  52. data/spec/channels/notification_api_channel_shared_examples.rb +0 -59
  53. data/spec/channels/notification_api_channel_spec.rb +0 -49
  54. data/spec/channels/notification_api_with_devise_channel_spec.rb +0 -76
  55. data/spec/channels/notification_channel_shared_examples.rb +0 -59
  56. data/spec/channels/notification_channel_spec.rb +0 -48
  57. data/spec/channels/notification_with_devise_channel_spec.rb +0 -97
  58. data/spec/concerns/apis/notification_api_spec.rb +0 -1627
  59. data/spec/concerns/apis/subscription_api_spec.rb +0 -474
  60. data/spec/concerns/common_spec.rb +0 -213
  61. data/spec/concerns/models/group_spec.rb +0 -61
  62. data/spec/concerns/models/notifiable_spec.rb +0 -782
  63. data/spec/concerns/models/notifier_spec.rb +0 -71
  64. data/spec/concerns/models/subscriber_spec.rb +0 -800
  65. data/spec/concerns/models/target_spec.rb +0 -1285
  66. data/spec/concerns/renderable_spec.rb +0 -129
  67. data/spec/config_spec.rb +0 -85
  68. data/spec/controllers/common_controller_spec.rb +0 -25
  69. data/spec/controllers/controller_spec_utility.rb +0 -100
  70. data/spec/controllers/dummy_common_controller.rb +0 -5
  71. data/spec/controllers/notifications_api_controller_shared_examples.rb +0 -619
  72. data/spec/controllers/notifications_api_controller_spec.rb +0 -19
  73. data/spec/controllers/notifications_api_with_devise_controller_spec.rb +0 -60
  74. data/spec/controllers/notifications_controller_shared_examples.rb +0 -743
  75. data/spec/controllers/notifications_controller_spec.rb +0 -11
  76. data/spec/controllers/notifications_with_devise_controller_spec.rb +0 -97
  77. data/spec/controllers/subscriptions_api_controller_shared_examples.rb +0 -750
  78. data/spec/controllers/subscriptions_api_controller_spec.rb +0 -19
  79. data/spec/controllers/subscriptions_api_with_devise_controller_spec.rb +0 -60
  80. data/spec/controllers/subscriptions_controller_shared_examples.rb +0 -946
  81. data/spec/controllers/subscriptions_controller_spec.rb +0 -11
  82. data/spec/controllers/subscriptions_with_devise_controller_spec.rb +0 -97
  83. data/spec/factories/admins.rb +0 -5
  84. data/spec/factories/articles.rb +0 -5
  85. data/spec/factories/comments.rb +0 -6
  86. data/spec/factories/dummy/dummy_group.rb +0 -4
  87. data/spec/factories/dummy/dummy_notifiable.rb +0 -4
  88. data/spec/factories/dummy/dummy_notifier.rb +0 -4
  89. data/spec/factories/dummy/dummy_subscriber.rb +0 -4
  90. data/spec/factories/dummy/dummy_target.rb +0 -4
  91. data/spec/factories/notifications.rb +0 -7
  92. data/spec/factories/subscriptions.rb +0 -8
  93. data/spec/factories/users.rb +0 -11
  94. data/spec/generators/controllers_generator_spec.rb +0 -85
  95. data/spec/generators/install_generator_spec.rb +0 -43
  96. data/spec/generators/migration/migration_generator_spec.rb +0 -80
  97. data/spec/generators/models_generator_spec.rb +0 -96
  98. data/spec/generators/views_generator_spec.rb +0 -195
  99. data/spec/helpers/polymorphic_helpers_spec.rb +0 -89
  100. data/spec/helpers/view_helpers_spec.rb +0 -547
  101. data/spec/jobs/notification_resilience_job_spec.rb +0 -167
  102. data/spec/jobs/notify_all_job_spec.rb +0 -23
  103. data/spec/jobs/notify_job_spec.rb +0 -23
  104. data/spec/jobs/notify_to_job_spec.rb +0 -23
  105. data/spec/mailers/mailer_spec.rb +0 -214
  106. data/spec/mailers/notification_resilience_spec.rb +0 -263
  107. data/spec/models/dummy/dummy_group_spec.rb +0 -10
  108. data/spec/models/dummy/dummy_notifiable_spec.rb +0 -10
  109. data/spec/models/dummy/dummy_notifier_spec.rb +0 -10
  110. data/spec/models/dummy/dummy_subscriber_spec.rb +0 -8
  111. data/spec/models/dummy/dummy_target_spec.rb +0 -10
  112. data/spec/models/notification_spec.rb +0 -472
  113. data/spec/models/subscription_spec.rb +0 -215
  114. data/spec/optional_targets/action_cable_api_channel_spec.rb +0 -34
  115. data/spec/optional_targets/action_cable_channel_spec.rb +0 -41
  116. data/spec/optional_targets/amazon_sns_spec.rb +0 -47
  117. data/spec/optional_targets/base_spec.rb +0 -45
  118. data/spec/optional_targets/slack_spec.rb +0 -44
  119. data/spec/orm/dynamoid_spec.rb +0 -115
  120. data/spec/rails_app/Rakefile +0 -15
  121. data/spec/rails_app/app/assets/config/manifest.js +0 -3
  122. data/spec/rails_app/app/assets/images/.keep +0 -0
  123. data/spec/rails_app/app/assets/javascripts/application.js +0 -3
  124. data/spec/rails_app/app/assets/javascripts/cable.js +0 -12
  125. data/spec/rails_app/app/assets/stylesheets/application.css +0 -15
  126. data/spec/rails_app/app/assets/stylesheets/reset.css +0 -85
  127. data/spec/rails_app/app/assets/stylesheets/style.css +0 -244
  128. data/spec/rails_app/app/controllers/admins_controller.rb +0 -21
  129. data/spec/rails_app/app/controllers/application_controller.rb +0 -5
  130. data/spec/rails_app/app/controllers/articles_controller.rb +0 -67
  131. data/spec/rails_app/app/controllers/comments_controller.rb +0 -36
  132. data/spec/rails_app/app/controllers/concerns/.keep +0 -0
  133. data/spec/rails_app/app/controllers/spa_controller.rb +0 -7
  134. data/spec/rails_app/app/controllers/users/notifications_controller.rb +0 -2
  135. data/spec/rails_app/app/controllers/users/notifications_with_devise_controller.rb +0 -2
  136. data/spec/rails_app/app/controllers/users/subscriptions_controller.rb +0 -2
  137. data/spec/rails_app/app/controllers/users/subscriptions_with_devise_controller.rb +0 -2
  138. data/spec/rails_app/app/controllers/users_controller.rb +0 -26
  139. data/spec/rails_app/app/helpers/application_helper.rb +0 -2
  140. data/spec/rails_app/app/helpers/devise_helper.rb +0 -2
  141. data/spec/rails_app/app/javascript/App.vue +0 -40
  142. data/spec/rails_app/app/javascript/components/DeviseTokenAuth.vue +0 -82
  143. data/spec/rails_app/app/javascript/components/Top.vue +0 -98
  144. data/spec/rails_app/app/javascript/components/notifications/Index.vue +0 -200
  145. data/spec/rails_app/app/javascript/components/notifications/Notification.vue +0 -133
  146. data/spec/rails_app/app/javascript/components/notifications/NotificationContent.vue +0 -122
  147. data/spec/rails_app/app/javascript/components/subscriptions/Index.vue +0 -279
  148. data/spec/rails_app/app/javascript/components/subscriptions/NewSubscription.vue +0 -112
  149. data/spec/rails_app/app/javascript/components/subscriptions/NotificationKey.vue +0 -141
  150. data/spec/rails_app/app/javascript/components/subscriptions/Subscription.vue +0 -226
  151. data/spec/rails_app/app/javascript/config/development.js +0 -5
  152. data/spec/rails_app/app/javascript/config/environment.js +0 -7
  153. data/spec/rails_app/app/javascript/config/production.js +0 -5
  154. data/spec/rails_app/app/javascript/config/test.js +0 -5
  155. data/spec/rails_app/app/javascript/packs/application.js +0 -18
  156. data/spec/rails_app/app/javascript/packs/spa.js +0 -14
  157. data/spec/rails_app/app/javascript/router/index.js +0 -73
  158. data/spec/rails_app/app/javascript/store/index.js +0 -37
  159. data/spec/rails_app/app/mailers/.keep +0 -0
  160. data/spec/rails_app/app/mailers/custom_notification_mailer.rb +0 -5
  161. data/spec/rails_app/app/models/admin.rb +0 -35
  162. data/spec/rails_app/app/models/article.rb +0 -54
  163. data/spec/rails_app/app/models/comment.rb +0 -81
  164. data/spec/rails_app/app/models/dummy/dummy_base.rb +0 -11
  165. data/spec/rails_app/app/models/dummy/dummy_group.rb +0 -23
  166. data/spec/rails_app/app/models/dummy/dummy_notifiable.rb +0 -15
  167. data/spec/rails_app/app/models/dummy/dummy_notifiable_target.rb +0 -27
  168. data/spec/rails_app/app/models/dummy/dummy_notifier.rb +0 -15
  169. data/spec/rails_app/app/models/dummy/dummy_subscriber.rb +0 -14
  170. data/spec/rails_app/app/models/dummy/dummy_target.rb +0 -16
  171. data/spec/rails_app/app/models/user.rb +0 -73
  172. data/spec/rails_app/app/views/activity_notification/mailer/dummy_subscribers/test_key.text.erb +0 -1
  173. data/spec/rails_app/app/views/activity_notification/notifications/default/article/_update.html.erb +0 -146
  174. data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_path_test.html.erb +0 -1
  175. data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_test.html.erb +0 -1
  176. data/spec/rails_app/app/views/activity_notification/notifications/users/_custom_index.html.erb +0 -1
  177. data/spec/rails_app/app/views/activity_notification/notifications/users/custom/_test.html.erb +0 -1
  178. data/spec/rails_app/app/views/activity_notification/notifications/users/overridden/custom/_test.html.erb +0 -1
  179. data/spec/rails_app/app/views/activity_notification/optional_targets/admins/amazon_sns/comment/_default.text.erb +0 -10
  180. data/spec/rails_app/app/views/articles/_form.html.erb +0 -24
  181. data/spec/rails_app/app/views/articles/edit.html.erb +0 -8
  182. data/spec/rails_app/app/views/articles/index.html.erb +0 -113
  183. data/spec/rails_app/app/views/articles/new.html.erb +0 -7
  184. data/spec/rails_app/app/views/articles/show.html.erb +0 -49
  185. data/spec/rails_app/app/views/layouts/_header.html.erb +0 -46
  186. data/spec/rails_app/app/views/layouts/application.html.erb +0 -15
  187. data/spec/rails_app/app/views/spa/index.html.erb +0 -2
  188. data/spec/rails_app/babel.config.js +0 -72
  189. data/spec/rails_app/bin/bundle +0 -3
  190. data/spec/rails_app/bin/rails +0 -4
  191. data/spec/rails_app/bin/rake +0 -4
  192. data/spec/rails_app/bin/setup +0 -29
  193. data/spec/rails_app/bin/webpack +0 -18
  194. data/spec/rails_app/bin/webpack-dev-server +0 -18
  195. data/spec/rails_app/config/application.rb +0 -54
  196. data/spec/rails_app/config/boot.rb +0 -5
  197. data/spec/rails_app/config/cable.yml +0 -8
  198. data/spec/rails_app/config/database.yml +0 -36
  199. data/spec/rails_app/config/dynamoid.rb +0 -13
  200. data/spec/rails_app/config/environment.rb +0 -26
  201. data/spec/rails_app/config/environments/development.rb +0 -60
  202. data/spec/rails_app/config/environments/production.rb +0 -85
  203. data/spec/rails_app/config/environments/test.rb +0 -53
  204. data/spec/rails_app/config/initializers/activity_notification.rb +0 -104
  205. data/spec/rails_app/config/initializers/assets.rb +0 -11
  206. data/spec/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  207. data/spec/rails_app/config/initializers/cookies_serializer.rb +0 -3
  208. data/spec/rails_app/config/initializers/copy_it.aws.rb.template +0 -6
  209. data/spec/rails_app/config/initializers/devise.rb +0 -278
  210. data/spec/rails_app/config/initializers/devise_token_auth.rb +0 -55
  211. data/spec/rails_app/config/initializers/filter_parameter_logging.rb +0 -4
  212. data/spec/rails_app/config/initializers/inflections.rb +0 -16
  213. data/spec/rails_app/config/initializers/mime_types.rb +0 -4
  214. data/spec/rails_app/config/initializers/mysql.rb +0 -9
  215. data/spec/rails_app/config/initializers/session_store.rb +0 -3
  216. data/spec/rails_app/config/initializers/wrap_parameters.rb +0 -14
  217. data/spec/rails_app/config/initializers/zeitwerk.rb +0 -10
  218. data/spec/rails_app/config/locales/activity_notification.en.yml +0 -26
  219. data/spec/rails_app/config/locales/devise.en.yml +0 -62
  220. data/spec/rails_app/config/mongoid.yml +0 -13
  221. data/spec/rails_app/config/routes.rb +0 -50
  222. data/spec/rails_app/config/secrets.yml +0 -22
  223. data/spec/rails_app/config/webpack/development.js +0 -5
  224. data/spec/rails_app/config/webpack/environment.js +0 -7
  225. data/spec/rails_app/config/webpack/loaders/vue.js +0 -6
  226. data/spec/rails_app/config/webpack/production.js +0 -5
  227. data/spec/rails_app/config/webpack/test.js +0 -5
  228. data/spec/rails_app/config/webpacker.yml +0 -97
  229. data/spec/rails_app/config.ru +0 -4
  230. data/spec/rails_app/db/migrate/20160716000000_create_test_tables.rb +0 -42
  231. data/spec/rails_app/db/migrate/20181209000000_create_activity_notification_tables.rb +0 -33
  232. data/spec/rails_app/db/migrate/20191201000000_add_tokens_to_users.rb +0 -10
  233. data/spec/rails_app/db/schema.rb +0 -98
  234. data/spec/rails_app/db/seeds.rb +0 -95
  235. data/spec/rails_app/lib/custom_optional_targets/console_output.rb +0 -16
  236. data/spec/rails_app/lib/custom_optional_targets/raise_error.rb +0 -14
  237. data/spec/rails_app/lib/custom_optional_targets/wrong_target.rb +0 -13
  238. data/spec/rails_app/lib/mailer_previews/mailer_preview.rb +0 -29
  239. data/spec/rails_app/package.json +0 -23
  240. data/spec/rails_app/postcss.config.js +0 -12
  241. data/spec/rails_app/public/404.html +0 -67
  242. data/spec/rails_app/public/422.html +0 -67
  243. data/spec/rails_app/public/500.html +0 -66
  244. data/spec/rails_app/public/favicon.ico +0 -0
  245. data/spec/roles/acts_as_group_spec.rb +0 -30
  246. data/spec/roles/acts_as_notifiable_spec.rb +0 -432
  247. data/spec/roles/acts_as_notifier_spec.rb +0 -30
  248. data/spec/roles/acts_as_target_spec.rb +0 -36
  249. data/spec/spec_helper.rb +0 -56
  250. data/spec/version_spec.rb +0 -31
@@ -1,1627 +0,0 @@
1
- shared_examples_for :notification_api do
2
- include ActiveJob::TestHelper
3
- let(:test_class_name) { described_class.to_s.underscore.split('/').last.to_sym }
4
- let(:test_instance) { create(test_class_name) }
5
- let(:notifiable_class) { test_instance.notifiable.class }
6
- before do
7
- ActiveJob::Base.queue_adapter = :test
8
- ActivityNotification::Mailer.deliveries.clear
9
- expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
10
- end
11
-
12
- describe "as public class methods" do
13
- before do
14
- @author_user = create(:confirmed_user)
15
- @user_1 = create(:confirmed_user)
16
- @user_2 = create(:confirmed_user)
17
- @article = create(:article, user: @author_user)
18
- @comment_1 = create(:comment, article: @article, user: @user_1)
19
- @comment_2 = create(:comment, article: @article, user: @user_2)
20
- expect(@author_user.notifications.count).to eq(0)
21
- expect(@user_1.notifications.count).to eq(0)
22
- expect(@user_2.notifications.count).to eq(0)
23
- end
24
-
25
- describe ".notify" do
26
- it "returns array of created notifications" do
27
- notifications = described_class.notify(:users, @comment_2)
28
- expect(notifications).to be_a Array
29
- expect(notifications.size).to eq(2)
30
- if notifications[0].target == @author_user
31
- validate_expected_notification(notifications[0], @author_user, @comment_2)
32
- validate_expected_notification(notifications[1], @user_1, @comment_2)
33
- else
34
- validate_expected_notification(notifications[0], @user_1, @comment_2)
35
- validate_expected_notification(notifications[1], @author_user, @comment_2)
36
- end
37
- end
38
-
39
- it "creates notification records" do
40
- described_class.notify(:users, @comment_2)
41
- expect(@author_user.notifications.unopened_only.count).to eq(1)
42
- expect(@user_1.notifications.unopened_only.count).to eq(1)
43
- expect(@user_2.notifications.unopened_only.count).to eq(0)
44
- end
45
-
46
- context "as default" do
47
- it "sends notification email later" do
48
- expect {
49
- perform_enqueued_jobs do
50
- described_class.notify(:users, @comment_2)
51
- end
52
- }.to change { ActivityNotification::Mailer.deliveries.size }.by(2)
53
- expect(ActivityNotification::Mailer.deliveries.size).to eq(2)
54
- expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@author_user.email)
55
- expect(ActivityNotification::Mailer.deliveries.last.to[0]).to eq(@user_1.email)
56
- end
57
-
58
- it "sends notification email with active job queue" do
59
- expect {
60
- described_class.notify(:users, @comment_2)
61
- }.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(2)
62
- end
63
- end
64
-
65
- context "with notify_later true" do
66
- it "generates notifications later" do
67
- expect {
68
- described_class.notify(:users, @comment_2, notify_later: true)
69
- }.to have_enqueued_job(ActivityNotification::NotifyJob)
70
- end
71
-
72
- it "creates notification records later" do
73
- perform_enqueued_jobs do
74
- described_class.notify(:users, @comment_2, notify_later: true)
75
- end
76
- expect(@author_user.notifications.unopened_only.count).to eq(1)
77
- expect(@user_1.notifications.unopened_only.count).to eq(1)
78
- expect(@user_2.notifications.unopened_only.count).to eq(0)
79
- end
80
- end
81
-
82
- context "with send_later false" do
83
- it "sends notification email now" do
84
- described_class.notify(:users, @comment_2, send_later: false)
85
- expect(ActivityNotification::Mailer.deliveries.size).to eq(2)
86
- expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@author_user.email)
87
- expect(ActivityNotification::Mailer.deliveries.last.to[0]).to eq(@user_1.email)
88
- end
89
- end
90
-
91
- context "with pass_full_options" do
92
- before do
93
- @original_targets = Comment._notification_targets[:users]
94
- end
95
-
96
- after do
97
- Comment._notification_targets[:users] = @original_targets
98
- end
99
-
100
- context "as false (as default)" do
101
- it "accepts specified lambda with notifiable and key arguments" do
102
- Comment._notification_targets[:users] = ->(notifiable, key){ User.all if key == 'dummy_key' }
103
- described_class.notify(:users, @comment_2, key: 'dummy_key')
104
- expect(@author_user.notifications.unopened_only.count).to eq(1)
105
- end
106
-
107
- it "cannot accept specified lambda with notifiable and options arguments" do
108
- Comment._notification_targets[:users] = ->(notifiable, options){ User.all if options[:key] == 'dummy_key' }
109
- expect { described_class.notify(:users, @comment_2, key: 'dummy_key') }.to raise_error(TypeError)
110
- end
111
- end
112
-
113
- context "as true" do
114
- it "cannot accept specified lambda with notifiable and key arguments" do
115
- Comment._notification_targets[:users] = ->(notifiable, key){ User.all if key == 'dummy_key' }
116
- expect { described_class.notify(:users, @comment_2, key: 'dummy_key', pass_full_options: true) }.to raise_error(NotImplementedError)
117
- end
118
-
119
- it "accepts specified lambda with notifiable and options arguments" do
120
- Comment._notification_targets[:users] = ->(notifiable, options){ User.all if options[:key] == 'dummy_key' }
121
- described_class.notify(:users, @comment_2, key: 'dummy_key', pass_full_options: true)
122
- expect(@author_user.notifications.unopened_only.count).to eq(1)
123
- end
124
- end
125
- end
126
-
127
- context "when some optional targets raise error" do
128
- before do
129
- require 'custom_optional_targets/raise_error'
130
- @optional_target = CustomOptionalTarget::RaiseError.new
131
- @current_optional_target = Comment._optional_targets[:users]
132
- Comment.acts_as_notifiable :users, optional_targets: ->{ [@optional_target] }
133
- end
134
-
135
- after do
136
- Comment._optional_targets[:users] = @current_optional_target
137
- end
138
-
139
- context "with true as ActivityNotification.config.rescue_optional_target_errors" do
140
- it "generates notifications even if some optional targets raise error" do
141
- rescue_optional_target_errors = ActivityNotification.config.rescue_optional_target_errors
142
- ActivityNotification.config.rescue_optional_target_errors = true
143
- notifications = described_class.notify(:users, @comment_2)
144
- expect(notifications.size).to eq(2)
145
- ActivityNotification.config.rescue_optional_target_errors = rescue_optional_target_errors
146
- end
147
- end
148
-
149
- context "with false as ActivityNotification.config.rescue_optional_target_errors" do
150
- it "raises an capturable exception" do
151
- rescue_optional_target_errors = ActivityNotification.config.rescue_optional_target_errors
152
- ActivityNotification.config.rescue_optional_target_errors = false
153
- expect { described_class.notify(:users, @comment_2) }.to raise_error(RuntimeError)
154
- ActivityNotification.config.rescue_optional_target_errors = rescue_optional_target_errors
155
- end
156
- end
157
-
158
- it "allows an exception to be captured to continue" do
159
- begin
160
- notifications = described_class.notify(:users, @comment_2)
161
- expect(notifications.size).to eq(2)
162
- rescue => e
163
- next
164
- end
165
- end
166
- end
167
- end
168
-
169
- describe ".notify_later" do
170
- it "generates notifications later" do
171
- expect {
172
- described_class.notify_later(:users, @comment_2)
173
- }.to have_enqueued_job(ActivityNotification::NotifyJob)
174
- end
175
-
176
- it "creates notification records later" do
177
- perform_enqueued_jobs do
178
- described_class.notify_later(:users, @comment_2)
179
- end
180
- expect(@author_user.notifications.unopened_only.count).to eq(1)
181
- expect(@user_1.notifications.unopened_only.count).to eq(1)
182
- expect(@user_2.notifications.unopened_only.count).to eq(0)
183
- end
184
- end
185
-
186
- describe ".notify_all" do
187
- it "returns array of created notifications" do
188
- notifications = described_class.notify_all([@author_user, @user_1], @comment_2)
189
- expect(notifications).to be_a Array
190
- expect(notifications.size).to eq(2)
191
- validate_expected_notification(notifications[0], @author_user, @comment_2)
192
- validate_expected_notification(notifications[1], @user_1, @comment_2)
193
- end
194
-
195
- it "creates notification records" do
196
- described_class.notify_all([@author_user, @user_1], @comment_2)
197
- expect(@author_user.notifications.unopened_only.count).to eq(1)
198
- expect(@user_1.notifications.unopened_only.count).to eq(1)
199
- expect(@user_2.notifications.unopened_only.count).to eq(0)
200
- end
201
-
202
- context "as default" do
203
- it "sends notification email later" do
204
- expect {
205
- perform_enqueued_jobs do
206
- described_class.notify_all([@author_user, @user_1], @comment_2)
207
- end
208
- }.to change { ActivityNotification::Mailer.deliveries.size }.by(2)
209
- expect(ActivityNotification::Mailer.deliveries.size).to eq(2)
210
- expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@author_user.email)
211
- expect(ActivityNotification::Mailer.deliveries.last.to[0]).to eq(@user_1.email)
212
- end
213
-
214
- it "sends notification email with active job queue" do
215
- expect {
216
- described_class.notify_all([@author_user, @user_1], @comment_2)
217
- }.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(2)
218
- end
219
- end
220
-
221
- context "with notify_later true" do
222
- it "generates notifications later" do
223
- expect {
224
- described_class.notify_all([@author_user, @user_1], @comment_2, notify_later: true)
225
- }.to have_enqueued_job(ActivityNotification::NotifyAllJob)
226
- end
227
-
228
- it "creates notification records later" do
229
- perform_enqueued_jobs do
230
- described_class.notify_all([@author_user, @user_1], @comment_2, notify_later: true)
231
- end
232
- expect(@author_user.notifications.unopened_only.count).to eq(1)
233
- expect(@user_1.notifications.unopened_only.count).to eq(1)
234
- expect(@user_2.notifications.unopened_only.count).to eq(0)
235
- end
236
- end
237
-
238
- context "with send_later false" do
239
- it "sends notification email now" do
240
- described_class.notify_all([@author_user, @user_1], @comment_2, send_later: false)
241
- expect(ActivityNotification::Mailer.deliveries.size).to eq(2)
242
- expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@author_user.email)
243
- expect(ActivityNotification::Mailer.deliveries.last.to[0]).to eq(@user_1.email)
244
- end
245
- end
246
- end
247
-
248
- describe ".notify_all_later" do
249
- it "generates notifications later" do
250
- expect {
251
- described_class.notify_all_later([@author_user, @user_1], @comment_2)
252
- }.to have_enqueued_job(ActivityNotification::NotifyAllJob)
253
- end
254
-
255
- it "creates notification records later" do
256
- perform_enqueued_jobs do
257
- described_class.notify_all_later([@author_user, @user_1], @comment_2)
258
- end
259
- expect(@author_user.notifications.unopened_only.count).to eq(1)
260
- expect(@user_1.notifications.unopened_only.count).to eq(1)
261
- expect(@user_2.notifications.unopened_only.count).to eq(0)
262
- end
263
- end
264
-
265
- describe ".notify_to" do
266
- it "returns created notification" do
267
- notification = described_class.notify_to(@user_1, @comment_2)
268
- validate_expected_notification(notification, @user_1, @comment_2)
269
- end
270
-
271
- it "creates notification records" do
272
- described_class.notify_to(@user_1, @comment_2)
273
- expect(@user_1.notifications.unopened_only.count).to eq(1)
274
- expect(@user_2.notifications.unopened_only.count).to eq(0)
275
- end
276
-
277
- context "as default" do
278
- it "sends notification email later" do
279
- expect {
280
- perform_enqueued_jobs do
281
- described_class.notify_to(@user_1, @comment_2)
282
- end
283
- }.to change { ActivityNotification::Mailer.deliveries.size }.by(1)
284
- expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
285
- expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@user_1.email)
286
- end
287
-
288
- it "sends notification email with active job queue" do
289
- expect {
290
- described_class.notify_to(@user_1, @comment_2)
291
- }.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(1)
292
- end
293
- end
294
-
295
- context "with notify_later true" do
296
- it "generates notifications later" do
297
- expect {
298
- described_class.notify_to(@user_1, @comment_2, notify_later: true)
299
- }.to have_enqueued_job(ActivityNotification::NotifyToJob)
300
- end
301
-
302
- it "creates notification records later" do
303
- perform_enqueued_jobs do
304
- described_class.notify_to(@user_1, @comment_2, notify_later: true)
305
- end
306
- expect(@user_1.notifications.unopened_only.count).to eq(1)
307
- expect(@user_2.notifications.unopened_only.count).to eq(0)
308
- end
309
- end
310
-
311
- context "with send_later false" do
312
- it "sends notification email now" do
313
- described_class.notify_to(@user_1, @comment_2, send_later: false)
314
- expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
315
- expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@user_1.email)
316
- end
317
- end
318
-
319
- context "with options" do
320
- context "as default" do
321
- let(:created_notification) {
322
- described_class.notify_to(@user_1, @comment_2)
323
- @user_1.notifications.latest
324
- }
325
-
326
- it "has key of notifiable.default_notification_key" do
327
- expect(created_notification.key)
328
- .to eq(created_notification.notifiable.default_notification_key)
329
- end
330
-
331
- it "has group of notifiable.notification_group" do
332
- expect(created_notification.group)
333
- .to eq(
334
- created_notification.notifiable.notification_group(
335
- @user_1.class,
336
- created_notification.key
337
- )
338
- )
339
- end
340
-
341
- it "has notifier of notifiable.notifier" do
342
- expect(created_notification.notifier)
343
- .to eq(
344
- created_notification.notifiable.notifier(
345
- @user_1.class,
346
- created_notification.key
347
- )
348
- )
349
- end
350
-
351
- it "has parameters of notifiable.notification_parameters" do
352
- expect(created_notification.parameters.stringify_keys)
353
- .to eq(
354
- created_notification.notifiable.notification_parameters(
355
- @user_1.class,
356
- created_notification.key
357
- )
358
- )
359
- end
360
- end
361
-
362
- context "as specified default value" do
363
- let(:created_notification) {
364
- described_class.notify_to(@user_1, @comment_2)
365
- }
366
-
367
- it "has key of [notifiable_class_name].default" do
368
- expect(created_notification.key).to eq('comment.default')
369
- end
370
-
371
- it "has group of group in acts_as_notifiable" do
372
- expect(created_notification.group).to eq(@article)
373
- end
374
-
375
- it "has notifier of notifier in acts_as_notifiable" do
376
- expect(created_notification.notifier).to eq(@user_2)
377
- end
378
-
379
- it "has parameters of parameters in acts_as_notifiable" do
380
- expect(created_notification.parameters).to eq({'test_default_param' => '1'})
381
- end
382
- end
383
-
384
- context "as api options" do
385
- let(:created_notification) {
386
- described_class.notify_to(
387
- @user_1, @comment_2,
388
- key: 'custom_test_key',
389
- group: @comment_2,
390
- notifier: @author_user,
391
- parameters: {custom_param_1: '1'},
392
- custom_param_2: '2'
393
- )
394
- }
395
-
396
- it "has key of key option" do
397
- expect(created_notification.key).to eq('custom_test_key')
398
- end
399
-
400
- it "has group of group option" do
401
- expect(created_notification.group).to eq(@comment_2)
402
- end
403
-
404
- it "has notifier of notifier option" do
405
- expect(created_notification.notifier).to eq(@author_user)
406
- end
407
-
408
- it "has parameters of parameters option" do
409
- expect(created_notification.parameters[:custom_param_1]).to eq('1')
410
- end
411
-
412
- it "has parameters from custom options" do
413
- expect(created_notification.parameters[:custom_param_2]).to eq('2')
414
- end
415
- end
416
- end
417
-
418
- context "with grouping" do
419
- it "creates group by specified group and the target" do
420
- owner_notification = described_class.notify_to(@user_1, @comment_1, group: @article)
421
- member_notification = described_class.notify_to(@user_1, @comment_2, group: @article)
422
- expect(member_notification.group_owner).to eq(owner_notification)
423
- end
424
-
425
- it "belongs to single group" do
426
- owner_notification = described_class.notify_to(@user_1, @comment_1, group: @article)
427
- member_notification_1 = described_class.notify_to(@user_1, @comment_2, group: @article)
428
- member_notification_2 = described_class.notify_to(@user_1, @comment_2, group: @article)
429
- expect(member_notification_1.group_owner).to eq(owner_notification)
430
- expect(member_notification_2.group_owner).to eq(owner_notification)
431
- end
432
-
433
- it "does not create group with opened notifications" do
434
- owner_notification = described_class.notify_to(@user_1, @comment_1, group: @article)
435
- owner_notification.open!
436
- member_notification = described_class.notify_to(@user_1, @comment_2, group: @article)
437
- expect(member_notification.group_owner).to eq(nil)
438
- end
439
-
440
- it "does not create group with different target" do
441
- owner_notification = described_class.notify_to(@user_1, @comment_1, group: @article)
442
- member_notification = described_class.notify_to(@user_2, @comment_2, group: @article)
443
- expect(member_notification.group_owner).to eq(nil)
444
- end
445
-
446
- it "does not create group with different group" do
447
- owner_notification = described_class.notify_to(@user_1, @comment_1, group: @article)
448
- member_notification = described_class.notify_to(@user_1, @comment_2, group: @comment_2)
449
- expect(member_notification.group_owner).to eq(nil)
450
- end
451
-
452
- it "does not create group with different notifiable type" do
453
- owner_notification = described_class.notify_to(@user_1, @comment_1, group: @article)
454
- member_notification = described_class.notify_to(@user_1, @article, group: @article)
455
- expect(member_notification.group_owner).to eq(nil)
456
- end
457
-
458
- it "does not create group with different key" do
459
- owner_notification = described_class.notify_to(@user_1, @comment_1, key: 'key1', group: @article)
460
- member_notification = described_class.notify_to(@user_1, @comment_2, key: 'key2', group: @article)
461
- expect(member_notification.group_owner).to eq(nil)
462
- end
463
-
464
- context "with group_expiry_delay option" do
465
- context "within the group expiry period" do
466
- it "belongs to single group" do
467
- owner_notification = described_class.notify_to(@user_1, @comment_1, group: @article, group_expiry_delay: 1.day)
468
- member_notification_1 = described_class.notify_to(@user_1, @comment_2, group: @article, group_expiry_delay: 1.day)
469
- member_notification_2 = described_class.notify_to(@user_1, @comment_2, group: @article, group_expiry_delay: 1.day)
470
- expect(member_notification_1.group_owner).to eq(owner_notification)
471
- expect(member_notification_2.group_owner).to eq(owner_notification)
472
- end
473
- end
474
-
475
- context "out of the group expiry period" do
476
- it "does not belong to single group" do
477
- Timecop.travel(90.seconds.ago)
478
- owner_notification = described_class.notify_to(@user_1, @comment_1, group: @article, group_expiry_delay: 1.minute)
479
- member_notification_1 = described_class.notify_to(@user_1, @comment_2, group: @article, group_expiry_delay: 1.minute)
480
- Timecop.return
481
- member_notification_2 = described_class.notify_to(@user_1, @comment_2, group: @article, group_expiry_delay: 1.minute)
482
- expect(member_notification_1.group_owner).to eq(owner_notification)
483
- expect(member_notification_2.group_owner).to be_nil
484
- end
485
- end
486
- end
487
- end
488
- end
489
-
490
- describe ".notify_later_to" do
491
- it "generates notifications later" do
492
- expect {
493
- described_class.notify_later_to(@user_1, @comment_2)
494
- }.to have_enqueued_job(ActivityNotification::NotifyToJob)
495
- end
496
-
497
- it "creates notification records later" do
498
- perform_enqueued_jobs do
499
- described_class.notify_later_to(@user_1, @comment_2)
500
- end
501
- expect(@user_1.notifications.unopened_only.count).to eq(1)
502
- expect(@user_2.notifications.unopened_only.count).to eq(0)
503
- end
504
- end
505
-
506
- describe ".open_all_of" do
507
- before do
508
- described_class.notify_to(@user_1, @article, group: @article, key: 'key.1')
509
- sleep(0.01)
510
- described_class.notify_to(@user_1, @comment_2, group: @comment_2, key: 'key.2')
511
- expect(@user_1.notifications.unopened_only.count).to eq(2)
512
- expect(@user_1.notifications.opened_only!.count).to eq(0)
513
- end
514
-
515
- it "returns array of opened notification records" do
516
- expect(described_class.open_all_of(@user_1).size).to eq(2)
517
- end
518
-
519
- it "opens all notifications of the target" do
520
- described_class.open_all_of(@user_1)
521
- expect(@user_1.notifications.unopened_only.count).to eq(0)
522
- expect(@user_1.notifications.opened_only!.count).to eq(2)
523
- end
524
-
525
- it "does not open any notifications of the other targets" do
526
- described_class.open_all_of(@user_2)
527
- expect(@user_1.notifications.unopened_only.count).to eq(2)
528
- expect(@user_1.notifications.opened_only!.count).to eq(0)
529
- end
530
-
531
- it "opens all notification with current time" do
532
- expect(@user_1.notifications.first.opened_at).to be_nil
533
- Timecop.freeze(Time.current)
534
- described_class.open_all_of(@user_1)
535
- expect(@user_1.notifications.first.opened_at.to_i).to eq(Time.current.to_i)
536
- Timecop.return
537
- end
538
-
539
- context "with opened_at option" do
540
- it "opens all notification with specified time" do
541
- expect(@user_1.notifications.first.opened_at).to be_nil
542
- opened_at = Time.current - 1.months
543
- described_class.open_all_of(@user_1, opened_at: opened_at)
544
- expect(@user_1.notifications.first.opened_at.to_i).to eq(opened_at.to_i)
545
- end
546
- end
547
-
548
- context 'with filtered_by_type options' do
549
- it "opens filtered notifications only" do
550
- described_class.open_all_of(@user_1, { filtered_by_type: @comment_2.to_class_name })
551
- expect(@user_1.notifications.unopened_only.count).to eq(1)
552
- expect(@user_1.notifications.opened_only!.count).to eq(1)
553
- end
554
- end
555
-
556
- context 'with filtered_by_group options' do
557
- it "opens filtered notifications only" do
558
- described_class.open_all_of(@user_1, { filtered_by_group: @comment_2 })
559
- expect(@user_1.notifications.unopened_only.count).to eq(1)
560
- expect(@user_1.notifications.opened_only!.count).to eq(1)
561
- end
562
- end
563
-
564
- context 'with filtered_by_group_type and :filtered_by_group_id options' do
565
- it "opens filtered notifications only" do
566
- described_class.open_all_of(@user_1, { filtered_by_group_type: 'Comment', filtered_by_group_id: @comment_2.id.to_s })
567
- expect(@user_1.notifications.unopened_only.count).to eq(1)
568
- expect(@user_1.notifications.opened_only!.count).to eq(1)
569
- end
570
- end
571
-
572
- context 'with filtered_by_key options' do
573
- it "opens filtered notifications only" do
574
- described_class.open_all_of(@user_1, { filtered_by_key: 'key.2' })
575
- expect(@user_1.notifications.unopened_only.count).to eq(1)
576
- expect(@user_1.notifications.opened_only!.count).to eq(1)
577
- end
578
- end
579
-
580
- context 'with later_than options' do
581
- it "opens filtered notifications only" do
582
- described_class.open_all_of(@user_1, { later_than: (@user_1.notifications.earliest.created_at.in_time_zone + 0.001).iso8601(3) })
583
- expect(@user_1.notifications.unopened_only.count).to eq(1)
584
- expect(@user_1.notifications.opened_only!.count).to eq(1)
585
- end
586
- end
587
-
588
- context 'with earlier_than options' do
589
- it "opens filtered notifications only" do
590
- described_class.open_all_of(@user_1, { earlier_than: @user_1.notifications.latest.created_at.iso8601(3) })
591
- expect(@user_1.notifications.unopened_only.count).to eq(1)
592
- expect(@user_1.notifications.opened_only!.count).to eq(1)
593
- end
594
- end
595
-
596
- context 'with ids options' do
597
- it "opens notifications with specified IDs only" do
598
- notification_to_open = @user_1.notifications.first
599
- described_class.open_all_of(@user_1, { ids: [notification_to_open.id] })
600
- expect(@user_1.notifications.unopened_only.count).to eq(1)
601
- expect(@user_1.notifications.opened_only!.count).to eq(1)
602
- expect(@user_1.notifications.opened_only!.first).to eq(notification_to_open)
603
- end
604
-
605
- it "applies other filter options when ids are specified" do
606
- notification_to_open = @user_1.notifications.first
607
- described_class.open_all_of(@user_1, {
608
- ids: [notification_to_open.id],
609
- filtered_by_key: 'non_existent_key'
610
- })
611
- expect(@user_1.notifications.unopened_only.count).to eq(2)
612
- expect(@user_1.notifications.opened_only!.count).to eq(0)
613
- end
614
-
615
- it "only opens unopened notifications even when opened notification IDs are provided" do
616
- # First open one notification
617
- notification_to_open = @user_1.notifications.first
618
- notification_to_open.open!
619
-
620
- # Try to open it again using ids parameter
621
- described_class.open_all_of(@user_1, { ids: [notification_to_open.id] })
622
-
623
- # Should not affect the count since it was already opened
624
- expect(@user_1.notifications.unopened_only.count).to eq(1)
625
- expect(@user_1.notifications.opened_only!.count).to eq(1)
626
- end
627
- end
628
- end
629
-
630
- describe ".destroy_all_of" do
631
- before do
632
- described_class.notify_to(@user_1, @article, group: @article, key: 'key.1')
633
- described_class.notify_to(@user_1, @comment_2, group: @comment_2, key: 'key.2')
634
- expect(@user_1.notifications.count).to eq(2)
635
- expect(@user_2.notifications.count).to eq(0)
636
- end
637
-
638
- it "returns array of destroyed notification records" do
639
- destroyed_notifications = described_class.destroy_all_of(@user_1)
640
- expect(destroyed_notifications).to be_a Array
641
- expect(destroyed_notifications.size).to eq(2)
642
- end
643
-
644
- it "destroys all notifications of the target" do
645
- described_class.destroy_all_of(@user_1)
646
- expect(@user_1.notifications.count).to eq(0)
647
- end
648
-
649
- it "does not destroy any notifications of the other targets" do
650
- described_class.destroy_all_of(@user_2)
651
- expect(@user_1.notifications.count).to eq(2)
652
- expect(@user_2.notifications.count).to eq(0)
653
- end
654
-
655
- context 'with filtered_by_type options' do
656
- it "destroys filtered notifications only" do
657
- described_class.destroy_all_of(@user_1, { filtered_by_type: @comment_2.to_class_name })
658
- expect(@user_1.notifications.count).to eq(1)
659
- expect(@user_1.notifications.first.notifiable).to eq(@article)
660
- end
661
- end
662
-
663
- context 'with filtered_by_group options' do
664
- it "destroys filtered notifications only" do
665
- described_class.destroy_all_of(@user_1, { filtered_by_group: @comment_2 })
666
- expect(@user_1.notifications.count).to eq(1)
667
- expect(@user_1.notifications.first.notifiable).to eq(@article)
668
- end
669
- end
670
-
671
- context 'with filtered_by_group_type and :filtered_by_group_id options' do
672
- it "destroys filtered notifications only" do
673
- described_class.destroy_all_of(@user_1, { filtered_by_group_type: 'Comment', filtered_by_group_id: @comment_2.id.to_s })
674
- expect(@user_1.notifications.count).to eq(1)
675
- expect(@user_1.notifications.first.notifiable).to eq(@article)
676
- end
677
- end
678
-
679
- context 'with filtered_by_key options' do
680
- it "destroys filtered notifications only" do
681
- described_class.destroy_all_of(@user_1, { filtered_by_key: 'key.2' })
682
- expect(@user_1.notifications.count).to eq(1)
683
- expect(@user_1.notifications.first.notifiable).to eq(@article)
684
- end
685
- end
686
-
687
- context 'with later_than options' do
688
- it "destroys filtered notifications only" do
689
- described_class.destroy_all_of(@user_1, { later_than: (@user_1.notifications.earliest.created_at.in_time_zone + 0.001).iso8601(3) })
690
- expect(@user_1.notifications.count).to eq(1)
691
- expect(@user_1.notifications.first).to eq(@user_1.notifications.earliest)
692
- end
693
- end
694
-
695
- context 'with earlier_than options' do
696
- it "destroys filtered notifications only" do
697
- described_class.destroy_all_of(@user_1, { earlier_than: @user_1.notifications.latest.created_at.iso8601(3) })
698
- expect(@user_1.notifications.count).to eq(1)
699
- expect(@user_1.notifications.first).to eq(@user_1.notifications.latest)
700
- end
701
- end
702
-
703
- context 'with ids options' do
704
- it "destroys notifications with specified IDs only" do
705
- notification_to_destroy = @user_1.notifications.first
706
- described_class.destroy_all_of(@user_1, { ids: [notification_to_destroy.id] })
707
- expect(@user_1.notifications.count).to eq(1)
708
- expect(@user_1.notifications.first).not_to eq(notification_to_destroy)
709
- end
710
-
711
- it "applies other filter options when ids are specified" do
712
- notification_to_destroy = @user_1.notifications.first
713
- described_class.destroy_all_of(@user_1, {
714
- ids: [notification_to_destroy.id],
715
- filtered_by_key: 'non_existent_key'
716
- })
717
- expect(@user_1.notifications.count).to eq(2)
718
- end
719
- end
720
- end
721
-
722
- describe ".group_member_exists?" do
723
- context "when specified notifications have any group members" do
724
- let(:owner_notifications) do
725
- target = create(:confirmed_user)
726
- group_owner = create(:notification, target: target, group_owner: nil)
727
- create(:notification, target: target, group_owner: nil)
728
- group_member = create(:notification, target: target, group_owner: group_owner)
729
- target.notifications.group_owners_only
730
- end
731
-
732
- it "returns true for DB query" do
733
- expect(described_class.group_member_exists?(owner_notifications))
734
- .to be_truthy
735
- end
736
-
737
- it "returns true for Array" do
738
- expect(described_class.group_member_exists?(owner_notifications.to_a))
739
- .to be_truthy
740
- end
741
- end
742
-
743
- context "when specified notifications have no group members" do
744
- let(:owner_notifications) do
745
- target = create(:confirmed_user)
746
- group_owner = create(:notification, target: target, group_owner: nil)
747
- create(:notification, target: target, group_owner: nil)
748
- target.notifications.group_owners_only
749
- end
750
-
751
- it "returns false" do
752
- expect(described_class.group_member_exists?(owner_notifications))
753
- .to be_falsey
754
- end
755
- end
756
- end
757
-
758
- describe ".send_batch_notification_email" do
759
- context "as default" do
760
- it "sends batch notification email later" do
761
- expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
762
- expect {
763
- perform_enqueued_jobs do
764
- described_class.send_batch_notification_email(test_instance.target, [test_instance])
765
- end
766
- }.to change { ActivityNotification::Mailer.deliveries.size }.by(1)
767
- expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
768
- expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(test_instance.target.email)
769
- end
770
-
771
- it "sends batch notification email with active job queue" do
772
- expect {
773
- described_class.send_batch_notification_email(test_instance.target, [test_instance])
774
- }.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(1)
775
- end
776
- end
777
-
778
- context "with send_later false" do
779
- it "sends notification email now" do
780
- expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
781
- described_class.send_batch_notification_email(test_instance.target, [test_instance], send_later: false)
782
- expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
783
- expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(test_instance.target.email)
784
- end
785
- end
786
- end
787
-
788
- describe ".available_options" do
789
- it "returns list of available options in notify api" do
790
- expect(described_class.available_options)
791
- .to eq([:key, :group, :group_expiry_delay, :notifier, :parameters, :send_email, :send_later, :pass_full_options])
792
- end
793
- end
794
- end
795
-
796
- describe "as private class methods" do
797
- describe ".store_notification" do
798
- it "is defined as private method" do
799
- expect(described_class.respond_to?(:store_notification)).to be_falsey
800
- expect(described_class.respond_to?(:store_notification, true)).to be_truthy
801
- end
802
- end
803
- end
804
-
805
- describe "as public instance methods" do
806
- describe "#send_notification_email" do
807
- context "as default" do
808
- it "sends notification email later" do
809
- expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
810
- expect {
811
- perform_enqueued_jobs do
812
- test_instance.send_notification_email
813
- end
814
- }.to change { ActivityNotification::Mailer.deliveries.size }.by(1)
815
- expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
816
- expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(test_instance.target.email)
817
- end
818
-
819
- it "sends notification email with active job queue" do
820
- expect {
821
- test_instance.send_notification_email
822
- }.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(1)
823
- end
824
- end
825
-
826
- context "with send_later false" do
827
- it "sends notification email now" do
828
- expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
829
- test_instance.send_notification_email send_later: false
830
- expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
831
- expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(test_instance.target.email)
832
- end
833
- end
834
- end
835
-
836
- describe "#publish_to_optional_targets" do
837
- before do
838
- require 'custom_optional_targets/console_output'
839
- @optional_target = CustomOptionalTarget::ConsoleOutput.new(console_out: false)
840
- notifiable_class.acts_as_notifiable test_instance.target.to_resources_name.to_sym, optional_targets: ->{ [@optional_target] }
841
- expect(test_instance.notifiable.optional_targets(test_instance.target.to_resources_name, test_instance.key)).to eq([@optional_target])
842
- end
843
-
844
- context "subscribed by target" do
845
- before do
846
- test_instance.target.create_subscription(key: test_instance.key, optional_targets: { subscribing_to_console_output: true })
847
- expect(test_instance.optional_target_subscribed?(:console_output)).to be_truthy
848
- end
849
-
850
- it "calls OptionalTarget#notify" do
851
- expect(@optional_target).to receive(:notify)
852
- test_instance.publish_to_optional_targets
853
- end
854
-
855
- it "returns truthy result hash" do
856
- expect(test_instance.publish_to_optional_targets).to eq({ console_output: true })
857
- end
858
- end
859
-
860
- context "unsubscribed by target" do
861
- before do
862
- test_instance.target.create_subscription(key: test_instance.key, optional_targets: { subscribing_to_console_output: false })
863
- expect(test_instance.optional_target_subscribed?(:console_output)).to be_falsey
864
- end
865
-
866
- it "does not call OptionalTarget#notify" do
867
- expect(@optional_target).not_to receive(:notify)
868
- test_instance.publish_to_optional_targets
869
- end
870
-
871
- it "returns truthy result hash" do
872
- expect(test_instance.publish_to_optional_targets).to eq({ console_output: false })
873
- end
874
- end
875
- end
876
-
877
- describe "#open!" do
878
- it "returns the number of opened notification records" do
879
- expect(test_instance.open!).to eq(1)
880
- end
881
-
882
- it "returns the number of opened notification records including group members" do
883
- group_member = create(test_class_name, group_owner: test_instance)
884
- expect(group_member.opened_at.blank?).to be_truthy
885
- expect(test_instance.open!).to eq(2)
886
- end
887
-
888
- context "as default" do
889
- it "open notification with current time" do
890
- expect(test_instance.opened_at.blank?).to be_truthy
891
- Timecop.freeze(Time.at(Time.now.to_i))
892
- test_instance.open!
893
- expect(test_instance.opened_at.blank?).to be_falsey
894
- expect(test_instance.opened_at).to eq(Time.current)
895
- Timecop.return
896
- end
897
-
898
- it "open group member notifications with current time" do
899
- group_member = create(test_class_name, group_owner: test_instance)
900
- expect(group_member.opened_at.blank?).to be_truthy
901
- Timecop.freeze(Time.at(Time.now.to_i))
902
- test_instance.open!
903
- group_member = group_member.reload
904
- expect(group_member.opened_at.blank?).to be_falsey
905
- expect(group_member.opened_at.to_i).to eq(Time.current.to_i)
906
- Timecop.return
907
- end
908
- end
909
-
910
- context "with opened_at option" do
911
- it "open notification with specified time" do
912
- expect(test_instance.opened_at.blank?).to be_truthy
913
- opened_at = Time.current - 1.months
914
- test_instance.open!(opened_at: opened_at)
915
- expect(test_instance.opened_at.blank?).to be_falsey
916
- expect(test_instance.opened_at.to_i).to eq(opened_at.to_i)
917
- end
918
-
919
- it "open group member notifications with specified time" do
920
- group_member = create(test_class_name, group_owner: test_instance)
921
- expect(group_member.opened_at.blank?).to be_truthy
922
- opened_at = Time.current - 1.months
923
- test_instance.open!(opened_at: opened_at)
924
- group_member = group_member.reload
925
- expect(group_member.opened_at.blank?).to be_falsey
926
- expect(group_member.opened_at.to_i).to eq(opened_at.to_i)
927
- end
928
- end
929
-
930
- context "with false as with_members" do
931
- it "does not open group member notifications" do
932
- group_member = create(test_class_name, group_owner: test_instance)
933
- expect(group_member.opened_at.blank?).to be_truthy
934
- opened_at = Time.current - 1.months
935
- test_instance.open!(with_members: false)
936
- group_member = group_member.reload
937
- expect(group_member.opened_at.blank?).to be_truthy
938
- end
939
-
940
- it "returns the number of opened notification records" do
941
- create(test_class_name, group_owner: test_instance, opened_at: nil)
942
- expect(test_instance.open!(with_members: false)).to eq(1)
943
- end
944
- end
945
-
946
- context "when the associated notifiable record has been deleted" do
947
- let(:notifiable_id) { test_instance.notifiable.id }
948
-
949
- before do
950
- notifiable_class.where(id: notifiable_id).delete_all
951
- test_instance.reload
952
- end
953
-
954
- it "ensures the notifiable is gone and the notification is still persisted" do
955
- expect(notifiable_class.exists?(notifiable_id)).to be_falsey
956
- expect(test_instance).to be_persisted
957
- end
958
-
959
- if ActivityNotification.config.orm == :active_record
960
- it "does not open the notification without skip_validation option when using ActiveRecord" do
961
- test_instance.open!
962
- expect(test_instance.reload.opened?).to be_falsey
963
- end
964
- else
965
- it "opens the notification without skip_validation option when using Mongoid or Dynamoid" do
966
- test_instance.open!
967
- expect(test_instance.reload.opened?).to be_truthy
968
- end
969
- end
970
-
971
- it "opens the notification when skip_validation is true" do
972
- test_instance.open!(skip_validation: true)
973
- expect(test_instance.reload.opened?).to be_truthy
974
- end
975
- end
976
- end
977
-
978
- describe "#unopened?" do
979
- context "when opened_at is blank" do
980
- it "returns true" do
981
- expect(test_instance.unopened?).to be_truthy
982
- end
983
- end
984
-
985
- context "when opened_at is present" do
986
- it "returns false" do
987
- test_instance.open!
988
- expect(test_instance.unopened?).to be_falsey
989
- end
990
- end
991
- end
992
-
993
- describe "#opened?" do
994
- context "when opened_at is blank" do
995
- it "returns false" do
996
- expect(test_instance.opened?).to be_falsey
997
- end
998
- end
999
-
1000
- context "when opened_at is present" do
1001
- it "returns true" do
1002
- test_instance.open!
1003
- expect(test_instance.opened?).to be_truthy
1004
- end
1005
- end
1006
- end
1007
-
1008
- describe "#group_owner?" do
1009
- context "when the notification is group owner" do
1010
- it "returns true" do
1011
- expect(test_instance.group_owner?).to be_truthy
1012
- end
1013
- end
1014
-
1015
- context "when the notification belongs to group" do
1016
- it "returns false" do
1017
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
1018
- expect(group_member.group_owner?).to be_falsey
1019
- end
1020
- end
1021
- end
1022
-
1023
- describe "#group_member?" do
1024
- context "when the notification is group owner" do
1025
- it "returns false" do
1026
- expect(test_instance.group_member?).to be_falsey
1027
- end
1028
- end
1029
-
1030
- context "when the notification belongs to group" do
1031
- it "returns true" do
1032
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
1033
- expect(group_member.group_member?).to be_truthy
1034
- end
1035
- end
1036
- end
1037
-
1038
- describe "#group_member_exists?" do
1039
- context "when the notification is group owner and has no group members" do
1040
- it "returns false" do
1041
- expect(test_instance.group_member_exists?).to be_falsey
1042
- end
1043
- end
1044
-
1045
- context "when the notification is group owner and has group members" do
1046
- it "returns true" do
1047
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1048
- expect(test_instance.group_member_exists?).to be_truthy
1049
- end
1050
- end
1051
-
1052
- context "when the notification belongs to group" do
1053
- it "returns true" do
1054
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
1055
- expect(group_member.group_member_exists?).to be_truthy
1056
- end
1057
- end
1058
- end
1059
-
1060
- # Returns if group member notifier except group owner notifier exists.
1061
- # It always returns false if group owner notifier is blank.
1062
- # It counts only the member notifier of the same type with group owner notifier.
1063
- describe "#group_member_notifier_exists?" do
1064
- context "with notifier" do
1065
- before do
1066
- test_instance.update(notifier: create(:user))
1067
- end
1068
-
1069
- context "when the notification is group owner and has no group members" do
1070
- it "returns false" do
1071
- expect(test_instance.group_member_notifier_exists?).to be_falsey
1072
- end
1073
- end
1074
-
1075
- context "when the notification is group owner and has group members with the same notifier with the owner's" do
1076
- it "returns false" do
1077
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1078
- expect(test_instance.group_member_notifier_exists?).to be_falsey
1079
- end
1080
- end
1081
-
1082
- context "when the notification is group owner and has group members with different notifier from the owner's" do
1083
- it "returns true" do
1084
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1085
- expect(test_instance.group_member_notifier_exists?).to be_truthy
1086
- end
1087
- end
1088
-
1089
- context "when the notification belongs to group and has group members with the same notifier with the owner's" do
1090
- it "returns false" do
1091
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1092
- expect(group_member.group_member_notifier_exists?).to be_falsey
1093
- end
1094
- end
1095
-
1096
- context "when the notification belongs to group and has group members with different notifier from the owner's" do
1097
- it "returns true" do
1098
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1099
- expect(group_member.group_member_notifier_exists?).to be_truthy
1100
- end
1101
- end
1102
- end
1103
-
1104
- context "without notifier" do
1105
- before do
1106
- test_instance.update(notifier: nil)
1107
- end
1108
-
1109
- context "when the notification is group owner and has no group members" do
1110
- it "returns false" do
1111
- expect(test_instance.group_member_notifier_exists?).to be_falsey
1112
- end
1113
- end
1114
-
1115
- context "when the notification is group owner and has group members without notifier" do
1116
- it "returns false" do
1117
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: nil)
1118
- expect(test_instance.group_member_notifier_exists?).to be_falsey
1119
- end
1120
- end
1121
-
1122
- context "when the notification is group owner and has group members with notifier" do
1123
- it "returns false" do
1124
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1125
- expect(test_instance.group_member_notifier_exists?).to be_falsey
1126
- end
1127
- end
1128
-
1129
- context "when the notification belongs to group and has group members without notifier" do
1130
- it "returns false" do
1131
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: nil)
1132
- expect(group_member.group_member_notifier_exists?).to be_falsey
1133
- end
1134
- end
1135
-
1136
- context "when the notification belongs to group and has group members with notifier" do
1137
- it "returns false" do
1138
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1139
- expect(group_member.group_member_notifier_exists?).to be_falsey
1140
- end
1141
- end
1142
- end
1143
- end
1144
-
1145
- describe "#group_member_count (with #group_notification_count)" do
1146
- context "for unopened notification" do
1147
- context "when the notification is group owner and has no group members" do
1148
- it "returns 0" do
1149
- expect(test_instance.group_member_count).to eq(0)
1150
- expect(test_instance.group_notification_count).to eq(1)
1151
- end
1152
- end
1153
-
1154
- context "when the notification is group owner and has group members" do
1155
- it "returns member count" do
1156
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1157
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1158
- expect(test_instance.group_member_count).to eq(2)
1159
- expect(test_instance.group_notification_count).to eq(3)
1160
- end
1161
- end
1162
-
1163
- context "when the notification belongs to group" do
1164
- it "returns member count" do
1165
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
1166
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1167
- expect(group_member.group_member_count).to eq(2)
1168
- expect(group_member.group_notification_count).to eq(3)
1169
- end
1170
- end
1171
- end
1172
-
1173
- context "for opened notification" do
1174
- context "when the notification is group owner and has no group members" do
1175
- it "returns 0" do
1176
- test_instance.open!
1177
- expect(test_instance.group_member_count).to eq(0)
1178
- expect(test_instance.group_notification_count).to eq(1)
1179
- end
1180
- end
1181
-
1182
- context "as default" do
1183
- context "when the notification is group owner and has group members" do
1184
- it "returns member count" do
1185
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1186
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1187
- test_instance.open!
1188
- expect(test_instance.group_member_count).to eq(2)
1189
- expect(test_instance.group_notification_count).to eq(3)
1190
- end
1191
- end
1192
-
1193
- context "when the notification belongs to group" do
1194
- it "returns member count" do
1195
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
1196
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1197
- test_instance.open!
1198
- expect(group_member.group_member_count).to eq(2)
1199
- expect(group_member.group_notification_count).to eq(3)
1200
- end
1201
- end
1202
- end
1203
-
1204
- context "with limit" do
1205
- context "when the notification is group owner and has group members" do
1206
- it "returns member count by limit 0" do
1207
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1208
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1209
- test_instance.open!
1210
- expect(test_instance.group_member_count(0)).to eq(0)
1211
- expect(test_instance.group_notification_count(0)).to eq(1)
1212
- end
1213
-
1214
- it "returns member count by limit 1" do
1215
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1216
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1217
- test_instance.open!
1218
- expect(test_instance.group_member_count(1)).to eq(1)
1219
- expect(test_instance.group_notification_count(1)).to eq(2)
1220
- end
1221
- end
1222
-
1223
- context "when the notification belongs to group" do
1224
- it "returns member count by limit 0" do
1225
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
1226
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1227
- test_instance.open!
1228
- expect(group_member.group_member_count(0)).to eq(0)
1229
- expect(group_member.group_notification_count(0)).to eq(1)
1230
- end
1231
-
1232
- it "returns member count by limit 1" do
1233
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance)
1234
- create(test_class_name, target: test_instance.target, group_owner: test_instance)
1235
- test_instance.open!
1236
- expect(group_member.group_member_count(1)).to eq(1)
1237
- expect(group_member.group_notification_count(1)).to eq(2)
1238
- end
1239
- end
1240
- end
1241
- end
1242
- end
1243
-
1244
- # Returns count of group member notifiers of the notification not including group owner notifier.
1245
- # It always returns 0 if group owner notifier is blank.
1246
- # It counts only the member notifier of the same type with group owner notifier.
1247
- describe "#group_member_notifier_count (with #group_notifier_count)" do
1248
- context "for unopened notification" do
1249
- context "with notifier" do
1250
- before do
1251
- test_instance.update(notifier: create(:user))
1252
- end
1253
-
1254
- context "when the notification is group owner and has no group members" do
1255
- it "returns 0" do
1256
- expect(test_instance.group_member_notifier_count).to eq(0)
1257
- expect(test_instance.group_notifier_count).to eq(1)
1258
- end
1259
- end
1260
-
1261
- context "when the notification is group owner and has group members with the same notifier with the owner's" do
1262
- it "returns 0" do
1263
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1264
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1265
- expect(test_instance.group_member_notifier_count).to eq(0)
1266
- expect(test_instance.group_notifier_count).to eq(1)
1267
- end
1268
- end
1269
-
1270
- context "when the notification is group owner and has group members with different notifier from the owner's" do
1271
- it "returns member notifier count" do
1272
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1273
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1274
- expect(test_instance.group_member_notifier_count).to eq(2)
1275
- expect(test_instance.group_notifier_count).to eq(3)
1276
- end
1277
-
1278
- it "returns member notifier count with selecting distinct notifier" do
1279
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1280
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: group_member.notifier)
1281
- expect(test_instance.group_member_notifier_count).to eq(1)
1282
- expect(test_instance.group_notifier_count).to eq(2)
1283
- end
1284
- end
1285
-
1286
- context "when the notification belongs to group and has group members with the same notifier with the owner's" do
1287
- it "returns 0" do
1288
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1289
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1290
- expect(group_member.group_member_notifier_count).to eq(0)
1291
- expect(group_member.group_notifier_count).to eq(1)
1292
- end
1293
- end
1294
-
1295
- context "when the notification belongs to group and has group members with different notifier from the owner's" do
1296
- it "returns member notifier count" do
1297
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1298
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1299
- expect(group_member.group_member_notifier_count).to eq(2)
1300
- expect(group_member.group_notifier_count).to eq(3)
1301
- end
1302
-
1303
- it "returns member notifier count with selecting distinct notifier" do
1304
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1305
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: group_member.notifier)
1306
- expect(group_member.group_member_notifier_count).to eq(1)
1307
- expect(group_member.group_notifier_count).to eq(2)
1308
- end
1309
- end
1310
- end
1311
-
1312
- context "without notifier" do
1313
- before do
1314
- test_instance.update(notifier: nil)
1315
- end
1316
-
1317
- context "when the notification is group owner and has no group members" do
1318
- it "returns 0" do
1319
- expect(test_instance.group_member_notifier_count).to eq(0)
1320
- expect(test_instance.group_notifier_count).to eq(0)
1321
- end
1322
- end
1323
-
1324
- context "when the notification is group owner and has group members with the same notifier with the owner's" do
1325
- it "returns 0" do
1326
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1327
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1328
- expect(test_instance.group_member_notifier_count).to eq(0)
1329
- expect(test_instance.group_notifier_count).to eq(0)
1330
- end
1331
- end
1332
-
1333
- context "when the notification is group owner and has group members with different notifier from the owner's" do
1334
- it "returns 0" do
1335
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1336
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1337
- expect(test_instance.group_member_notifier_count).to eq(0)
1338
- expect(test_instance.group_notifier_count).to eq(0)
1339
- end
1340
- end
1341
-
1342
- context "when the notification belongs to group and has group members with the same notifier with the owner's" do
1343
- it "returns 0" do
1344
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1345
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1346
- expect(group_member.group_member_notifier_count).to eq(0)
1347
- expect(group_member.group_notifier_count).to eq(0)
1348
- end
1349
- end
1350
-
1351
- context "when the notification belongs to group and has group members with different notifier from the owner's" do
1352
- it "returns 0" do
1353
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1354
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1355
- expect(group_member.group_member_notifier_count).to eq(0)
1356
- expect(group_member.group_notifier_count).to eq(0)
1357
- end
1358
- end
1359
- end
1360
- end
1361
-
1362
- context "for opened notification" do
1363
- context "as default" do
1364
- context "with notifier" do
1365
- before do
1366
- test_instance.update(notifier: create(:user))
1367
- end
1368
-
1369
- context "when the notification is group owner and has group members with the same notifier with the owner's" do
1370
- it "returns 0" do
1371
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1372
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1373
- test_instance.open!
1374
- expect(test_instance.group_member_notifier_count).to eq(0)
1375
- end
1376
- end
1377
-
1378
- context "when the notification is group owner and has group members with different notifier from the owner's" do
1379
- it "returns member notifier count" do
1380
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1381
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1382
- test_instance.open!
1383
- expect(test_instance.group_member_notifier_count).to eq(2)
1384
- end
1385
-
1386
- it "returns member notifier count with selecting distinct notifier" do
1387
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1388
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: group_member.notifier)
1389
- test_instance.open!
1390
- expect(test_instance.group_member_notifier_count).to eq(1)
1391
- end
1392
- end
1393
-
1394
- context "when the notification belongs to group and has group members with the same notifier with the owner's" do
1395
- it "returns 0" do
1396
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1397
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1398
- test_instance.open!
1399
- expect(group_member.group_member_notifier_count).to eq(0)
1400
- end
1401
- end
1402
-
1403
- context "when the notification belongs to group and has group members with different notifier from the owner's" do
1404
- it "returns member notifier count" do
1405
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1406
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1407
- test_instance.open!
1408
- expect(group_member.group_member_notifier_count).to eq(2)
1409
- end
1410
-
1411
- it "returns member notifier count with selecting distinct notifier" do
1412
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1413
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: group_member.notifier)
1414
- test_instance.open!
1415
- expect(group_member.group_member_notifier_count).to eq(1)
1416
- end
1417
- end
1418
- end
1419
-
1420
- context "without notifier" do
1421
- before do
1422
- test_instance.update(notifier: nil)
1423
- end
1424
-
1425
- context "when the notification is group owner and has no group members" do
1426
- it "returns 0" do
1427
- test_instance.open!
1428
- expect(test_instance.group_member_notifier_count).to eq(0)
1429
- end
1430
- end
1431
-
1432
- context "when the notification is group owner and has group members with the same notifier with the owner's" do
1433
- it "returns 0" do
1434
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1435
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1436
- test_instance.open!
1437
- expect(test_instance.group_member_notifier_count).to eq(0)
1438
- end
1439
- end
1440
-
1441
- context "when the notification is group owner and has group members with different notifier from the owner's" do
1442
- it "returns 0" do
1443
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1444
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1445
- test_instance.open!
1446
- expect(test_instance.group_member_notifier_count).to eq(0)
1447
- end
1448
- end
1449
-
1450
- context "when the notification belongs to group and has group members with the same notifier with the owner's" do
1451
- it "returns 0" do
1452
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1453
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: test_instance.notifier)
1454
- test_instance.open!
1455
- expect(group_member.group_member_notifier_count).to eq(0)
1456
- end
1457
- end
1458
-
1459
- context "when the notification belongs to group and has group members with different notifier from the owner's" do
1460
- it "returns 0" do
1461
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1462
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1463
- test_instance.open!
1464
- expect(group_member.group_member_notifier_count).to eq(0)
1465
- end
1466
- end
1467
- end
1468
- end
1469
-
1470
- context "with limit" do
1471
- before do
1472
- test_instance.update(notifier: create(:user))
1473
- end
1474
-
1475
- context "when the notification is group owner and has group members with different notifier from the owner's" do
1476
- it "returns member notifier count by limit" do
1477
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1478
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1479
- test_instance.open!
1480
- expect(test_instance.group_member_notifier_count(0)).to eq(0)
1481
- end
1482
- end
1483
-
1484
- context "when the notification belongs to group and has group members with different notifier from the owner's" do
1485
- it "returns member count by limit" do
1486
- group_member = create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1487
- create(test_class_name, target: test_instance.target, group_owner: test_instance, notifier: create(:user))
1488
- test_instance.open!
1489
- expect(group_member.group_member_notifier_count(0)).to eq(0)
1490
- end
1491
- end
1492
- end
1493
- end
1494
- end
1495
-
1496
- describe "#latest_group_member" do
1497
- context "with group member" do
1498
- it "returns latest group member" do
1499
- member1 = create(test_class_name, target: test_instance.target, group_owner: test_instance)
1500
- member2 = create(test_class_name, target: test_instance.target, group_owner: test_instance, created_at: member1.created_at + 10.second)
1501
- expect(test_instance.latest_group_member.becomes(ActivityNotification::Notification)).to eq(member2)
1502
- end
1503
- end
1504
-
1505
- context "without group members" do
1506
- it "returns group owner self" do
1507
- expect(test_instance.latest_group_member).to eq(test_instance)
1508
- end
1509
- end
1510
- end
1511
-
1512
- describe "#remove_from_group" do
1513
- before do
1514
- @member1 = create(test_class_name, target: test_instance.target, group_owner: test_instance)
1515
- @member2 = create(test_class_name, target: test_instance.target, group_owner: test_instance)
1516
- expect(test_instance.group_member_count).to eq(2)
1517
- expect(@member1.group_owner?).to be_falsey
1518
- end
1519
-
1520
- it "removes from notification group" do
1521
- test_instance.remove_from_group
1522
- expect(test_instance.group_member_count).to eq(0)
1523
- end
1524
-
1525
- it "makes a new group owner" do
1526
- test_instance.remove_from_group
1527
- expect(@member1.reload.group_owner?).to be_truthy
1528
- expect(@member1.group_members.size).to eq(1)
1529
- expect(@member1.group_members.first.becomes(ActivityNotification::Notification)).to eq(@member2)
1530
- end
1531
-
1532
- it "returns new group owner instance" do
1533
- expect(test_instance.remove_from_group.becomes(ActivityNotification::Notification)).to eq(@member1)
1534
- end
1535
- end
1536
-
1537
- describe "#notifiable_path" do
1538
- it "returns notifiable.notifiable_path" do
1539
- expect(test_instance.notifiable_path)
1540
- .to eq(test_instance.notifiable.notifiable_path(test_instance.target_type, test_instance.key))
1541
- end
1542
- end
1543
-
1544
- describe "#subscribed?" do
1545
- it "returns target.subscribes_to_notification?" do
1546
- expect(test_instance.subscribed?)
1547
- .to eq(test_instance.target.subscribes_to_notification?(test_instance.key))
1548
- end
1549
- end
1550
-
1551
- describe "#email_subscribed?" do
1552
- it "returns target.subscribes_to_notification_email?" do
1553
- expect(test_instance.subscribed?)
1554
- .to eq(test_instance.target.subscribes_to_notification_email?(test_instance.key))
1555
- end
1556
- end
1557
-
1558
- describe "#optional_target_subscribed?" do
1559
- it "returns target.subscribes_to_optional_target?" do
1560
- test_instance.target.create_subscription(key: test_instance.key, optional_targets: { subscribing_to_console_output: false })
1561
- expect(test_instance.optional_target_subscribed?(:console_output)).to be_falsey
1562
- expect(test_instance.optional_target_subscribed?(:console_output))
1563
- .to eq(test_instance.target.subscribes_to_optional_target?(test_instance.key, :console_output))
1564
- end
1565
- end
1566
-
1567
- describe "#optional_targets" do
1568
- it "returns notifiable.optional_targets" do
1569
- require 'custom_optional_targets/console_output'
1570
- @optional_target = CustomOptionalTarget::ConsoleOutput.new
1571
- notifiable_class.acts_as_notifiable test_instance.target.to_resources_name.to_sym, optional_targets: ->{ [@optional_target] }
1572
- expect(test_instance.optional_targets).to eq([@optional_target])
1573
- expect(test_instance.optional_targets)
1574
- .to eq(test_instance.notifiable.optional_targets(test_instance.target.to_resources_name, test_instance.key))
1575
- end
1576
- end
1577
-
1578
- describe "#optional_target_names" do
1579
- it "returns notifiable.optional_target_names" do
1580
- require 'custom_optional_targets/console_output'
1581
- @optional_target = CustomOptionalTarget::ConsoleOutput.new
1582
- notifiable_class.acts_as_notifiable test_instance.target.to_resources_name.to_sym, optional_targets: ->{ [@optional_target] }
1583
- expect(test_instance.optional_target_names).to eq([:console_output])
1584
- expect(test_instance.optional_target_names)
1585
- .to eq(test_instance.notifiable.optional_target_names(test_instance.target.to_resources_name, test_instance.key))
1586
- end
1587
- end
1588
- end
1589
-
1590
- describe "as protected instance methods" do
1591
- describe "#unopened_group_member_count" do
1592
- it "is defined as protected method" do
1593
- expect(test_instance.respond_to?(:unopened_group_member_count)).to be_falsey
1594
- expect(test_instance.respond_to?(:unopened_group_member_count, true)).to be_truthy
1595
- end
1596
- end
1597
-
1598
- describe "#opened_group_member_count" do
1599
- it "is defined as protected method" do
1600
- expect(test_instance.respond_to?(:opened_group_member_count)).to be_falsey
1601
- expect(test_instance.respond_to?(:opened_group_member_count, true)).to be_truthy
1602
- end
1603
- end
1604
-
1605
- describe "#unopened_group_member_notifier_count" do
1606
- it "is defined as protected method" do
1607
- expect(test_instance.respond_to?(:unopened_group_member_notifier_count)).to be_falsey
1608
- expect(test_instance.respond_to?(:unopened_group_member_notifier_count, true)).to be_truthy
1609
- end
1610
- end
1611
-
1612
- describe "#opened_group_member_notifier_count" do
1613
- it "is defined as protected method" do
1614
- expect(test_instance.respond_to?(:opened_group_member_notifier_count)).to be_falsey
1615
- expect(test_instance.respond_to?(:opened_group_member_notifier_count, true)).to be_truthy
1616
- end
1617
- end
1618
- end
1619
-
1620
- private
1621
- def validate_expected_notification(notification, target, notifiable)
1622
- expect(notification).to be_a described_class
1623
- expect(notification.target).to eq(target)
1624
- expect(notification.notifiable).to eq(notifiable)
1625
- end
1626
-
1627
- end