2013-02-05 13:16:51 -06:00
|
|
|
require 'spec_helper'
|
|
|
|
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
|
|
|
|
Discourse.current_hostname.should == 'foo.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'base_url' do
|
2014-01-08 17:51:38 -06:00
|
|
|
context 'when https is off' do
|
2013-02-05 13:16:51 -06:00
|
|
|
before do
|
2014-01-08 17:51:38 -06:00
|
|
|
SiteSetting.expects(:use_https?).returns(false)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2014-01-08 17:51:38 -06:00
|
|
|
it 'has a non https base url' do
|
2013-02-05 13:16:51 -06:00
|
|
|
Discourse.base_url.should == "http://foo.com"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-08 17:51:38 -06:00
|
|
|
context 'when https is on' do
|
2013-02-05 13:16:51 -06:00
|
|
|
before do
|
2014-01-08 17:51:38 -06:00
|
|
|
SiteSetting.expects(:use_https?).returns(true)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a non-ssl base url' do
|
|
|
|
Discourse.base_url.should == "https://foo.com"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a non standard port specified' do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:port).returns(3000)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the non standart port in the base url" do
|
|
|
|
Discourse.base_url.should == "http://foo.com:3000"
|
|
|
|
end
|
2013-05-10 15:58:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-06 02:28:37 -05:00
|
|
|
context '#site_contact_user' do
|
2013-05-10 15:58:23 -05:00
|
|
|
|
|
|
|
let!(:admin) { Fabricate(:admin) }
|
2013-05-31 10:41:40 -05:00
|
|
|
let!(:another_admin) { Fabricate(:admin) }
|
2013-05-10 15:58:23 -05:00
|
|
|
|
2013-09-06 02:28:37 -05:00
|
|
|
it 'returns the user specified by the site setting site_contact_username' do
|
|
|
|
SiteSetting.stubs(:site_contact_username).returns(another_admin.username)
|
|
|
|
Discourse.site_contact_user.should == another_admin
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2014-01-23 04:26:31 -06:00
|
|
|
it 'returns the user specified by the site setting site_contact_username regardless of its case' do
|
|
|
|
SiteSetting.stubs(:site_contact_username).returns(another_admin.username.upcase)
|
|
|
|
Discourse.site_contact_user.should == another_admin
|
|
|
|
end
|
|
|
|
|
2013-05-10 15:58:23 -05:00
|
|
|
it 'returns the first admin user otherwise' do
|
2013-09-06 02:28:37 -05:00
|
|
|
SiteSetting.stubs(:site_contact_username).returns(nil)
|
|
|
|
Discourse.site_contact_user.should == admin
|
2013-05-10 15:58:23 -05:00
|
|
|
end
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-07-31 16:26:34 -05:00
|
|
|
context "#store" do
|
|
|
|
|
|
|
|
it "returns LocalStore by default" do
|
2013-11-05 12:04:47 -06:00
|
|
|
Discourse.store.should be_a(FileStore::LocalStore)
|
2013-07-31 16:26:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns S3Store when S3 is enabled" do
|
2014-09-24 15:52:09 -05:00
|
|
|
SiteSetting.stubs(:enable_s3_uploads?).returns(true)
|
|
|
|
SiteSetting.stubs(:s3_upload_bucket).returns("s3_bucket")
|
|
|
|
SiteSetting.stubs(:s3_access_key_id).returns("s3_access_key_id")
|
|
|
|
SiteSetting.stubs(:s3_secret_access_key).returns("s3_secret_access_key")
|
2013-11-05 12:04:47 -06:00
|
|
|
Discourse.store.should be_a(FileStore::S3Store)
|
2013-07-31 16:26:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-02-12 22:37:28 -06:00
|
|
|
context "#enable_readonly_mode" do
|
|
|
|
|
|
|
|
it "adds a key in redis and publish a message through the message bus" do
|
|
|
|
$redis.expects(:set).with(Discourse.readonly_mode_key, 1)
|
|
|
|
MessageBus.expects(:publish).with(Discourse.readonly_channel, true)
|
|
|
|
Discourse.enable_readonly_mode
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "#disable_readonly_mode" do
|
|
|
|
|
|
|
|
it "removes a key from redis and publish a message through the message bus" do
|
|
|
|
$redis.expects(:del).with(Discourse.readonly_mode_key)
|
|
|
|
MessageBus.expects(:publish).with(Discourse.readonly_channel, false)
|
|
|
|
Discourse.disable_readonly_mode
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "#readonly_mode?" do
|
|
|
|
|
|
|
|
it "returns true when the key is present in redis" do
|
|
|
|
$redis.expects(:get).with(Discourse.readonly_mode_key).returns("1")
|
|
|
|
Discourse.readonly_mode?.should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when the key is not present in redis" do
|
|
|
|
$redis.expects(:get).with(Discourse.readonly_mode_key).returns(nil)
|
|
|
|
Discourse.readonly_mode?.should == false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-02-20 21:30:25 -06:00
|
|
|
context "#handle_exception" do
|
2014-07-17 15:22:46 -05:00
|
|
|
|
|
|
|
class TempSidekiqLogger < Sidekiq::ExceptionHandler::Logger
|
2014-02-20 21:30:25 -06:00
|
|
|
attr_accessor :exception, :context
|
2014-07-17 15:22:46 -05:00
|
|
|
def call(ex, ctx)
|
|
|
|
self.exception = ex
|
|
|
|
self.context = ctx
|
2014-02-20 21:30:25 -06:00
|
|
|
end
|
|
|
|
end
|
2014-07-17 15:22:46 -05:00
|
|
|
|
|
|
|
let!(:logger) { TempSidekiqLogger.new }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Sidekiq.error_handlers.clear
|
|
|
|
Sidekiq.error_handlers << logger
|
|
|
|
end
|
2014-09-24 15:52:09 -05:00
|
|
|
|
2014-02-20 21:30:25 -06:00
|
|
|
it "should not fail when called" do
|
|
|
|
exception = StandardError.new
|
|
|
|
|
2014-07-17 15:22:46 -05:00
|
|
|
Discourse.handle_exception(exception, nil, nil)
|
2014-02-20 21:30:25 -06:00
|
|
|
logger.exception.should == exception
|
|
|
|
logger.context.keys.should == [:current_db, :current_hostname]
|
|
|
|
end
|
2014-07-17 15:22:46 -05:00
|
|
|
|
|
|
|
it "correctly passes extra context" do
|
|
|
|
exception = StandardError.new
|
|
|
|
|
2014-07-17 17:19:58 -05:00
|
|
|
Discourse.handle_exception(exception, {message: "Doing a test", post_id: 31}, nil)
|
2014-07-17 15:22:46 -05:00
|
|
|
logger.exception.should == exception
|
2014-07-17 17:19:58 -05:00
|
|
|
logger.context.keys.sort.should == [:current_db, :current_hostname, :message, :post_id].sort
|
2014-07-17 15:22:46 -05:00
|
|
|
end
|
2014-02-20 21:30:25 -06:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|