123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- # frozen_string_literal: true
- require 'rails_helper'
- RSpec.describe 'Notifications' do
- let(:user) { Fabricate(:user, account_attributes: { username: 'alice' }) }
- let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
- let(:scopes) { 'read:notifications write:notifications' }
- let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
- describe 'GET /api/v1/notifications/unread_count', :inline_jobs do
- subject do
- get '/api/v1/notifications/unread_count', headers: headers, params: params
- end
- let(:params) { {} }
- before do
- first_status = PostStatusService.new.call(user.account, text: 'Test')
- ReblogService.new.call(Fabricate(:account), first_status)
- PostStatusService.new.call(Fabricate(:account), text: 'Hello @alice')
- FavouriteService.new.call(Fabricate(:account), first_status)
- FavouriteService.new.call(Fabricate(:account), first_status)
- FollowService.new.call(Fabricate(:account), user.account)
- end
- it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
- context 'with no options' do
- it 'returns expected notifications count' do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body[:count]).to eq 5
- end
- end
- context 'with a read marker' do
- before do
- id = user.account.notifications.browserable.order(id: :desc).offset(2).first.id
- user.markers.create!(timeline: 'notifications', last_read_id: id)
- end
- it 'returns expected notifications count' do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body[:count]).to eq 2
- end
- end
- context 'with exclude_types param' do
- let(:params) { { exclude_types: %w(mention) } }
- it 'returns expected notifications count' do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body[:count]).to eq 4
- end
- end
- context 'with a user-provided limit' do
- let(:params) { { limit: 2 } }
- it 'returns a capped value' do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body[:count]).to eq 2
- end
- end
- context 'when there are more notifications than the limit' do
- before do
- stub_const('Api::V1::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT', 2)
- end
- it 'returns a capped value' do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body[:count]).to eq Api::V1::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT
- end
- end
- end
- describe 'GET /api/v1/notifications', :inline_jobs do
- subject do
- get '/api/v1/notifications', headers: headers, params: params
- end
- let(:bob) { Fabricate(:user) }
- let(:tom) { Fabricate(:user) }
- let(:params) { {} }
- before do
- first_status = PostStatusService.new.call(user.account, text: 'Test')
- ReblogService.new.call(bob.account, first_status)
- PostStatusService.new.call(bob.account, text: 'Hello @alice')
- PostStatusService.new.call(tom.account, text: 'Hello @alice', visibility: :direct) # Filtered by default
- FavouriteService.new.call(bob.account, first_status)
- FavouriteService.new.call(tom.account, first_status)
- FollowService.new.call(bob.account, user.account)
- end
- it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
- context 'with no options' do
- it 'returns expected notification types', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body.size).to eq 5
- expect(body_json_types).to include('reblog', 'mention', 'favourite', 'follow')
- expect(response.parsed_body.any? { |x| x[:filtered] }).to be false
- end
- end
- context 'with include_filtered' do
- let(:params) { { include_filtered: true } }
- it 'returns expected notification types, including filtered notifications' do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body.size).to eq 6
- expect(body_json_types).to include('reblog', 'mention', 'favourite', 'follow')
- expect(response.parsed_body.any? { |x| x[:filtered] }).to be true
- end
- end
- context 'with account_id param' do
- let(:params) { { account_id: tom.account.id } }
- it 'returns only notifications from specified user', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(body_json_account_ids.uniq).to eq [tom.account.id.to_s]
- end
- def body_json_account_ids
- response.parsed_body.map { |x| x[:account][:id] }
- end
- end
- context 'with invalid account_id param' do
- let(:params) { { account_id: 'foo' } }
- it 'returns nothing', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body.size).to eq 0
- end
- end
- context 'with exclude_types param' do
- let(:params) { { exclude_types: %w(mention) } }
- it 'returns everything but excluded type', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(response.parsed_body.size).to_not eq 0
- expect(body_json_types.uniq).to_not include 'mention'
- end
- end
- context 'with types param' do
- let(:params) { { types: %w(mention) } }
- it 'returns only requested type', :aggregate_failures do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect(body_json_types.uniq).to eq ['mention']
- end
- end
- context 'with limit param' do
- let(:params) { { limit: 3 } }
- it 'returns the requested number of notifications paginated', :aggregate_failures do
- subject
- notifications = user.account.notifications.browserable.order(id: :asc)
- expect(response.parsed_body.size)
- .to eq(params[:limit])
- expect(response)
- .to include_pagination_headers(
- prev: api_v1_notifications_url(limit: params[:limit], min_id: notifications.last.id),
- next: api_v1_notifications_url(limit: params[:limit], max_id: notifications[2].id)
- )
- end
- end
- def body_json_types
- response.parsed_body.pluck(:type)
- end
- end
- describe 'GET /api/v1/notifications/:id' do
- subject do
- get "/api/v1/notifications/#{notification.id}", headers: headers
- end
- let(:notification) { Fabricate(:notification, account: user.account) }
- it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
- it 'returns http success' do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- end
- context 'when notification belongs to someone else' do
- let(:notification) { Fabricate(:notification) }
- it 'returns http not found' do
- subject
- expect(response).to have_http_status(404)
- expect(response.content_type)
- .to start_with('application/json')
- end
- end
- end
- describe 'POST /api/v1/notifications/:id/dismiss' do
- subject do
- post "/api/v1/notifications/#{notification.id}/dismiss", headers: headers
- end
- let!(:notification) { Fabricate(:notification, account: user.account) }
- it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
- it 'destroys the notification' do
- subject
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
- end
- context 'when notification belongs to someone else' do
- let(:notification) { Fabricate(:notification) }
- it 'returns http not found' do
- subject
- expect(response).to have_http_status(404)
- expect(response.content_type)
- .to start_with('application/json')
- end
- end
- end
- describe 'POST /api/v1/notifications/clear' do
- subject do
- post '/api/v1/notifications/clear', headers: headers
- end
- before do
- Fabricate(:notification, account: user.account)
- end
- it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
- it 'clears notifications for the account' do
- subject
- expect(user.account.reload.notifications).to be_empty
- expect(response).to have_http_status(200)
- expect(response.content_type)
- .to start_with('application/json')
- end
- end
- end
|