File: notifications_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (117 lines) | stat: -rw-r--r-- 4,044 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WorkItems::Callbacks::Notifications, feature_category: :team_planning do
  let_it_be(:group) { create(:group) }
  let_it_be(:project) { create(:project, :private, group: group) }
  let_it_be(:guest) { create(:user, guest_of: project) }
  let_it_be(:author) { create(:user, guest_of: project) }
  let_it_be_with_reload(:work_item) { create(:work_item, project: project, author: author) }
  let_it_be(:current_user) { guest }

  let(:widget) { work_item.widgets.find { |widget| widget.is_a?(WorkItems::Callbacks::Notifications) } }
  let(:service) { described_class.new(issuable: work_item, current_user: current_user, params: params) }

  describe '#before_update_in_transaction' do
    let(:expected) { params[:subscribed] }

    subject(:update_notifications) { service.before_update }

    shared_examples 'failing to update subscription' do
      context 'when user is subscribed with a subscription record' do
        let_it_be(:subscription) { create_subscription(:subscribed) }

        it "does not update the work item's subscription" do
          expect do
            update_notifications
            subscription.reload
          end.to not_change { subscription.subscribed }
            .and(not_change { work_item.subscribed?(current_user, project) })
        end
      end

      context 'when user is subscribed by being a participant' do
        let_it_be(:current_user) { author }

        it 'does not create subscription record or change subscription state' do
          expect { update_notifications }
            .to not_change { Subscription.count }
            .and(not_change { work_item.subscribed?(current_user, project) })
        end
      end
    end

    shared_examples 'updating notifications subscription successfully' do
      it 'updates existing subscription record' do
        expect do
          update_notifications
          subscription.reload
        end.to change { subscription.subscribed }.to(expected)
          .and(change { work_item.subscribed?(current_user, project) }.to(expected))
      end
    end

    context 'when update fails' do
      context 'when user lack update_subscription permissions' do
        let_it_be(:params) { { subscribed: false } }

        before do
          allow(Ability).to receive(:allowed?).and_call_original
          allow(Ability).to receive(:allowed?)
            .with(current_user, :update_subscription, work_item)
            .and_return(false)
        end

        it_behaves_like 'failing to update subscription'
      end

      context 'when notifications params are not present' do
        let_it_be(:params) { {} }

        it_behaves_like 'failing to update subscription'
      end
    end

    context 'when update is successful' do
      context 'when subscribing' do
        let_it_be(:subscription) { create_subscription(:unsubscribed) }
        let(:params) { { subscribed: true } }

        it_behaves_like 'updating notifications subscription successfully'
      end

      context 'when unsubscribing' do
        let(:params) { { subscribed: false } }

        context 'when user is subscribed with a subscription record' do
          let_it_be(:subscription) { create_subscription(:subscribed) }

          it_behaves_like 'updating notifications subscription successfully'
        end

        context 'when user is subscribed by being a participant' do
          let_it_be(:current_user) { author }

          it 'creates a subscription with expected value' do
            expect { update_notifications }
              .to change { Subscription.count }.by(1)
              .and(change { work_item.subscribed?(current_user, project) }.to(expected))

            expect(Subscription.last.subscribed).to eq(expected)
          end
        end
      end
    end
  end

  def create_subscription(state)
    create(
      :subscription,
      project: project,
      user: current_user,
      subscribable: work_item,
      subscribed: (state == :subscribed)
    )
  end
end