From ba6e601c9c0bd7207fcc4a2d77d91f8a92d6e927 Mon Sep 17 00:00:00 2001 From: Kirill Leonov Date: Wed, 17 Jul 2024 13:47:39 +0300 Subject: [PATCH] init --- config/settings/production.yaml | 2 +- lib/utils/config_file_builder.rb | 17 +++-- spec/lib/utils/config_file_builder_spec.rb | 74 ++++++++++++++++++++++ spec/spec_helper.rb | 1 + 4 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 spec/lib/utils/config_file_builder_spec.rb diff --git a/config/settings/production.yaml b/config/settings/production.yaml index ee41629..9515bd5 100644 --- a/config/settings/production.yaml +++ b/config/settings/production.yaml @@ -4,7 +4,7 @@ wg_default_address: <%= ENV['WG_DEFAULT_ADDRESS'] || '10.8.0.x' %> wg_allowed_ips: <%= ENV['WG_ALLOWED_IPS'] || '0.0.0.0/0, ::/0' %> wg_host: <%= ENV['WG_HOST'] %> wg_port: <%= ENV['WG_PORT'] || '51820' %> -wg_default_dns: <%= ENV['WG_DEFAULT_DNS'] || '1.1.1.1' %> +wg_default_dns: <%= ENV['WG_DEFAULT_DNS'] %> wg_pre_up: <%= ENV['WG_PRE_UP'] || '' %> wg_pre_down: <%= ENV['WG_PRE_DOWN'] || '' %> wg_post_up: <%= ENV['WG_POST_UP'] %> diff --git a/lib/utils/config_file_builder.rb b/lib/utils/config_file_builder.rb index bf7c2b2..762b1c0 100644 --- a/lib/utils/config_file_builder.rb +++ b/lib/utils/config_file_builder.rb @@ -12,12 +12,23 @@ def initialize(config) end def build - <<~TEXT + "#{interface}\n#{peer}" + end + + private + + def interface + result = <<~TEXT [Interface] PrivateKey = #{key['private_key']} Address = #{key['address']} - DNS = #{key['dns']} + TEXT + result << "DNS = #{key['dns']}\n" if key['dns'] + result + end + def peer + <<~TEXT [Peer] PublicKey = #{key['server_public_key']} PresharedKey = #{key['preshared_key']} @@ -27,8 +38,6 @@ def build TEXT end - private - attr_reader :key end end diff --git a/spec/lib/utils/config_file_builder_spec.rb b/spec/lib/utils/config_file_builder_spec.rb new file mode 100644 index 0000000..b8a2c09 --- /dev/null +++ b/spec/lib/utils/config_file_builder_spec.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +RSpec.describe Utils::ConfigFileBuilder do + subject(:build) { described_class.build(config.to_json) } + + context 'when there is dns in the config' do + let(:config) do + { + private_key: '1', + address: '23.23.23.23', + dns: '1.1.1.1', + server_public_key: '2', + preshared_key: '3', + allowed_ips: '1.2.3.4', + persistent_keepalive: 4, + endpoint: '2.3.4.1' + } + end + + let(:expected_result) do + <<~TEXT + [Interface] + PrivateKey = 1 + Address = 23.23.23.23 + DNS = 1.1.1.1 + + [Peer] + PublicKey = 2 + PresharedKey = 3 + AllowedIPs = 1.2.3.4 + PersistentKeepalive = 4 + Endpoint = 2.3.4.1 + TEXT + end + + it 'returns the expected result' do + expect(build).to eq(expected_result) + end + end + + context 'when there is no dns in the config' do + let(:config) do + { + private_key: '1', + address: '23.23.23.23', + dns: nil, + server_public_key: '2', + preshared_key: '3', + allowed_ips: '1.2.3.4', + persistent_keepalive: 4, + endpoint: '2.3.4.1' + } + end + + let(:expected_result) do + <<~TEXT + [Interface] + PrivateKey = 1 + Address = 23.23.23.23 + + [Peer] + PublicKey = 2 + PresharedKey = 3 + AllowedIPs = 1.2.3.4 + PersistentKeepalive = 4 + Endpoint = 2.3.4.1 + TEXT + end + + it 'returns the expected result' do + expect(build).to eq(expected_result) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5e03ccb..406045c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,7 @@ require 'ipaddr' require 'fileutils' require 'ruby_units/namespaced' +require 'byebug' Config.load_and_set_settings('config/settings/test.yaml')