summaryrefslogtreecommitdiff
path: root/spec/controllers/notification_settings_controller_spec.rb
blob: 9e3a31e1a6b9fb2c3ec8dbda0260e797cd873674 (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
require 'spec_helper'

describe NotificationSettingsController do
  let(:project) { create(:empty_project) }
  let(:group) { create(:group, :internal) }
  let(:user) { create(:user) }

  before do
    project.team << [user, :developer]
  end

  describe '#create' do
    context 'when not authorized' do
      it 'redirects to sign in page' do
        post :create,
             project_id: project.id,
             notification_setting: { level: :participating }

        expect(response).to redirect_to(new_user_session_path)
      end
    end

    context 'when authorized' do
      let(:custom_events) do
        events = {}

        NotificationSetting::EMAIL_EVENTS.each do |event|
          events[event.to_s] = true
        end

        events
      end

      before do
        sign_in(user)
      end

      context 'for projects' do
        let(:notification_setting) { user.notification_settings_for(project) }

        it 'creates notification setting' do
          post :create,
               project_id: project.id,
               notification_setting: { level: :participating }

          expect(response.status).to eq 200
          expect(notification_setting.level).to eq("participating")
          expect(notification_setting.user_id).to eq(user.id)
          expect(notification_setting.source_id).to eq(project.id)
          expect(notification_setting.source_type).to eq("Project")
        end

        context 'with custom settings' do
          it 'creates notification setting' do
            post :create,
                 project_id: project.id,
                 notification_setting: { level: :custom }.merge(custom_events)

            expect(response.status).to eq 200
            expect(notification_setting.level).to eq("custom")
            expect(notification_setting.events).to eq(custom_events)
          end
        end
      end

      context 'for groups' do
        let(:notification_setting) { user.notification_settings_for(group) }

        it 'creates notification setting' do
          post :create,
               namespace_id: group.id,
               notification_setting: { level: :watch }

          expect(response.status).to eq 200
          expect(notification_setting.level).to eq("watch")
          expect(notification_setting.user_id).to eq(user.id)
          expect(notification_setting.source_id).to eq(group.id)
          expect(notification_setting.source_type).to eq("Namespace")
        end

        context 'with custom settings' do
          it 'creates notification setting' do
            post :create,
                 namespace_id: group.id,
                 notification_setting: { level: :custom }.merge(custom_events)

            expect(response.status).to eq 200
            expect(notification_setting.level).to eq("custom")
            expect(notification_setting.events).to eq(custom_events)
          end
        end
      end
    end

    context 'not authorized' do
      let(:private_project) { create(:empty_project, :private) }
      before { sign_in(user) }

      it 'returns 404' do
        post :create,
             project_id: private_project.id,
             notification_setting: { level: :participating }

        expect(response).to have_http_status(404)
      end
    end
  end

  describe '#update' do
    let(:notification_setting) { user.global_notification_setting }

    context 'when not authorized' do
      it 'redirects to sign in page' do
        put :update,
            id: notification_setting,
            notification_setting: { level: :participating }

        expect(response).to redirect_to(new_user_session_path)
      end
    end

    context 'when authorized' do
      before{ sign_in(user) }

      it 'returns success' do
        put :update,
            id: notification_setting,
            notification_setting: { level: :participating }

        expect(response.status).to eq 200
      end

      context 'and setting custom notification setting' do
        let(:custom_events) do
          events = {}

          NotificationSetting::EMAIL_EVENTS.each do |event|
            events[event] = "true"
          end
        end

        it 'returns success' do
          put :update,
              id: notification_setting,
              notification_setting: { level: :participating, events: custom_events }

          expect(response.status).to eq 200
        end
      end
    end

    context 'not authorized' do
      let(:other_user) { create(:user) }

      before { sign_in(other_user) }

      it 'returns 404' do
        put :update,
            id: notification_setting,
            notification_setting: { level: :participating }

        expect(response).to have_http_status(404)
      end
    end
  end
end