2017-05-21 21:00:07 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-03 20:49:53 -07:00
|
|
|
describe ApplicationController do
|
2017-05-22 08:58:57 -07:00
|
|
|
controller do
|
|
|
|
def success
|
|
|
|
head 200
|
|
|
|
end
|
|
|
|
|
|
|
|
def routing_error
|
|
|
|
raise ActionController::RoutingError, ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def record_not_found
|
|
|
|
raise ActiveRecord::RecordNotFound, ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_authenticity_token
|
|
|
|
raise ActionController::InvalidAuthenticityToken, ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'respond_with_error' do |code|
|
|
|
|
it "returns http #{code} for http" do
|
|
|
|
subject
|
|
|
|
expect(response).to have_http_status(code)
|
|
|
|
end
|
|
|
|
|
2023-02-18 14:38:14 -08:00
|
|
|
it 'renders template for http' do
|
2023-02-19 20:00:48 -08:00
|
|
|
expect(subject).to render_template("errors/#{code}", layout: 'error')
|
2017-05-22 08:58:57 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-03 20:49:08 -07:00
|
|
|
context 'with a forgery' do
|
2017-06-01 11:56:55 -07:00
|
|
|
subject do
|
|
|
|
ActionController::Base.allow_forgery_protection = true
|
|
|
|
routes.draw { post 'success' => 'anonymous#success' }
|
|
|
|
post 'success'
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'respond_with_error', 422
|
|
|
|
end
|
|
|
|
|
2017-05-22 08:58:57 -07:00
|
|
|
describe 'helper_method :current_account' do
|
|
|
|
it 'returns nil if not signed in' do
|
|
|
|
expect(controller.view_context.current_account).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns account if signed in' do
|
|
|
|
account = Fabricate(:account)
|
2022-01-27 15:46:42 -08:00
|
|
|
sign_in(account.user)
|
2017-05-22 08:58:57 -07:00
|
|
|
expect(controller.view_context.current_account).to eq account
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-21 21:00:07 -07:00
|
|
|
describe 'helper_method :single_user_mode?' do
|
|
|
|
it 'returns false if it is in single_user_mode but there is no account' do
|
|
|
|
allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
|
2023-02-19 21:14:50 -08:00
|
|
|
expect(controller.view_context.single_user_mode?).to be false
|
2017-05-21 21:00:07 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if there is an account but it is not in single_user_mode' do
|
|
|
|
allow(Rails.configuration.x).to receive(:single_user_mode).and_return(false)
|
|
|
|
Fabricate(:account)
|
2023-02-19 21:14:50 -08:00
|
|
|
expect(controller.view_context.single_user_mode?).to be false
|
2017-05-21 21:00:07 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if it is in single_user_mode and there is an account' do
|
|
|
|
allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
|
|
|
|
Fabricate(:account)
|
2023-02-19 21:14:50 -08:00
|
|
|
expect(controller.view_context.single_user_mode?).to be true
|
2017-05-21 21:00:07 -07:00
|
|
|
end
|
|
|
|
end
|
2017-05-22 08:58:57 -07:00
|
|
|
|
2018-08-24 04:34:51 -07:00
|
|
|
describe 'helper_method :current_flavour' do
|
|
|
|
it 'returns "glitch" when theme wasn\'t changed in admin settings' do
|
2023-04-09 02:25:30 -07:00
|
|
|
allow(Setting).to receive(:default_settings).and_return({ 'skin' => 'default' })
|
|
|
|
allow(Setting).to receive(:default_settings).and_return({ 'flavour' => 'glitch' })
|
2018-08-23 05:17:35 -07:00
|
|
|
|
2018-08-24 04:34:51 -07:00
|
|
|
expect(controller.view_context.current_flavour).to eq 'glitch'
|
2018-08-23 05:17:35 -07:00
|
|
|
end
|
|
|
|
|
2018-08-24 04:34:51 -07:00
|
|
|
it 'returns instances\'s flavour when user is not signed in' do
|
|
|
|
allow(Setting).to receive(:[]).with('skin').and_return 'default'
|
|
|
|
allow(Setting).to receive(:[]).with('flavour').and_return 'vanilla'
|
2018-08-23 05:17:35 -07:00
|
|
|
|
2018-08-24 04:34:51 -07:00
|
|
|
expect(controller.view_context.current_flavour).to eq 'vanilla'
|
2018-08-23 05:17:35 -07:00
|
|
|
end
|
|
|
|
|
2018-08-24 04:34:51 -07:00
|
|
|
it 'returns instances\'s default flavour when user didn\'t set theme' do
|
2018-08-23 05:17:35 -07:00
|
|
|
current_user = Fabricate(:user)
|
|
|
|
sign_in current_user
|
|
|
|
|
2018-08-24 04:34:51 -07:00
|
|
|
allow(Setting).to receive(:[]).with('skin').and_return 'default'
|
|
|
|
allow(Setting).to receive(:[]).with('flavour').and_return 'vanilla'
|
2019-09-10 23:44:58 -07:00
|
|
|
allow(Setting).to receive(:[]).with('noindex').and_return false
|
2018-08-23 05:17:35 -07:00
|
|
|
|
2018-08-24 04:34:51 -07:00
|
|
|
expect(controller.view_context.current_flavour).to eq 'vanilla'
|
2018-08-23 05:17:35 -07:00
|
|
|
end
|
|
|
|
|
2018-08-24 04:34:51 -07:00
|
|
|
it 'returns user\'s flavour when it is set' do
|
2018-08-23 05:17:35 -07:00
|
|
|
current_user = Fabricate(:user)
|
Merge branch 'main' into glitch-soc/merge-upstream
Conflicts:
- `README.md`:
Upstream added a link to the roadmap, but we have a completely different README.
Kept ours.
- `app/models/media_attachment.rb`:
Upstream upped media attachment limits.
Updated the default according to upstream's.
- `db/migrate/20180831171112_create_bookmarks.rb`:
Upstream changed the migration compatibility level.
Did so too.
- `config/initializers/content_security_policy.rb`:
Upstream refactored this file but we have a different version.
Kept our version.
- `app/controllers/settings/preferences_controller.rb`:
Upstream completely refactored user settings storage, and glitch-soc has a
different set of settings.
The file does not directly references individual settings anymore.
Applied upstream changes.
- `app/lib/user_settings_decorator.rb`:
Upstream completely refactored user settings storage, and glitch-soc has a
different set of settings.
The file got removed entirely.
Removed it as well.
- `app/models/user.rb`:
Upstream completely refactored user settings storage, and glitch-soc has a
different set of settings.
References to individual settings have been removed from the file.
Removed them as well.
- `app/views/settings/preferences/appearance/show.html.haml`:
Upstream completely refactored user settings storage, and glitch-soc has a
different set of settings.
Applied upstream's changes and ported ours back.
- `app/views/settings/preferences/notifications/show.html.haml`:
Upstream completely refactored user settings storage, and glitch-soc has a
different set of settings.
Applied upstream's changes and ported ours back.
- `app/views/settings/preferences/other/show.html.haml`:
Upstream completely refactored user settings storage, and glitch-soc has a
different set of settings.
Applied upstream's changes and ported ours back.
- `config/settings.yml`:
Upstream completely refactored user settings storage, and glitch-soc has a
different set of settings.
In particular, upstream removed user-specific and unused settings.
Did the same in glitch-soc.
- `spec/controllers/application_controller_spec.rb`:
Conflicts due to glitch-soc's theming system.
Mostly kept our version, as upstream messed up the tests.
2023-03-31 12:30:27 -07:00
|
|
|
current_user.settings.update(flavour: 'glitch')
|
2023-03-30 05:44:00 -07:00
|
|
|
current_user.save
|
2018-08-23 05:17:35 -07:00
|
|
|
sign_in current_user
|
|
|
|
|
2018-08-24 04:34:51 -07:00
|
|
|
allow(Setting).to receive(:[]).with('skin').and_return 'default'
|
|
|
|
allow(Setting).to receive(:[]).with('flavour').and_return 'vanilla'
|
2018-08-23 05:17:35 -07:00
|
|
|
|
2018-08-24 04:34:51 -07:00
|
|
|
expect(controller.view_context.current_flavour).to eq 'glitch'
|
2018-08-23 05:17:35 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-03 20:49:08 -07:00
|
|
|
context 'with ActionController::RoutingError' do
|
2017-05-22 08:58:57 -07:00
|
|
|
subject do
|
|
|
|
routes.draw { get 'routing_error' => 'anonymous#routing_error' }
|
|
|
|
get 'routing_error'
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'respond_with_error', 404
|
|
|
|
end
|
|
|
|
|
2023-05-03 20:49:08 -07:00
|
|
|
context 'with ActiveRecord::RecordNotFound' do
|
2017-05-22 08:58:57 -07:00
|
|
|
subject do
|
|
|
|
routes.draw { get 'record_not_found' => 'anonymous#record_not_found' }
|
|
|
|
get 'record_not_found'
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'respond_with_error', 404
|
|
|
|
end
|
|
|
|
|
2023-05-03 20:49:08 -07:00
|
|
|
context 'with ActionController::InvalidAuthenticityToken' do
|
2017-05-22 08:58:57 -07:00
|
|
|
subject do
|
|
|
|
routes.draw { get 'invalid_authenticity_token' => 'anonymous#invalid_authenticity_token' }
|
|
|
|
get 'invalid_authenticity_token'
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'respond_with_error', 422
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'before_action :check_suspension' do
|
|
|
|
before do
|
|
|
|
routes.draw { get 'success' => 'anonymous#success' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does nothing if not signed in' do
|
|
|
|
get 'success'
|
2018-04-21 12:35:07 -07:00
|
|
|
expect(response).to have_http_status(200)
|
2017-05-22 08:58:57 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does nothing if user who signed in is not suspended' do
|
2022-01-27 15:46:42 -08:00
|
|
|
sign_in(Fabricate(:account, suspended: false).user)
|
2017-05-22 08:58:57 -07:00
|
|
|
get 'success'
|
2018-04-21 12:35:07 -07:00
|
|
|
expect(response).to have_http_status(200)
|
2017-05-22 08:58:57 -07:00
|
|
|
end
|
|
|
|
|
Change unconfirmed user login behaviour (#11375)
Allow access to account settings, 2FA, authorized applications, and
account deletions to unconfirmed and pending users, as well as
users who had their accounts disabled. Suspended users cannot update
their e-mail or password or delete their account.
Display account status on account settings page, for example, when
an account is frozen, limited, unconfirmed or pending review.
After sign up, login users straight away and show a simple page that
tells them the status of their account with links to account settings
and logout, to reduce onboarding friction and allow users to correct
wrongly typed e-mail addresses.
Move the final sign-up step of SSO integrations to be the same
as above to reduce code duplication.
2019-07-22 01:48:50 -07:00
|
|
|
it 'redirects to account status page' do
|
2022-01-27 15:46:42 -08:00
|
|
|
sign_in(Fabricate(:account, suspended: true).user)
|
2017-05-22 08:58:57 -07:00
|
|
|
get 'success'
|
Change unconfirmed user login behaviour (#11375)
Allow access to account settings, 2FA, authorized applications, and
account deletions to unconfirmed and pending users, as well as
users who had their accounts disabled. Suspended users cannot update
their e-mail or password or delete their account.
Display account status on account settings page, for example, when
an account is frozen, limited, unconfirmed or pending review.
After sign up, login users straight away and show a simple page that
tells them the status of their account with links to account settings
and logout, to reduce onboarding friction and allow users to correct
wrongly typed e-mail addresses.
Move the final sign-up step of SSO integrations to be the same
as above to reduce code duplication.
2019-07-22 01:48:50 -07:00
|
|
|
expect(response).to redirect_to(edit_user_registration_path)
|
2017-05-22 08:58:57 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'raise_not_found' do
|
|
|
|
it 'raises error' do
|
|
|
|
controller.params[:unmatched_route] = 'unmatched'
|
2018-10-04 03:36:53 -07:00
|
|
|
expect { controller.raise_not_found }.to raise_error(ActionController::RoutingError, 'No route matches unmatched')
|
2017-05-22 08:58:57 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'forbidden' do
|
|
|
|
controller do
|
|
|
|
def route_forbidden
|
|
|
|
forbidden
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
|
|
|
routes.draw { get 'route_forbidden' => 'anonymous#route_forbidden' }
|
|
|
|
get 'route_forbidden'
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'respond_with_error', 403
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'not_found' do
|
|
|
|
controller do
|
|
|
|
def route_not_found
|
|
|
|
not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
|
|
|
routes.draw { get 'route_not_found' => 'anonymous#route_not_found' }
|
|
|
|
get 'route_not_found'
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'respond_with_error', 404
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'gone' do
|
|
|
|
controller do
|
|
|
|
def route_gone
|
|
|
|
gone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
|
|
|
routes.draw { get 'route_gone' => 'anonymous#route_gone' }
|
|
|
|
get 'route_gone'
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'respond_with_error', 410
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'unprocessable_entity' do
|
|
|
|
controller do
|
|
|
|
def route_unprocessable_entity
|
|
|
|
unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
|
|
|
routes.draw { get 'route_unprocessable_entity' => 'anonymous#route_unprocessable_entity' }
|
|
|
|
get 'route_unprocessable_entity'
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'respond_with_error', 422
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'cache_collection' do
|
2023-05-03 01:32:30 -07:00
|
|
|
subject do
|
|
|
|
Class.new(ApplicationController) do
|
|
|
|
public :cache_collection
|
|
|
|
end
|
2017-05-22 08:58:57 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'receives :with_includes' do |fabricator, klass|
|
|
|
|
it 'uses raw if it is not an ActiveRecord::Relation' do
|
|
|
|
record = Fabricate(fabricator)
|
2023-05-03 01:32:30 -07:00
|
|
|
expect(subject.new.cache_collection([record], klass)).to eq [record]
|
2017-05-22 08:58:57 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'cacheable' do |fabricator, klass|
|
|
|
|
include_examples 'receives :with_includes', fabricator, klass
|
|
|
|
|
|
|
|
it 'calls cache_ids of raw if it is an ActiveRecord::Relation' do
|
|
|
|
record = Fabricate(fabricator)
|
|
|
|
relation = klass.none
|
|
|
|
allow(relation).to receive(:cache_ids).and_return([record])
|
2023-05-03 01:32:30 -07:00
|
|
|
expect(subject.new.cache_collection(relation, klass)).to eq [record]
|
2017-05-22 08:58:57 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns raw unless class responds to :with_includes' do
|
|
|
|
raw = Object.new
|
2023-05-03 01:32:30 -07:00
|
|
|
expect(subject.new.cache_collection(raw, Object)).to eq raw
|
2017-05-22 08:58:57 -07:00
|
|
|
end
|
|
|
|
|
2023-05-03 20:49:08 -07:00
|
|
|
context 'with a Status' do
|
2017-05-22 08:58:57 -07:00
|
|
|
include_examples 'cacheable', :status, Status
|
|
|
|
end
|
|
|
|
end
|
2017-05-21 21:00:07 -07:00
|
|
|
end
|