remote_follow_controller_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe RemoteFollowController do
  4. render_views
  5. describe '#new' do
  6. it 'returns success when session is empty' do
  7. account = Fabricate(:account)
  8. get :new, params: { account_username: account.to_param }
  9. expect(response).to have_http_status(200)
  10. expect(response).to render_template(:new)
  11. expect(assigns(:remote_follow).acct).to be_nil
  12. end
  13. it 'populates the remote follow with session data when session exists' do
  14. session[:remote_follow] = 'user@example.com'
  15. account = Fabricate(:account)
  16. get :new, params: { account_username: account.to_param }
  17. expect(response).to have_http_status(200)
  18. expect(response).to render_template(:new)
  19. expect(assigns(:remote_follow).acct).to eq 'user@example.com'
  20. end
  21. end
  22. describe '#create' do
  23. before do
  24. @account = Fabricate(:account, username: 'test_user')
  25. end
  26. context 'with a valid acct' do
  27. context 'when webfinger values are wrong' do
  28. it 'renders new when redirect url is nil' do
  29. resource_with_nil_link = double(link: nil)
  30. allow_any_instance_of(WebfingerHelper).to receive(:webfinger!).with('acct:user@example.com').and_return(resource_with_nil_link)
  31. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  32. expect(response).to render_template(:new)
  33. expect(response.body).to include(I18n.t('remote_follow.missing_resource'))
  34. end
  35. it 'renders new when template is nil' do
  36. resource_with_link = double(link: nil)
  37. allow_any_instance_of(WebfingerHelper).to receive(:webfinger!).with('acct:user@example.com').and_return(resource_with_link)
  38. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  39. expect(response).to render_template(:new)
  40. expect(response.body).to include(I18n.t('remote_follow.missing_resource'))
  41. end
  42. end
  43. context 'when webfinger values are good' do
  44. before do
  45. resource_with_link = double(link: 'http://example.com/follow_me?acct={uri}')
  46. allow_any_instance_of(WebfingerHelper).to receive(:webfinger!).with('acct:user@example.com').and_return(resource_with_link)
  47. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  48. end
  49. it 'saves the session' do
  50. expect(session[:remote_follow]).to eq 'user@example.com'
  51. end
  52. it 'redirects to the remote location' do
  53. expect(response).to redirect_to("http://example.com/follow_me?acct=https%3A%2F%2F#{Rails.configuration.x.local_domain}%2Fusers%2Ftest_user")
  54. end
  55. end
  56. end
  57. context 'with an invalid acct' do
  58. it 'renders new when acct is missing' do
  59. post :create, params: { account_username: @account.to_param, remote_follow: { acct: '' } }
  60. expect(response).to render_template(:new)
  61. end
  62. it 'renders new with error when webfinger fails' do
  63. allow_any_instance_of(WebfingerHelper).to receive(:webfinger!).with('acct:user@example.com').and_raise(Webfinger::Error)
  64. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  65. expect(response).to render_template(:new)
  66. expect(response.body).to include(I18n.t('remote_follow.missing_resource'))
  67. end
  68. it 'renders new when occur HTTP::ConnectionError' do
  69. allow_any_instance_of(WebfingerHelper).to receive(:webfinger!).with('acct:user@unknown').and_raise(HTTP::ConnectionError)
  70. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@unknown' } }
  71. expect(response).to render_template(:new)
  72. expect(response.body).to include(I18n.t('remote_follow.missing_resource'))
  73. end
  74. end
  75. end
  76. context 'with a permanently suspended account' do
  77. before do
  78. @account = Fabricate(:account)
  79. @account.suspend!
  80. @account.deletion_request.destroy
  81. end
  82. it 'returns http gone on GET to #new' do
  83. get :new, params: { account_username: @account.to_param }
  84. expect(response).to have_http_status(410)
  85. end
  86. it 'returns http gone on POST to #create' do
  87. post :create, params: { account_username: @account.to_param }
  88. expect(response).to have_http_status(410)
  89. end
  90. end
  91. context 'with a temporarily suspended account' do
  92. before do
  93. @account = Fabricate(:account)
  94. @account.suspend!
  95. end
  96. it 'returns http forbidden on GET to #new' do
  97. get :new, params: { account_username: @account.to_param }
  98. expect(response).to have_http_status(403)
  99. end
  100. it 'returns http forbidden on POST to #create' do
  101. post :create, params: { account_username: @account.to_param }
  102. expect(response).to have_http_status(403)
  103. end
  104. end
  105. end