email_domain_blocks_spec.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. context 'when there is no email domain block' do
  19. it 'returns an empty list' do
  20. subject
  21. expect(response)
  22. .to have_http_status(200)
  23. expect(response.content_type)
  24. .to start_with('application/json')
  25. expect(response.parsed_body)
  26. .to be_empty
  27. end
  28. end
  29. context 'when there are email domain blocks' do
  30. let!(:email_domain_blocks) { Fabricate.times(5, :email_domain_block) }
  31. let(:blocked_email_domains) { email_domain_blocks.pluck(:domain) }
  32. it 'return the correct blocked email domains' do
  33. subject
  34. expect(response)
  35. .to have_http_status(200)
  36. expect(response.content_type)
  37. .to start_with('application/json')
  38. expect(response.parsed_body.pluck(:domain))
  39. .to match_array(blocked_email_domains)
  40. end
  41. context 'with limit param' do
  42. let(:params) { { limit: 2 } }
  43. it 'returns only the requested number of email domain blocks' do
  44. subject
  45. expect(response.parsed_body.size).to eq(params[:limit])
  46. end
  47. end
  48. context 'with since_id param' do
  49. let(:params) { { since_id: email_domain_blocks[1].id } }
  50. it 'returns only the email domain blocks after since_id' do
  51. subject
  52. email_domain_blocks_ids = email_domain_blocks.pluck(:id).map(&:to_s)
  53. expect(response.parsed_body.pluck(:id)).to match_array(email_domain_blocks_ids[2..])
  54. end
  55. end
  56. context 'with max_id param' do
  57. let(:params) { { max_id: email_domain_blocks[3].id } }
  58. it 'returns only the email domain blocks before max_id' do
  59. subject
  60. email_domain_blocks_ids = email_domain_blocks.pluck(:id).map(&:to_s)
  61. expect(response.parsed_body.pluck(:id)).to match_array(email_domain_blocks_ids[..2])
  62. end
  63. end
  64. end
  65. end
  66. describe 'GET /api/v1/admin/email_domain_blocks/:id' do
  67. subject do
  68. get "/api/v1/admin/email_domain_blocks/#{email_domain_block.id}", headers: headers
  69. end
  70. let!(:email_domain_block) { Fabricate(:email_domain_block) }
  71. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  72. it_behaves_like 'forbidden for wrong role', ''
  73. it_behaves_like 'forbidden for wrong role', 'Moderator'
  74. context 'when email domain block exists' do
  75. it 'returns the correct blocked domain', :aggregate_failures do
  76. subject
  77. expect(response).to have_http_status(200)
  78. expect(response.content_type)
  79. .to start_with('application/json')
  80. expect(response.parsed_body[:domain]).to eq(email_domain_block.domain)
  81. end
  82. end
  83. context 'when email domain block does not exist' do
  84. it 'returns http not found' do
  85. get '/api/v1/admin/email_domain_blocks/-1', headers: headers
  86. expect(response).to have_http_status(404)
  87. expect(response.content_type)
  88. .to start_with('application/json')
  89. end
  90. end
  91. end
  92. describe 'POST /api/v1/admin/email_domain_blocks' do
  93. subject do
  94. post '/api/v1/admin/email_domain_blocks', headers: headers, params: params
  95. end
  96. let(:params) { { domain: 'example.com' } }
  97. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  98. it_behaves_like 'forbidden for wrong role', ''
  99. it_behaves_like 'forbidden for wrong role', 'Moderator'
  100. it 'returns the correct blocked email domain', :aggregate_failures do
  101. subject
  102. expect(response).to have_http_status(200)
  103. expect(response.content_type)
  104. .to start_with('application/json')
  105. expect(response.parsed_body[:domain]).to eq(params[:domain])
  106. end
  107. context 'when domain param is not provided' do
  108. let(:params) { { domain: '' } }
  109. it 'returns http unprocessable entity' do
  110. subject
  111. expect(response).to have_http_status(422)
  112. expect(response.content_type)
  113. .to start_with('application/json')
  114. end
  115. end
  116. context 'when provided domain name has an invalid character' do
  117. let(:params) { { domain: 'do\uD800.com' } }
  118. it 'returns http unprocessable entity' do
  119. subject
  120. expect(response).to have_http_status(422)
  121. expect(response.content_type)
  122. .to start_with('application/json')
  123. end
  124. end
  125. context 'when provided domain is already blocked' do
  126. before do
  127. EmailDomainBlock.create(params)
  128. end
  129. it 'returns http unprocessable entity' do
  130. subject
  131. expect(response).to have_http_status(422)
  132. expect(response.content_type)
  133. .to start_with('application/json')
  134. end
  135. end
  136. end
  137. describe 'DELETE /api/v1/admin/email_domain_blocks' do
  138. subject do
  139. delete "/api/v1/admin/email_domain_blocks/#{email_domain_block.id}", headers: headers
  140. end
  141. let!(:email_domain_block) { Fabricate(:email_domain_block) }
  142. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  143. it_behaves_like 'forbidden for wrong role', ''
  144. it_behaves_like 'forbidden for wrong role', 'Moderator'
  145. it 'deletes email domain block', :aggregate_failures do
  146. subject
  147. expect(response).to have_http_status(200)
  148. expect(response.content_type)
  149. .to start_with('application/json')
  150. expect(response.parsed_body).to be_empty
  151. expect(EmailDomainBlock.find_by(id: email_domain_block.id)).to be_nil
  152. end
  153. context 'when email domain block does not exist' do
  154. it 'returns http not found' do
  155. delete '/api/v1/admin/email_domain_blocks/-1', headers: headers
  156. expect(response).to have_http_status(404)
  157. expect(response.content_type)
  158. .to start_with('application/json')
  159. end
  160. end
  161. end
  162. end