diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/api/web/embeds_controller_spec.rb | 52 | ||||
-rw-r--r-- | spec/controllers/settings/migrations_controller_spec.rb | 79 | ||||
-rw-r--r-- | spec/controllers/settings/sessions_controller_spec.rb | 30 | ||||
-rw-r--r-- | spec/lib/status_finder_spec.rb | 7 | ||||
-rw-r--r-- | spec/models/account_spec.rb | 31 | ||||
-rw-r--r-- | spec/models/glitch/keyword_mute_spec.rb | 8 | ||||
-rw-r--r-- | spec/models/status_spec.rb | 14 |
7 files changed, 215 insertions, 6 deletions
diff --git a/spec/controllers/api/web/embeds_controller_spec.rb b/spec/controllers/api/web/embeds_controller_spec.rb new file mode 100644 index 000000000..6b7297189 --- /dev/null +++ b/spec/controllers/api/web/embeds_controller_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Api::Web::EmbedsController do + render_views + + let(:user) { Fabricate(:user) } + before { sign_in user } + + describe 'POST #create' do + subject(:response) { post :create, params: { url: url } } + subject(:body) { JSON.parse(response.body, symbolize_names: true) } + + context 'when successfully finds status' do + let(:status) { Fabricate(:status) } + let(:url) { "http://#{ Rails.configuration.x.web_domain }/@#{status.account.username}/#{status.id}" } + + it 'returns a right response' do + expect(response).to have_http_status :ok + expect(body[:author_name]).to eq status.account.username + end + end + + context 'when fails to find status' do + let(:url) { 'https://host.test/oembed.html' } + let(:service_instance) { double('fetch_oembed_service') } + + before do + allow(FetchOEmbedService).to receive(:new) { service_instance } + allow(service_instance).to receive(:call) { call_result } + end + + context 'when successfully fetching oembed' do + let(:call_result) { { result: :ok } } + + it 'returns a right response' do + expect(response).to have_http_status :ok + expect(body[:result]).to eq 'ok' + end + end + + context 'when fails to fetch oembed' do + let(:call_result) { nil } + + it 'returns a right response' do + expect(response).to have_http_status :not_found + end + end + end + end +end diff --git a/spec/controllers/settings/migrations_controller_spec.rb b/spec/controllers/settings/migrations_controller_spec.rb new file mode 100644 index 000000000..a621bcf1c --- /dev/null +++ b/spec/controllers/settings/migrations_controller_spec.rb @@ -0,0 +1,79 @@ +require 'rails_helper' + +describe Settings::MigrationsController do + render_views + + shared_examples 'authenticate user' do + it 'redirects to sign_in page' do + is_expected.to redirect_to new_user_session_path + end + end + + describe 'GET #show' do + + context 'when user is not sign in' do + subject { get :show } + + it_behaves_like 'authenticate user' + end + + context 'when user is sign in' do + subject { get :show } + + let(:user) { Fabricate(:user, account: account) } + let(:account) { Fabricate(:account, moved_to_account: moved_to_account) } + before { sign_in user, scope: :user } + + context 'when user does not have moved to account' do + let(:moved_to_account) { nil } + + it 'renders show page' do + is_expected.to have_http_status 200 + is_expected.to render_template :show + end + end + + context 'when user does not have moved to account' do + let(:moved_to_account) { Fabricate(:account) } + + it 'renders show page' do + is_expected.to have_http_status 200 + is_expected.to render_template :show + end + end + end + end + + describe 'PUT #update' do + + context 'when user is not sign in' do + subject { put :update } + + it_behaves_like 'authenticate user' + end + + context 'when user is sign in' do + subject { put :update, params: { migration: { acct: acct } } } + + let(:user) { Fabricate(:user) } + before { sign_in user, scope: :user } + + context 'when migration account is changed' do + let(:acct) { Fabricate(:account) } + + it 'updates moved to account' do + is_expected.to redirect_to settings_migration_path + expect(user.account.reload.moved_to_account_id).to eq acct.id + end + end + + context 'when acct is a current account' do + let(:acct) { user.account } + + it 'renders show' do + is_expected.to render_template :show + end + end + end + end +end diff --git a/spec/controllers/settings/sessions_controller_spec.rb b/spec/controllers/settings/sessions_controller_spec.rb new file mode 100644 index 000000000..52b204a6a --- /dev/null +++ b/spec/controllers/settings/sessions_controller_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +describe Settings::SessionsController do + render_views + + let(:user) { Fabricate(:user) } + let(:session_activation) { Fabricate(:session_activation, user: user) } + before { sign_in user, scope: :user } + + describe 'DELETE #destroy' do + subject { delete :destroy, params: { id: id } } + + context 'when session activation exists' do + let(:id) { session_activation.id } + + it 'destroys session activation' do + is_expected.to redirect_to edit_user_registration_path + expect(SessionActivation.find_by(id: id)).to be_nil + end + end + + context 'when session activation does not exist' do + let(:id) { session_activation.id + 1000 } + + it 'destroys session activation' do + is_expected.to have_http_status :not_found + end + end + end +end diff --git a/spec/lib/status_finder_spec.rb b/spec/lib/status_finder_spec.rb index 3ef086736..6b4ee434f 100644 --- a/spec/lib/status_finder_spec.rb +++ b/spec/lib/status_finder_spec.rb @@ -6,10 +6,11 @@ describe StatusFinder do include RoutingHelper describe '#status' do + subject { described_class.new(url) } + context 'with a status url' do let(:status) { Fabricate(:status) } let(:url) { short_account_status_url(account_username: status.account.username, id: status.id) } - subject { described_class.new(url) } it 'finds the stream entry' do expect(subject.status).to eq(status) @@ -27,7 +28,6 @@ describe StatusFinder do context 'with a stream entry url' do let(:stream_entry) { Fabricate(:stream_entry) } let(:url) { account_stream_entry_url(stream_entry.account, stream_entry) } - subject { described_class.new(url) } it 'finds the stream entry' do expect(subject.status).to eq(stream_entry.status) @@ -37,7 +37,6 @@ describe StatusFinder do context 'with a remote url even if id exists on local' do let(:status) { Fabricate(:status) } let(:url) { "https://example.com/users/test/statuses/#{status.id}" } - subject { described_class.new(url) } it 'raises an error' do expect { subject.status }.to raise_error(ActiveRecord::RecordNotFound) @@ -46,7 +45,6 @@ describe StatusFinder do context 'with a plausible url' do let(:url) { 'https://example.com/users/test/updates/123/embed' } - subject { described_class.new(url) } it 'raises an error' do expect { subject.status }.to raise_error(ActiveRecord::RecordNotFound) @@ -55,7 +53,6 @@ describe StatusFinder do context 'with an unrecognized url' do let(:url) { 'https://example.com/about' } - subject { described_class.new(url) } it 'raises an error' do expect { subject.status }.to raise_error(ActiveRecord::RecordNotFound) diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index a88b11482..512b6e661 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -525,6 +525,37 @@ RSpec.describe Account, type: :model do end end + describe '#statuses_count' do + subject { Fabricate(:account) } + + it 'counts statuses' do + Fabricate(:status, account: subject) + Fabricate(:status, account: subject) + expect(subject.statuses_count).to eq 2 + end + + it 'does not count direct statuses' do + Fabricate(:status, account: subject, visibility: :direct) + expect(subject.statuses_count).to eq 0 + end + + it 'is decremented when status is removed' do + status = Fabricate(:status, account: subject) + expect(subject.statuses_count).to eq 1 + status.destroy + expect(subject.statuses_count).to eq 0 + end + + it 'is decremented when status is removed when account is not preloaded' do + status = Fabricate(:status, account: subject) + expect(subject.reload.statuses_count).to eq 1 + clean_status = Status.find(status.id) + expect(clean_status.association(:account).loaded?).to be false + clean_status.destroy + expect(subject.reload.statuses_count).to eq 0 + end + end + describe '.following_map' do it 'returns an hash' do expect(Account.following_map([], 1)).to be_a Hash diff --git a/spec/models/glitch/keyword_mute_spec.rb b/spec/models/glitch/keyword_mute_spec.rb index 79225e3b9..443832ac7 100644 --- a/spec/models/glitch/keyword_mute_spec.rb +++ b/spec/models/glitch/keyword_mute_spec.rb @@ -79,12 +79,18 @@ RSpec.describe Glitch::KeywordMute, type: :model do expect(matcher.matches?('(hot take)')).to be_truthy end - it 'escapes metacharacters in keywords' do + it 'escapes metacharacters in whole-word keywords' do Glitch::KeywordMute.create!(account: alice, keyword: '(hot take)') expect(matcher.matches?('(hot take)')).to be_truthy end + it 'escapes metacharacters in non-whole-word keywords' do + Glitch::KeywordMute.create!(account: alice, keyword: '(-', whole_word: false) + + expect(matcher.matches?('bad (-)')).to be_truthy + end + it 'uses case-folding rules appropriate for more than just English' do Glitch::KeywordMute.create!(account: alice, keyword: 'großeltern') diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb index 03d1a94de..14233e824 100644 --- a/spec/models/status_spec.rb +++ b/spec/models/status_spec.rb @@ -175,6 +175,13 @@ RSpec.describe Status, type: :model do expect(subject.reblogs_count).to eq 2 end + + it 'is decremented when reblog is removed' do + reblog = Fabricate(:status, account: bob, reblog: subject) + expect(subject.reblogs_count).to eq 1 + reblog.destroy + expect(subject.reblogs_count).to eq 0 + end end describe '#favourites_count' do @@ -184,6 +191,13 @@ RSpec.describe Status, type: :model do expect(subject.favourites_count).to eq 2 end + + it 'is decremented when favourite is removed' do + favourite = Fabricate(:favourite, account: bob, status: subject) + expect(subject.favourites_count).to eq 1 + favourite.destroy + expect(subject.favourites_count).to eq 0 + end end describe '#proper' do |