Add custom filter tests of kmyblue original params

This commit is contained in:
KMY 2023-08-11 10:16:56 +09:00
parent 5e687f4d93
commit 18583e5275
5 changed files with 129 additions and 5 deletions

View file

@ -0,0 +1,75 @@
# frozen_string_literal: true
require 'rails_helper'
describe Api::V1::Timelines::PublicController do
render_views
let!(:account) { Fabricate(:account) }
let!(:user) { Fabricate(:user, account: account) }
let!(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read') }
before do
allow(controller).to receive(:doorkeeper_token) { token }
end
describe 'GET #show' do
subject do
get :show
body_as_json
end
let!(:local_account) { Fabricate(:account, domain: nil) }
let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
let!(:local_status) { Fabricate(:status, account: local_account, text: 'ohagi is good') }
let!(:remote_status) { Fabricate(:status, account: remote_account, text: 'ohagi is ohagi') }
it 'load statuses', :aggregate_failures do
json = subject
expect(response).to have_http_status(200)
expect(json).to be_an Array
expect(json.any? { |status| status[:id] == local_status.id.to_s }).to be true
expect(json.any? { |status| status[:id] == remote_status.id.to_s }).to be true
end
context 'with filter' do
subject do
Fabricate(:follow, account: account, target_account: remote_account)
get :show
body_as_json.filter { |status| status[:filtered].empty? || status[:filtered][0][:filter][:id] != filter.id.to_s }.map { |status| status[:id].to_i }
end
let(:exclude_follows) { false }
let(:exclude_localusers) { false }
let!(:filter) { Fabricate(:custom_filter, account: account, exclude_follows: exclude_follows, exclude_localusers: exclude_localusers) }
let!(:filter_keyword) { Fabricate(:custom_filter_keyword, custom_filter: filter, keyword: 'ohagi') } # rubocop:disable RSpec/LetSetup
it 'load statuses', :aggregate_failures do
ids = subject
expect(ids).to exclude(local_status.id)
expect(ids).to exclude(remote_status.id)
end
context 'when exclude_followers' do
let(:exclude_follows) { true }
it 'load statuses', :aggregate_failures do
ids = subject
expect(ids).to exclude(local_status.id)
expect(ids).to include(remote_status.id)
end
end
context 'when exclude_localusers' do
let(:exclude_localusers) { true }
it 'load statuses', :aggregate_failures do
ids = subject
expect(ids).to include(local_status.id)
expect(ids).to exclude(remote_status.id)
end
end
end
end
end

View file

@ -5,4 +5,6 @@ Fabricator(:custom_filter) do
expires_at nil expires_at nil
phrase 'discourse' phrase 'discourse'
context %w(home notifications) context %w(home notifications)
exclude_follows false
exclude_localusers false
end end

View file

@ -178,6 +178,26 @@ RSpec.describe Status do
expect(subject.compute_searchability).to eq 'public' expect(subject.compute_searchability).to eq 'public'
end end
end end
context 'when direct-public of local account' do
let(:account_searchability) { :direct }
let(:account_domain) { nil }
let(:status_searchability) { :public }
it 'returns public' do
expect(subject.compute_searchability).to eq 'public'
end
end
context 'when limited-public of local account' do
let(:account_searchability) { :limited }
let(:account_domain) { nil }
let(:status_searchability) { :public }
it 'returns public' do
expect(subject.compute_searchability).to eq 'public'
end
end
end end
describe '#content' do describe '#content' do

View file

@ -47,7 +47,16 @@ RSpec.describe 'Filters' do
it_behaves_like 'unauthorized for invalid token' it_behaves_like 'unauthorized for invalid token'
context 'with valid params' do context 'with valid params' do
let(:params) { { title: 'magic', context: %w(home), filter_action: 'hide', keywords_attributes: [keyword: 'magic', whole_word: true] } } let(:params) do
{
title: 'magic',
context: %w(home),
filter_action: 'hide',
exclude_follows: true,
exclude_localusers: true,
keywords_attributes: [keyword: 'magic', whole_word: true],
}
end
it 'returns http success' do it 'returns http success' do
subject subject
@ -64,6 +73,8 @@ RSpec.describe 'Filters' do
expect(json[:filter_action]).to eq 'hide' expect(json[:filter_action]).to eq 'hide'
expect(json[:context]).to eq ['home'] expect(json[:context]).to eq ['home']
expect(json[:keywords].map { |keyword| keyword.slice(:keyword, :whole_word) }).to eq [{ keyword: 'magic', whole_word: true }] expect(json[:keywords].map { |keyword| keyword.slice(:keyword, :whole_word) }).to eq [{ keyword: 'magic', whole_word: true }]
expect(json[:exclude_follows]).to be true
expect(json[:exclude_localusers]).to be true
end end
it 'creates a filter', :aggregate_failures do it 'creates a filter', :aggregate_failures do
@ -74,13 +85,15 @@ RSpec.describe 'Filters' do
expect(filter).to be_present expect(filter).to be_present
expect(filter.keywords.pluck(:keyword)).to eq ['magic'] expect(filter.keywords.pluck(:keyword)).to eq ['magic']
expect(filter.context).to eq %w(home) expect(filter.context).to eq %w(home)
expect(filter.exclude_follows).to be true
expect(filter.exclude_localusers).to be true
expect(filter.irreversible?).to be true expect(filter.irreversible?).to be true
expect(filter.expires_at).to be_nil expect(filter.expires_at).to be_nil
end end
end end
context 'when the required title param is missing' do context 'when the required title param is missing' do
let(:params) { { context: %w(home), filter_action: 'hide', keywords_attributes: [keyword: 'magic'] } } let(:params) { { context: %w(home), filter_action: 'hide', keywords_attributes: [keyword: 'magic'], exclude_follows: false, exclude_localusers: false } }
it 'returns http unprocessable entity' do it 'returns http unprocessable entity' do
subject subject
@ -90,7 +103,7 @@ RSpec.describe 'Filters' do
end end
context 'when the required context param is missing' do context 'when the required context param is missing' do
let(:params) { { title: 'magic', filter_action: 'hide', keywords_attributes: [keyword: 'magic'] } } let(:params) { { title: 'magic', filter_action: 'hide', keywords_attributes: [keyword: 'magic'], exclude_follows: false, exclude_localusers: false } }
it 'returns http unprocessable entity' do it 'returns http unprocessable entity' do
subject subject
@ -99,8 +112,18 @@ RSpec.describe 'Filters' do
end end
end end
context 'when the required kmyblue original params are missing' do
let(:params) { { title: 'magic', context: %w(home), filter_action: 'hide', keywords_attributes: [keyword: 'magic'] } }
it 'returns http success' do
subject
expect(response).to have_http_status(200)
end
end
context 'when the given context value is invalid' do context 'when the given context value is invalid' do
let(:params) { { title: 'magic', context: %w(shaolin), filter_action: 'hide', keywords_attributes: [keyword: 'magic'] } } let(:params) { { title: 'magic', context: %w(shaolin), filter_action: 'hide', keywords_attributes: [keyword: 'magic'], exclude_follows: false, exclude_localusers: false } }
it 'returns http unprocessable entity' do it 'returns http unprocessable entity' do
subject subject
@ -152,7 +175,7 @@ RSpec.describe 'Filters' do
context 'when updating filter parameters' do context 'when updating filter parameters' do
context 'with valid params' do context 'with valid params' do
let(:params) { { title: 'updated', context: %w(home public) } } let(:params) { { title: 'updated', context: %w(home public), exclude_follows: true, exclude_localusers: true } }
it 'updates the filter successfully', :aggregate_failures do it 'updates the filter successfully', :aggregate_failures do
subject subject
@ -162,6 +185,8 @@ RSpec.describe 'Filters' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(filter.title).to eq 'updated' expect(filter.title).to eq 'updated'
expect(filter.reload.context).to eq %w(home public) expect(filter.reload.context).to eq %w(home public)
expect(filter.exclude_follows).to be true
expect(filter.exclude_localusers).to be true
end end
end end

View file

@ -2,6 +2,8 @@
require 'rspec/retry' require 'rspec/retry'
RSpec::Matchers.define_negated_matcher :exclude, :include
if ENV['DISABLE_SIMPLECOV'] != 'true' if ENV['DISABLE_SIMPLECOV'] != 'true'
require 'simplecov' require 'simplecov'
SimpleCov.start 'rails' do SimpleCov.start 'rails' do