Skip to content

Commit 40fa119

Browse files
committed
feat: Move some validation to the config.
1 parent 8997c90 commit 40fa119

File tree

6 files changed

+82
-31
lines changed

6 files changed

+82
-31
lines changed

hyperproof/lib/cfa_security_controls/hyperproof/clients/hyperproof.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ def conn
124124
#
125125
# @return [String] The authentication token.
126126
def auth_token
127-
unless config.hyperproof_client_id && config.hyperproof_client_secret
128-
raise Unauthorized, 'Missing Hyperproof credentials'
129-
end
130-
131127
response = Faraday.post(
132128
'https://accounts.hyperproof.app/oauth/token',
133129
{

hyperproof/spec/spec_helper.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@
1313
end
1414
end
1515

16-
# Include the gem.
16+
# Include the gem and test helpers.
1717
require_relative '../lib/cfa-security-controls-hyperproof'
18+
require_relative 'support/helpers'
1819

1920
RSpec.configure do |config|
2021
# Keep the original $stderr and $stdout so that we can suppress output during
2122
# tests.
2223
original_stderr = $stderr
2324
original_stdout = $stdout
2425

26+
config.include Helpers::Config
27+
2528
config.before do
2629
# Clear the configuration before each test.
27-
CfaSecurityControls::Hyperproof.instance_variable_set(:@config, nil)
28-
30+
stub_const('ENV', default_config_env)
31+
clear_config
32+
allow(File).to receive(:exist?).and_call_original
2933
end
3034

3135
config.before(:all) do
32-
# Suppress output.
36+
# Suppress logger output.
3337
$stderr = File.new(File::NULL, 'w')
3438
$stdout = File.new(File::NULL, 'w')
3539
end

hyperproof/spec/support/helpers.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'helpers/config'
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
module Helpers
4+
# Test helpers for system configuration.
5+
module Config
6+
# Clear the current configuration.
7+
def clear_config
8+
CfaSecurityControls::Hyperproof.instance_variable_set(:@config, nil)
9+
end
10+
11+
# Default configuration environment variables.
12+
#
13+
# This is useful for tests that need to override the environment, but need
14+
# the configuration to be valid.
15+
#
16+
# @return [Hash] A hash of environment variables.
17+
def default_config_env
18+
{
19+
'APTIBLE_PASSWORD' => 'aptible_password',
20+
'APTIBLE_USERNAME' => 'aptible_username',
21+
'HYPERPROOF_CLIENT_ID' => 'hyperproof_client_id',
22+
'HYPERPROOF_CLIENT_SECRET' => 'hyperproof_client_secret'
23+
}
24+
end
25+
26+
# Force the configuration to be valid without actually validating it.
27+
#
28+
# This is useful for tests that need to run without a valid configuration
29+
# but still want to avoid validation errors.
30+
#
31+
# rubocop:disable RSpec/AnyInstance
32+
def force_valid_config
33+
allow_any_instance_of(CfaSecurityControls::Hyperproof::Config).to \
34+
receive(:validate!).and_return(true)
35+
end
36+
# rubocop:enable RSpec/AnyInstance
37+
38+
# Set the configuration for the system.
39+
#
40+
# @param options [Hash] Options to set in the configuration.
41+
# @return [CfaSecurityControls::Hyperproof::Config] The configuration object.
42+
def set_config(options = {})
43+
config = CfaSecurityControls::Hyperproof::Config.new(options)
44+
CfaSecurityControls::Hyperproof.config(config)
45+
config
46+
end
47+
end
48+
end

hyperproof/spec/unit/cfa_security_controls/hyperproof/clients/aptible_spec.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
before do
1414
allow(File).to receive(:exist?).with(described_class::TOKEN_FILE).and_return(true)
1515
allow(File).to receive(:read).with(described_class::TOKEN_FILE).and_return(sso_token_data)
16-
stub_const('ENV', {})
1716
allow(Aptible::Api::Database).to receive(:all).and_return(databases)
1817
end
1918

@@ -26,7 +25,10 @@
2625
context 'when no valid credentials are found' do
2726
before do
2827
allow(File).to receive(:exist?).with(described_class::TOKEN_FILE).and_return(false)
29-
stub_const('ENV', {})
28+
stub_const('ENV', default_config_env.merge(
29+
'APTIBLE_USERNAME' => nil,
30+
'APTIBLE_PASSWORD' => nil
31+
))
3032
end
3133

3234
it 'raises an error' do
@@ -38,10 +40,10 @@
3840
let(:token) { instance_double(Aptible::Auth::Token) }
3941

4042
before do
41-
stub_const('ENV', {
42-
'APTIBLE_USERNAME' => 'rspec',
43-
'APTIBLE_PASSWORD' => 'rspecPassw0rd!'
44-
})
43+
stub_const('ENV', default_config_env.merge(
44+
'APTIBLE_USERNAME' => 'rspec',
45+
'APTIBLE_PASSWORD' => 'rspecPassw0rd!'
46+
))
4547
allow(Aptible::Auth::Token).to receive(:create).and_return(token)
4648
end
4749

@@ -54,7 +56,11 @@
5456
before do
5557
allow(File).to receive(:exist?).with(described_class::TOKEN_FILE).and_return(true)
5658
allow(File).to receive(:read).with(described_class::TOKEN_FILE).and_return(sso_token_data)
57-
stub_const('ENV', {})
59+
60+
stub_const('ENV', default_config_env.merge(
61+
'APTIBLE_USERNAME' => nil,
62+
'APTIBLE_PASSWORD' => nil
63+
))
5864
end
5965

6066
it 'returns the sso token' do
@@ -99,10 +105,10 @@
99105
let(:username) { 'rspec' }
100106

101107
before do
102-
env = {}
103-
env['APTIBLE_USERNAME'] = username if username
104-
env['APTIBLE_PASSWORD'] = password if password
105-
stub_const('ENV', env)
108+
stub_const('ENV', default_config_env.merge(
109+
'APTIBLE_USERNAME' => username,
110+
'APTIBLE_PASSWORD' => password
111+
))
106112
end
107113

108114
context 'when no username is set' do

hyperproof/spec/unit/cfa_security_controls/hyperproof/clients/hyperproof_spec.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,6 @@
125125
end
126126

127127
describe '#auth_token' do
128-
before do
129-
stub_const('ENV', {
130-
'HYPERPROOF_CLIENT_ID' => 'client_id',
131-
'HYPERPROOF_CLIENT_SECRET' => 'client_secret'
132-
})
133-
end
134-
135128
context 'when the credentials are invalid' do
136129
let(:response) do
137130
conn.post('/oauth/token')
@@ -175,14 +168,15 @@
175168

176169
context 'when no credentials are provided' do
177170
before do
178-
stub_const('ENV', {})
171+
stub_const('ENV', default_config_env.merge(
172+
'HYPERPROOF_CLIENT_ID' => nil,
173+
'HYPERPROOF_CLIENT_SECRET' => nil
174+
))
179175
end
180176

181177
it 'raises an error' do
182-
expect { client.send(:auth_token) }.to raise_error(
183-
CfaSecurityControls::Hyperproof::Clients::Hyperproof::Unauthorized,
184-
'Missing Hyperproof credentials'
185-
)
178+
expect { client.send(:auth_token) }.to \
179+
raise_error(ConfigSL::ValidationError)
186180
end
187181
end
188182
end

0 commit comments

Comments
 (0)