require 'test_helper' # # Test the system configuration model object # class SystemConfigurationTest < ActiveSupport::TestCase should_have_field :environment, type: String, klass: SystemConfiguration, default: 'test' should_have_field :default_email, type: String, klass: SystemConfiguration, default: 'support@app47.com' should_have_field :support_email, type: String, klass: SystemConfiguration, default: 'support@app47.com' should_have_field :smtp_name, type: String, klass: SystemConfiguration should_have_field :smtp_address, type: String, klass: SystemConfiguration should_have_field :smtp_domain, type: String, klass: SystemConfiguration should_have_field :smtp_port, type: Integer, klass: SystemConfiguration, default: 587 should_have_field :smtp_user_name, type: String, klass: SystemConfiguration should_have_field :smtp_password, type: String, klass: SystemConfiguration should_have_field :smtp_enable_starttls_auto, type: Mongoid::Boolean, klass: SystemConfiguration should_have_field :mailgun_api_key, type: String, klass: SystemConfiguration should_have_field :base_url, type: String, klass: SystemConfiguration should_have_field :cdn_url, type: String, klass: SystemConfiguration # Slack should_have_field :slack_api_url, type: String, klass: SystemConfiguration should_have_field :slack_support_channel, type: String, klass: SystemConfiguration, default: 'support' should_have_field :slack_sales_channel, type: String, klass: SystemConfiguration, default: 'sales' # Twillo should_have_field :twilio_account_id, type: String, klass: SystemConfiguration should_have_field :twilio_auth_token, type: String, klass: SystemConfiguration should_have_field :twilio_phone_number, type: String, klass: SystemConfiguration # Zendesk should_have_field :zendesk_token, type: String, klass: SystemConfiguration should_have_field :zendesk_base_url, type: String, default: 'https://app47.zendesk.com', klass: SystemConfiguration should_have_field :zendesk_documentation_path, type: String, default: 'hc', klass: SystemConfiguration should_have_field :zendesk_support_path, type: String, default: 'hc/en-us/requests', klass: SystemConfiguration should_have_field :zendesk_updates_path, type: String, klass: SystemConfiguration, default: 'hc' # AWS should_have_field :aws_region, type: String, klass: SystemConfiguration should_have_field :aws_secret_access_key, type: String, klass: SystemConfiguration should_have_field :aws_secret_access_key, type: String, klass: SystemConfiguration should_have_field :aws_auto_scaling_group_name, type: String, klass: SystemConfiguration # Switchboard should_have_field :switchboard_base_url, type: String, klass: SystemConfiguration, default: 'https://switchboard.app47.com' should_have_field :switchboard_stack_id, type: String, klass: SystemConfiguration should_have_field :switchboard_stack_api_token, type: String, klass: SystemConfiguration teardown do Rails.cache.delete 'SystemConfiguration::test' end context 'default behavior' do should 'automatically create a system configuration' do SystemConfiguration.destroy_all assert_equal 0, SystemConfiguration.all.count SystemConfiguration.configuration assert_equal 1, SystemConfiguration.all.count end should 'have default values' do assert_equal 587, SystemConfiguration.smtp_port end end context 'handle exception' do should 'deal with rails catch throwing an exception' do Rails.cache.expects(:fetch).once.raises(ArgumentError) Rails.cache.expects(:delete).twice SystemConfiguration.expects(:create).never SystemConfiguration.base_url Rails.cache.unstub(:fetch) Rails.cache.unstub(:delete) end end context 'handle' do should 'respond_to_missing?' do assert SystemConfiguration.respond_to? 'base_url' refute SystemConfiguration.respond_to? 'url' end end context 'only call things once' do setup do if SystemConfiguration.all.blank? SystemConfiguration.create!(base_url: 'http://somewhere.com', environment: Rails.env, smtp_name: 'SMTP') end SystemConfiguration.expects(:where).once.returns(SystemConfiguration.all) end should 'only call once' do SystemConfiguration.base_url SystemConfiguration.smtp_port SystemConfiguration.smtp_name end should 'update base_url' do url = 'http://something.new' config = SystemConfiguration.all.first || SystemConfiguration.new config.update_attributes!(base_url: url) assert_equal url, SystemConfiguration.base_url end end context 'smtp configuration' do should 'have smtp config' do smtp_config = SystemConfiguration.smtp_configuration assert_not_nil smtp_config assert_not_empty smtp_config end should 'not be configured' do refute SystemConfiguration.smtp_configured? refute SystemConfiguration.mailgun_configured? end should 'be configured' do assert SystemConfiguration.configuration.update smtp_name: 'app47', smtp_address: 'app47.com', smtp_domain: 'app47.com' assert SystemConfiguration.smtp_configured? refute SystemConfiguration.mailgun_configured? end should 'be configured with mailgun' do assert SystemConfiguration.configuration.update smtp_name: 'app47', smtp_address: 'app47.com', smtp_domain: 'app47.com', mailgun_api_key: 'abc123' assert SystemConfiguration.smtp_configured? assert SystemConfiguration.mailgun_configured? end end context 'aws configuration' do should 'not be configured' do refute SystemConfiguration.aws_configured? refute SystemConfiguration.aws_auto_scaling_configured? end should 'aws is configured' do assert SystemConfiguration.configuration.update aws_region: 'us' refute SystemConfiguration.aws_configured? refute SystemConfiguration.aws_auto_scaling_configured? assert SystemConfiguration.configuration.update aws_access_key_id: 'key' refute SystemConfiguration.aws_configured? refute SystemConfiguration.aws_auto_scaling_configured? assert SystemConfiguration.configuration.update aws_secret_access_key: 'abc123' assert SystemConfiguration.aws_configured? refute SystemConfiguration.aws_auto_scaling_configured? assert SystemConfiguration.configuration.update aws_auto_scaling_group_name: 'g1' assert SystemConfiguration.aws_configured? assert SystemConfiguration.aws_auto_scaling_configured? end end context 'twilio configuration' do should 'not be configured' do refute SystemConfiguration.twilio_configured? end should 'configured' do assert SystemConfiguration.configuration.update twilio_account_id: 'us' refute SystemConfiguration.twilio_configured? assert SystemConfiguration.configuration.update twilio_auth_token: 'key' refute SystemConfiguration.twilio_configured? assert SystemConfiguration.configuration.update twilio_phone_number: 'abc123' assert SystemConfiguration.twilio_configured? end end context 'switchboard configuration' do should 'not be configured' do refute SystemConfiguration.switchboard_configured? end should 'configured' do assert SystemConfiguration.configuration.update switchboard_base_url: 'us' refute SystemConfiguration.switchboard_configured? assert SystemConfiguration.configuration.update switchboard_stack_api_token: 'key' refute SystemConfiguration.switchboard_configured? assert SystemConfiguration.configuration.update switchboard_stack_id: 'abc123' assert SystemConfiguration.switchboard_configured? end end context 'zendesk configuration' do should 'not be configured' do refute SystemConfiguration.zendesk_configured? end should 'configured' do assert SystemConfiguration.configuration.update zendesk_token: 'us' assert SystemConfiguration.zendesk_configured? end should 'zendesk url with no user' do assert_equal 'https://app47.zendesk.com/hc', SystemConfiguration.zendesk_url assert_equal 'https://app47.zendesk.com/hc', SystemConfiguration.zendesk_documentation_url assert_equal 'https://app47.zendesk.com/hc/en-us/requests', SystemConfiguration.zendesk_requests_url assert_equal 'https://app47.zendesk.com/hc/en-us/requests/new', SystemConfiguration.zendesk_new_request_url assert_equal 'https://app47.zendesk.com/hc', SystemConfiguration.zendesk_updates_url end should 'zendesk url with user but not configured' do user = MockUser.new email: 'foo@bar.com' assert_equal 'https://app47.zendesk.com/hc', SystemConfiguration.zendesk_url(user: user) assert_equal 'https://app47.zendesk.com/hc', SystemConfiguration.zendesk_documentation_url(user) assert_equal 'https://app47.zendesk.com/hc/en-us/requests', SystemConfiguration.zendesk_requests_url(user) assert_equal 'https://app47.zendesk.com/hc/en-us/requests/new', SystemConfiguration.zendesk_new_request_url(user) assert_equal 'https://app47.zendesk.com/hc', SystemConfiguration.zendesk_updates_url(user) end should 'zendesk url with user and configured' do assert SystemConfiguration.configuration.update zendesk_token: 'us' user = MockUser.create(email: 'foo@bar.com', name: 'Foo') assert_not_nil user assert SystemConfiguration.zendesk_url(user: user).start_with?('https://app47.zendesk.com/access/jwt?jwt=') assert SystemConfiguration.zendesk_documentation_url(user).start_with?('https://app47.zendesk.com/access/jwt?jwt=') assert SystemConfiguration.zendesk_requests_url(user).start_with?('https://app47.zendesk.com/access/jwt?jwt=') assert SystemConfiguration.zendesk_new_request_url(user).start_with?('https://app47.zendesk.com/access/jwt?jwt=') assert SystemConfiguration.zendesk_updates_url(user).start_with?('https://app47.zendesk.com/access/jwt?jwt=') end end end class MockUser include StandardModel field :email, type: String field :name, type: String end