Merge commit '0dac31dfd588e4cd866f382ed001a9535f06234a' into glitch-soc/merge-upstream

This commit is contained in:
Claire committed 2025-12-04 20:59:54 +01:00
commit c8f365fd1d
79 files changed
+843 -227

No files matched your search

+22
View File
@@ -176,6 +176,28 @@ RSpec.describe StatusCacheHydrator do
end
end
context 'when the quoted post has a poll authored by the user' do
let(:poll) { Fabricate(:poll, account: account) }
let(:quoted_status) { Fabricate(:status, poll: poll, account: account) }
it 'renders the same attributes as a full render' do
expect(subject).to eql(compare_to_hash)
end
end
context 'when the quoted post has been voted in' do
let(:poll) { Fabricate(:poll, options: %w(Yellow Blue)) }
let(:quoted_status) { Fabricate(:status, poll: poll) }
before do
VoteService.new.call(account, poll, [0])
end
it 'renders the same attributes as a full render' do
expect(subject).to eql(compare_to_hash)
end
end
context 'when the quoted post matches account filters' do
let(:quoted_status) { Fabricate(:status, text: 'this toot is about that banned word') }
+54
View File
@@ -72,4 +72,58 @@ RSpec.describe Collection do
end
end
end
describe '#tag_name=' do
context 'when the collection is new and has no tag' do
subject { Fabricate.build(:collection) }
context 'when the tag exists' do
let!(:tag) { Fabricate.create(:tag, name: 'people') }
it 'correctly assigns the existing tag' do
subject.tag_name = '#people'
expect(subject.tag).to eq tag
end
end
context 'when the tag does not exist' do
it 'creates and assigns a new tag' do
expect do
subject.tag_name = '#people'
end.to change(Tag, :count).by(1)
expect(subject.tag).to be_present
expect(subject.tag.name).to eq 'people'
end
end
end
context 'when the collection is persisted and has a tag' do
subject { Fabricate(:collection, tag:) }
let!(:tag) { Fabricate(:tag, name: 'people') }
context 'when the new tag is the same' do
it 'does not change the object' do
subject.tag_name = '#People'
expect(subject.tag).to eq tag
expect(subject).to_not be_changed
end
end
context 'when the new tag is different' do
it 'creates and assigns a new tag' do
expect do
subject.tag_name = '#bots'
end.to change(Tag, :count).by(1)
expect(subject.tag).to be_present
expect(subject.tag.name).to eq 'bots'
expect(subject).to be_changed
end
end
end
end
end
+27 -16
View File
@@ -57,6 +57,11 @@ RSpec.describe Tag do
end
end
describe 'Normalizations' do
it { is_expected.to normalize(:display_name).from('#HelloWorld').to('HelloWorld') }
it { is_expected.to normalize(:display_name).from('Hello❤️World').to('HelloWorld') }
end
describe 'HASHTAG_RE' do
subject { described_class::HASHTAG_RE }
@@ -251,17 +256,29 @@ RSpec.describe Tag do
end
describe '.find_or_create_by_names' do
let(:upcase_string) { 'abcABCabcABCやゆよ' }
let(:downcase_string) { 'abcabcabcabcやゆよ' }
context 'when called with a block' do
let(:upcase_string) { 'abcABCabcABCやゆよ' }
let(:downcase_string) { 'abcabcabcabcやゆよ' }
let(:args) { [upcase_string, downcase_string] }
it 'runs a passed block once per tag regardless of duplicates' do
count = 0
described_class.find_or_create_by_names([upcase_string, downcase_string]) do |_tag|
count += 1
it 'runs the block once per normalized tag regardless of duplicates' do
expect { |block| described_class.find_or_create_by_names(args, &block) }
.to yield_control.once
end
end
expect(count).to eq 1
context 'when passed an array' do
it 'creates multiples tags' do
expect { described_class.find_or_create_by_names(%w(tips tags toes)) }
.to change(described_class, :count).by(3)
end
end
context 'when passed a string' do
it 'creates a single tag' do
expect { described_class.find_or_create_by_names('test') }
.to change(described_class, :count).by(1)
end
end
end
@@ -270,16 +287,10 @@ RSpec.describe Tag do
tag_name_upper = 'Rails'
tag_name_lower = 'rails'
threads = []
2.times do |i|
threads << Thread.new do
described_class.find_or_create_by_names(i.zero? ? tag_name_upper : tag_name_lower)
end
multi_threaded_execution(2) do |index|
described_class.find_or_create_by_names(index.zero? ? tag_name_upper : tag_name_lower)
end
threads.each(&:join)
tags = described_class.where('lower(name) = ?', tag_name_lower.downcase)
expect(tags.count).to eq(1)
expect(tags.first.name.downcase).to eq(tag_name_lower.downcase)
+43
View File
@@ -0,0 +1,43 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe CollectionPolicy do
let(:policy) { described_class }
let(:collection) { Fabricate(:collection) }
let(:owner) { collection.account }
let(:other_user) { Fabricate(:account) }
permissions :index? do
it 'permits everyone to index' do
expect(policy).to permit(nil, Collection)
expect(policy).to permit(owner, Collection)
end
end
permissions :show? do
it 'permits everyone to show' do
expect(policy).to permit(nil, collection)
expect(policy).to permit(owner, collection)
expect(policy).to permit(other_user, collection)
end
end
permissions :create? do
it 'permits any user' do
expect(policy).to_not permit(nil, Collection)
expect(policy).to permit(owner, Collection)
expect(policy).to permit(other_user, Collection)
end
end
permissions :update?, :destroy? do
it 'only permits the owner' do
expect(policy).to_not permit(nil, collection)
expect(policy).to_not permit(other_user, collection)
expect(policy).to permit(owner, collection)
end
end
end
@@ -95,4 +95,103 @@ RSpec.describe 'Api::V1Alpha::Collections', feature: :collections do
end
end
end
describe 'PATCH /api/v1_alpha/collections/:id' do
subject do
patch "/api/v1_alpha/collections/#{collection.id}", headers: headers, params: params
end
let(:collection) { Fabricate(:collection) }
let(:params) { {} }
context 'when user is not owner' do
it 'returns http forbidden' do
subject
expect(response).to have_http_status(403)
end
end
context 'when user is the owner' do
let(:collection) do
Fabricate(:collection,
account: user.account,
name: 'Pople to follow',
description: 'Cool pople',
sensitive: true,
discoverable: false)
end
it_behaves_like 'forbidden for wrong scope', 'read:collections'
context 'with valid params' do
let(:params) do
{
name: 'People to follow',
description: 'Cool people',
sensitive: '0',
discoverable: '1',
}
end
it 'updates the collection and returns http success' do
subject
collection.reload
expect(response).to have_http_status(200)
expect(collection.name).to eq 'People to follow'
expect(collection.description).to eq 'Cool people'
expect(collection.sensitive).to be false
expect(collection.discoverable).to be true
end
end
context 'with invalid params' do
let(:params) { { name: '' } }
it 'returns http unprocessable content and detailed errors' do
subject
expect(response).to have_http_status(422)
expect(response.parsed_body).to include({
'error' => a_hash_including({
'details' => a_hash_including({
'name' => [{ 'error' => 'ERR_BLANK', 'description' => "can't be blank" }],
}),
}),
})
end
end
end
end
describe 'DELETE /api/v1_alpha/collections/:id' do
subject do
delete "/api/v1_alpha/collections/#{collection.id}", headers: headers
end
let(:collection) { Fabricate(:collection) }
context 'when user is not owner' do
it 'returns http forbidden' do
subject
expect(response).to have_http_status(403)
end
end
context 'when user is the owner' do
let(:collection) { Fabricate(:collection, account: user.account) }
it_behaves_like 'forbidden for wrong scope', 'read:collections'
it 'deletes the collection and returns http success' do
collection
expect { subject }.to change(Collection, :count).by(-1)
expect(response).to have_http_status(200)
end
end
end
end
@@ -45,7 +45,7 @@ RSpec.describe CreateCollectionService do
end
context 'when given a tag' do
let(:params) { base_params.merge(tag: '#people') }
let(:params) { base_params.merge(tag_name: '#people') }
context 'when the tag exists' do
let!(:tag) { Fabricate.create(:tag, name: 'people') }
+2 -2
View File
@@ -6,10 +6,10 @@ module ThreadingHelpers
def multi_threaded_execution(thread_count)
barrier = Concurrent::CyclicBarrier.new(thread_count)
threads = Array.new(thread_count) do
threads = Array.new(thread_count) do |index|
Thread.new do
barrier.wait
yield
yield(index)
end
end