keywords_controller_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::V2::Filters::KeywordsController, type: :controller do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. let(:filter) { Fabricate(:custom_filter, account: user.account) }
  8. let(:other_user) { Fabricate(:user) }
  9. let(:other_filter) { Fabricate(:custom_filter, account: other_user.account) }
  10. before do
  11. allow(controller).to receive(:doorkeeper_token) { token }
  12. end
  13. describe 'GET #index' do
  14. let(:scopes) { 'read:filters' }
  15. let!(:keyword) { Fabricate(:custom_filter_keyword, custom_filter: filter) }
  16. it 'returns http success' do
  17. get :index, params: { filter_id: filter.id }
  18. expect(response).to have_http_status(200)
  19. end
  20. context "when trying to access another's user filters" do
  21. it 'returns http not found' do
  22. get :index, params: { filter_id: other_filter.id }
  23. expect(response).to have_http_status(404)
  24. end
  25. end
  26. end
  27. describe 'POST #create' do
  28. let(:scopes) { 'write:filters' }
  29. let(:filter_id) { filter.id }
  30. before do
  31. post :create, params: { filter_id: filter_id, keyword: 'magic', whole_word: false }
  32. end
  33. it 'returns http success' do
  34. expect(response).to have_http_status(200)
  35. end
  36. it 'returns a keyword' do
  37. json = body_as_json
  38. expect(json[:keyword]).to eq 'magic'
  39. expect(json[:whole_word]).to be false
  40. end
  41. it 'creates a keyword' do
  42. filter = user.account.custom_filters.first
  43. expect(filter).to_not be_nil
  44. expect(filter.keywords.pluck(:keyword)).to eq ['magic']
  45. end
  46. context "when trying to add to another another's user filters" do
  47. let(:filter_id) { other_filter.id }
  48. it 'returns http not found' do
  49. expect(response).to have_http_status(404)
  50. end
  51. end
  52. end
  53. describe 'GET #show' do
  54. let(:scopes) { 'read:filters' }
  55. let(:keyword) { Fabricate(:custom_filter_keyword, keyword: 'foo', whole_word: false, custom_filter: filter) }
  56. before do
  57. get :show, params: { id: keyword.id }
  58. end
  59. it 'returns http success' do
  60. expect(response).to have_http_status(200)
  61. end
  62. it 'returns expected data' do
  63. json = body_as_json
  64. expect(json[:keyword]).to eq 'foo'
  65. expect(json[:whole_word]).to be false
  66. end
  67. context "when trying to access another user's filter keyword" do
  68. let(:keyword) { Fabricate(:custom_filter_keyword, custom_filter: other_filter) }
  69. it 'returns http not found' do
  70. expect(response).to have_http_status(404)
  71. end
  72. end
  73. end
  74. describe 'PUT #update' do
  75. let(:scopes) { 'write:filters' }
  76. let(:keyword) { Fabricate(:custom_filter_keyword, custom_filter: filter) }
  77. before do
  78. get :update, params: { id: keyword.id, keyword: 'updated' }
  79. end
  80. it 'returns http success' do
  81. expect(response).to have_http_status(200)
  82. end
  83. it 'updates the keyword' do
  84. expect(keyword.reload.keyword).to eq 'updated'
  85. end
  86. context "when trying to update another user's filter keyword" do
  87. let(:keyword) { Fabricate(:custom_filter_keyword, custom_filter: other_filter) }
  88. it 'returns http not found' do
  89. expect(response).to have_http_status(404)
  90. end
  91. end
  92. end
  93. describe 'DELETE #destroy' do
  94. let(:scopes) { 'write:filters' }
  95. let(:keyword) { Fabricate(:custom_filter_keyword, custom_filter: filter) }
  96. before do
  97. delete :destroy, params: { id: keyword.id }
  98. end
  99. it 'returns http success' do
  100. expect(response).to have_http_status(200)
  101. end
  102. it 'removes the filter' do
  103. expect { keyword.reload }.to raise_error ActiveRecord::RecordNotFound
  104. end
  105. context "when trying to update another user's filter keyword" do
  106. let(:keyword) { Fabricate(:custom_filter_keyword, custom_filter: other_filter) }
  107. it 'returns http not found' do
  108. expect(response).to have_http_status(404)
  109. end
  110. end
  111. end
  112. end