You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.4 KiB

  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(Goldfinger).to receive(:finger).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. link_with_nil_template = double(template: nil)
  37. resource_with_link = double(link: link_with_nil_template)
  38. allow(Goldfinger).to receive(:finger).with('acct:user@example.com').and_return(resource_with_link)
  39. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  40. expect(response).to render_template(:new)
  41. expect(response.body).to include(I18n.t('remote_follow.missing_resource'))
  42. end
  43. end
  44. context 'when webfinger values are good' do
  45. before do
  46. link_with_template = double(template: 'http://example.com/follow_me?acct={uri}')
  47. resource_with_link = double(link: link_with_template)
  48. allow(Goldfinger).to receive(:finger).with('acct:user@example.com').and_return(resource_with_link)
  49. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  50. end
  51. it 'saves the session' do
  52. expect(session[:remote_follow]).to eq 'user@example.com'
  53. end
  54. it 'redirects to the remote location' do
  55. expect(response).to redirect_to("http://example.com/follow_me?acct=https%3A%2F%2F#{Rails.configuration.x.local_domain}%2Fusers%2Ftest_user")
  56. end
  57. end
  58. end
  59. context 'with an invalid acct' do
  60. it 'renders new when acct is missing' do
  61. post :create, params: { account_username: @account.to_param, remote_follow: { acct: '' } }
  62. expect(response).to render_template(:new)
  63. end
  64. it 'renders new with error when goldfinger fails' do
  65. allow(Goldfinger).to receive(:finger).with('acct:user@example.com').and_raise(Goldfinger::Error)
  66. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  67. expect(response).to render_template(:new)
  68. expect(response.body).to include(I18n.t('remote_follow.missing_resource'))
  69. end
  70. it 'renders new when occur HTTP::ConnectionError' do
  71. allow(Goldfinger).to receive(:finger).with('acct:user@unknown').and_raise(HTTP::ConnectionError)
  72. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@unknown' } }
  73. expect(response).to render_template(:new)
  74. expect(response.body).to include(I18n.t('remote_follow.missing_resource'))
  75. end
  76. end
  77. end
  78. describe 'with a suspended account' do
  79. before do
  80. @account = Fabricate(:account, suspended: true)
  81. end
  82. it 'returns 410 gone on GET to #new' do
  83. get :new, params: { account_username: @account.to_param }
  84. expect(response).to have_http_status(:gone)
  85. end
  86. it 'returns 410 gone on POST to #create' do
  87. post :create, params: { account_username: @account.to_param }
  88. expect(response).to have_http_status(:gone)
  89. end
  90. end
  91. end