BigW Consortium Gitlab

sent_notifications_controller_spec.rb 3.42 KB
Newer Older
1 2 3
require 'rails_helper'

describe SentNotificationsController, type: :controller do
4 5 6
  let(:user) { create(:user) }
  let(:project) { create(:empty_project) }
  let(:sent_notification) { create(:sent_notification, noteable: issue, recipient: user) }
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
  let(:issue) do
    create(:issue, project: project, author: user) do |issue|
      issue.subscriptions.create(user: user, subscribed: true)
    end
  end

  describe 'GET unsubscribe' do
    context 'when the user is not logged in' do
      context 'when the force param is passed' do
        before { get(:unsubscribe, id: sent_notification.reply_key, force: true) }

        it 'unsubscribes the user' do
          expect(issue.subscribed?(user)).to be_falsey
        end

        it 'sets the flash message' do
          expect(controller).to set_flash[:notice].to(/unsubscribed/).now
        end

        it 'redirects to the login page' do
          expect(response).to redirect_to(new_user_session_path)
        end
      end

      context 'when the force param is not passed' do
        before { get(:unsubscribe, id: sent_notification.reply_key) }

        it 'does not unsubscribe the user' do
          expect(issue.subscribed?(user)).to be_truthy
        end
38

39 40 41 42 43
        it 'does not set the flash message' do
          expect(controller).not_to set_flash[:notice]
        end

        it 'redirects to the login page' do
44
          expect(response).to render_template :unsubscribe
45 46
        end
      end
47 48
    end

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
    context 'when the user is logged in' do
      before { sign_in(user) }

      context 'when the ID passed does not exist' do
        before { get(:unsubscribe, id: sent_notification.reply_key.reverse) }

        it 'does not unsubscribe the user' do
          expect(issue.subscribed?(user)).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_http_status(:not_found)
        end
      end

      context 'when the force param is passed' do
        before { get(:unsubscribe, id: sent_notification.reply_key, force: true) }

        it 'unsubscribes the user' do
          expect(issue.subscribed?(user)).to be_falsey
        end

        it 'sets the flash message' do
          expect(controller).to set_flash[:notice].to(/unsubscribed/).now
        end

        it 'redirects to the issue page' do
          expect(response).
            to redirect_to(namespace_project_issue_path(project.namespace, project, issue))
        end
      end

      context 'when the force param is not passed' do
86 87 88 89 90 91
        let(:merge_request) do
          create(:merge_request, source_project: project, author: user) do |merge_request|
            merge_request.subscriptions.create(user: user, subscribed: true)
          end
        end
        let(:sent_notification) { create(:sent_notification, noteable: merge_request, recipient: user) }
92 93 94
        before { get(:unsubscribe, id: sent_notification.reply_key) }

        it 'unsubscribes the user' do
95
          expect(merge_request.subscribed?(user)).to be_falsey
96
        end
97

98 99 100
        it 'sets the flash message' do
          expect(controller).to set_flash[:notice].to(/unsubscribed/).now
        end
101

102
        it 'redirects to the merge request page' do
103
          expect(response).
104
            to redirect_to(namespace_project_merge_request_path(project.namespace, project, merge_request))
105
        end
106 107 108 109
      end
    end
  end
end