domain_allows_controller_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. require 'rails_helper'
  2. RSpec.describe Api::V1::Admin::DomainAllowsController, type: :controller do
  3. render_views
  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. before do
  9. allow(controller).to receive(:doorkeeper_token) { token }
  10. end
  11. shared_examples 'forbidden for wrong scope' do |wrong_scope|
  12. let(:scopes) { wrong_scope }
  13. it 'returns http forbidden' do
  14. expect(response).to have_http_status(403)
  15. end
  16. end
  17. shared_examples 'forbidden for wrong role' do |wrong_role|
  18. let(:role) { UserRole.find_by(name: wrong_role) }
  19. it 'returns http forbidden' do
  20. expect(response).to have_http_status(403)
  21. end
  22. end
  23. describe 'GET #index' do
  24. let!(:domain_allow) { Fabricate(:domain_allow) }
  25. before do
  26. get :index
  27. end
  28. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  29. it_behaves_like 'forbidden for wrong role', ''
  30. it_behaves_like 'forbidden for wrong role', 'Moderator'
  31. it 'returns http success' do
  32. expect(response).to have_http_status(200)
  33. end
  34. it 'returns the expected domain allows' do
  35. json = body_as_json
  36. expect(json.length).to eq 1
  37. expect(json[0][:id].to_i).to eq domain_allow.id
  38. end
  39. end
  40. describe 'GET #show' do
  41. let!(:domain_allow) { Fabricate(:domain_allow) }
  42. before do
  43. get :show, params: { id: domain_allow.id }
  44. end
  45. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  46. it_behaves_like 'forbidden for wrong role', ''
  47. it_behaves_like 'forbidden for wrong role', 'Moderator'
  48. it 'returns http success' do
  49. expect(response).to have_http_status(200)
  50. end
  51. it 'returns expected domain name' do
  52. json = body_as_json
  53. expect(json[:domain]).to eq domain_allow.domain
  54. end
  55. end
  56. describe 'DELETE #destroy' do
  57. let!(:domain_allow) { Fabricate(:domain_allow) }
  58. before do
  59. delete :destroy, params: { id: domain_allow.id }
  60. end
  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 http success' do
  65. expect(response).to have_http_status(200)
  66. end
  67. it 'deletes the block' do
  68. expect(DomainAllow.find_by(id: domain_allow.id)).to be_nil
  69. end
  70. end
  71. describe 'POST #create' do
  72. let!(:domain_allow) { Fabricate(:domain_allow, domain: 'example.com') }
  73. context do
  74. before do
  75. post :create, params: { domain: 'foo.bar.com' }
  76. end
  77. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  78. it_behaves_like 'forbidden for wrong role', ''
  79. it_behaves_like 'forbidden for wrong role', 'Moderator'
  80. it 'returns http success' do
  81. expect(response).to have_http_status(200)
  82. end
  83. it 'returns expected domain name' do
  84. json = body_as_json
  85. expect(json[:domain]).to eq 'foo.bar.com'
  86. end
  87. it 'creates a domain block' do
  88. expect(DomainAllow.find_by(domain: 'foo.bar.com')).to_not be_nil
  89. end
  90. end
  91. context 'with invalid domain name' do
  92. before do
  93. post :create, params: { domain: 'foo bar' }
  94. end
  95. it 'returns http unprocessable entity' do
  96. expect(response).to have_http_status(422)
  97. end
  98. end
  99. end
  100. end