domain_allows_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Domain Allows' do
  4. let(:role) { UserRole.find_by(name: 'Admin') }
  5. let(:user) { Fabricate(:user, role: role) }
  6. let(:scopes) { 'admin:read admin:write' }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  8. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  9. describe 'GET /api/v1/admin/domain_allows' do
  10. subject do
  11. get '/api/v1/admin/domain_allows', headers: headers, params: params
  12. end
  13. let(:params) { {} }
  14. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  15. it_behaves_like 'forbidden for wrong role', ''
  16. it_behaves_like 'forbidden for wrong role', 'Moderator'
  17. context 'when there is no allowed domains' do
  18. it 'returns an empty body' do
  19. subject
  20. expect(response)
  21. .to have_http_status(200)
  22. expect(response.content_type)
  23. .to start_with('application/json')
  24. expect(response.parsed_body).to be_empty
  25. end
  26. end
  27. context 'when there are allowed domains' do
  28. let!(:domain_allows) { Fabricate.times(2, :domain_allow) }
  29. let(:expected_response) do
  30. domain_allows.map do |domain_allow|
  31. {
  32. id: domain_allow.id.to_s,
  33. domain: domain_allow.domain,
  34. created_at: domain_allow.created_at.strftime('%Y-%m-%dT%H:%M:%S.%LZ'),
  35. }
  36. end
  37. end
  38. it 'returns the correct allowed domains' do
  39. subject
  40. expect(response)
  41. .to have_http_status(200)
  42. expect(response.content_type)
  43. .to start_with('application/json')
  44. expect(response.parsed_body)
  45. .to match_array(expected_response)
  46. end
  47. context 'with limit param' do
  48. let(:params) { { limit: 1 } }
  49. it 'returns only the requested number of allowed domains' do
  50. subject
  51. expect(response.parsed_body.size).to eq(params[:limit])
  52. end
  53. end
  54. end
  55. end
  56. describe 'GET /api/v1/admin/domain_allows/:id' do
  57. subject do
  58. get "/api/v1/admin/domain_allows/#{domain_allow.id}", headers: headers
  59. end
  60. let!(:domain_allow) { Fabricate(:domain_allow) }
  61. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  62. it_behaves_like 'forbidden for wrong role', ''
  63. it_behaves_like 'forbidden for wrong role', 'Moderator'
  64. it 'returns the expected allowed domain name', :aggregate_failures do
  65. subject
  66. expect(response).to have_http_status(200)
  67. expect(response.content_type)
  68. .to start_with('application/json')
  69. expect(response.parsed_body[:domain]).to eq domain_allow.domain
  70. end
  71. context 'when the requested allowed domain does not exist' do
  72. it 'returns http not found' do
  73. get '/api/v1/admin/domain_allows/-1', headers: headers
  74. expect(response).to have_http_status(404)
  75. expect(response.content_type)
  76. .to start_with('application/json')
  77. end
  78. end
  79. end
  80. describe 'POST /api/v1/admin/domain_allows' do
  81. subject do
  82. post '/api/v1/admin/domain_allows', headers: headers, params: params
  83. end
  84. let(:params) { { domain: 'foo.bar.com' } }
  85. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  86. it_behaves_like 'forbidden for wrong role', ''
  87. it_behaves_like 'forbidden for wrong role', 'Moderator'
  88. context 'with a valid domain name' do
  89. it 'returns the expected domain name', :aggregate_failures do
  90. subject
  91. expect(response).to have_http_status(200)
  92. expect(response.content_type)
  93. .to start_with('application/json')
  94. expect(response.parsed_body[:domain]).to eq 'foo.bar.com'
  95. expect(DomainAllow.find_by(domain: 'foo.bar.com')).to be_present
  96. end
  97. end
  98. context 'with invalid domain name' do
  99. let(:params) { { domain: 'foo bar' } }
  100. it 'returns http unprocessable entity' do
  101. subject
  102. expect(response).to have_http_status(422)
  103. expect(response.content_type)
  104. .to start_with('application/json')
  105. end
  106. end
  107. context 'when domain name is not specified' do
  108. let(:params) { {} }
  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 the domain is already allowed' do
  117. before do
  118. DomainAllow.create(params)
  119. end
  120. it 'returns the existing allowed domain name' do
  121. subject
  122. expect(response.parsed_body[:domain]).to eq(params[:domain])
  123. end
  124. end
  125. end
  126. describe 'DELETE /api/v1/admin/domain_allows/:id' do
  127. subject do
  128. delete "/api/v1/admin/domain_allows/#{domain_allow.id}", headers: headers
  129. end
  130. let!(:domain_allow) { Fabricate(:domain_allow) }
  131. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  132. it_behaves_like 'forbidden for wrong role', ''
  133. it_behaves_like 'forbidden for wrong role', 'Moderator'
  134. it 'deletes the allowed domain', :aggregate_failures do
  135. subject
  136. expect(response).to have_http_status(200)
  137. expect(response.content_type)
  138. .to start_with('application/json')
  139. expect(DomainAllow.find_by(id: domain_allow.id)).to be_nil
  140. end
  141. context 'when the allowed domain does not exist' do
  142. it 'returns http not found' do
  143. delete '/api/v1/admin/domain_allows/-1', headers: headers
  144. expect(response).to have_http_status(404)
  145. expect(response.content_type)
  146. .to start_with('application/json')
  147. end
  148. end
  149. end
  150. end