push_subscription_spec.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Web::PushSubscription do
  4. subject { described_class.new(data: data) }
  5. let(:account) { Fabricate(:account) }
  6. let(:policy) { 'all' }
  7. let(:data) do
  8. {
  9. policy: policy,
  10. alerts: {
  11. mention: true,
  12. reblog: false,
  13. follow: true,
  14. follow_request: false,
  15. favourite: true,
  16. },
  17. }
  18. end
  19. describe '#pushable?' do
  20. let(:notification_type) { :mention }
  21. let(:notification) { Fabricate(:notification, account: account, type: notification_type) }
  22. %i(mention reblog follow follow_request favourite).each do |type|
  23. context "when notification is a #{type}" do
  24. let(:notification_type) { type }
  25. it 'returns boolean corresponding to alert setting' do
  26. expect(subject.pushable?(notification)).to eq data[:alerts][type]
  27. end
  28. end
  29. end
  30. context 'when policy is all' do
  31. let(:policy) { 'all' }
  32. it 'returns true' do
  33. expect(subject.pushable?(notification)).to be true
  34. end
  35. end
  36. context 'when policy is none' do
  37. let(:policy) { 'none' }
  38. it 'returns false' do
  39. expect(subject.pushable?(notification)).to be false
  40. end
  41. end
  42. context 'when policy is followed' do
  43. let(:policy) { 'followed' }
  44. context 'when notification is from someone you follow' do
  45. before do
  46. account.follow!(notification.from_account)
  47. end
  48. it 'returns true' do
  49. expect(subject.pushable?(notification)).to be true
  50. end
  51. end
  52. context 'when notification is not from someone you follow' do
  53. it 'returns false' do
  54. expect(subject.pushable?(notification)).to be false
  55. end
  56. end
  57. end
  58. context 'when policy is follower' do
  59. let(:policy) { 'follower' }
  60. context 'when notification is from someone who follows you' do
  61. before do
  62. notification.from_account.follow!(account)
  63. end
  64. it 'returns true' do
  65. expect(subject.pushable?(notification)).to be true
  66. end
  67. end
  68. context 'when notification is not from someone who follows you' do
  69. it 'returns false' do
  70. expect(subject.pushable?(notification)).to be false
  71. end
  72. end
  73. end
  74. end
  75. end