1# frozen_string_literal: true 2 3require 'spec_helper' 4 5RSpec.describe SentNotificationsController do 6 let(:user) { create(:user) } 7 let(:project) { create(:project, :public) } 8 let(:private_project) { create(:project, :private) } 9 let(:sent_notification) { create(:sent_notification, project: target_project, noteable: noteable, recipient: user) } 10 11 let(:issue) do 12 create(:issue, project: target_project) do |issue| 13 issue.subscriptions.create!(user: user, project: target_project, subscribed: true) 14 end 15 end 16 17 let(:confidential_issue) do 18 create(:issue, project: target_project, confidential: true) do |issue| 19 issue.subscriptions.create!(user: user, project: target_project, subscribed: true) 20 end 21 end 22 23 let(:merge_request) do 24 create(:merge_request, source_project: target_project, target_project: target_project) do |mr| 25 mr.subscriptions.create!(user: user, project: target_project, subscribed: true) 26 end 27 end 28 29 let(:noteable) { issue } 30 let(:target_project) { project } 31 32 describe 'GET unsubscribe' do 33 shared_examples 'returns 404' do 34 it 'does not set the flash message' do 35 expect(controller).not_to set_flash[:notice] 36 end 37 38 it 'returns a 404' do 39 expect(response).to have_gitlab_http_status(:not_found) 40 end 41 end 42 43 context 'when the user is not logged in' do 44 context 'when the force param is passed' do 45 before do 46 get(:unsubscribe, params: { id: sent_notification.reply_key, force: true }) 47 end 48 49 it 'unsubscribes the user' do 50 expect(issue.subscribed?(user, project)).to be_falsey 51 end 52 53 it 'sets the flash message' do 54 expect(controller).to set_flash[:notice].to(/unsubscribed/) 55 end 56 57 it 'redirects to the login page' do 58 expect(response).to redirect_to(new_user_session_path) 59 end 60 end 61 62 context 'when the force param is not passed' do 63 render_views 64 65 before do 66 get(:unsubscribe, params: { id: sent_notification.reply_key }) 67 end 68 69 shared_examples 'unsubscribing as anonymous' do |project_visibility| 70 it 'does not unsubscribe the user' do 71 expect(noteable.subscribed?(user, target_project)).to be_truthy 72 end 73 74 it 'does not set the flash message' do 75 expect(controller).not_to set_flash[:notice] 76 end 77 78 it 'renders unsubscribe page' do 79 expect(response).to have_gitlab_http_status(:ok) 80 expect(response).to render_template :unsubscribe 81 end 82 83 if project_visibility == :private 84 it 'does not show project name or path' do 85 expect(response.body).not_to include(noteable.project.name) 86 expect(response.body).not_to include(noteable.project.full_name) 87 end 88 else 89 it 'shows project name or path' do 90 expect(response.body).to include(noteable.project.name) 91 expect(response.body).to include(noteable.project.full_name) 92 end 93 end 94 end 95 96 context 'when project is public' do 97 context 'when unsubscribing from issue' do 98 let(:noteable) { issue } 99 100 it 'shows issue title' do 101 expect(response.body).to include(issue.title) 102 end 103 104 it_behaves_like 'unsubscribing as anonymous', :public 105 end 106 107 context 'when unsubscribing from confidential issue' do 108 let(:noteable) { confidential_issue } 109 110 it 'does not show issue title' do 111 expect(response.body).not_to include(confidential_issue.title) 112 expect(response.body).to include(confidential_issue.to_reference) 113 end 114 115 it_behaves_like 'unsubscribing as anonymous', :public 116 end 117 118 context 'when unsubscribing from merge request' do 119 let(:noteable) { merge_request } 120 121 it 'shows merge request title' do 122 expect(response.body).to include(merge_request.title) 123 end 124 125 it 'shows project name or path' do 126 expect(response.body).to include(issue.project.name) 127 expect(response.body).to include(issue.project.full_name) 128 end 129 130 it_behaves_like 'unsubscribing as anonymous', :public 131 end 132 end 133 134 context 'when project is not public' do 135 let(:target_project) { private_project } 136 137 context 'when unsubscribing from issue' do 138 let(:noteable) { issue } 139 140 it 'does not show issue title' do 141 expect(response.body).not_to include(issue.title) 142 end 143 144 it_behaves_like 'unsubscribing as anonymous', :private 145 end 146 147 context 'when unsubscribing from confidential issue' do 148 let(:noteable) { confidential_issue } 149 150 it 'does not show issue title' do 151 expect(response.body).not_to include(confidential_issue.title) 152 expect(response.body).to include(confidential_issue.to_reference) 153 end 154 155 it_behaves_like 'unsubscribing as anonymous', :private 156 end 157 158 context 'when unsubscribing from merge request' do 159 let(:noteable) { merge_request } 160 161 it 'dos not show merge request title' do 162 expect(response.body).not_to include(merge_request.title) 163 end 164 165 it_behaves_like 'unsubscribing as anonymous', :private 166 end 167 end 168 end 169 170 context 'when the noteable associated to the notification has been deleted' do 171 before do 172 sent_notification.noteable.destroy! 173 174 get(:unsubscribe, params: { id: sent_notification.reply_key }) 175 end 176 177 it_behaves_like 'returns 404' 178 end 179 end 180 181 context 'when the user is logged in' do 182 before do 183 sign_in(user) 184 end 185 186 context 'when the ID passed does not exist' do 187 before do 188 get(:unsubscribe, params: { id: sent_notification.reply_key.reverse }) 189 end 190 191 it_behaves_like 'returns 404' 192 end 193 194 context 'when the force param is passed' do 195 before do 196 get(:unsubscribe, params: { id: sent_notification.reply_key, force: true }) 197 end 198 199 it 'unsubscribes the user' do 200 expect(issue.subscribed?(user, project)).to be_falsey 201 end 202 203 it 'sets the flash message' do 204 expect(controller).to set_flash[:notice].to(/unsubscribed/) 205 end 206 207 it 'redirects to the issue page' do 208 expect(response) 209 .to redirect_to(project_issue_path(project, issue)) 210 end 211 end 212 213 context 'when the force param is not passed' do 214 let(:merge_request) do 215 create(:merge_request, source_project: project, author: user) do |merge_request| 216 merge_request.subscriptions.create!(user: user, project: project, subscribed: true) 217 end 218 end 219 220 let(:sent_notification) { create(:sent_notification, project: project, noteable: merge_request, recipient: user) } 221 222 before do 223 get(:unsubscribe, params: { id: sent_notification.reply_key }) 224 end 225 226 it 'unsubscribes the user' do 227 expect(merge_request.subscribed?(user, project)).to be_falsey 228 end 229 230 it 'sets the flash message' do 231 expect(controller).to set_flash[:notice].to(/unsubscribed/) 232 end 233 234 it 'redirects to the merge request page' do 235 expect(response) 236 .to redirect_to(project_merge_request_path(project, merge_request)) 237 end 238 end 239 240 context 'when project is private' do 241 context 'and user does not have access' do 242 let(:noteable) { issue } 243 let(:target_project) { private_project } 244 245 before do 246 get(:unsubscribe, params: { id: sent_notification.reply_key }) 247 end 248 249 it 'unsubscribes user and redirects to root path' do 250 expect(response).to redirect_to(root_path) 251 end 252 end 253 254 context 'and user has access' do 255 let(:noteable) { issue } 256 let(:target_project) { private_project } 257 258 before do 259 private_project.add_developer(user) 260 get(:unsubscribe, params: { id: sent_notification.reply_key }) 261 end 262 263 it 'unsubscribes user and redirects to issue path' do 264 expect(response).to redirect_to(project_issue_path(private_project, issue)) 265 end 266 end 267 end 268 269 context 'when the noteable associated to the notification has been deleted' do 270 before do 271 sent_notification.noteable.destroy! 272 273 get(:unsubscribe, params: { id: sent_notification.reply_key }) 274 end 275 276 it_behaves_like 'returns 404' 277 end 278 end 279 end 280end 281