diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/fabricators/user_fabricator.rb | 3 | ||||
-rw-r--r-- | spec/helpers/admin/trends/statuses_helper_spec.rb | 54 | ||||
-rw-r--r-- | spec/helpers/home_helper_spec.rb | 112 | ||||
-rw-r--r-- | spec/mailers/admin_mailer_spec.rb | 62 | ||||
-rw-r--r-- | spec/models/account_filter_spec.rb | 26 | ||||
-rw-r--r-- | spec/models/poll_vote_spec.rb | 49 | ||||
-rw-r--r-- | spec/services/search_service_spec.rb | 2 | ||||
-rw-r--r-- | spec/spec_helper.rb | 4 | ||||
-rw-r--r-- | spec/workers/scheduler/follow_recommendations_scheduler_spec.rb | 43 |
9 files changed, 352 insertions, 3 deletions
diff --git a/spec/fabricators/user_fabricator.rb b/spec/fabricators/user_fabricator.rb index 967347319..9031d5cd0 100644 --- a/spec/fabricators/user_fabricator.rb +++ b/spec/fabricators/user_fabricator.rb @@ -5,5 +5,6 @@ Fabricator(:user) do email { sequence(:email) { |i| "#{i}#{Faker::Internet.email}" } } password '123456789' confirmed_at { Time.zone.now } - agreement true + current_sign_in_at { Time.zone.now } + agreement true end diff --git a/spec/helpers/admin/trends/statuses_helper_spec.rb b/spec/helpers/admin/trends/statuses_helper_spec.rb new file mode 100644 index 000000000..92caae690 --- /dev/null +++ b/spec/helpers/admin/trends/statuses_helper_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Admin::Trends::StatusesHelper do + describe '.one_line_preview' do + before do + allow(helper).to receive(:current_user).and_return(Fabricate.build(:user)) + end + + context 'with a local status' do + let(:status) { Fabricate.build(:status, text: 'Test local status') } + + it 'renders a correct preview text' do + result = helper.one_line_preview(status) + + expect(result).to eq 'Test local status' + end + end + + context 'with a remote status' do + let(:status) { Fabricate.build(:status, uri: 'https://sfd.sdf', text: '<html><body><p>Test remote status</p><p>text</p></body></html>') } + + it 'renders a correct preview text' do + result = helper.one_line_preview(status) + + expect(result).to eq 'Test remote status' + end + end + + context 'with a status that has empty text' do + let(:status) { Fabricate.build(:status, text: '') } + + it 'renders a correct preview text' do + result = helper.one_line_preview(status) + + expect(result).to eq '' + end + end + + context 'with a status that has emoji' do + before { Fabricate(:custom_emoji, shortcode: 'florpy') } + + let(:status) { Fabricate(:status, text: 'hello there :florpy:') } + + it 'renders a correct preview text' do + result = helper.one_line_preview(status) + + expect(result).to match 'hello there' + expect(result).to match '<img rel="emoji"' + end + end + end +end diff --git a/spec/helpers/home_helper_spec.rb b/spec/helpers/home_helper_spec.rb index 77db327c2..3d2c5fe24 100644 --- a/spec/helpers/home_helper_spec.rb +++ b/spec/helpers/home_helper_spec.rb @@ -8,4 +8,116 @@ RSpec.describe HomeHelper, type: :helper do expect(helper.default_props).to eq locale: I18n.locale end end + + describe 'account_link_to' do + context 'with a missing account' do + let(:account) { nil } + + it 'returns a button' do + result = helper.account_link_to(account) + + expect(result).to match t('about.contact_missing') + end + end + + context 'with a valid account' do + let(:account) { Fabricate(:account) } + + it 'returns a link to the account' do + without_partial_double_verification do + allow(helper).to receive(:current_account).and_return(account) + allow(helper).to receive(:prefers_autoplay?).and_return(false) + result = helper.account_link_to(account) + + expect(result).to match "@#{account.acct}" + end + end + end + end + + describe 'obscured_counter' do + context 'with a value of less than zero' do + let(:count) { -10 } + + it 'returns the correct string' do + expect(helper.obscured_counter(count)).to eq '0' + end + end + + context 'with a value of zero' do + let(:count) { 0 } + + it 'returns the correct string' do + expect(helper.obscured_counter(count)).to eq '0' + end + end + + context 'with a value of one' do + let(:count) { 1 } + + it 'returns the correct string' do + expect(helper.obscured_counter(count)).to eq '1' + end + end + + context 'with a value of more than one' do + let(:count) { 10 } + + it 'returns the correct string' do + expect(helper.obscured_counter(count)).to eq '1+' + end + end + end + + describe 'custom_field_classes' do + context 'with a verified field' do + let(:field) { instance_double(Account::Field, verified?: true) } + + it 'returns verified string' do + result = helper.custom_field_classes(field) + expect(result).to eq 'verified' + end + end + + context 'with a non-verified field' do + let(:field) { instance_double(Account::Field, verified?: false) } + + it 'returns verified string' do + result = helper.custom_field_classes(field) + expect(result).to eq 'emojify' + end + end + end + + describe 'sign_up_messages' do + context 'with closed registrations' do + it 'returns correct sign up message' do + allow(helper).to receive(:closed_registrations?).and_return(true) + result = helper.sign_up_message + + expect(result).to eq t('auth.registration_closed', instance: 'cb6e6126.ngrok.io') + end + end + + context 'with open registrations' do + it 'returns correct sign up message' do + allow(helper).to receive(:closed_registrations?).and_return(false) + allow(helper).to receive(:open_registrations?).and_return(true) + result = helper.sign_up_message + + expect(result).to eq t('auth.register') + end + end + + context 'with approved registrations' do + it 'returns correct sign up message' do + allow(helper).to receive(:closed_registrations?).and_return(false) + allow(helper).to receive(:open_registrations?).and_return(false) + allow(helper).to receive(:approved_registrations?).and_return(true) + result = helper.sign_up_message + + expect(result).to eq t('auth.apply_for_account') + end + end + end end diff --git a/spec/mailers/admin_mailer_spec.rb b/spec/mailers/admin_mailer_spec.rb index 29fb586a3..132c6c758 100644 --- a/spec/mailers/admin_mailer_spec.rb +++ b/spec/mailers/admin_mailer_spec.rb @@ -23,4 +23,66 @@ RSpec.describe AdminMailer, type: :mailer do expect(mail.body.encoded).to eq("Mike,\r\n\r\nJohn has reported Mike\r\n\r\nView: https://cb6e6126.ngrok.io/admin/reports/#{report.id}\r\n") end end + + describe '.new_appeal' do + let(:appeal) { Fabricate(:appeal) } + let(:recipient) { Fabricate(:account, username: 'Kurt') } + let(:mail) { described_class.new_appeal(recipient, appeal) } + + before do + recipient.user.update(locale: :en) + end + + it 'renders the headers' do + expect(mail.subject).to eq("#{appeal.account.username} is appealing a moderation decision on cb6e6126.ngrok.io") + expect(mail.to).to eq [recipient.user_email] + expect(mail.from).to eq ['notifications@localhost'] + end + + it 'renders the body' do + expect(mail.body.encoded).to match "#{appeal.account.username} is appealing a moderation decision by #{appeal.strike.account.username}" + end + end + + describe '.new_pending_account' do + let(:recipient) { Fabricate(:account, username: 'Barklums') } + let(:user) { Fabricate(:user) } + let(:mail) { described_class.new_pending_account(recipient, user) } + + before do + recipient.user.update(locale: :en) + end + + it 'renders the headers' do + expect(mail.subject).to eq("New account up for review on cb6e6126.ngrok.io (#{user.account.username})") + expect(mail.to).to eq [recipient.user_email] + expect(mail.from).to eq ['notifications@localhost'] + end + + it 'renders the body' do + expect(mail.body.encoded).to match 'The details of the new account are below. You can approve or reject this application.' + end + end + + describe '.new_trends' do + let(:recipient) { Fabricate(:account, username: 'Snurf') } + let(:links) { [] } + let(:statuses) { [] } + let(:tags) { [] } + let(:mail) { described_class.new_trends(recipient, links, tags, statuses) } + + before do + recipient.user.update(locale: :en) + end + + it 'renders the headers' do + expect(mail.subject).to eq('New trends up for review on cb6e6126.ngrok.io') + expect(mail.to).to eq [recipient.user_email] + expect(mail.from).to eq ['notifications@localhost'] + end + + it 'renders the body' do + expect(mail.body.encoded).to match 'The following items need a review before they can be displayed publicly' + end + end end diff --git a/spec/models/account_filter_spec.rb b/spec/models/account_filter_spec.rb index 853d20a0c..3032260fe 100644 --- a/spec/models/account_filter_spec.rb +++ b/spec/models/account_filter_spec.rb @@ -18,4 +18,30 @@ describe AccountFilter do expect { filter.results }.to raise_error(/wrong/) end end + + describe 'with origin and by_domain interacting' do + let!(:local_account) { Fabricate(:account, domain: nil) } + let!(:remote_account_one) { Fabricate(:account, domain: 'example.org') } + let(:remote_account_two) { Fabricate(:account, domain: 'other.domain') } + + it 'works with domain first and origin remote' do + filter = described_class.new(by_domain: 'example.org', origin: 'remote') + expect(filter.results).to match_array [remote_account_one] + end + + it 'works with domain last and origin remote' do + filter = described_class.new(origin: 'remote', by_domain: 'example.org') + expect(filter.results).to match_array [remote_account_one] + end + + it 'works with domain first and origin local' do + filter = described_class.new(by_domain: 'example.org', origin: 'local') + expect(filter.results).to match_array [local_account] + end + + it 'works with domain last and origin local' do + filter = described_class.new(origin: 'local', by_domain: 'example.org') + expect(filter.results).to match_array [remote_account_one] + end + end end diff --git a/spec/models/poll_vote_spec.rb b/spec/models/poll_vote_spec.rb index 563f34699..6886a82aa 100644 --- a/spec/models/poll_vote_spec.rb +++ b/spec/models/poll_vote_spec.rb @@ -10,4 +10,53 @@ RSpec.describe PollVote, type: :model do expect(poll_vote.object_type).to eq :vote end end + + describe 'validations' do + context 'with a vote on an expired poll' do + it 'marks the vote invalid' do + poll = Fabricate.build(:poll, expires_at: 30.days.ago) + + vote = Fabricate.build(:poll_vote, poll: poll) + expect(vote).to_not be_valid + end + end + + context 'with invalid choices' do + it 'marks vote invalid with negative choice' do + poll = Fabricate.build(:poll) + + vote = Fabricate.build(:poll_vote, poll: poll, choice: -100) + expect(vote).to_not be_valid + end + + it 'marks vote invalid with choice in excess of options' do + poll = Fabricate.build(:poll, options: %w(a b c)) + + vote = Fabricate.build(:poll_vote, poll: poll, choice: 10) + expect(vote).to_not be_valid + end + end + + context 'with a poll where multiple is true' do + it 'does not allow a second vote on same choice from same account' do + poll = Fabricate(:poll, multiple: true, options: %w(a b c)) + first_vote = Fabricate(:poll_vote, poll: poll, choice: 1) + expect(first_vote).to be_valid + + second_vote = Fabricate.build(:poll_vote, account: first_vote.account, poll: poll, choice: 1) + expect(second_vote).to_not be_valid + end + end + + context 'with a poll where multiple is false' do + it 'does not allow a second vote from same account' do + poll = Fabricate(:poll, multiple: false, options: %w(a b c)) + first_vote = Fabricate(:poll_vote, poll: poll) + expect(first_vote).to be_valid + + second_vote = Fabricate.build(:poll_vote, account: first_vote.account, poll: poll) + expect(second_vote).to_not be_valid + end + end + end end diff --git a/spec/services/search_service_spec.rb b/spec/services/search_service_spec.rb index 7ec334a56..1ad0efe0a 100644 --- a/spec/services/search_service_spec.rb +++ b/spec/services/search_service_spec.rb @@ -77,7 +77,7 @@ describe SearchService, type: :service do it 'includes the tag in the results' do query = '#tag' tag = Tag.new - allow(Tag).to receive(:search_for).with('tag', 10, 0, exclude_unreviewed: nil).and_return([tag]) + allow(Tag).to receive(:search_for).with('tag', 10, 0, { exclude_unreviewed: nil }).and_return([tag]) results = subject.call(query, nil, 10) expect(Tag).to have_received(:search_for).with('tag', 10, 0, exclude_unreviewed: nil) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ddc872fc8..25f314002 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,8 +5,10 @@ GC.disable if ENV['DISABLE_SIMPLECOV'] != 'true' require 'simplecov' SimpleCov.start 'rails' do - add_group 'Services', 'app/services' + add_group 'Policies', 'app/policies' add_group 'Presenters', 'app/presenters' + add_group 'Serializers', 'app/serializers' + add_group 'Services', 'app/services' add_group 'Validators', 'app/validators' end end diff --git a/spec/workers/scheduler/follow_recommendations_scheduler_spec.rb b/spec/workers/scheduler/follow_recommendations_scheduler_spec.rb new file mode 100644 index 000000000..18d5260e4 --- /dev/null +++ b/spec/workers/scheduler/follow_recommendations_scheduler_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Scheduler::FollowRecommendationsScheduler do + let!(:target_accounts) do + Fabricate.times(3, :account) do + statuses(count: 6) + end + end + let!(:follower_accounts) do + Fabricate.times(5, :account) do + statuses(count: 6) + end + end + + describe '#perform' do + subject(:scheduled_run) { described_class.new.perform } + + context 'when there are accounts to recommend' do + before do + # Follow the target accounts by follow accounts to make them recommendable + follower_accounts.each do |follower_account| + target_accounts.each do |target_account| + Fabricate(:follow, account: follower_account, target_account: target_account) + end + end + end + + it 'creates recommendations' do + expect { scheduled_run }.to change(FollowRecommendation, :count).from(0).to(target_accounts.size) + expect(redis.zrange('follow_recommendations:en', 0, -1)).to match_array(target_accounts.pluck(:id).map(&:to_s)) + end + end + + context 'when there are no accounts to recommend' do + it 'does not create follow recommendations' do + expect { scheduled_run }.to_not change(FollowRecommendation, :count) + expect(redis.zrange('follow_recommendations:en', 0, -1)).to be_empty + end + end + end +end |