2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 08:34:40 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe ApplicationController do
|
|
|
|
describe '#redirect_to_login_if_required' do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
admin # to skip welcome wizard at home page `/`
|
|
|
|
SiteSetting.login_required = true
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:14:35 -05:00
|
|
|
it "should never cache a login redirect" do
|
|
|
|
get "/"
|
|
|
|
expect(response.headers["Cache-Control"]).to eq("no-cache, no-store")
|
|
|
|
end
|
2019-11-13 12:28:12 -05:00
|
|
|
|
|
|
|
it "should redirect to login normally" do
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/login")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should redirect to SSO if enabled" do
|
|
|
|
SiteSetting.sso_url = 'http://someurl.com'
|
|
|
|
SiteSetting.enable_sso = true
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/session/sso")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should redirect to authenticator if only one, and local logins disabled" do
|
|
|
|
# Local logins and google enabled, direct to login UI
|
|
|
|
SiteSetting.enable_google_oauth2_logins = true
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/login")
|
|
|
|
|
|
|
|
# Only google enabled, login immediately
|
|
|
|
SiteSetting.enable_local_logins = false
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/auth/google_oauth2")
|
|
|
|
|
|
|
|
# Google and GitHub enabled, direct to login UI
|
|
|
|
SiteSetting.enable_github_logins = true
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/login")
|
|
|
|
end
|
2019-11-19 14:15:11 -05:00
|
|
|
|
|
|
|
context "with omniauth in test mode" do
|
|
|
|
before do
|
|
|
|
OmniAuth.config.test_mode = true
|
|
|
|
OmniAuth.config.add_mock(:google_oauth2,
|
|
|
|
info: OmniAuth::AuthHash::InfoHash.new(
|
|
|
|
email: "address@example.com",
|
|
|
|
),
|
|
|
|
extra: {
|
|
|
|
raw_info: OmniAuth::AuthHash.new(
|
|
|
|
email_verified: true,
|
|
|
|
email: "address@example.com",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2] = nil
|
|
|
|
OmniAuth.config.test_mode = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not redirect to authenticator if registration in progress" do
|
|
|
|
SiteSetting.enable_local_logins = false
|
|
|
|
SiteSetting.enable_google_oauth2_logins = true
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/auth/google_oauth2")
|
|
|
|
|
|
|
|
expect(cookies[:authentication_data]).to eq(nil)
|
|
|
|
|
|
|
|
get "/auth/google_oauth2/callback.json"
|
|
|
|
expect(response).to redirect_to("/")
|
|
|
|
expect(cookies[:authentication_data]).not_to eq(nil)
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/login")
|
|
|
|
end
|
|
|
|
end
|
2020-03-21 16:24:49 -04:00
|
|
|
|
|
|
|
it 'contains authentication data when cookies exist' do
|
|
|
|
COOKIE_DATA = "someauthenticationdata"
|
|
|
|
cookies['authentication_data'] = COOKIE_DATA
|
|
|
|
get '/login'
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.body).to include("data-authentication-data=\"#{COOKIE_DATA }\"")
|
|
|
|
end
|
2018-03-27 08:34:40 -04:00
|
|
|
end
|
2018-05-23 16:58:47 -04:00
|
|
|
|
2019-03-15 07:09:37 -04:00
|
|
|
describe '#redirect_to_second_factor_if_required' do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2019-03-15 07:09:37 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
admin # to skip welcome wizard at home page `/`
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should redirect admins when enforce_second_factor is 'all'" do
|
|
|
|
SiteSetting.enforce_second_factor = "all"
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/u/#{admin.username}/preferences/second-factor")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should redirect users when enforce_second_factor is 'all'" do
|
|
|
|
SiteSetting.enforce_second_factor = "all"
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/u/#{user.username}/preferences/second-factor")
|
|
|
|
end
|
|
|
|
|
2019-06-27 18:01:27 -04:00
|
|
|
it "should not redirect anonymous users when enforce_second_factor is 'all'" do
|
|
|
|
SiteSetting.enforce_second_factor = "all"
|
|
|
|
SiteSetting.allow_anonymous_posting = true
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
post "/u/toggle-anon.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
2019-03-15 07:09:37 -04:00
|
|
|
it "should redirect admins when enforce_second_factor is 'staff'" do
|
|
|
|
SiteSetting.enforce_second_factor = "staff"
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/u/#{admin.username}/preferences/second-factor")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not redirect users when enforce_second_factor is 'staff'" do
|
|
|
|
SiteSetting.enforce_second_factor = "staff"
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not redirect admins when turned off" do
|
|
|
|
SiteSetting.enforce_second_factor = "no"
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not redirect users when turned off" do
|
|
|
|
SiteSetting.enforce_second_factor = "no"
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2020-01-15 05:27:12 -05:00
|
|
|
|
2020-06-05 12:31:58 -04:00
|
|
|
it "correctly redirects for Unicode usernames" do
|
|
|
|
SiteSetting.enforce_second_factor = "all"
|
|
|
|
SiteSetting.unicode_usernames = true
|
|
|
|
user = sign_in(Fabricate(:unicode_user))
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/u/#{user.encoded_username}/preferences/second-factor")
|
|
|
|
end
|
|
|
|
|
2020-01-15 05:27:12 -05:00
|
|
|
context "when enforcing second factor for staff" do
|
|
|
|
before do
|
|
|
|
SiteSetting.enforce_second_factor = "staff"
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the staff member has not enabled TOTP or security keys" do
|
|
|
|
it "redirects the staff to the second factor preferences" do
|
|
|
|
get "/"
|
|
|
|
expect(response).to redirect_to("/u/#{admin.username}/preferences/second-factor")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the staff member has enabled TOTP" do
|
|
|
|
before do
|
|
|
|
Fabricate(:user_second_factor_totp, user: admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not redirects the staff to set up 2FA" do
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the staff member has enabled security keys" do
|
|
|
|
before do
|
|
|
|
Fabricate(:user_security_key_with_random_credential, user: admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not redirects the staff to set up 2FA" do
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-15 07:09:37 -04:00
|
|
|
end
|
|
|
|
|
2018-10-05 00:33:08 -04:00
|
|
|
describe 'invalid request params' do
|
|
|
|
before do
|
|
|
|
@old_logger = Rails.logger
|
|
|
|
@logs = StringIO.new
|
|
|
|
Rails.logger = Logger.new(@logs)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Rails.logger = @old_logger
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not raise a 500 (nor should it log a warning) for bad params' do
|
2019-04-29 20:27:42 -04:00
|
|
|
bad_str = (+"d\xDE").force_encoding('utf-8')
|
2018-10-05 00:33:08 -04:00
|
|
|
expect(bad_str.valid_encoding?).to eq(false)
|
|
|
|
|
|
|
|
get "/latest.json", params: { test: bad_str }
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
2018-12-02 19:31:12 -05:00
|
|
|
|
|
|
|
log = @logs.string
|
|
|
|
|
|
|
|
if (log.include? 'exception app middleware')
|
|
|
|
# heisentest diagnostics
|
|
|
|
puts
|
|
|
|
puts "EXTRA DIAGNOSTICS FOR INTERMITENT TEST FAIL"
|
|
|
|
puts log
|
|
|
|
puts ">> action_dispatch.exception"
|
|
|
|
ex = request.env['action_dispatch.exception']
|
|
|
|
puts ">> exception class: #{ex.class} : #{ex}"
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(log).not_to include('exception app middleware')
|
2018-10-05 00:33:08 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
expect(response.parsed_body).to eq(
|
2018-10-05 00:33:08 -04:00
|
|
|
"status" => 400,
|
|
|
|
"error" => "Bad Request"
|
|
|
|
)
|
2018-12-02 19:31:12 -05:00
|
|
|
|
2018-10-05 00:33:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-30 22:02:48 -04:00
|
|
|
describe 'missing required param' do
|
|
|
|
it 'should return a 400' do
|
|
|
|
get "/search/query.json", params: { trem: "misspelled term" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
2020-05-07 11:04:12 -04:00
|
|
|
expect(response.parsed_body).to eq(
|
2018-10-30 22:02:48 -04:00
|
|
|
"errors" => ["param is missing or the value is empty: term"]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-23 16:58:47 -04:00
|
|
|
describe 'build_not_found_page' do
|
|
|
|
describe 'topic not found' do
|
2018-08-09 01:05:12 -04:00
|
|
|
|
2018-08-19 23:10:49 -04:00
|
|
|
it 'should not redirect to permalink if topic/category does not exist' do
|
|
|
|
topic = create_post.topic
|
|
|
|
Permalink.create!(url: topic.relative_url, topic_id: topic.id + 1)
|
|
|
|
topic.trash!
|
2019-10-08 07:15:08 -04:00
|
|
|
|
|
|
|
SiteSetting.detailed_404 = false
|
|
|
|
get topic.relative_url
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
|
|
|
SiteSetting.detailed_404 = true
|
2018-08-19 23:10:49 -04:00
|
|
|
get topic.relative_url
|
|
|
|
expect(response.status).to eq(410)
|
|
|
|
end
|
|
|
|
|
2018-08-09 01:05:12 -04:00
|
|
|
it 'should return permalink for deleted topics' do
|
|
|
|
topic = create_post.topic
|
|
|
|
external_url = 'https://somewhere.over.rainbow'
|
|
|
|
Permalink.create!(url: topic.relative_url, external_url: external_url)
|
|
|
|
topic.trash!
|
|
|
|
|
|
|
|
get topic.relative_url
|
|
|
|
expect(response.status).to eq(301)
|
|
|
|
expect(response).to redirect_to(external_url)
|
|
|
|
|
|
|
|
get "/t/#{topic.id}.json"
|
|
|
|
expect(response.status).to eq(301)
|
|
|
|
expect(response).to redirect_to(external_url)
|
|
|
|
|
|
|
|
get "/t/#{topic.id}.json", xhr: true
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.body).to eq(external_url)
|
|
|
|
end
|
|
|
|
|
2018-08-09 11:05:08 -04:00
|
|
|
it 'supports subfolder with permalinks' do
|
2019-11-15 00:48:24 -05:00
|
|
|
set_subfolder "/forum"
|
2018-08-09 11:05:08 -04:00
|
|
|
|
|
|
|
trashed_topic = create_post.topic
|
|
|
|
trashed_topic.trash!
|
|
|
|
new_topic = create_post.topic
|
|
|
|
permalink = Permalink.create!(url: trashed_topic.relative_url, topic_id: new_topic.id)
|
|
|
|
|
|
|
|
# no subfolder because router doesn't know about subfolder in this test
|
|
|
|
get "/t/#{trashed_topic.slug}/#{trashed_topic.id}"
|
|
|
|
expect(response.status).to eq(301)
|
|
|
|
expect(response).to redirect_to("/forum/t/#{new_topic.slug}/#{new_topic.id}")
|
|
|
|
|
|
|
|
permalink.destroy
|
|
|
|
category = Fabricate(:category)
|
|
|
|
permalink = Permalink.create!(url: trashed_topic.relative_url, category_id: category.id)
|
|
|
|
get "/t/#{trashed_topic.slug}/#{trashed_topic.id}"
|
|
|
|
expect(response.status).to eq(301)
|
2020-03-20 18:26:45 -04:00
|
|
|
expect(response).to redirect_to("/forum/c/#{category.slug}/#{category.id}")
|
2018-08-09 11:05:08 -04:00
|
|
|
|
|
|
|
permalink.destroy
|
|
|
|
permalink = Permalink.create!(url: trashed_topic.relative_url, post_id: new_topic.posts.last.id)
|
|
|
|
get "/t/#{trashed_topic.slug}/#{trashed_topic.id}"
|
|
|
|
expect(response.status).to eq(301)
|
|
|
|
expect(response).to redirect_to("/forum/t/#{new_topic.slug}/#{new_topic.id}/#{new_topic.posts.last.post_number}")
|
|
|
|
end
|
|
|
|
|
2019-05-16 22:07:18 -04:00
|
|
|
it 'should return 404 and show Google search for an invalid topic route' do
|
2018-05-23 16:58:47 -04:00
|
|
|
get "/t/nope-nope/99999999"
|
2019-05-16 22:07:18 -04:00
|
|
|
|
2018-05-23 16:58:47 -04:00
|
|
|
expect(response.status).to eq(404)
|
2019-05-16 22:07:18 -04:00
|
|
|
|
|
|
|
response_body = response.body
|
|
|
|
|
|
|
|
expect(response_body).to include(I18n.t('page_not_found.search_button'))
|
2019-10-08 07:15:08 -04:00
|
|
|
expect(response_body).to have_tag("input", with: { value: 'nope nope' })
|
2018-05-23 16:58:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not include Google search if login_required is enabled' do
|
|
|
|
SiteSetting.login_required = true
|
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
get "/t/nope-nope/99999999"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.body).to_not include('google.com/search')
|
|
|
|
end
|
2019-02-12 05:20:33 -05:00
|
|
|
|
2019-02-14 01:58:16 -05:00
|
|
|
describe 'no logspam' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
@orig_logger = Rails.logger
|
|
|
|
Rails.logger = @fake_logger = FakeLogger.new
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Rails.logger = @orig_logger
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should handle 404 to a css file' do
|
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.del("page_not_found_topics")
|
2019-02-14 01:58:16 -05:00
|
|
|
|
|
|
|
topic1 = Fabricate(:topic)
|
|
|
|
get '/stylesheets/mobile_1_4cd559272273fe6d3c7db620c617d596a5fdf240.css', headers: { 'HTTP_ACCEPT' => 'text/css,*/*,q=0.1' }
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.body).to include(topic1.title)
|
|
|
|
|
|
|
|
topic2 = Fabricate(:topic)
|
|
|
|
get '/stylesheets/mobile_1_4cd559272273fe6d3c7db620c617d596a5fdf240.css', headers: { 'HTTP_ACCEPT' => 'text/css,*/*,q=0.1' }
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.body).to include(topic1.title)
|
|
|
|
expect(response.body).to_not include(topic2.title)
|
|
|
|
|
|
|
|
expect(Rails.logger.fatals.length).to eq(0)
|
|
|
|
expect(Rails.logger.errors.length).to eq(0)
|
|
|
|
expect(Rails.logger.warnings.length).to eq(0)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-12 05:20:33 -05:00
|
|
|
it 'should cache results' do
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.del("page_not_found_topics")
|
2019-02-12 05:20:33 -05:00
|
|
|
|
|
|
|
topic1 = Fabricate(:topic)
|
|
|
|
get '/t/nope-nope/99999999'
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.body).to include(topic1.title)
|
|
|
|
|
|
|
|
topic2 = Fabricate(:topic)
|
|
|
|
get '/t/nope-nope/99999999'
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.body).to include(topic1.title)
|
|
|
|
expect(response.body).to_not include(topic2.title)
|
|
|
|
end
|
2018-05-23 16:58:47 -04:00
|
|
|
end
|
|
|
|
end
|
2018-07-03 20:07:14 -04:00
|
|
|
|
2018-08-08 00:46:34 -04:00
|
|
|
describe "#handle_theme" do
|
2019-05-06 23:12:20 -04:00
|
|
|
let!(:theme) { Fabricate(:theme, user_selectable: true) }
|
|
|
|
let!(:theme2) { Fabricate(:theme, user_selectable: true) }
|
|
|
|
let!(:non_selectable_theme) { Fabricate(:theme, user_selectable: false) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2018-08-08 00:46:34 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "selects the theme the user has selected" do
|
|
|
|
user.user_option.update_columns(theme_ids: [theme.id])
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(controller.theme_ids).to eq([theme.id])
|
|
|
|
|
|
|
|
theme.update_attribute(:user_selectable, false)
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(controller.theme_ids).to eq([SiteSetting.default_theme_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be overridden with a cookie" do
|
|
|
|
user.user_option.update_columns(theme_ids: [theme.id])
|
|
|
|
|
|
|
|
cookies['theme_ids'] = "#{theme2.id}|#{user.user_option.theme_key_seq}"
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(controller.theme_ids).to eq([theme2.id])
|
|
|
|
|
2018-08-23 21:30:00 -04:00
|
|
|
theme2.update!(user_selectable: false, component: true)
|
2019-11-28 00:19:01 -05:00
|
|
|
theme.add_relative_theme!(:child, theme2)
|
2018-08-08 00:46:34 -04:00
|
|
|
cookies['theme_ids'] = "#{theme.id},#{theme2.id}|#{user.user_option.theme_key_seq}"
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(controller.theme_ids).to eq([theme.id, theme2.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "falls back to the default theme when the user has no cookies or preferences" do
|
|
|
|
user.user_option.update_columns(theme_ids: [])
|
|
|
|
cookies["theme_ids"] = nil
|
|
|
|
theme2.set_default!
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(controller.theme_ids).to eq([theme2.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be overridden with preview_theme_id param" do
|
|
|
|
sign_in(admin)
|
|
|
|
cookies['theme_ids'] = "#{theme.id},#{theme2.id}|#{admin.user_option.theme_key_seq}"
|
|
|
|
|
|
|
|
get "/", params: { preview_theme_id: theme2.id }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(controller.theme_ids).to eq([theme2.id])
|
2018-09-06 20:44:57 -04:00
|
|
|
|
|
|
|
get "/", params: { preview_theme_id: non_selectable_theme.id }
|
|
|
|
expect(controller.theme_ids).to eq([non_selectable_theme.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow non privileged user to preview themes" do
|
|
|
|
sign_in(user)
|
|
|
|
get "/", params: { preview_theme_id: non_selectable_theme.id }
|
|
|
|
expect(controller.theme_ids).to eq([SiteSetting.default_theme_id])
|
2018-08-08 00:46:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cookie can fail back to user if out of sync" do
|
|
|
|
user.user_option.update_columns(theme_ids: [theme.id])
|
|
|
|
cookies['theme_ids'] = "#{theme2.id}|#{user.user_option.theme_key_seq - 1}"
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(controller.theme_ids).to eq([theme.id])
|
|
|
|
end
|
|
|
|
end
|
2018-10-22 13:22:23 -04:00
|
|
|
|
2018-11-14 23:22:02 -05:00
|
|
|
describe 'Custom hostname' do
|
|
|
|
|
|
|
|
it 'does not allow arbitrary host injection' do
|
|
|
|
get("/latest",
|
|
|
|
headers: {
|
|
|
|
"X-Forwarded-Host" => "test123.com"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(response.body).not_to include("test123")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-05 16:45:09 -05:00
|
|
|
describe 'allow_embedding_site_in_an_iframe' do
|
|
|
|
|
|
|
|
it "should have the 'X-Frame-Options' header with value 'sameorigin'" do
|
|
|
|
get("/latest")
|
|
|
|
expect(response.headers['X-Frame-Options']).to eq("SAMEORIGIN")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not include the 'X-Frame-Options' header" do
|
|
|
|
SiteSetting.allow_embedding_site_in_an_iframe = true
|
|
|
|
get("/latest")
|
|
|
|
expect(response.headers).not_to include('X-Frame-Options')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-01 13:18:53 -04:00
|
|
|
describe 'Delegated auth' do
|
|
|
|
let :public_key do
|
|
|
|
<<~TXT
|
|
|
|
-----BEGIN PUBLIC KEY-----
|
|
|
|
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDh7BS7Ey8hfbNhlNAW/47pqT7w
|
|
|
|
IhBz3UyBYzin8JurEQ2pY9jWWlY8CH147KyIZf1fpcsi7ZNxGHeDhVsbtUKZxnFV
|
|
|
|
p16Op3CHLJnnJKKBMNdXMy0yDfCAHZtqxeBOTcCo1Vt/bHpIgiK5kmaekyXIaD0n
|
|
|
|
w0z/BYpOgZ8QwnI5ZwIDAQAB
|
|
|
|
-----END PUBLIC KEY-----
|
|
|
|
TXT
|
|
|
|
end
|
|
|
|
|
|
|
|
let :args do
|
|
|
|
{
|
|
|
|
auth_redirect: 'http://no-good.com',
|
|
|
|
user_api_public_key: "not-a-valid-public-key"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'disallows invalid public_key param' do
|
|
|
|
args[:auth_redirect] = "discourse://auth_redirect"
|
|
|
|
get "/latest", params: args
|
|
|
|
|
|
|
|
expect(response.body).to eq(I18n.t("user_api_key.invalid_public_key"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow invalid auth_redirect' do
|
|
|
|
args[:user_api_public_key] = public_key
|
|
|
|
get "/latest", params: args
|
|
|
|
|
|
|
|
expect(response.body).to eq(I18n.t("user_api_key.invalid_auth_redirect"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not redirect if one_time_password scope is disallowed' do
|
|
|
|
SiteSetting.allow_user_api_key_scopes = "read|write"
|
|
|
|
args[:user_api_public_key] = public_key
|
|
|
|
args[:auth_redirect] = "discourse://auth_redirect"
|
|
|
|
|
|
|
|
get "/latest", params: args
|
|
|
|
|
|
|
|
expect(response.status).to_not eq(302)
|
|
|
|
expect(response).to_not redirect_to("#{args[:auth_redirect]}?otp=true")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects correctly with valid params' do
|
2019-04-01 22:13:53 -04:00
|
|
|
SiteSetting.login_required = true
|
2019-04-01 13:18:53 -04:00
|
|
|
args[:user_api_public_key] = public_key
|
|
|
|
args[:auth_redirect] = "discourse://auth_redirect"
|
|
|
|
|
|
|
|
get "/categories", params: args
|
|
|
|
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
expect(response).to redirect_to("#{args[:auth_redirect]}?otp=true")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-22 13:22:23 -04:00
|
|
|
describe 'Content Security Policy' do
|
|
|
|
it 'is enabled by SiteSettings' do
|
|
|
|
SiteSetting.content_security_policy = false
|
|
|
|
SiteSetting.content_security_policy_report_only = false
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
|
|
|
|
expect(response.headers).to_not include('Content-Security-Policy')
|
|
|
|
expect(response.headers).to_not include('Content-Security-Policy-Report-Only')
|
|
|
|
|
|
|
|
SiteSetting.content_security_policy = true
|
|
|
|
SiteSetting.content_security_policy_report_only = true
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
|
|
|
|
expect(response.headers).to include('Content-Security-Policy')
|
|
|
|
expect(response.headers).to include('Content-Security-Policy-Report-Only')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be customized with SiteSetting' do
|
|
|
|
SiteSetting.content_security_policy = true
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
script_src = parse(response.headers['Content-Security-Policy'])['script-src']
|
|
|
|
|
|
|
|
expect(script_src).to_not include('example.com')
|
|
|
|
|
|
|
|
SiteSetting.content_security_policy_script_src = 'example.com'
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
script_src = parse(response.headers['Content-Security-Policy'])['script-src']
|
|
|
|
|
|
|
|
expect(script_src).to include('example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not set CSP when responding to non-HTML' do
|
|
|
|
SiteSetting.content_security_policy = true
|
|
|
|
SiteSetting.content_security_policy_report_only = true
|
|
|
|
|
|
|
|
get '/latest.json'
|
|
|
|
|
|
|
|
expect(response.headers).to_not include('Content-Security-Policy')
|
|
|
|
expect(response.headers).to_not include('Content-Security-Policy-Report-Only')
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse(csp_string)
|
|
|
|
csp_string.split(';').map do |policy|
|
|
|
|
directive, *sources = policy.split
|
|
|
|
[directive, sources]
|
|
|
|
end.to_h
|
|
|
|
end
|
|
|
|
end
|
2019-07-23 13:17:44 -04:00
|
|
|
|
|
|
|
it 'can respond to a request with */* accept header' do
|
|
|
|
get '/', headers: { HTTP_ACCEPT: '*/*' }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.body).to include('Discourse')
|
|
|
|
end
|
2020-05-11 19:13:20 -04:00
|
|
|
|
|
|
|
it 'has canonical tag' do
|
|
|
|
get '/', headers: { HTTP_ACCEPT: '*/*' }
|
|
|
|
expect(response.body).to have_tag("link", with: { rel: "canonical", href: "http://test.localhost/" })
|
|
|
|
get '/?query_param=true', headers: { HTTP_ACCEPT: '*/*' }
|
|
|
|
expect(response.body).to have_tag("link", with: { rel: "canonical", href: "http://test.localhost/" })
|
|
|
|
get '/latest?page=2&additional_param=true', headers: { HTTP_ACCEPT: '*/*' }
|
|
|
|
expect(response.body).to have_tag("link", with: { rel: "canonical", href: "http://test.localhost/latest?page=2" })
|
|
|
|
get '/404', headers: { HTTP_ACCEPT: '*/*' }
|
|
|
|
expect(response.body).to have_tag("link", with: { rel: "canonical", href: "http://test.localhost/404" })
|
|
|
|
topic = create_post.topic
|
|
|
|
get "/t/#{topic.slug}/#{topic.id}"
|
|
|
|
expect(response.body).to have_tag("link", with: { rel: "canonical", href: "http://test.localhost/t/#{topic.slug}/#{topic.id}" })
|
|
|
|
end
|
2020-07-22 12:30:26 -04:00
|
|
|
|
|
|
|
describe "set_locale" do
|
|
|
|
# Using /bootstrap.json because it returns a locale-dependent value
|
|
|
|
def headers(locale)
|
|
|
|
{ HTTP_ACCEPT_LANGUAGE: locale }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "allow_user_locale disabled" do
|
|
|
|
context "accept-language header differs from default locale" do
|
|
|
|
before do
|
|
|
|
SiteSetting.allow_user_locale = false
|
|
|
|
SiteSetting.default_locale = "en"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with an anonymous user" do
|
|
|
|
it "uses the default locale" do
|
|
|
|
get "/bootstrap.json", headers: headers("fr")
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body['bootstrap']['locale_script']).to end_with("en.js")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a logged in user" do
|
|
|
|
it "it uses the default locale" do
|
|
|
|
user = Fabricate(:user, locale: :fr)
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get "/bootstrap.json", headers: headers("fr")
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body['bootstrap']['locale_script']).to end_with("en.js")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "set_locale_from_accept_language_header enabled" do
|
|
|
|
context "accept-language header differs from default locale" do
|
|
|
|
before do
|
|
|
|
SiteSetting.allow_user_locale = true
|
|
|
|
SiteSetting.set_locale_from_accept_language_header = true
|
|
|
|
SiteSetting.default_locale = "en"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with an anonymous user" do
|
|
|
|
it "uses the locale from the headers" do
|
|
|
|
get "/bootstrap.json", headers: headers("fr")
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body['bootstrap']['locale_script']).to end_with("fr.js")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't leak after requests" do
|
|
|
|
get "/bootstrap.json", headers: headers("fr")
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body['bootstrap']['locale_script']).to end_with("fr.js")
|
|
|
|
expect(I18n.locale.to_s).to eq(SiteSettings::DefaultsProvider::DEFAULT_LOCALE)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a logged in user" do
|
|
|
|
before do
|
|
|
|
user = Fabricate(:user, locale: :fr)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the user's preferred locale" do
|
|
|
|
get "/bootstrap.json", headers: headers("fr")
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body['bootstrap']['locale_script']).to end_with("fr.js")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "serves a 404 page in the preferred locale" do
|
|
|
|
get "/missingroute", headers: headers("fr")
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expected_title = I18n.t("page_not_found.title", locale: :fr)
|
|
|
|
expect(response.body).to include(CGI.escapeHTML(expected_title))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "the preferred locale includes a region" do
|
|
|
|
it "returns the locale and region separated by an underscore" do
|
|
|
|
SiteSetting.allow_user_locale = true
|
|
|
|
SiteSetting.set_locale_from_accept_language_header = true
|
|
|
|
SiteSetting.default_locale = "en"
|
|
|
|
|
|
|
|
get "/bootstrap.json", headers: headers("zh-CN")
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body['bootstrap']['locale_script']).to end_with("zh_CN.js")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'accept-language header is not set' do
|
|
|
|
it 'uses the site default locale' do
|
|
|
|
SiteSetting.allow_user_locale = true
|
|
|
|
SiteSetting.default_locale = 'en'
|
|
|
|
|
|
|
|
get "/bootstrap.json", headers: headers("")
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body['bootstrap']['locale_script']).to end_with("en.js")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-27 08:34:40 -04:00
|
|
|
end
|