domain_blocks_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Domain blocks' do
  4. let(:user) { Fabricate(:user) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'read:blocks write:blocks' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/domain_blocks' do
  9. subject do
  10. get '/api/v1/domain_blocks', headers: headers, params: params
  11. end
  12. let(:blocked_domains) { ['example.com', 'example.net', 'example.org', 'example.com.br'] }
  13. let(:params) { {} }
  14. before do
  15. blocked_domains.each { |domain| user.account.block_domain!(domain) }
  16. end
  17. it_behaves_like 'forbidden for wrong scope', 'write:blocks'
  18. it 'returns the domains blocked by the requesting user', :aggregate_failures do
  19. subject
  20. expect(response).to have_http_status(200)
  21. expect(response.content_type)
  22. .to start_with('application/json')
  23. expect(response.parsed_body).to match_array(blocked_domains)
  24. end
  25. context 'with limit param' do
  26. let(:params) { { limit: 2 } }
  27. it 'returns only the requested number of blocked domains' do
  28. subject
  29. expect(response.parsed_body.size).to eq(params[:limit])
  30. end
  31. end
  32. end
  33. describe 'POST /api/v1/domain_blocks' do
  34. subject do
  35. post '/api/v1/domain_blocks', headers: headers, params: params
  36. end
  37. let(:params) { { domain: 'example.com' } }
  38. it_behaves_like 'forbidden for wrong scope', 'read read:blocks'
  39. it 'creates a domain block', :aggregate_failures do
  40. subject
  41. expect(response).to have_http_status(200)
  42. expect(response.content_type)
  43. .to start_with('application/json')
  44. expect(user.account.domain_blocking?(params[:domain])).to be(true)
  45. end
  46. context 'when no domain name is given' do
  47. let(:params) { { domain: '' } }
  48. it 'returns http unprocessable entity' do
  49. subject
  50. expect(response).to have_http_status(422)
  51. expect(response.content_type)
  52. .to start_with('application/json')
  53. end
  54. end
  55. context 'when the given domain name is invalid' do
  56. let(:params) { { domain: 'example com' } }
  57. it 'returns unprocessable entity' do
  58. subject
  59. expect(response).to have_http_status(422)
  60. expect(response.content_type)
  61. .to start_with('application/json')
  62. end
  63. end
  64. end
  65. describe 'DELETE /api/v1/domain_blocks' do
  66. subject do
  67. delete '/api/v1/domain_blocks/', headers: headers, params: params
  68. end
  69. let(:params) { { domain: 'example.com' } }
  70. before do
  71. user.account.block_domain!('example.com')
  72. end
  73. it_behaves_like 'forbidden for wrong scope', 'read read:blocks'
  74. it 'deletes the specified domain block', :aggregate_failures do
  75. subject
  76. expect(response).to have_http_status(200)
  77. expect(response.content_type)
  78. .to start_with('application/json')
  79. expect(user.account.domain_blocking?('example.com')).to be(false)
  80. end
  81. context 'when the given domain name is not blocked' do
  82. let(:params) { { domain: 'example.org' } }
  83. it 'returns http success' do
  84. subject
  85. expect(response).to have_http_status(200)
  86. expect(response.content_type)
  87. .to start_with('application/json')
  88. end
  89. end
  90. end
  91. end