mail_subscriptions_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'MailSubscriptionsController' do
  4. let(:user) { Fabricate(:user) }
  5. let(:token) { user.to_sgid(for: 'unsubscribe').to_s }
  6. let(:type) { 'follow' }
  7. shared_examples 'not found with invalid token' do
  8. context 'with invalid token' do
  9. let(:token) { 'invalid-token' }
  10. it 'returns http not found' do
  11. expect(response).to have_http_status(404)
  12. end
  13. end
  14. end
  15. shared_examples 'not found with invalid type' do
  16. context 'with invalid type' do
  17. let(:type) { 'invalid_type' }
  18. it 'returns http not found' do
  19. expect(response).to have_http_status(404)
  20. end
  21. end
  22. end
  23. describe 'on the unsubscribe confirmation page' do
  24. before do
  25. get unsubscribe_url(token: token, type: type)
  26. end
  27. it_behaves_like 'not found with invalid token'
  28. it_behaves_like 'not found with invalid type'
  29. it 'shows unsubscribe form' do
  30. expect(response).to have_http_status(200)
  31. expect(response.body).to include(
  32. I18n.t('mail_subscriptions.unsubscribe.action')
  33. )
  34. expect(response.body).to include(user.email)
  35. end
  36. end
  37. describe 'submitting the unsubscribe confirmation page' do
  38. before do
  39. user.settings.update('notification_emails.follow': true)
  40. user.save!
  41. post unsubscribe_url, params: { token: token, type: type }
  42. end
  43. it_behaves_like 'not found with invalid token'
  44. it_behaves_like 'not found with invalid type'
  45. it 'shows confirmation page' do
  46. expect(response).to have_http_status(200)
  47. expect(response.body).to include(
  48. I18n.t('mail_subscriptions.unsubscribe.complete')
  49. )
  50. expect(response.body).to include(user.email)
  51. end
  52. it 'updates notification settings' do
  53. user.reload
  54. expect(user.settings['notification_emails.follow']).to be false
  55. end
  56. end
  57. describe 'unsubscribing with List-Unsubscribe-Post' do
  58. around do |example|
  59. old = ActionController::Base.allow_forgery_protection
  60. ActionController::Base.allow_forgery_protection = true
  61. example.run
  62. ActionController::Base.allow_forgery_protection = old
  63. end
  64. before do
  65. user.settings.update('notification_emails.follow': true)
  66. user.save!
  67. post unsubscribe_url(token: token, type: type), params: { 'List-Unsubscribe' => 'One-Click' }
  68. end
  69. it_behaves_like 'not found with invalid token'
  70. it_behaves_like 'not found with invalid type'
  71. it 'return http success' do
  72. expect(response).to have_http_status(200)
  73. end
  74. it 'updates notification settings' do
  75. user.reload
  76. expect(user.settings['notification_emails.follow']).to be false
  77. end
  78. end
  79. end