summaryrefslogtreecommitdiff
path: root/spec/controllers/profiles/notifications_controller_spec.rb
blob: 1ebf4363ba640ae6fa4d6c4fd6dd2e57f0cbda8a (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Profiles::NotificationsController do
  let(:user) do
    create(:user) do |user|
      user.emails.create!(email: 'original@example.com', confirmed_at: Time.current)
      user.emails.create!(email: 'new@example.com', confirmed_at: Time.current)
      user.notification_email = 'original@example.com'
      user.save!
    end
  end

  describe 'GET show' do
    it 'renders' do
      sign_in(user)

      get :show

      expect(response).to render_template :show
    end

    context 'when personal projects are present', :request_store do
      let!(:personal_project_1) { create(:project, namespace: user.namespace) }

      context 'N+1 query check' do
        render_views

        it 'does not have an N+1' do
          sign_in(user)

          get :show

          control = ActiveRecord::QueryRecorder.new do
            get :show
          end

          create_list(:project, 2, namespace: user.namespace)

          expect do
            get :show
          end.not_to exceed_query_limit(control)
        end
      end
    end

    context 'with groups that do not have notification preferences' do
      let_it_be(:group) { create(:group) }
      let_it_be(:subgroup) { create(:group, parent: group) }

      before do
        group.add_developer(user)
      end

      it 'still shows up in the list' do
        sign_in(user)

        get :show

        expect(assigns(:group_notifications).map(&:source_id)).to include(subgroup.id)
      end

      context 'N+1 query check' do
        render_views

        it 'does not have an N+1' do
          sign_in(user)

          get :show

          control = ActiveRecord::QueryRecorder.new do
            get :show
          end

          create_list(:group, 2, parent: group)

          expect do
            get :show
          end.not_to exceed_query_limit(control)
        end
      end
    end

    context 'with group notifications' do
      let(:notifications_per_page) { 5 }

      let_it_be(:group) { create(:group) }
      let_it_be(:subgroups) { create_list(:group, 10, parent: group) }

      before do
        group.add_developer(user)
        sign_in(user)
        allow(Kaminari.config).to receive(:default_per_page).and_return(notifications_per_page)
      end

      it 'paginates the groups' do
        get :show

        expect(assigns(:group_notifications).count).to eq(5)
      end

      context 'when the user is not a member' do
        let(:notifications_per_page) { 20 }

        let_it_be(:public_group) { create(:group, :public) }

        it 'does not show public groups', :aggregate_failures do
          get :show

          # Let's make sure we're grabbing all groups in one page, just in case
          expect(assigns(:user_groups).count).to eq(11)
          expect(assigns(:user_groups)).not_to include(public_group)
        end
      end
    end

    context 'with project notifications' do
      let!(:notification_setting) { create(:notification_setting, source: project, user: user, level: :watch) }

      before do
        sign_in(user)
        get :show
      end

      context 'when project is public' do
        let(:project) { create(:project, :public) }

        it 'shows notification setting for project' do
          expect(assigns(:project_notifications).map(&:source_id)).to include(project.id)
        end
      end

      context 'when project is public' do
        let(:project) { create(:project, :private) }

        it 'shows notification setting for project' do
          # notification settings for given project were created before project was set to private
          expect(user.notification_settings.for_projects.map(&:source_id)).to include(project.id)

          # check that notification settings for project where user does not have access are filtered
          expect(assigns(:project_notifications)).to be_empty
        end
      end
    end
  end

  describe 'POST update' do
    it 'updates only permitted attributes' do
      sign_in(user)

      put :update, params: { user: { notification_email: 'new@example.com', email_opted_in: true, notified_of_own_activity: true, admin: true } }

      user.reload
      expect(user.notification_email).to eq('new@example.com')
      expect(user.email_opted_in).to eq(true)
      expect(user.notified_of_own_activity).to eq(true)
      expect(user.admin).to eq(false)
      expect(controller).to set_flash[:notice].to('Notification settings saved')
    end

    it 'shows an error message if the params are invalid' do
      sign_in(user)

      put :update, params: { user: { notification_email: '' } }

      expect(user.reload.notification_email).to eq('original@example.com')
      expect(controller).to set_flash[:alert].to('Failed to save new settings')
    end
  end
end