requests_spec.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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', :inline_jobs 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. end
  16. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  17. context 'with no options' do
  18. it 'returns http success', :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. end
  24. end
  25. end
  26. describe 'POST /api/v1/notifications/requests/:id/accept' do
  27. subject do
  28. post "/api/v1/notifications/requests/#{notification_request.id}/accept", headers: headers
  29. end
  30. let(:notification_request) { Fabricate(:notification_request, account: user.account) }
  31. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  32. it 'returns http success and creates notification permission' do
  33. subject
  34. expect(response).to have_http_status(200)
  35. expect(response.content_type)
  36. .to start_with('application/json')
  37. expect(NotificationPermission.find_by(account: notification_request.account, from_account: notification_request.from_account)).to_not be_nil
  38. end
  39. context 'when notification request belongs to someone else' do
  40. let(:notification_request) { Fabricate(:notification_request) }
  41. it 'returns http not found' do
  42. subject
  43. expect(response).to have_http_status(404)
  44. expect(response.content_type)
  45. .to start_with('application/json')
  46. end
  47. end
  48. end
  49. describe 'POST /api/v1/notifications/requests/:id/dismiss' do
  50. subject do
  51. post "/api/v1/notifications/requests/#{notification_request.id}/dismiss", headers: headers
  52. end
  53. let!(:notification_request) { Fabricate(:notification_request, account: user.account) }
  54. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  55. it 'returns http success and destroys the notification request', :aggregate_failures do
  56. expect { subject }.to change(NotificationRequest, :count).by(-1)
  57. expect(response).to have_http_status(200)
  58. expect(response.content_type)
  59. .to start_with('application/json')
  60. end
  61. context 'when notification request belongs to someone else' do
  62. let(:notification_request) { Fabricate(:notification_request) }
  63. it 'returns http not found' do
  64. subject
  65. expect(response).to have_http_status(404)
  66. expect(response.content_type)
  67. .to start_with('application/json')
  68. end
  69. end
  70. end
  71. describe 'POST /api/v1/notifications/requests/accept' do
  72. subject do
  73. post '/api/v1/notifications/requests/accept', params: { id: [notification_request.id] }, headers: headers
  74. end
  75. let!(:notification_request) { Fabricate(:notification_request, account: user.account) }
  76. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  77. it 'returns http success and creates notification permission', :aggregate_failures do
  78. subject
  79. expect(NotificationPermission.find_by(account: notification_request.account, from_account: notification_request.from_account)).to_not be_nil
  80. expect(response).to have_http_status(200)
  81. expect(response.content_type)
  82. .to start_with('application/json')
  83. end
  84. end
  85. describe 'POST /api/v1/notifications/requests/dismiss' do
  86. subject do
  87. post '/api/v1/notifications/requests/dismiss', params: { id: [notification_request.id] }, headers: headers
  88. end
  89. let!(:notification_request) { Fabricate(:notification_request, account: user.account) }
  90. it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
  91. it 'returns http success and destroys the notification request', :aggregate_failures do
  92. expect { subject }.to change(NotificationRequest, :count).by(-1)
  93. expect(response).to have_http_status(200)
  94. expect(response.content_type)
  95. .to start_with('application/json')
  96. end
  97. end
  98. describe 'GET /api/v1/notifications/requests/merged' do
  99. subject do
  100. get '/api/v1/notifications/requests/merged', headers: headers
  101. end
  102. it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
  103. context 'when the user has no accepted request pending merge' do
  104. it 'returns http success and returns merged: true' do
  105. subject
  106. expect(response).to have_http_status(200)
  107. expect(response.content_type)
  108. .to start_with('application/json')
  109. expect(response.parsed_body).to match({ merged: true })
  110. end
  111. end
  112. context 'when the user has an accepted request pending merge' do
  113. before do
  114. redis.set("notification_unfilter_jobs:#{user.account_id}", 1)
  115. end
  116. it 'returns http success and returns merged: false' do
  117. subject
  118. expect(response).to have_http_status(200)
  119. expect(response.content_type)
  120. .to start_with('application/json')
  121. expect(response.parsed_body).to match({ merged: false })
  122. end
  123. end
  124. end
  125. end