email_domain_blocks_spec.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Email Domain Blocks' do
  4. let(:role) { UserRole.find_by(name: 'Admin') }
  5. let(:user) { Fabricate(:user, role: role) }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. let(:account) { Fabricate(:account) }
  8. let(:scopes) { 'admin:read:email_domain_blocks admin:write:email_domain_blocks' }
  9. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  10. describe 'GET /api/v1/admin/email_domain_blocks' do
  11. subject do
  12. get '/api/v1/admin/email_domain_blocks', headers: headers, params: params
  13. end
  14. let(:params) { {} }
  15. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  16. it_behaves_like 'forbidden for wrong role', ''
  17. it_behaves_like 'forbidden for wrong role', 'Moderator'
  18. it 'returns http success' do
  19. subject
  20. expect(response).to have_http_status(200)
  21. end
  22. context 'when there is no email domain block' do
  23. it 'returns an empty list' do
  24. subject
  25. expect(body_as_json).to be_empty
  26. end
  27. end
  28. context 'when there are email domain blocks' do
  29. let!(:email_domain_blocks) { Fabricate.times(5, :email_domain_block) }
  30. let(:blocked_email_domains) { email_domain_blocks.pluck(:domain) }
  31. it 'return the correct blocked email domains' do
  32. subject
  33. expect(body_as_json.pluck(:domain)).to match_array(blocked_email_domains)
  34. end
  35. context 'with limit param' do
  36. let(:params) { { limit: 2 } }
  37. it 'returns only the requested number of email domain blocks' do
  38. subject
  39. expect(body_as_json.size).to eq(params[:limit])
  40. end
  41. end
  42. context 'with since_id param' do
  43. let(:params) { { since_id: email_domain_blocks[1].id } }
  44. it 'returns only the email domain blocks after since_id' do
  45. subject
  46. email_domain_blocks_ids = email_domain_blocks.pluck(:id).map(&:to_s)
  47. expect(body_as_json.pluck(:id)).to match_array(email_domain_blocks_ids[2..])
  48. end
  49. end
  50. context 'with max_id param' do
  51. let(:params) { { max_id: email_domain_blocks[3].id } }
  52. it 'returns only the email domain blocks before max_id' do
  53. subject
  54. email_domain_blocks_ids = email_domain_blocks.pluck(:id).map(&:to_s)
  55. expect(body_as_json.pluck(:id)).to match_array(email_domain_blocks_ids[..2])
  56. end
  57. end
  58. end
  59. end
  60. describe 'GET /api/v1/admin/email_domain_blocks/:id' do
  61. subject do
  62. get "/api/v1/admin/email_domain_blocks/#{email_domain_block.id}", headers: headers
  63. end
  64. let!(:email_domain_block) { Fabricate(:email_domain_block) }
  65. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  66. it_behaves_like 'forbidden for wrong role', ''
  67. it_behaves_like 'forbidden for wrong role', 'Moderator'
  68. context 'when email domain block exists' do
  69. it 'returns the correct blocked domain', :aggregate_failures do
  70. subject
  71. expect(response).to have_http_status(200)
  72. expect(body_as_json[:domain]).to eq(email_domain_block.domain)
  73. end
  74. end
  75. context 'when email domain block does not exist' do
  76. it 'returns http not found' do
  77. get '/api/v1/admin/email_domain_blocks/-1', headers: headers
  78. expect(response).to have_http_status(404)
  79. end
  80. end
  81. end
  82. describe 'POST /api/v1/admin/email_domain_blocks' do
  83. subject do
  84. post '/api/v1/admin/email_domain_blocks', headers: headers, params: params
  85. end
  86. let(:params) { { domain: 'example.com' } }
  87. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  88. it_behaves_like 'forbidden for wrong role', ''
  89. it_behaves_like 'forbidden for wrong role', 'Moderator'
  90. it 'returns the correct blocked email domain', :aggregate_failures do
  91. subject
  92. expect(response).to have_http_status(200)
  93. expect(body_as_json[:domain]).to eq(params[:domain])
  94. end
  95. context 'when domain param is not provided' do
  96. let(:params) { { domain: '' } }
  97. it 'returns http unprocessable entity' do
  98. subject
  99. expect(response).to have_http_status(422)
  100. end
  101. end
  102. context 'when provided domain name has an invalid character' do
  103. let(:params) { { domain: 'do\uD800.com' } }
  104. it 'returns http unprocessable entity' do
  105. subject
  106. expect(response).to have_http_status(422)
  107. end
  108. end
  109. context 'when provided domain is already blocked' do
  110. before do
  111. EmailDomainBlock.create(params)
  112. end
  113. it 'returns http unprocessable entity' do
  114. subject
  115. expect(response).to have_http_status(422)
  116. end
  117. end
  118. end
  119. describe 'DELETE /api/v1/admin/email_domain_blocks' do
  120. subject do
  121. delete "/api/v1/admin/email_domain_blocks/#{email_domain_block.id}", headers: headers
  122. end
  123. let!(:email_domain_block) { Fabricate(:email_domain_block) }
  124. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  125. it_behaves_like 'forbidden for wrong role', ''
  126. it_behaves_like 'forbidden for wrong role', 'Moderator'
  127. it 'deletes email domain block', :aggregate_failures do
  128. subject
  129. expect(response).to have_http_status(200)
  130. expect(body_as_json).to be_empty
  131. expect(EmailDomainBlock.find_by(id: email_domain_block.id)).to be_nil
  132. end
  133. context 'when email domain block does not exist' do
  134. it 'returns http not found' do
  135. delete '/api/v1/admin/email_domain_blocks/-1', headers: headers
  136. expect(response).to have_http_status(404)
  137. end
  138. end
  139. end
  140. end