about summary refs log tree commit diff
path: root/spec/models/web/push_subscription_spec.rb
blob: bd57195937204858505e063a6e02fcfedba8437c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require 'rails_helper'

RSpec.describe Web::PushSubscription, type: :model do
  let(:account) { Fabricate(:account) }

  let(:policy) { 'all' }

  let(:data) do
    {
      policy: policy,

      alerts: {
        mention: true,
        reblog: false,
        follow: true,
        follow_request: false,
        favourite: true,
      },
    }
  end

  subject { described_class.new(data: data) }

  describe '#pushable?' do
    let(:notification_type) { :mention }
    let(:notification) { Fabricate(:notification, account: account, type: notification_type) }

    %i(mention reblog follow follow_request favourite).each do |type|
      context "when notification is a #{type}" do
        let(:notification_type) { type }

        it "returns boolean corresponding to alert setting" do
          expect(subject.pushable?(notification)).to eq data[:alerts][type]
        end
      end
    end

    context 'when policy is all' do
      let(:policy) { 'all' }

      it 'returns true' do
        expect(subject.pushable?(notification)).to eq true
      end
    end

    context 'when policy is none' do
      let(:policy) { 'none' }

      it 'returns false' do
        expect(subject.pushable?(notification)).to eq false
      end
    end

    context 'when policy is followed' do
      let(:policy) { 'followed' }

      context 'and notification is from someone you follow' do
        before do
          account.follow!(notification.from_account)
        end

        it 'returns true' do
          expect(subject.pushable?(notification)).to eq true
        end
      end

      context 'and notification is not from someone you follow' do
        it 'returns false' do
          expect(subject.pushable?(notification)).to eq false
        end
      end
    end

    context 'when policy is follower' do
      let(:policy) { 'follower' }

      context 'and notification is from someone who follows you' do
        before do
          notification.from_account.follow!(account)
        end

        it 'returns true' do
          expect(subject.pushable?(notification)).to eq true
        end
      end

      context 'and notification is not from someone who follows you' do
        it 'returns false' do
          expect(subject.pushable?(notification)).to eq false
        end
      end
    end
  end
end