|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +class ThreeScale::ContentSecurityPolicy::AdminPortalTest < ActiveSupport::TestCase |
| 4 | + test 'config returns Rails configuration for CSP' do |
| 5 | + config = ThreeScale::ContentSecurityPolicy::AdminPortal.config |
| 6 | + |
| 7 | + assert_not_nil config |
| 8 | + assert_kind_of ActiveSupport::OrderedOptions, config |
| 9 | + end |
| 10 | + |
| 11 | + test 'enabled? returns true in test environment' do |
| 12 | + assert_equal true, ThreeScale::ContentSecurityPolicy::AdminPortal.enabled? |
| 13 | + end |
| 14 | + |
| 15 | + test 'policy_config returns hash of CSP directives from YAML' do |
| 16 | + policy_hash = ThreeScale::ContentSecurityPolicy::AdminPortal.policy_config |
| 17 | + |
| 18 | + assert_kind_of Hash, policy_hash |
| 19 | + assert policy_hash.present? |
| 20 | + |
| 21 | + # Verify it contains restrictive directives |
| 22 | + assert policy_hash.key?(:default_src) |
| 23 | + assert_includes policy_hash[:default_src], "'self'" |
| 24 | + assert policy_hash.key?(:script_src) |
| 25 | + assert_includes policy_hash[:script_src], "'self'" |
| 26 | + end |
| 27 | + |
| 28 | + test 'report_only? returns false from YAML config' do |
| 29 | + assert_equal false, ThreeScale::ContentSecurityPolicy::AdminPortal.report_only? |
| 30 | + end |
| 31 | + |
| 32 | + test 'policy_config returns empty hash when config.admin_portal_policy is nil' do |
| 33 | + ThreeScale::ContentSecurityPolicy::AdminPortal.config.stubs(:admin_portal_policy).returns(nil) |
| 34 | + policy_hash = ThreeScale::ContentSecurityPolicy::AdminPortal.policy_config |
| 35 | + |
| 36 | + assert_equal({}, policy_hash) |
| 37 | + end |
| 38 | +end |
| 39 | + |
| 40 | +class ThreeScale::ContentSecurityPolicy::DeveloperPortalTest < ActiveSupport::TestCase |
| 41 | + test 'config returns Rails configuration for CSP' do |
| 42 | + config = ThreeScale::ContentSecurityPolicy::DeveloperPortal.config |
| 43 | + |
| 44 | + assert_not_nil config |
| 45 | + assert_kind_of ActiveSupport::OrderedOptions, config |
| 46 | + end |
| 47 | + |
| 48 | + test 'enabled? returns true in test environment' do |
| 49 | + assert_equal true, ThreeScale::ContentSecurityPolicy::DeveloperPortal.enabled? |
| 50 | + end |
| 51 | + |
| 52 | + test 'policy_config returns hash of CSP directives from YAML' do |
| 53 | + policy_hash = ThreeScale::ContentSecurityPolicy::DeveloperPortal.policy_config |
| 54 | + |
| 55 | + assert_kind_of Hash, policy_hash |
| 56 | + assert policy_hash.present? |
| 57 | + |
| 58 | + # Verify it contains the permissive default_src directive |
| 59 | + assert policy_hash.key?(:default_src) |
| 60 | + assert_includes policy_hash[:default_src], "*" |
| 61 | + assert_includes policy_hash[:default_src], "'unsafe-eval'" |
| 62 | + assert_includes policy_hash[:default_src], "'unsafe-inline'" |
| 63 | + end |
| 64 | + |
| 65 | + test 'report_only? returns false from YAML config' do |
| 66 | + assert_equal false, ThreeScale::ContentSecurityPolicy::DeveloperPortal.report_only? |
| 67 | + end |
| 68 | + |
| 69 | + test 'policy_config returns empty hash when config.developer_portal_policy is nil' do |
| 70 | + ThreeScale::ContentSecurityPolicy::DeveloperPortal.config.stubs(:developer_portal_policy).returns(nil) |
| 71 | + policy_hash = ThreeScale::ContentSecurityPolicy::DeveloperPortal.policy_config |
| 72 | + |
| 73 | + assert_equal({}, policy_hash) |
| 74 | + end |
| 75 | +end |
| 76 | + |
| 77 | +class ThreeScale::ContentSecurityPolicyWithoutYAMLTest < ActiveSupport::TestCase |
| 78 | + test 'AdminPortal enabled? returns false when config is missing' do |
| 79 | + empty_config = ActiveSupport::OrderedOptions.new |
| 80 | + ThreeScale::ContentSecurityPolicy::AdminPortal.stubs(:config).returns(empty_config) |
| 81 | + |
| 82 | + assert_equal false, ThreeScale::ContentSecurityPolicy::AdminPortal.enabled? |
| 83 | + end |
| 84 | + |
| 85 | + test 'AdminPortal policy_config returns empty hash when config is missing' do |
| 86 | + empty_config = ActiveSupport::OrderedOptions.new |
| 87 | + ThreeScale::ContentSecurityPolicy::AdminPortal.stubs(:config).returns(empty_config) |
| 88 | + |
| 89 | + policy_hash = ThreeScale::ContentSecurityPolicy::AdminPortal.policy_config |
| 90 | + |
| 91 | + assert_equal({}, policy_hash) |
| 92 | + end |
| 93 | + |
| 94 | + test 'AdminPortal report_only? returns false when config is missing' do |
| 95 | + empty_config = ActiveSupport::OrderedOptions.new |
| 96 | + ThreeScale::ContentSecurityPolicy::AdminPortal.stubs(:config).returns(empty_config) |
| 97 | + |
| 98 | + assert_equal false, ThreeScale::ContentSecurityPolicy::AdminPortal.report_only? |
| 99 | + end |
| 100 | + |
| 101 | + test 'AdminPortal enabled? handles nil config values gracefully' do |
| 102 | + config = ActiveSupport::OrderedOptions.new |
| 103 | + config.enabled = nil |
| 104 | + ThreeScale::ContentSecurityPolicy::AdminPortal.stubs(:config).returns(config) |
| 105 | + |
| 106 | + assert_equal false, ThreeScale::ContentSecurityPolicy::AdminPortal.enabled? |
| 107 | + end |
| 108 | + |
| 109 | + test 'AdminPortal report_only? handles nil config values gracefully' do |
| 110 | + config = ActiveSupport::OrderedOptions.new |
| 111 | + config.report_only = nil |
| 112 | + ThreeScale::ContentSecurityPolicy::AdminPortal.stubs(:config).returns(config) |
| 113 | + |
| 114 | + assert_equal false, ThreeScale::ContentSecurityPolicy::AdminPortal.report_only? |
| 115 | + end |
| 116 | +end |
0 commit comments