2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
require 'discourse'
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Discourse do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
RailsMultisite::ConnectionManagement.stubs(:current_hostname).returns('foo.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'current_hostname' do
|
|
|
|
it 'returns the hostname from the current db connection' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Discourse.current_hostname).to eq('foo.com')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2019-08-27 23:03:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-08-27 23:03:20 +08:00
|
|
|
context 'avatar_sizes' do
|
|
|
|
it 'returns a list of integers' do
|
|
|
|
expect(Discourse.avatar_sizes).to contain_exactly(20, 25, 30, 32, 37, 40, 45, 48, 50, 60, 64, 67, 75, 90, 96, 120, 135, 180, 240, 360)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-11-16 05:39:11 +08:00
|
|
|
context 'running_in_rack' do
|
|
|
|
after do
|
|
|
|
ENV.delete("DISCOURSE_RUNNING_IN_RACK")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be running in rack' do
|
|
|
|
expect(Discourse.running_in_rack?).to eq(false)
|
|
|
|
ENV["DISCOURSE_RUNNING_IN_RACK"] = "1"
|
|
|
|
expect(Discourse.running_in_rack?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'base_url' do
|
2014-01-09 07:51:38 +08:00
|
|
|
context 'when https is off' do
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
2016-07-29 01:54:17 +08:00
|
|
|
SiteSetting.force_https = false
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-01-09 07:51:38 +08:00
|
|
|
it 'has a non https base url' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Discourse.base_url).to eq("http://foo.com")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-09 07:51:38 +08:00
|
|
|
context 'when https is on' do
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
2016-07-29 01:54:17 +08:00
|
|
|
SiteSetting.force_https = true
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a non-ssl base url' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Discourse.base_url).to eq("https://foo.com")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a non standard port specified' do
|
|
|
|
before do
|
2016-07-29 01:54:17 +08:00
|
|
|
SiteSetting.port = 3000
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the non standart port in the base url" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Discourse.base_url).to eq("http://foo.com:3000")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-11 04:58:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-23 22:24:42 +08:00
|
|
|
context "asset_filter_options" do
|
|
|
|
it "obmits path if request is missing" do
|
|
|
|
opts = Discourse.asset_filter_options(:js, nil)
|
|
|
|
expect(opts[:path]).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a hash with a path from the request" do
|
|
|
|
req = stub(fullpath: "/hello", headers: {})
|
|
|
|
opts = Discourse.asset_filter_options(:js, req)
|
|
|
|
expect(opts[:path]).to eq("/hello")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-01 17:50:31 +08:00
|
|
|
context 'plugins' do
|
|
|
|
let(:plugin_class) do
|
|
|
|
Class.new(Plugin::Instance) do
|
|
|
|
attr_accessor :enabled
|
|
|
|
def enabled?
|
|
|
|
@enabled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 23:30:31 +08:00
|
|
|
let(:plugin1) { plugin_class.new.tap { |p| p.enabled = true; p.path = "my-plugin-1" } }
|
|
|
|
let(:plugin2) { plugin_class.new.tap { |p| p.enabled = false; p.path = "my-plugin-1" } }
|
2019-11-01 17:50:31 +08:00
|
|
|
|
2021-03-15 16:11:23 +08:00
|
|
|
before do
|
|
|
|
Discourse.plugins.append(plugin1, plugin2)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Discourse.plugins.delete plugin1
|
|
|
|
Discourse.plugins.delete plugin2
|
2021-10-25 16:24:21 +08:00
|
|
|
DiscoursePluginRegistry.reset!
|
2021-03-15 16:11:23 +08:00
|
|
|
end
|
2019-11-01 17:50:31 +08:00
|
|
|
|
2020-03-13 23:30:31 +08:00
|
|
|
before do
|
|
|
|
plugin_class.any_instance.stubs(:css_asset_exists?).returns(true)
|
|
|
|
plugin_class.any_instance.stubs(:js_asset_exists?).returns(true)
|
|
|
|
end
|
|
|
|
|
2019-11-01 17:50:31 +08:00
|
|
|
it 'can find plugins correctly' do
|
2021-03-15 16:11:23 +08:00
|
|
|
expect(Discourse.plugins).to include(plugin1, plugin2)
|
2019-11-01 17:50:31 +08:00
|
|
|
|
|
|
|
# Exclude disabled plugins by default
|
2021-03-15 16:11:23 +08:00
|
|
|
expect(Discourse.find_plugins({})).to include(plugin1)
|
2019-11-01 17:50:31 +08:00
|
|
|
|
|
|
|
# Include disabled plugins when requested
|
2021-03-15 16:11:23 +08:00
|
|
|
expect(Discourse.find_plugins(include_disabled: true)).to include(plugin1, plugin2)
|
2019-11-01 17:50:31 +08:00
|
|
|
end
|
2020-03-13 23:30:31 +08:00
|
|
|
|
|
|
|
it 'can find plugin assets' do
|
|
|
|
plugin2.enabled = true
|
|
|
|
|
|
|
|
expect(Discourse.find_plugin_css_assets({}).length).to eq(2)
|
|
|
|
expect(Discourse.find_plugin_js_assets({}).length).to eq(2)
|
2021-04-23 22:24:42 +08:00
|
|
|
plugin1.register_asset_filter do |type, request, opts|
|
2020-03-13 23:30:31 +08:00
|
|
|
false
|
|
|
|
end
|
|
|
|
expect(Discourse.find_plugin_css_assets({}).length).to eq(1)
|
|
|
|
expect(Discourse.find_plugin_js_assets({}).length).to eq(1)
|
|
|
|
end
|
|
|
|
|
2019-11-01 17:50:31 +08:00
|
|
|
end
|
|
|
|
|
2018-07-23 23:51:57 +08:00
|
|
|
context 'authenticators' do
|
|
|
|
it 'returns inbuilt authenticators' do
|
2018-07-31 23:18:50 +08:00
|
|
|
expect(Discourse.authenticators).to match_array(Discourse::BUILTIN_AUTH.map(&:authenticator))
|
2018-07-23 23:51:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with authentication plugin installed' do
|
|
|
|
let(:plugin_auth_provider) do
|
|
|
|
authenticator_class = Class.new(Auth::Authenticator) do
|
|
|
|
def name
|
|
|
|
'pluginauth'
|
|
|
|
end
|
|
|
|
|
2022-03-04 00:17:02 +08:00
|
|
|
def enabled?
|
2018-07-23 23:51:57 +08:00
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-31 23:18:50 +08:00
|
|
|
provider = Auth::AuthProvider.new
|
2018-07-23 23:51:57 +08:00
|
|
|
provider.authenticator = authenticator_class.new
|
|
|
|
provider
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
DiscoursePluginRegistry.register_auth_provider(plugin_auth_provider)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
DiscoursePluginRegistry.reset!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns inbuilt and plugin authenticators' do
|
|
|
|
expect(Discourse.authenticators).to match_array(
|
2018-07-31 23:18:50 +08:00
|
|
|
Discourse::BUILTIN_AUTH.map(&:authenticator) + [plugin_auth_provider.authenticator])
|
2018-07-23 23:51:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'enabled_authenticators' do
|
|
|
|
it 'only returns enabled authenticators' do
|
|
|
|
expect(Discourse.enabled_authenticators.length).to be(0)
|
|
|
|
expect { SiteSetting.enable_twitter_logins = true }
|
|
|
|
.to change { Discourse.enabled_authenticators.length }.by(1)
|
|
|
|
expect(Discourse.enabled_authenticators.length).to be(1)
|
|
|
|
expect(Discourse.enabled_authenticators.first).to be_instance_of(Auth::TwitterAuthenticator)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 18:21:10 +08:00
|
|
|
describe '#site_contact_user' do
|
2013-05-11 04:58:23 +08:00
|
|
|
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:another_admin) { Fabricate(:admin) }
|
2013-05-11 04:58:23 +08:00
|
|
|
|
2013-09-06 15:28:37 +08:00
|
|
|
it 'returns the user specified by the site setting site_contact_username' do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.site_contact_username = another_admin.username
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Discourse.site_contact_user).to eq(another_admin)
|
2014-01-23 18:26:31 +08:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:37:33 +08:00
|
|
|
it 'returns the system user otherwise' do
|
2016-07-29 01:54:17 +08:00
|
|
|
SiteSetting.site_contact_username = nil
|
2015-11-25 03:37:33 +08:00
|
|
|
expect(Discourse.site_contact_user.username).to eq("system")
|
2013-05-11 04:58:23 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2022-07-27 18:21:10 +08:00
|
|
|
describe '#system_user' do
|
2019-10-31 23:16:26 +08:00
|
|
|
it 'returns the system user' do
|
|
|
|
expect(Discourse.system_user.id).to eq(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 18:21:10 +08:00
|
|
|
describe "#store" do
|
2013-08-01 05:26:34 +08:00
|
|
|
|
|
|
|
it "returns LocalStore by default" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Discourse.store).to be_a(FileStore::LocalStore)
|
2013-08-01 05:26:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns S3Store when S3 is enabled" do
|
2016-07-29 01:54:17 +08:00
|
|
|
SiteSetting.enable_s3_uploads = true
|
|
|
|
SiteSetting.s3_upload_bucket = "s3bucket"
|
|
|
|
SiteSetting.s3_access_key_id = "s3_access_key_id"
|
|
|
|
SiteSetting.s3_secret_access_key = "s3_secret_access_key"
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Discourse.store).to be_a(FileStore::S3Store)
|
2013-08-01 05:26:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-06-29 13:55:17 +08:00
|
|
|
context 'readonly mode' do
|
2016-06-29 14:19:18 +08:00
|
|
|
let(:readonly_mode_key) { Discourse::READONLY_MODE_KEY }
|
2016-06-29 13:55:17 +08:00
|
|
|
let(:readonly_mode_ttl) { Discourse::READONLY_MODE_KEY_TTL }
|
2016-06-29 14:19:18 +08:00
|
|
|
let(:user_readonly_mode_key) { Discourse::USER_READONLY_MODE_KEY }
|
2014-02-13 12:37:28 +08:00
|
|
|
|
2016-06-29 13:55:17 +08:00
|
|
|
after do
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.del(readonly_mode_key)
|
|
|
|
Discourse.redis.del(user_readonly_mode_key)
|
2014-02-13 12:37:28 +08:00
|
|
|
end
|
|
|
|
|
2016-06-29 14:19:18 +08:00
|
|
|
def assert_readonly_mode(message, key, ttl = -1)
|
|
|
|
expect(message.channel).to eq(Discourse.readonly_channel)
|
2016-06-29 13:55:17 +08:00
|
|
|
expect(message.data).to eq(true)
|
2019-12-03 17:05:53 +08:00
|
|
|
expect(Discourse.redis.get(key)).to eq("1")
|
|
|
|
expect(Discourse.redis.ttl(key)).to eq(ttl)
|
2016-06-29 13:55:17 +08:00
|
|
|
end
|
2014-02-13 12:37:28 +08:00
|
|
|
|
2016-06-29 14:19:18 +08:00
|
|
|
def assert_readonly_mode_disabled(message, key)
|
|
|
|
expect(message.channel).to eq(Discourse.readonly_channel)
|
2016-06-29 13:55:17 +08:00
|
|
|
expect(message.data).to eq(false)
|
2019-12-03 17:05:53 +08:00
|
|
|
expect(Discourse.redis.get(key)).to eq(nil)
|
2016-06-29 13:55:17 +08:00
|
|
|
end
|
2014-02-13 12:37:28 +08:00
|
|
|
|
2016-06-29 14:19:18 +08:00
|
|
|
describe ".enable_readonly_mode" do
|
2016-06-29 13:55:17 +08:00
|
|
|
it "adds a key in redis and publish a message through the message bus" do
|
2019-12-03 17:05:53 +08:00
|
|
|
expect(Discourse.redis.get(readonly_mode_key)).to eq(nil)
|
2016-06-29 14:19:18 +08:00
|
|
|
end
|
2014-02-13 12:37:28 +08:00
|
|
|
|
2016-06-29 14:19:18 +08:00
|
|
|
context 'user enabled readonly mode' do
|
|
|
|
it "adds a key in redis and publish a message through the message bus" do
|
2019-12-03 17:05:53 +08:00
|
|
|
expect(Discourse.redis.get(user_readonly_mode_key)).to eq(nil)
|
2016-06-29 14:19:18 +08:00
|
|
|
end
|
2016-06-29 13:55:17 +08:00
|
|
|
end
|
2014-02-13 12:37:28 +08:00
|
|
|
end
|
|
|
|
|
2016-06-29 14:19:18 +08:00
|
|
|
describe ".disable_readonly_mode" do
|
|
|
|
context 'user disabled readonly mode' do
|
|
|
|
it "removes readonly key in redis and publish a message through the message bus" do
|
2020-07-14 16:15:58 +08:00
|
|
|
message = MessageBus.track_publish { Discourse.disable_readonly_mode(user_readonly_mode_key) }.first
|
|
|
|
assert_readonly_mode_disabled(message, user_readonly_mode_key)
|
2016-06-29 14:19:18 +08:00
|
|
|
end
|
2016-06-29 13:55:17 +08:00
|
|
|
end
|
2015-04-25 02:32:18 +08:00
|
|
|
end
|
|
|
|
|
2016-06-29 14:19:18 +08:00
|
|
|
describe ".readonly_mode?" do
|
2016-06-29 13:55:17 +08:00
|
|
|
it "is false by default" do
|
|
|
|
expect(Discourse.readonly_mode?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when the key is present in redis" do
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.set(readonly_mode_key, 1)
|
2016-02-29 18:58:42 +08:00
|
|
|
expect(Discourse.readonly_mode?).to eq(true)
|
|
|
|
end
|
2014-02-13 12:37:28 +08:00
|
|
|
|
2019-06-21 22:08:57 +08:00
|
|
|
it "returns true when postgres is recently read only" do
|
|
|
|
Discourse.received_postgres_readonly!
|
|
|
|
expect(Discourse.readonly_mode?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when redis is recently read only" do
|
|
|
|
Discourse.received_redis_readonly!
|
2016-06-29 13:55:17 +08:00
|
|
|
expect(Discourse.readonly_mode?).to eq(true)
|
|
|
|
end
|
2016-08-25 23:30:41 +08:00
|
|
|
|
|
|
|
it "returns true when user enabled readonly mode key is present in redis" do
|
2017-01-11 16:38:07 +08:00
|
|
|
Discourse.enable_readonly_mode(user_readonly_mode_key)
|
2016-08-25 23:30:41 +08:00
|
|
|
expect(Discourse.readonly_mode?).to eq(true)
|
2018-06-12 00:21:29 +08:00
|
|
|
expect(Discourse.readonly_mode?(readonly_mode_key)).to eq(false)
|
2016-08-25 23:30:41 +08:00
|
|
|
|
2017-01-11 16:38:07 +08:00
|
|
|
Discourse.disable_readonly_mode(user_readonly_mode_key)
|
2016-08-25 23:30:41 +08:00
|
|
|
expect(Discourse.readonly_mode?).to eq(false)
|
|
|
|
end
|
2014-02-13 12:37:28 +08:00
|
|
|
end
|
|
|
|
|
2019-06-21 22:08:57 +08:00
|
|
|
describe ".received_postgres_readonly!" do
|
|
|
|
it "sets the right time" do
|
|
|
|
time = Discourse.received_postgres_readonly!
|
|
|
|
expect(Discourse.postgres_last_read_only['default']).to eq(time)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".received_redis_readonly!" do
|
2016-06-29 13:55:17 +08:00
|
|
|
it "sets the right time" do
|
2019-06-21 22:08:57 +08:00
|
|
|
time = Discourse.received_redis_readonly!
|
|
|
|
expect(Discourse.redis_last_read_only['default']).to eq(time)
|
2016-06-29 13:55:17 +08:00
|
|
|
end
|
2016-02-29 18:58:42 +08:00
|
|
|
end
|
2019-01-22 09:51:45 +08:00
|
|
|
|
|
|
|
describe ".clear_readonly!" do
|
|
|
|
it "publishes the right message" do
|
2019-06-21 22:08:57 +08:00
|
|
|
Discourse.received_postgres_readonly!
|
2019-01-22 09:51:45 +08:00
|
|
|
messages = []
|
|
|
|
|
|
|
|
expect do
|
|
|
|
messages = MessageBus.track_publish { Discourse.clear_readonly! }
|
2019-06-21 22:08:57 +08:00
|
|
|
end.to change { Discourse.postgres_last_read_only['default'] }.to(nil)
|
2019-01-22 09:51:45 +08:00
|
|
|
|
|
|
|
expect(messages.any? { |m| m.channel == Site::SITE_JSON_CHANNEL })
|
|
|
|
.to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2016-02-29 18:58:42 +08:00
|
|
|
end
|
|
|
|
|
2022-07-27 18:21:10 +08:00
|
|
|
describe "#handle_exception" do
|
2014-07-18 04:22:46 +08:00
|
|
|
|
2022-06-21 09:23:36 +08:00
|
|
|
class TempSidekiqLogger
|
2014-02-21 11:30:25 +08:00
|
|
|
attr_accessor :exception, :context
|
2022-06-21 09:23:36 +08:00
|
|
|
|
2014-07-18 04:22:46 +08:00
|
|
|
def call(ex, ctx)
|
|
|
|
self.exception = ex
|
|
|
|
self.context = ctx
|
2014-02-21 11:30:25 +08:00
|
|
|
end
|
|
|
|
end
|
2014-07-18 04:22:46 +08:00
|
|
|
|
|
|
|
let!(:logger) { TempSidekiqLogger.new }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Sidekiq.error_handlers << logger
|
|
|
|
end
|
2014-09-25 04:52:09 +08:00
|
|
|
|
2022-06-21 09:23:36 +08:00
|
|
|
after do
|
|
|
|
Sidekiq.error_handlers.delete(logger)
|
|
|
|
end
|
|
|
|
|
2014-02-21 11:30:25 +08:00
|
|
|
it "should not fail when called" do
|
|
|
|
exception = StandardError.new
|
|
|
|
|
2019-04-08 22:57:47 +08:00
|
|
|
expect do
|
|
|
|
Discourse.handle_job_exception(exception, nil, nil)
|
|
|
|
end.to raise_error(StandardError) # Raises in test mode, catch it
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(logger.exception).to eq(exception)
|
|
|
|
expect(logger.context.keys).to eq([:current_db, :current_hostname])
|
2014-02-21 11:30:25 +08:00
|
|
|
end
|
2014-07-18 04:22:46 +08:00
|
|
|
|
|
|
|
it "correctly passes extra context" do
|
|
|
|
exception = StandardError.new
|
|
|
|
|
2019-04-08 22:57:47 +08:00
|
|
|
expect do
|
|
|
|
Discourse.handle_job_exception(exception, { message: "Doing a test", post_id: 31 }, nil)
|
|
|
|
end.to raise_error(StandardError) # Raises in test mode, catch it
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(logger.exception).to eq(exception)
|
|
|
|
expect(logger.context.keys.sort).to eq([:current_db, :current_hostname, :message, :post_id].sort)
|
2014-07-18 04:22:46 +08:00
|
|
|
end
|
2014-02-21 11:30:25 +08:00
|
|
|
end
|
|
|
|
|
2022-07-27 18:21:10 +08:00
|
|
|
describe '#deprecate' do
|
2018-06-20 15:50:11 +08:00
|
|
|
def old_method(m)
|
|
|
|
Discourse.deprecate(m)
|
|
|
|
end
|
|
|
|
|
|
|
|
def old_method_caller(m)
|
|
|
|
old_method(m)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
@orig_logger = Rails.logger
|
|
|
|
Rails.logger = @fake_logger = FakeLogger.new
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Rails.logger = @orig_logger
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can deprecate usage' do
|
|
|
|
k = SecureRandom.hex
|
|
|
|
expect(old_method_caller(k)).to include("old_method_caller")
|
|
|
|
expect(old_method_caller(k)).to include("discourse_spec")
|
|
|
|
expect(old_method_caller(k)).to include(k)
|
|
|
|
|
2022-05-05 09:53:54 +08:00
|
|
|
expect(@fake_logger.warnings).to eq([old_method_caller(k)])
|
2018-06-20 15:50:11 +08:00
|
|
|
end
|
2018-12-06 19:38:01 +08:00
|
|
|
|
|
|
|
it 'can report the deprecated version' do
|
|
|
|
Discourse.deprecate(SecureRandom.hex, since: "2.1.0.beta1")
|
|
|
|
|
2022-05-05 09:53:54 +08:00
|
|
|
expect(@fake_logger.warnings[0]).to include("(deprecated since Discourse 2.1.0.beta1)")
|
2018-12-06 19:38:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can report the drop version' do
|
|
|
|
Discourse.deprecate(SecureRandom.hex, drop_from: "2.3.0")
|
|
|
|
|
2022-05-05 09:53:54 +08:00
|
|
|
expect(@fake_logger.warnings[0]).to include("(removal in Discourse 2.3.0)")
|
2018-12-06 19:38:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can raise deprecation error' do
|
|
|
|
expect {
|
|
|
|
Discourse.deprecate(SecureRandom.hex, raise_error: true)
|
|
|
|
}.to raise_error(Discourse::Deprecation)
|
|
|
|
end
|
2018-06-20 15:50:11 +08:00
|
|
|
end
|
|
|
|
|
2019-11-07 23:47:16 +08:00
|
|
|
describe "Utils.execute_command" do
|
|
|
|
it "works for individual commands" do
|
|
|
|
expect(Discourse::Utils.execute_command("pwd").strip).to eq(Rails.root.to_s)
|
|
|
|
expect(Discourse::Utils.execute_command("pwd", chdir: "plugins").strip).to eq("#{Rails.root.to_s}/plugins")
|
|
|
|
end
|
|
|
|
|
2021-04-12 11:55:54 +08:00
|
|
|
it "supports timeouts" do
|
|
|
|
expect do
|
|
|
|
Discourse::Utils.execute_command("sleep", "999999999999", timeout: 0.001)
|
2021-04-12 20:53:41 +08:00
|
|
|
end.to raise_error(RuntimeError)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
Discourse::Utils.execute_command({ "MYENV" => "MYVAL" }, "sleep", "999999999999", timeout: 0.001)
|
2021-04-12 11:55:54 +08:00
|
|
|
end.to raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
|
2019-11-07 23:47:16 +08:00
|
|
|
it "works with a block" do
|
|
|
|
Discourse::Utils.execute_command do |runner|
|
|
|
|
expect(runner.exec("pwd").strip).to eq(Rails.root.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
result = Discourse::Utils.execute_command(chdir: "plugins") do |runner|
|
|
|
|
expect(runner.exec("pwd").strip).to eq("#{Rails.root.to_s}/plugins")
|
|
|
|
runner.exec("pwd")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Should return output of block
|
|
|
|
expect(result.strip).to eq("#{Rails.root.to_s}/plugins")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not leak chdir between threads" do
|
|
|
|
has_done_chdir = false
|
|
|
|
has_checked_chdir = false
|
|
|
|
|
|
|
|
thread = Thread.new do
|
|
|
|
Discourse::Utils.execute_command(chdir: "plugins") do
|
|
|
|
has_done_chdir = true
|
|
|
|
sleep(0.01) until has_checked_chdir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sleep(0.01) until has_done_chdir
|
|
|
|
expect(Discourse::Utils.execute_command("pwd").strip).to eq(Rails.root.to_s)
|
|
|
|
has_checked_chdir = true
|
|
|
|
thread.join
|
|
|
|
end
|
2021-04-15 23:29:37 +08:00
|
|
|
|
|
|
|
it "raises error for unsafe shell" do
|
|
|
|
expect(Discourse::Utils.execute_command("pwd").strip).to eq(Rails.root.to_s)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
Discourse::Utils.execute_command("echo a b c")
|
|
|
|
end.to raise_error(RuntimeError)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
Discourse::Utils.execute_command({ "ENV1" => "VAL" }, "echo a b c")
|
|
|
|
end.to raise_error(RuntimeError)
|
|
|
|
|
|
|
|
expect(Discourse::Utils.execute_command("echo", "a", "b", "c").strip).to eq("a b c")
|
|
|
|
expect(Discourse::Utils.execute_command("echo a b c", unsafe_shell: true).strip).to eq("a b c")
|
|
|
|
end
|
2019-11-07 23:47:16 +08:00
|
|
|
end
|
|
|
|
|
2022-07-27 18:21:10 +08:00
|
|
|
describe ".clear_all_theme_cache!" do
|
2022-07-22 14:46:52 +08:00
|
|
|
before do
|
|
|
|
setup_s3
|
|
|
|
SiteSetting.s3_cdn_url = "https://s3.cdn.com/gg"
|
|
|
|
stub_s3_store
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:theme) { Fabricate(:theme) }
|
|
|
|
let!(:upload) { Fabricate(:s3_image_upload) }
|
|
|
|
let!(:upload_theme_field) do
|
|
|
|
Fabricate(
|
|
|
|
:theme_field,
|
|
|
|
theme: theme,
|
|
|
|
upload: upload,
|
|
|
|
type_id: ThemeField.types[:theme_upload_var],
|
|
|
|
target_id: Theme.targets[:common],
|
|
|
|
name: "imajee",
|
|
|
|
value: "",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
let!(:basic_html_field) do
|
|
|
|
Fabricate(
|
|
|
|
:theme_field,
|
|
|
|
theme: theme,
|
|
|
|
type_id: ThemeField.types[:html],
|
|
|
|
target_id: Theme.targets[:common],
|
|
|
|
name: "head_tag",
|
|
|
|
value: <<~HTML
|
|
|
|
<script type="text/discourse-plugin" version="0.1">
|
|
|
|
console.log(settings.uploads.imajee);
|
|
|
|
</script>
|
|
|
|
HTML
|
|
|
|
)
|
|
|
|
end
|
|
|
|
let!(:js_field) do
|
|
|
|
Fabricate(
|
|
|
|
:theme_field,
|
|
|
|
theme: theme,
|
|
|
|
type_id: ThemeField.types[:js],
|
|
|
|
target_id: Theme.targets[:extra_js],
|
|
|
|
name: "somefile.js",
|
|
|
|
value: <<~JS
|
|
|
|
console.log(settings.uploads.imajee);
|
|
|
|
JS
|
|
|
|
)
|
|
|
|
end
|
|
|
|
let!(:scss_field) do
|
|
|
|
Fabricate(
|
|
|
|
:theme_field,
|
|
|
|
theme: theme,
|
|
|
|
type_id: ThemeField.types[:scss],
|
|
|
|
target_id: Theme.targets[:common],
|
|
|
|
name: "scss",
|
|
|
|
value: <<~SCSS
|
|
|
|
.something { background: url($imajee); }
|
|
|
|
SCSS
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "invalidates all JS and CSS caches" do
|
|
|
|
Stylesheet::Manager.clear_theme_cache!
|
|
|
|
|
|
|
|
old_upload_url = Discourse.store.cdn_url(upload.url)
|
|
|
|
|
|
|
|
head_tag_script = Nokogiri::HTML5.fragment(
|
|
|
|
Theme.lookup_field(theme.id, :desktop, "head_tag")
|
|
|
|
).css('script').first
|
|
|
|
head_tag_js = JavascriptCache.find_by(digest: head_tag_script[:src][/\h{40}/]).content
|
|
|
|
expect(head_tag_js).to include(old_upload_url)
|
|
|
|
|
|
|
|
js_file_script = Nokogiri::HTML5.fragment(
|
|
|
|
Theme.lookup_field(theme.id, :extra_js, nil)
|
|
|
|
).css('script').first
|
|
|
|
file_js = JavascriptCache.find_by(digest: js_file_script[:src][/\h{40}/]).content
|
|
|
|
expect(file_js).to include(old_upload_url)
|
|
|
|
|
|
|
|
css_link_tag = Nokogiri::HTML5.fragment(
|
|
|
|
Stylesheet::Manager.new(theme_id: theme.id).stylesheet_link_tag(:desktop_theme, 'all')
|
|
|
|
).css('link').first
|
|
|
|
css = StylesheetCache.find_by(digest: css_link_tag[:href][/\h{40}/]).content
|
|
|
|
expect(css).to include("url(#{old_upload_url})")
|
|
|
|
|
|
|
|
SiteSetting.s3_cdn_url = "https://new.s3.cdn.com/gg"
|
|
|
|
new_upload_url = Discourse.store.cdn_url(upload.url)
|
|
|
|
|
|
|
|
head_tag_script = Nokogiri::HTML5.fragment(
|
|
|
|
Theme.lookup_field(theme.id, :desktop, "head_tag")
|
|
|
|
).css('script').first
|
|
|
|
head_tag_js = JavascriptCache.find_by(digest: head_tag_script[:src][/\h{40}/]).content
|
|
|
|
expect(head_tag_js).to include(old_upload_url)
|
|
|
|
|
|
|
|
js_file_script = Nokogiri::HTML5.fragment(
|
|
|
|
Theme.lookup_field(theme.id, :extra_js, nil)
|
|
|
|
).css('script').first
|
|
|
|
file_js = JavascriptCache.find_by(digest: js_file_script[:src][/\h{40}/]).content
|
|
|
|
expect(file_js).to include(old_upload_url)
|
|
|
|
|
|
|
|
css_link_tag = Nokogiri::HTML5.fragment(
|
|
|
|
Stylesheet::Manager.new(theme_id: theme.id).stylesheet_link_tag(:desktop_theme, 'all')
|
|
|
|
).css('link').first
|
|
|
|
css = StylesheetCache.find_by(digest: css_link_tag[:href][/\h{40}/]).content
|
|
|
|
expect(css).to include("url(#{old_upload_url})")
|
|
|
|
|
|
|
|
Discourse.clear_all_theme_cache!
|
|
|
|
|
|
|
|
head_tag_script = Nokogiri::HTML5.fragment(
|
|
|
|
Theme.lookup_field(theme.id, :desktop, "head_tag")
|
|
|
|
).css('script').first
|
|
|
|
head_tag_js = JavascriptCache.find_by(digest: head_tag_script[:src][/\h{40}/]).content
|
|
|
|
expect(head_tag_js).to include(new_upload_url)
|
|
|
|
|
|
|
|
js_file_script = Nokogiri::HTML5.fragment(
|
|
|
|
Theme.lookup_field(theme.id, :extra_js, nil)
|
|
|
|
).css('script').first
|
|
|
|
file_js = JavascriptCache.find_by(digest: js_file_script[:src][/\h{40}/]).content
|
|
|
|
expect(file_js).to include(new_upload_url)
|
|
|
|
|
|
|
|
css_link_tag = Nokogiri::HTML5.fragment(
|
|
|
|
Stylesheet::Manager.new(theme_id: theme.id).stylesheet_link_tag(:desktop_theme, 'all')
|
|
|
|
).css('link').first
|
|
|
|
css = StylesheetCache.find_by(digest: css_link_tag[:href][/\h{40}/]).content
|
|
|
|
expect(css).to include("url(#{new_upload_url})")
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|