authorize_interactions_controller_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe AuthorizeInteractionsController do
  4. render_views
  5. describe 'GET #show' do
  6. describe 'when signed out' do
  7. it 'redirects to sign in page' do
  8. get :show
  9. expect(response).to redirect_to(new_user_session_path)
  10. end
  11. end
  12. describe 'when signed in' do
  13. let(:user) { Fabricate(:user) }
  14. let(:account) { Fabricate(:account, user: user) }
  15. before do
  16. sign_in(user)
  17. end
  18. it 'renders error without acct param' do
  19. get :show
  20. expect(response).to render_template(:error)
  21. end
  22. it 'renders error when account cant be found' do
  23. service = double
  24. allow(ResolveAccountService).to receive(:new).and_return(service)
  25. allow(service).to receive(:call).with('missing@hostname').and_return(nil)
  26. get :show, params: { acct: 'acct:missing@hostname' }
  27. expect(response).to render_template(:error)
  28. expect(service).to have_received(:call).with('missing@hostname')
  29. end
  30. it 'sets resource from url' do
  31. account = Account.new
  32. service = double
  33. allow(ResolveURLService).to receive(:new).and_return(service)
  34. allow(service).to receive(:call).with('http://example.com').and_return(account)
  35. get :show, params: { acct: 'http://example.com' }
  36. expect(response).to have_http_status(200)
  37. expect(assigns(:resource)).to eq account
  38. end
  39. it 'sets resource from acct uri' do
  40. account = Account.new
  41. service = double
  42. allow(ResolveAccountService).to receive(:new).and_return(service)
  43. allow(service).to receive(:call).with('found@hostname').and_return(account)
  44. get :show, params: { acct: 'acct:found@hostname' }
  45. expect(response).to have_http_status(200)
  46. expect(assigns(:resource)).to eq account
  47. end
  48. end
  49. end
  50. describe 'POST #create' do
  51. describe 'when signed out' do
  52. it 'redirects to sign in page' do
  53. post :create
  54. expect(response).to redirect_to(new_user_session_path)
  55. end
  56. end
  57. describe 'when signed in' do
  58. let!(:user) { Fabricate(:user) }
  59. let!(:account) { user.account }
  60. before do
  61. sign_in(user)
  62. end
  63. it 'shows error when account not found' do
  64. service = double
  65. allow(ResolveAccountService).to receive(:new).and_return(service)
  66. allow(service).to receive(:call).with('user@hostname').and_return(nil)
  67. post :create, params: { acct: 'acct:user@hostname' }
  68. expect(response).to render_template(:error)
  69. end
  70. it 'follows account when found' do
  71. target_account = Fabricate(:account)
  72. service = double
  73. allow(ResolveAccountService).to receive(:new).and_return(service)
  74. allow(service).to receive(:call).with('user@hostname').and_return(target_account)
  75. allow(service).to receive(:call).with(target_account, skip_webfinger: true).and_return(target_account)
  76. post :create, params: { acct: 'acct:user@hostname' }
  77. expect(service).to have_received(:call).with(target_account, skip_webfinger: true)
  78. expect(account.following?(target_account)).to be true
  79. expect(response).to render_template(:success)
  80. end
  81. end
  82. end
  83. end