policies_spec.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Policies' do
  4. let(:user) { Fabricate(:user, account_attributes: { username: 'alice' }) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'read:notifications write:notifications' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/notifications/policy', :sidekiq_inline do
  9. subject do
  10. get '/api/v1/notifications/policy', headers: headers, params: params
  11. end
  12. let(:params) { {} }
  13. before do
  14. Fabricate(:notification_request, account: user.account)
  15. end
  16. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  17. context 'with no options' do
  18. it 'returns json with expected attributes', :aggregate_failures do
  19. subject
  20. expect(response).to have_http_status(200)
  21. expect(body_as_json).to include(
  22. filter_not_following: false,
  23. filter_not_followers: false,
  24. filter_new_accounts: false,
  25. filter_private_mentions: true,
  26. summary: a_hash_including(
  27. pending_requests_count: 1,
  28. pending_notifications_count: 0
  29. )
  30. )
  31. end
  32. end
  33. end
  34. describe 'PUT /api/v1/notifications/policy' do
  35. subject do
  36. put '/api/v1/notifications/policy', headers: headers, params: params
  37. end
  38. let(:params) { { filter_not_following: true } }
  39. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  40. it 'changes notification policy and returns an updated json object', :aggregate_failures do
  41. expect { subject }
  42. .to change { NotificationPolicy.find_or_initialize_by(account: user.account).filter_not_following }.from(false).to(true)
  43. expect(response).to have_http_status(200)
  44. expect(body_as_json).to include(
  45. filter_not_following: true,
  46. filter_not_followers: false,
  47. filter_new_accounts: false,
  48. filter_private_mentions: true,
  49. summary: a_hash_including(
  50. pending_requests_count: 0,
  51. pending_notifications_count: 0
  52. )
  53. )
  54. end
  55. end
  56. end