unsubscribe_links_spec.rb 2.59 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'Unsubscribe links' do
4 5 6 7
  include Warden::Test::Helpers

  let(:recipient) { create(:user) }
  let(:author) { create(:user) }
8
  let(:project) { create(:project, :public) }
9
  let(:params) { { title: 'A bug!', description: 'Fix it!', assignees: [recipient] } }
10 11 12 13
  let(:issue) { Issues::CreateService.new(project, author, params).execute }

  let(:mail) { ActionMailer::Base.deliveries.last }
  let(:body) { Capybara::Node::Simple.new(mail.default_part_body.to_s) }
14
  let(:header_link) { mail.header['List-Unsubscribe'].to_s[1..-2] } # Strip angle brackets
15 16 17 18 19 20 21
  let(:body_link) { body.find_link('unsubscribe')['href'] }

  before do
    perform_enqueued_jobs { issue }
  end

  context 'when logged out' do
22 23 24 25 26
    context 'when visiting the link from the body' do
      it 'shows the unsubscribe confirmation page and redirects to root path when confirming' do
        visit body_link

        expect(current_path).to eq unsubscribe_sent_notification_path(SentNotification.last)
27 28
        expect(page).to have_text(%(Unsubscribe from issue))
        expect(page).to have_text(%(Are you sure you want to unsubscribe from the issue: #{issue.title} (#{issue.to_reference})?))
29
        expect(issue.subscribed?(recipient, project)).to be_truthy
30 31 32

        click_link 'Unsubscribe'

33
        expect(issue.subscribed?(recipient, project)).to be_falsey
34 35 36 37 38 39 40
        expect(current_path).to eq new_user_session_path
      end

      it 'shows the unsubscribe confirmation page and redirects to root path when canceling' do
        visit body_link

        expect(current_path).to eq unsubscribe_sent_notification_path(SentNotification.last)
41
        expect(issue.subscribed?(recipient, project)).to be_truthy
42 43

        click_link 'Cancel'
44

45
        expect(issue.subscribed?(recipient, project)).to be_truthy
46 47
        expect(current_path).to eq new_user_session_path
      end
48 49 50 51 52 53
    end

    it 'unsubscribes from the issue when visiting the link from the header' do
      visit header_link

      expect(page).to have_text('unsubscribed')
54
      expect(issue.subscribed?(recipient, project)).to be_falsey
55 56 57 58
    end
  end

  context 'when logged in' do
59
    before do
60
      sign_in(recipient)
61
    end
62 63 64 65 66

    it 'unsubscribes from the issue when visiting the link from the email body' do
      visit body_link

      expect(page).to have_text('unsubscribed')
67
      expect(issue.subscribed?(recipient, project)).to be_falsey
68 69 70 71 72 73
    end

    it 'unsubscribes from the issue when visiting the link from the header' do
      visit header_link

      expect(page).to have_text('unsubscribed')
74
      expect(issue.subscribed?(recipient, project)).to be_falsey
75 76 77
    end
  end
end