requests_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Requests' 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/requests', :sidekiq_inline do
  9. subject do
  10. get '/api/v1/notifications/requests', headers: headers, params: params
  11. end
  12. let(:params) { {} }
  13. before do
  14. Fabricate(:notification_request, account: user.account)
  15. Fabricate(:notification_request, account: user.account, dismissed: true)
  16. end
  17. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  18. context 'with no options' do
  19. it 'returns http success', :aggregate_failures do
  20. subject
  21. expect(response).to have_http_status(200)
  22. end
  23. end
  24. context 'with dismissed' do
  25. let(:params) { { dismissed: '1' } }
  26. it 'returns http success', :aggregate_failures do
  27. subject
  28. expect(response).to have_http_status(200)
  29. end
  30. end
  31. end
  32. describe 'POST /api/v1/notifications/requests/:id/accept' do
  33. subject do
  34. post "/api/v1/notifications/requests/#{notification_request.id}/accept", headers: headers
  35. end
  36. let(:notification_request) { Fabricate(:notification_request, account: user.account) }
  37. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  38. it 'returns http success' do
  39. subject
  40. expect(response).to have_http_status(200)
  41. end
  42. it 'creates notification permission' do
  43. subject
  44. expect(NotificationPermission.find_by(account: notification_request.account, from_account: notification_request.from_account)).to_not be_nil
  45. end
  46. context 'when notification request belongs to someone else' do
  47. let(:notification_request) { Fabricate(:notification_request) }
  48. it 'returns http not found' do
  49. subject
  50. expect(response).to have_http_status(404)
  51. end
  52. end
  53. end
  54. describe 'POST /api/v1/notifications/requests/:id/dismiss' do
  55. subject do
  56. post "/api/v1/notifications/requests/#{notification_request.id}/dismiss", headers: headers
  57. end
  58. let(:notification_request) { Fabricate(:notification_request, account: user.account) }
  59. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  60. it 'returns http success and dismisses the notification request', :aggregate_failures do
  61. subject
  62. expect(response).to have_http_status(200)
  63. expect(notification_request.reload.dismissed?).to be true
  64. end
  65. context 'when notification request belongs to someone else' do
  66. let(:notification_request) { Fabricate(:notification_request) }
  67. it 'returns http not found' do
  68. subject
  69. expect(response).to have_http_status(404)
  70. end
  71. end
  72. end
  73. end