2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-16 18:09:13 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'site_settings/validations'
|
|
|
|
|
|
|
|
describe SiteSettings::Validations do
|
|
|
|
subject { Class.new.include(described_class).new }
|
|
|
|
|
2019-07-11 13:41:51 -04:00
|
|
|
context "default_categories" do
|
|
|
|
fab!(:category) { Fabricate(:category) }
|
|
|
|
|
|
|
|
it "supports valid categories" do
|
|
|
|
expect { subject.validate_default_categories_watching("#{category.id}") }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "won't allow you to input junk categories" do
|
|
|
|
expect {
|
|
|
|
subject.validate_default_categories_watching("junk")
|
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
subject.validate_default_categories_watching("#{category.id}|12312323")
|
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
2019-10-06 14:50:07 -04:00
|
|
|
|
|
|
|
it "prevents using the same category in more than one default group" do
|
|
|
|
SiteSetting.default_categories_watching = "#{category.id}"
|
|
|
|
|
|
|
|
expect {
|
|
|
|
SiteSetting.default_categories_tracking = "#{category.id}"
|
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
2019-07-11 13:41:51 -04:00
|
|
|
end
|
|
|
|
|
2018-12-16 18:09:13 -05:00
|
|
|
context "s3 buckets reusage" do
|
|
|
|
let(:error_message) { I18n.t("errors.site_settings.s3_bucket_reused") }
|
|
|
|
|
|
|
|
shared_examples "s3 bucket validation" do
|
|
|
|
def change_bucket_value(value)
|
2019-05-06 21:00:09 -04:00
|
|
|
SiteSetting.set(other_setting_name, value)
|
2018-12-16 18:09:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "shouldn't raise an error when both buckets are blank" do
|
|
|
|
change_bucket_value("")
|
2018-12-17 10:10:10 -05:00
|
|
|
validate("")
|
2018-12-16 18:09:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "shouldn't raise an error when only one bucket is set" do
|
|
|
|
change_bucket_value("")
|
2018-12-17 10:10:10 -05:00
|
|
|
validate("my-awesome-bucket")
|
2018-12-16 18:09:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "shouldn't raise an error when both buckets are equal, but use a different path" do
|
|
|
|
change_bucket_value("my-awesome-bucket/foo")
|
2018-12-17 10:10:10 -05:00
|
|
|
validate("my-awesome-bucket/bar")
|
2018-12-16 18:09:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an error when both buckets are equal" do
|
|
|
|
change_bucket_value("my-awesome-bucket")
|
|
|
|
expect { validate("my-awesome-bucket") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an error when both buckets are equal except for a trailing slash" do
|
|
|
|
change_bucket_value("my-awesome-bucket/")
|
|
|
|
expect { validate("my-awesome-bucket") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
|
|
|
|
change_bucket_value("my-awesome-bucket")
|
|
|
|
expect { validate("my-awesome-bucket/") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#validate_s3_backup_bucket" do
|
|
|
|
let(:other_setting_name) { "s3_upload_bucket" }
|
|
|
|
|
|
|
|
def validate(new_value)
|
|
|
|
subject.validate_s3_backup_bucket(new_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like "s3 bucket validation"
|
|
|
|
|
|
|
|
it "shouldn't raise an error when the 's3_backup_bucket' is a subdirectory of 's3_upload_bucket'" do
|
|
|
|
SiteSetting.s3_upload_bucket = "my-awesome-bucket"
|
2018-12-17 10:10:10 -05:00
|
|
|
validate("my-awesome-bucket/backups")
|
2018-12-16 18:09:13 -05:00
|
|
|
|
|
|
|
SiteSetting.s3_upload_bucket = "my-awesome-bucket/foo"
|
2018-12-17 10:10:10 -05:00
|
|
|
validate("my-awesome-bucket/foo/backups")
|
2018-12-16 18:09:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#validate_s3_upload_bucket" do
|
|
|
|
let(:other_setting_name) { "s3_backup_bucket" }
|
|
|
|
|
|
|
|
def validate(new_value)
|
|
|
|
subject.validate_s3_upload_bucket(new_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like "s3 bucket validation"
|
|
|
|
|
|
|
|
it "should raise an error when the 's3_upload_bucket' is a subdirectory of 's3_backup_bucket'" do
|
|
|
|
SiteSetting.s3_backup_bucket = "my-awesome-bucket"
|
|
|
|
expect { validate("my-awesome-bucket/uploads") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
|
|
|
|
SiteSetting.s3_backup_bucket = "my-awesome-bucket/foo"
|
|
|
|
expect { validate("my-awesome-bucket/foo/uploads") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
2020-03-06 08:49:28 -05:00
|
|
|
|
|
|
|
it "cannot be made blank unless the setting is false" do
|
|
|
|
SiteSetting.s3_backup_bucket = "really-real-cool-bucket"
|
|
|
|
SiteSetting.enable_s3_uploads = true
|
|
|
|
|
|
|
|
expect { validate("") }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
SiteSetting.enable_s3_uploads = false
|
|
|
|
validate("")
|
|
|
|
end
|
2018-12-16 18:09:13 -05:00
|
|
|
end
|
|
|
|
end
|
2019-11-15 01:05:10 -05:00
|
|
|
|
2020-08-19 14:16:31 -04:00
|
|
|
describe "enforce second factor & local/auth provider login interplay" do
|
2019-11-15 01:05:10 -05:00
|
|
|
describe "#validate_enforce_second_factor" do
|
|
|
|
context "when local logins are disabled" do
|
2020-08-19 14:16:31 -04:00
|
|
|
let(:error_message) { I18n.t("errors.site_settings.second_factor_cannot_be_enforced_with_disabled_local_login") }
|
2019-11-15 01:05:10 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.enable_local_logins = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an error" do
|
|
|
|
expect { subject.validate_enforce_second_factor("t") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when local logins are enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_local_logins = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be ok" do
|
2019-11-19 16:46:44 -05:00
|
|
|
expect { subject.validate_enforce_second_factor("t") }.not_to raise_error
|
2019-11-15 01:05:10 -05:00
|
|
|
end
|
|
|
|
end
|
2020-08-19 14:16:31 -04:00
|
|
|
|
|
|
|
context "when social logins are enabled" do
|
|
|
|
let(:error_message) { I18n.t("errors.site_settings.second_factor_cannot_enforce_with_socials", auth_provider_names: "facebook, github") }
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_facebook_logins = true
|
|
|
|
SiteSetting.enable_github_logins = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises and error, and specifies the auth providers" do
|
|
|
|
expect { subject.validate_enforce_second_factor("all") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
end
|
2020-10-09 13:06:38 -04:00
|
|
|
|
|
|
|
context "when SSO is enabled" do
|
2021-02-08 05:04:33 -05:00
|
|
|
let(:error_message) { I18n.t("errors.site_settings.second_factor_cannot_be_enforced_with_discourse_connect_enabled") }
|
2020-10-09 13:06:38 -04:00
|
|
|
before do
|
2021-02-08 05:04:33 -05:00
|
|
|
SiteSetting.discourse_connect_url = "https://www.example.com/sso"
|
|
|
|
SiteSetting.enable_discourse_connect = true
|
2020-10-09 13:06:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an error" do
|
|
|
|
expect { subject.validate_enforce_second_factor("t") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
end
|
2019-11-15 01:05:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#validate_enable_local_logins" do
|
|
|
|
let(:error_message) { I18n.t("errors.site_settings.local_login_cannot_be_disabled_if_second_factor_enforced") }
|
|
|
|
|
|
|
|
context "when the new value is false" do
|
|
|
|
context "when enforce second factor is enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.enforce_second_factor = "all"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an error" do
|
|
|
|
expect { subject.validate_enable_local_logins("f") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when enforce second factor is disabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.enforce_second_factor = "no"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be ok" do
|
2019-11-19 16:46:44 -05:00
|
|
|
expect { subject.validate_enable_local_logins("f") }.not_to raise_error
|
2019-11-15 01:05:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the new value is true" do
|
|
|
|
it "should be ok" do
|
2019-11-19 16:46:44 -05:00
|
|
|
expect { subject.validate_enable_local_logins("t") }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-28 22:01:06 -04:00
|
|
|
describe "#validate_cors_origins" do
|
|
|
|
let(:error_message) { I18n.t("errors.site_settings.cors_origins_should_not_have_trailing_slash") }
|
|
|
|
|
|
|
|
context "when the new value has trailing slash" do
|
|
|
|
it "should raise an error" do
|
|
|
|
expect { subject.validate_cors_origins("https://www.rainbows.com/") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-05 19:33:19 -05:00
|
|
|
describe "#validate_enable_page_publishing" do
|
|
|
|
context "when the new value is true" do
|
|
|
|
it "is ok" do
|
|
|
|
expect { subject.validate_enable_page_publishing("t") }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
context "if secure media is enabled" do
|
|
|
|
let(:error_message) { I18n.t("errors.site_settings.page_publishing_requirements") }
|
|
|
|
before do
|
|
|
|
enable_secure_media
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is not ok" do
|
|
|
|
expect { subject.validate_enable_page_publishing("t") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-19 16:46:44 -05:00
|
|
|
describe "#validate_secure_media" do
|
|
|
|
let(:error_message) { I18n.t("errors.site_settings.secure_media_requirements") }
|
|
|
|
|
|
|
|
context "when the new value is true" do
|
|
|
|
context 'if site setting for enable_s3_uploads is enabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_s3_uploads = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be ok" do
|
|
|
|
expect { subject.validate_secure_media("t") }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if site setting for enable_s3_uploads is not enabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_s3_uploads = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is not ok" do
|
|
|
|
expect { subject.validate_secure_media("t") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "if global s3 setting is enabled" do
|
|
|
|
before do
|
|
|
|
GlobalSetting.stubs(:use_s3?).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be ok" do
|
|
|
|
expect { subject.validate_secure_media("t") }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#validate_enable_s3_uploads" do
|
|
|
|
let(:error_message) { I18n.t("errors.site_settings.cannot_enable_s3_uploads_when_s3_enabled_globally") }
|
|
|
|
|
|
|
|
context "when the new value is true" do
|
|
|
|
context "when s3 uploads are already globally enabled" do
|
|
|
|
before do
|
|
|
|
GlobalSetting.stubs(:use_s3?).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is not ok" do
|
|
|
|
expect { subject.validate_enable_s3_uploads("t") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when s3 uploads are not already globally enabled" do
|
|
|
|
before do
|
|
|
|
GlobalSetting.stubs(:use_s3?).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be ok" do
|
|
|
|
expect { subject.validate_enable_s3_uploads("t") }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the s3_upload_bucket is blank" do
|
|
|
|
let(:error_message) { I18n.t("errors.site_settings.s3_upload_bucket_is_required") }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.s3_upload_bucket = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is not ok" do
|
|
|
|
expect { subject.validate_enable_s3_uploads("t") }.to raise_error(Discourse::InvalidParameters, error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the s3_upload_bucket is not blank" do
|
|
|
|
before do
|
|
|
|
SiteSetting.s3_upload_bucket = "some-bucket"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be ok" do
|
|
|
|
expect { subject.validate_enable_s3_uploads("t") }.not_to raise_error
|
|
|
|
end
|
2019-11-15 01:05:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-16 18:09:13 -05:00
|
|
|
end
|