policies_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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', :inline_jobs 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(response.content_type)
  22. .to start_with('application/json')
  23. expect(response.parsed_body).to include(
  24. filter_not_following: false,
  25. filter_not_followers: false,
  26. filter_new_accounts: false,
  27. filter_private_mentions: true,
  28. summary: a_hash_including(
  29. pending_requests_count: 1,
  30. pending_notifications_count: 0
  31. )
  32. )
  33. end
  34. end
  35. end
  36. describe 'PUT /api/v1/notifications/policy' do
  37. subject do
  38. put '/api/v1/notifications/policy', headers: headers, params: params
  39. end
  40. let(:params) { { filter_not_following: true } }
  41. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  42. it 'changes notification policy and returns an updated json object', :aggregate_failures do
  43. expect { subject }
  44. .to change { NotificationPolicy.find_or_initialize_by(account: user.account).for_not_following.to_sym }.from(:accept).to(:filter)
  45. expect(response).to have_http_status(200)
  46. expect(response.content_type)
  47. .to start_with('application/json')
  48. expect(response.parsed_body).to include(
  49. filter_not_following: true,
  50. filter_not_followers: false,
  51. filter_new_accounts: false,
  52. filter_private_mentions: true,
  53. summary: a_hash_including(
  54. pending_requests_count: 0,
  55. pending_notifications_count: 0
  56. )
  57. )
  58. end
  59. end
  60. end