Commit e8c92405 authored by Nick Thomas's avatar Nick Thomas

Merge branch '199034-nomethoderror-undefined-method-unsubscribe-for-nil-nilclass' into 'master'

Resolve "NoMethodError: undefined method `unsubscribe' for nil:NilClass"

Closes #199034

See merge request gitlab-org/gitlab!23747
parents faa22f56 c35e5b20
...@@ -6,14 +6,24 @@ class SentNotificationsController < ApplicationController ...@@ -6,14 +6,24 @@ class SentNotificationsController < ApplicationController
def unsubscribe def unsubscribe
@sent_notification = SentNotification.for(params[:id]) @sent_notification = SentNotification.for(params[:id])
return render_404 unless @sent_notification && @sent_notification.unsubscribable? return render_404 unless unsubscribe_prerequisites_met?
return unsubscribe_and_redirect if current_user || params[:force] return unsubscribe_and_redirect if current_user || params[:force]
end end
private private
def unsubscribe_prerequisites_met?
@sent_notification.present? &&
@sent_notification.unsubscribable? &&
noteable.present?
end
def noteable
@sent_notification.noteable
end
def unsubscribe_and_redirect def unsubscribe_and_redirect
noteable = @sent_notification.noteable
noteable.unsubscribe(@sent_notification.recipient, @sent_notification.project) noteable.unsubscribe(@sent_notification.recipient, @sent_notification.project)
flash[:notice] = _("You have been unsubscribed from this thread.") flash[:notice] = _("You have been unsubscribed from this thread.")
......
---
title: Fix 500 error when trying to unsubscribe from an already deleted entity
merge_request: 23747
author:
type: fixed
...@@ -30,6 +30,16 @@ describe SentNotificationsController do ...@@ -30,6 +30,16 @@ describe SentNotificationsController do
let(:target_project) { project } let(:target_project) { project }
describe 'GET unsubscribe' do describe 'GET unsubscribe' do
shared_examples 'returns 404' do
it 'does not set the flash message' do
expect(controller).not_to set_flash[:notice]
end
it 'returns a 404' do
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when the user is not logged in' do context 'when the user is not logged in' do
context 'when the force param is passed' do context 'when the force param is passed' do
before do before do
...@@ -156,6 +166,16 @@ describe SentNotificationsController do ...@@ -156,6 +166,16 @@ describe SentNotificationsController do
end end
end end
end end
context 'when the noteable associated to the notification has been deleted' do
before do
sent_notification.noteable.destroy!
get(:unsubscribe, params: { id: sent_notification.reply_key })
end
it_behaves_like 'returns 404'
end
end end
context 'when the user is logged in' do context 'when the user is logged in' do
...@@ -168,17 +188,7 @@ describe SentNotificationsController do ...@@ -168,17 +188,7 @@ describe SentNotificationsController do
get(:unsubscribe, params: { id: sent_notification.reply_key.reverse }) get(:unsubscribe, params: { id: sent_notification.reply_key.reverse })
end end
it 'does not unsubscribe the user' do it_behaves_like 'returns 404'
expect(issue.subscribed?(user, project)).to be_truthy
end
it 'does not set the flash message' do
expect(controller).not_to set_flash[:notice]
end
it 'returns a 404' do
expect(response).to have_gitlab_http_status(:not_found)
end
end end
context 'when the force param is passed' do context 'when the force param is passed' do
...@@ -254,6 +264,16 @@ describe SentNotificationsController do ...@@ -254,6 +264,16 @@ describe SentNotificationsController do
end end
end end
end end
context 'when the noteable associated to the notification has been deleted' do
before do
sent_notification.noteable.destroy!
get(:unsubscribe, params: { id: sent_notification.reply_key })
end
it_behaves_like 'returns 404'
end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment