embeds_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe '/api/web/embed' do
  4. subject { get "/api/web/embeds/#{id}", headers: headers }
  5. context 'when accessed anonymously' do
  6. let(:headers) { {} }
  7. context 'when the requested status is local' do
  8. let(:id) { status.id }
  9. context 'when the requested status is public' do
  10. let(:status) { Fabricate(:status, visibility: :public) }
  11. it 'returns JSON with an html attribute' do
  12. subject
  13. expect(response).to have_http_status(200)
  14. expect(body_as_json[:html]).to be_present
  15. end
  16. end
  17. context 'when the requested status is private' do
  18. let(:status) { Fabricate(:status, visibility: :private) }
  19. it 'returns http not found' do
  20. subject
  21. expect(response).to have_http_status(404)
  22. end
  23. end
  24. end
  25. context 'when the requested status is remote' do
  26. let(:remote_account) { Fabricate(:account, domain: 'example.com') }
  27. let(:status) { Fabricate(:status, visibility: :public, account: remote_account, url: 'https://example.com/statuses/1') }
  28. let(:id) { status.id }
  29. it 'returns http not found' do
  30. subject
  31. expect(response).to have_http_status(404)
  32. end
  33. end
  34. context 'when the requested status does not exist' do
  35. let(:id) { -1 }
  36. it 'returns http not found' do
  37. subject
  38. expect(response).to have_http_status(404)
  39. end
  40. end
  41. end
  42. context 'with an API token' do
  43. let(:user) { Fabricate(:user) }
  44. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read') }
  45. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  46. context 'when the requested status is local' do
  47. let(:id) { status.id }
  48. context 'when the requested status is public' do
  49. let(:status) { Fabricate(:status, visibility: :public) }
  50. it 'returns JSON with an html attribute' do
  51. subject
  52. expect(response).to have_http_status(200)
  53. expect(body_as_json[:html]).to be_present
  54. end
  55. context 'when the requesting user is blocked' do
  56. before do
  57. status.account.block!(user.account)
  58. end
  59. it 'returns http not found' do
  60. subject
  61. expect(response).to have_http_status(404)
  62. end
  63. end
  64. end
  65. context 'when the requested status is private' do
  66. let(:status) { Fabricate(:status, visibility: :private) }
  67. before do
  68. user.account.follow!(status.account)
  69. end
  70. it 'returns http not found' do
  71. subject
  72. expect(response).to have_http_status(404)
  73. end
  74. end
  75. end
  76. context 'when the requested status is remote' do
  77. let(:remote_account) { Fabricate(:account, domain: 'example.com') }
  78. let(:status) { Fabricate(:status, visibility: :public, account: remote_account, url: 'https://example.com/statuses/1') }
  79. let(:id) { status.id }
  80. let(:service_instance) { instance_double(FetchOEmbedService) }
  81. before do
  82. allow(FetchOEmbedService).to receive(:new) { service_instance }
  83. allow(service_instance).to receive(:call) { call_result }
  84. end
  85. context 'when the requesting user is blocked' do
  86. before do
  87. status.account.block!(user.account)
  88. end
  89. it 'returns http not found' do
  90. subject
  91. expect(response).to have_http_status(404)
  92. end
  93. end
  94. context 'when successfully fetching OEmbed' do
  95. let(:call_result) { { html: 'ok' } }
  96. it 'returns JSON with an html attribute' do
  97. subject
  98. expect(response).to have_http_status(200)
  99. expect(body_as_json[:html]).to be_present
  100. end
  101. end
  102. context 'when failing to fetch OEmbed' do
  103. let(:call_result) { nil }
  104. it 'returns http not found' do
  105. subject
  106. expect(response).to have_http_status(404)
  107. end
  108. end
  109. end
  110. context 'when the requested status does not exist' do
  111. let(:id) { -1 }
  112. it 'returns http not found' do
  113. subject
  114. expect(response).to have_http_status(404)
  115. end
  116. end
  117. end
  118. end