2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
require 'discourse'
|
|
|
|
|
|
|
|
describe Discourse do
|
|
|
|
|
|
|
|
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-09 11:34:37 -05:00
|
|
|
expect(Discourse.current_hostname).to eq('foo.com')
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'base_url' do
|
2014-01-08 18:51:38 -05:00
|
|
|
context 'when https is off' do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2016-07-28 13:54:17 -04:00
|
|
|
SiteSetting.force_https = false
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-01-08 18:51:38 -05:00
|
|
|
it 'has a non https base url' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(Discourse.base_url).to eq("http://foo.com")
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-08 18:51:38 -05:00
|
|
|
context 'when https is on' do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2016-07-28 13:54:17 -04:00
|
|
|
SiteSetting.force_https = true
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a non-ssl base url' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(Discourse.base_url).to eq("https://foo.com")
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a non standard port specified' do
|
|
|
|
before do
|
2016-07-28 13:54:17 -04:00
|
|
|
SiteSetting.port = 3000
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the non standart port in the base url" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(Discourse.base_url).to eq("http://foo.com:3000")
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-10 16:58:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-06 03:28:37 -04:00
|
|
|
context '#site_contact_user' do
|
2013-05-10 16:58:23 -04:00
|
|
|
|
|
|
|
let!(:admin) { Fabricate(:admin) }
|
2013-05-31 11:41:40 -04:00
|
|
|
let!(:another_admin) { Fabricate(:admin) }
|
2013-05-10 16:58:23 -04:00
|
|
|
|
2013-09-06 03:28:37 -04:00
|
|
|
it 'returns the user specified by the site setting site_contact_username' do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.site_contact_username = another_admin.username
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(Discourse.site_contact_user).to eq(another_admin)
|
2014-01-23 05:26:31 -05:00
|
|
|
end
|
|
|
|
|
2015-11-24 14:37:33 -05:00
|
|
|
it 'returns the system user otherwise' do
|
2016-07-28 13:54:17 -04:00
|
|
|
SiteSetting.site_contact_username = nil
|
2015-11-24 14:37:33 -05:00
|
|
|
expect(Discourse.site_contact_user.username).to eq("system")
|
2013-05-10 16:58:23 -04:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-31 17:26:34 -04:00
|
|
|
context "#store" do
|
|
|
|
|
|
|
|
it "returns LocalStore by default" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(Discourse.store).to be_a(FileStore::LocalStore)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns S3Store when S3 is enabled" do
|
2016-07-28 13:54:17 -04: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-09 11:34:37 -05:00
|
|
|
expect(Discourse.store).to be_a(FileStore::S3Store)
|
2013-07-31 17:26:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-06-29 01:55:17 -04:00
|
|
|
context 'readonly mode' do
|
2016-06-29 02:19:18 -04:00
|
|
|
let(:readonly_mode_key) { Discourse::READONLY_MODE_KEY }
|
2016-06-29 01:55:17 -04:00
|
|
|
let(:readonly_mode_ttl) { Discourse::READONLY_MODE_KEY_TTL }
|
2016-06-29 02:19:18 -04:00
|
|
|
let(:user_readonly_mode_key) { Discourse::USER_READONLY_MODE_KEY }
|
2014-02-12 23:37:28 -05:00
|
|
|
|
2016-06-29 01:55:17 -04:00
|
|
|
after do
|
2016-06-29 02:19:18 -04:00
|
|
|
$redis.del(readonly_mode_key)
|
|
|
|
$redis.del(user_readonly_mode_key)
|
2014-02-12 23:37:28 -05:00
|
|
|
end
|
|
|
|
|
2016-06-29 02:19:18 -04:00
|
|
|
def assert_readonly_mode(message, key, ttl = -1)
|
|
|
|
expect(message.channel).to eq(Discourse.readonly_channel)
|
2016-06-29 01:55:17 -04:00
|
|
|
expect(message.data).to eq(true)
|
|
|
|
expect($redis.get(key)).to eq("1")
|
|
|
|
expect($redis.ttl(key)).to eq(ttl)
|
|
|
|
end
|
2014-02-12 23:37:28 -05:00
|
|
|
|
2016-06-29 02:19:18 -04:00
|
|
|
def assert_readonly_mode_disabled(message, key)
|
|
|
|
expect(message.channel).to eq(Discourse.readonly_channel)
|
2016-06-29 01:55:17 -04:00
|
|
|
expect(message.data).to eq(false)
|
|
|
|
expect($redis.get(key)).to eq(nil)
|
|
|
|
end
|
2014-02-12 23:37:28 -05:00
|
|
|
|
2017-07-26 13:21:33 -04:00
|
|
|
def get_readonly_message
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
messages.first { |m| m.channel == Discourse.readonly_channel }
|
2017-07-26 13:21:33 -04:00
|
|
|
end
|
|
|
|
|
2016-06-29 02:19:18 -04:00
|
|
|
describe ".enable_readonly_mode" do
|
2016-06-29 01:55:17 -04:00
|
|
|
it "adds a key in redis and publish a message through the message bus" do
|
2016-06-29 02:19:18 -04:00
|
|
|
expect($redis.get(readonly_mode_key)).to eq(nil)
|
2017-07-26 13:21:33 -04:00
|
|
|
message = get_readonly_message { Discourse.enable_readonly_mode }
|
2016-06-29 02:19:18 -04:00
|
|
|
assert_readonly_mode(message, readonly_mode_key, readonly_mode_ttl)
|
|
|
|
end
|
2014-02-12 23:37:28 -05:00
|
|
|
|
2016-06-29 02:19:18 -04:00
|
|
|
context 'user enabled readonly mode' do
|
|
|
|
it "adds a key in redis and publish a message through the message bus" do
|
|
|
|
expect($redis.get(user_readonly_mode_key)).to eq(nil)
|
2017-07-26 13:21:33 -04:00
|
|
|
message = get_readonly_message { Discourse.enable_readonly_mode(user_readonly_mode_key) }
|
2016-06-29 02:19:18 -04:00
|
|
|
assert_readonly_mode(message, user_readonly_mode_key)
|
|
|
|
end
|
2016-06-29 01:55:17 -04:00
|
|
|
end
|
2014-02-12 23:37:28 -05:00
|
|
|
end
|
|
|
|
|
2016-06-29 02:19:18 -04:00
|
|
|
describe ".disable_readonly_mode" do
|
2016-06-29 01:55:17 -04:00
|
|
|
it "removes a key from redis and publish a message through the message bus" do
|
|
|
|
Discourse.enable_readonly_mode
|
|
|
|
|
2017-07-26 13:21:33 -04:00
|
|
|
message = get_readonly_message do
|
2016-06-29 01:55:17 -04:00
|
|
|
Discourse.disable_readonly_mode
|
2017-07-26 13:21:33 -04:00
|
|
|
end
|
2014-02-12 23:37:28 -05:00
|
|
|
|
2016-06-29 02:19:18 -04:00
|
|
|
assert_readonly_mode_disabled(message, readonly_mode_key)
|
|
|
|
end
|
2014-02-12 23:37:28 -05:00
|
|
|
|
2016-06-29 02:19:18 -04:00
|
|
|
context 'user disabled readonly mode' do
|
|
|
|
it "removes readonly key in redis and publish a message through the message bus" do
|
|
|
|
Discourse.enable_readonly_mode(user_enabled: true)
|
2017-07-26 13:21:33 -04:00
|
|
|
message = get_readonly_message { Discourse.disable_readonly_mode(user_enabled: true) }
|
2016-06-29 02:19:18 -04:00
|
|
|
assert_readonly_mode_disabled(message, user_readonly_mode_key)
|
|
|
|
end
|
2016-06-29 01:55:17 -04:00
|
|
|
end
|
2015-04-24 14:32:18 -04:00
|
|
|
end
|
|
|
|
|
2016-06-29 02:19:18 -04:00
|
|
|
describe ".readonly_mode?" do
|
2016-06-29 01:55:17 -04: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
|
2016-06-29 02:19:18 -04:00
|
|
|
$redis.set(readonly_mode_key, 1)
|
2016-02-29 05:58:42 -05:00
|
|
|
expect(Discourse.readonly_mode?).to eq(true)
|
|
|
|
end
|
2014-02-12 23:37:28 -05:00
|
|
|
|
2016-06-29 01:55:17 -04:00
|
|
|
it "returns true when Discourse is recently read only" do
|
|
|
|
Discourse.received_readonly!
|
|
|
|
expect(Discourse.readonly_mode?).to eq(true)
|
|
|
|
end
|
2016-08-25 11:30:41 -04:00
|
|
|
|
|
|
|
it "returns true when user enabled readonly mode key is present in redis" do
|
2017-01-11 03:38:07 -05:00
|
|
|
Discourse.enable_readonly_mode(user_readonly_mode_key)
|
2016-08-25 11:30:41 -04:00
|
|
|
expect(Discourse.readonly_mode?).to eq(true)
|
|
|
|
|
2017-01-11 03:38:07 -05:00
|
|
|
Discourse.disable_readonly_mode(user_readonly_mode_key)
|
2016-08-25 11:30:41 -04:00
|
|
|
expect(Discourse.readonly_mode?).to eq(false)
|
|
|
|
end
|
2014-02-12 23:37:28 -05:00
|
|
|
end
|
|
|
|
|
2016-06-29 02:19:18 -04:00
|
|
|
describe ".received_readonly!" do
|
2016-06-29 01:55:17 -04:00
|
|
|
it "sets the right time" do
|
|
|
|
time = Discourse.received_readonly!
|
|
|
|
expect(Discourse.last_read_only['default']).to eq(time)
|
|
|
|
end
|
2016-02-29 05:58:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-20 22:30:25 -05:00
|
|
|
context "#handle_exception" do
|
2014-07-17 16:22:46 -04:00
|
|
|
|
|
|
|
class TempSidekiqLogger < Sidekiq::ExceptionHandler::Logger
|
2014-02-20 22:30:25 -05:00
|
|
|
attr_accessor :exception, :context
|
2014-07-17 16:22:46 -04:00
|
|
|
def call(ex, ctx)
|
|
|
|
self.exception = ex
|
|
|
|
self.context = ctx
|
2014-02-20 22:30:25 -05:00
|
|
|
end
|
|
|
|
end
|
2014-07-17 16:22:46 -04:00
|
|
|
|
|
|
|
let!(:logger) { TempSidekiqLogger.new }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Sidekiq.error_handlers.clear
|
|
|
|
Sidekiq.error_handlers << logger
|
|
|
|
end
|
2014-09-24 16:52:09 -04:00
|
|
|
|
2014-02-20 22:30:25 -05:00
|
|
|
it "should not fail when called" do
|
|
|
|
exception = StandardError.new
|
|
|
|
|
2015-02-09 15:47:46 -05:00
|
|
|
Discourse.handle_job_exception(exception, nil, nil)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(logger.exception).to eq(exception)
|
|
|
|
expect(logger.context.keys).to eq([:current_db, :current_hostname])
|
2014-02-20 22:30:25 -05:00
|
|
|
end
|
2014-07-17 16:22:46 -04:00
|
|
|
|
|
|
|
it "correctly passes extra context" do
|
|
|
|
exception = StandardError.new
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
Discourse.handle_job_exception(exception, { message: "Doing a test", post_id: 31 }, nil)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(logger.exception).to eq(exception)
|
|
|
|
expect(logger.context.keys.sort).to eq([:current_db, :current_hostname, :message, :post_id].sort)
|
2014-07-17 16:22:46 -04:00
|
|
|
end
|
2014-02-20 22:30:25 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|