Migrate to request specs in /api/v1/notifications (#25553)

This commit is contained in:
Daniel M Brasil 2023-10-16 12:15:24 -03:00 committed by GitHub
parent 08a376cbcb
commit e0ed0f8c7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 183 additions and 137 deletions

View File

@ -1,137 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Api::V1::NotificationsController do
render_views
let(:user) { Fabricate(:user, account_attributes: { username: 'alice' }) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:other) { Fabricate(:user) }
let(:third) { Fabricate(:user) }
before do
allow(controller).to receive(:doorkeeper_token) { token }
end
describe 'GET #show' do
let(:scopes) { 'read:notifications' }
it 'returns http success' do
notification = Fabricate(:notification, account: user.account)
get :show, params: { id: notification.id }
expect(response).to have_http_status(200)
end
end
describe 'POST #dismiss' do
let(:scopes) { 'write:notifications' }
it 'destroys the notification' do
notification = Fabricate(:notification, account: user.account)
post :dismiss, params: { id: notification.id }
expect(response).to have_http_status(200)
expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
describe 'POST #clear' do
let(:scopes) { 'write:notifications' }
it 'clears notifications for the account' do
notification = Fabricate(:notification, account: user.account)
post :clear
expect(notification.account.reload.notifications).to be_empty
expect(response).to have_http_status(200)
end
end
describe 'GET #index' do
let(:scopes) { 'read:notifications' }
before do
first_status = PostStatusService.new.call(user.account, text: 'Test')
@reblog_of_first_status = ReblogService.new.call(other.account, first_status)
mentioning_status = PostStatusService.new.call(other.account, text: 'Hello @alice')
@mention_from_status = mentioning_status.mentions.first
@favourite = FavouriteService.new.call(other.account, first_status)
@second_favourite = FavouriteService.new.call(third.account, first_status)
@follow = FollowService.new.call(other.account, user.account)
end
describe 'with no options' do
before do
get :index
end
it 'returns expected notification types', :aggregate_failures do
expect(response).to have_http_status(200)
expect(body_json_types).to include 'reblog'
expect(body_json_types).to include 'mention'
expect(body_json_types).to include 'favourite'
expect(body_json_types).to include 'follow'
end
end
describe 'with account_id param' do
before do
get :index, params: { account_id: third.account.id }
end
it 'returns only notifications from specified user', :aggregate_failures do
expect(response).to have_http_status(200)
expect(body_json_account_ids.uniq).to eq [third.account.id.to_s]
end
def body_json_account_ids
body_as_json.map { |x| x[:account][:id] }
end
end
describe 'with invalid account_id param' do
before do
get :index, params: { account_id: 'foo' }
end
it 'returns nothing', :aggregate_failures do
expect(response).to have_http_status(200)
expect(body_as_json.size).to eq 0
end
end
describe 'with exclude_types param' do
before do
get :index, params: { exclude_types: %w(mention) }
end
it 'returns everything but excluded type', :aggregate_failures do
expect(response).to have_http_status(200)
expect(body_as_json.size).to_not eq 0
expect(body_json_types.uniq).to_not include 'mention'
end
end
describe 'with types param' do
before do
get :index, params: { types: %w(mention) }
end
it 'returns only requested type', :aggregate_failures do
expect(response).to have_http_status(200)
expect(body_json_types.uniq).to eq ['mention']
end
end
def body_json_types
body_as_json.pluck(:type)
end
end
end

View File

@ -0,0 +1,183 @@
# 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' 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)
mentioning_status = PostStatusService.new.call(bob.account, text: 'Hello @alice')
mentioning_status.mentions.first
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(body_json_types).to include 'reblog'
expect(body_json_types).to include 'mention'
expect(body_json_types).to include 'favourite'
expect(body_json_types).to include 'follow'
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(body_json_account_ids.uniq).to eq [tom.account.id.to_s]
end
def body_json_account_ids
body_as_json.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(body_as_json.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(body_as_json.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(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
expect(body_as_json.size).to eq(params[:limit])
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_notifications_url(limit: params[:limit], min_id: notifications.last.id.to_s))
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_notifications_url(limit: params[:limit], max_id: notifications[2].id.to_s))
end
end
def body_json_types
body_as_json.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)
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)
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 { 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)
end
end
end
describe 'POST /api/v1/notifications/clear' do
subject do
post '/api/v1/notifications/clear', headers: headers
end
before do
Fabricate.times(3, :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)
end
end
end