~cytrogen/masto-fe

ref: 91040da871dba71a6eb05cd7ae3ed8b9b255680c masto-fe/spec/requests/mail_subscriptions_spec.rb -rw-r--r-- 2.7 KiB
91040da8 — Claire Fix confusing behavior of mute button and volume slider in web UI (#26860) 2 years ago
                                                                                
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
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'MailSubscriptionsController' do
  let(:user) { Fabricate(:user) }
  let(:token) { user.to_sgid(for: 'unsubscribe').to_s }
  let(:type) { 'follow' }

  shared_examples 'not found with invalid token' do
    context 'with invalid token' do
      let(:token) { 'invalid-token' }

      it 'returns http not found' do
        expect(response).to have_http_status(404)
      end
    end
  end

  shared_examples 'not found with invalid type' do
    context 'with invalid type' do
      let(:type) { 'invalid_type' }

      it 'returns http not found' do
        expect(response).to have_http_status(404)
      end
    end
  end

  describe 'on the unsubscribe confirmation page' do
    before do
      get unsubscribe_url(token: token, type: type)
    end

    it_behaves_like 'not found with invalid token'
    it_behaves_like 'not found with invalid type'

    it 'shows unsubscribe form' do
      expect(response).to have_http_status(200)

      expect(response.body).to include(
        I18n.t('mail_subscriptions.unsubscribe.action')
      )
      expect(response.body).to include(user.email)
    end
  end

  describe 'submitting the unsubscribe confirmation page' do
    before do
      user.settings.update('notification_emails.follow': true)
      user.save!

      post unsubscribe_url, params: { token: token, type: type }
    end

    it_behaves_like 'not found with invalid token'
    it_behaves_like 'not found with invalid type'

    it 'shows confirmation page' do
      expect(response).to have_http_status(200)

      expect(response.body).to include(
        I18n.t('mail_subscriptions.unsubscribe.complete')
      )
      expect(response.body).to include(user.email)
    end

    it 'updates notification settings' do
      user.reload
      expect(user.settings['notification_emails.follow']).to be false
    end
  end

  describe 'unsubscribing with List-Unsubscribe-Post' do
    around do |example|
      old = ActionController::Base.allow_forgery_protection
      ActionController::Base.allow_forgery_protection = true

      example.run

      ActionController::Base.allow_forgery_protection = old
    end

    before do
      user.settings.update('notification_emails.follow': true)
      user.save!

      post unsubscribe_url(token: token, type: type), params: { 'List-Unsubscribe' => 'One-Click' }
    end

    it_behaves_like 'not found with invalid token'
    it_behaves_like 'not found with invalid type'

    it 'return http success' do
      expect(response).to have_http_status(200)
    end

    it 'updates notification settings' do
      user.reload
      expect(user.settings['notification_emails.follow']).to be false
    end
  end
end