From 1d63fc7f08bf2ebe931eb583f582b27fccbcf13a Mon Sep 17 00:00:00 2001 From: Kirill Leonov Date: Sun, 19 May 2024 18:01:29 +0300 Subject: [PATCH] done --- README.md | 1 + app/clients_validator.rb | 3 +- spec/app/clients_controller_spec.rb | 113 ++++++++++++++++++++++++++++ spec/app/clients_validator_spec.rb | 72 ++++++++++++++++++ 4 files changed, 188 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 843f2f2..2532c29 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ Example request: "enable": false, // bool "data": {} // object } +``` The enable parameter allows you to enable or disable the client without removing it from the server. diff --git a/app/clients_validator.rb b/app/clients_validator.rb index c311907..679c32f 100644 --- a/app/clients_validator.rb +++ b/app/clients_validator.rb @@ -15,7 +15,8 @@ class ClientsValidator 'preshared_key' => { 'type' => 'string' }, 'enable' => { 'type' => 'boolean' }, 'data' => { 'type' => 'object' } - } + }, + 'additionalProperties' => false }.freeze def initialize(params) diff --git a/spec/app/clients_controller_spec.rb b/spec/app/clients_controller_spec.rb index 48b066d..5a8ee96 100644 --- a/spec/app/clients_controller_spec.rb +++ b/spec/app/clients_controller_spec.rb @@ -274,5 +274,118 @@ end describe '#update' do + before do + create_conf_file('spec/fixtures/wg0.json') + end + + context 'when the necessary config is available' do + context 'when the parameters are valid' do # rubocop:disable RSpec/NestedGroups + let(:params) do + { + 'address' => '10.8.0.200', + 'private_key' => 'a', + 'public_key' => 'b', + 'preshared_key' => 'c', + 'enable' => false, + 'data' => {} + } + end + let(:expected_result) do + { + 'server' => { + 'private_key' => '6Mlqg+1Umojm7a4VvgIi+YMp4oPrWNnZ5HLRFu4my2w=', + 'public_key' => 'uygGKpQt7gOwrP+bqkiXytafHiM+XqFGc0jtZVJ5bnw=', + 'address' => '10.8.0.1' + }, + 'configs' => { + 'last_id' => 3, + 'last_address' => '10.8.0.4', + '1' => { + 'id' => 1, + 'address' => '10.8.0.200', + 'private_key' => 'a', + 'public_key' => 'b', + 'preshared_key' => 'c', + 'enable' => false, + 'data' => {} + }, + '2' => { + 'id' => 2, + 'address' => '10.8.0.3', + 'private_key' => 'aN7ye98FKrmydwfA6tHgHE1PbiidWzUJ9cltnies8F4=', + 'public_key' => 'hvIyIW2o8JROVKuY2yYFdUn0oA+43aLuT8KCy0YbORE=', + 'preshared_key' => 'dVW/5kF8wnsx0zAwR4uPIa06btACxpQ/rHBL1B3qPnk=', + 'enable' => false, + 'data' => { + 'cheburek' => 'hah' + } + }, + '3' => { + 'id' => 3, + 'address' => '10.8.0.4', + 'private_key' => 'eF3Owsqd5MGAIXjmALGBi8ea8mkFUmAiyh80U3hVXn8=', + 'public_key' => 'bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=', + 'preshared_key' => 'IyVg7fktkSBxJ0uK82j6nlI7Vmo0E53eBmYZ723/45E=', + 'enable' => true, + 'data' => { + 'key' => 'value' + } + } + } + } + end + let(:expected_updated_config) do + { + id: 1, + server_public_key: 'uygGKpQt7gOwrP+bqkiXytafHiM+XqFGc0jtZVJ5bnw=', + address: '10.8.0.200/24', + private_key: 'a', + public_key: 'b', + preshared_key: 'c', + enable: false, + allowed_ips: '0.0.0.0/0, ::/0', + dns: '1.1.1.1', + persistent_keepalive: 0, + endpoint: '2.2.2.2:51820', + data: {} + } + end + + it 'updates the config from the server' do + controller.update('1', params) + + config = File.read(wg_conf_path) + + expect(config).to eq(JSON.pretty_generate(expected_result)) + end + + it 'returns the updated serialized config' do + expect(controller.update('1', params)).to eq(expected_updated_config.to_json) + end + end + + context 'when the parameters are not valid' do # rubocop:disable RSpec/NestedGroups + let(:params) do + { + 'address' => '10.8.0.200', + 'private_key' => 'a', + 'public_key' => 1, + 'preshared_key' => 'c', + 'enable' => 'false', + 'data' => {} + } + end + + it 'raises a validation error' do + expect { controller.update('1', params) }.to raise_error(JSON::Schema::ValidationError) + end + end + end + + context 'when the required config is missing' do + it 'raises an error stating that this config is not on the server' do + expect { controller.update('17', {}) }.to raise_error(Errors::ConfigNotFoundError) + end + end end end diff --git a/spec/app/clients_validator_spec.rb b/spec/app/clients_validator_spec.rb index cfaacb1..36a14d8 100644 --- a/spec/app/clients_validator_spec.rb +++ b/spec/app/clients_validator_spec.rb @@ -1,4 +1,76 @@ # frozen_string_literal: true RSpec.describe ClientsValidator do + subject(:validate) { described_class.new(params).validate! } + + context 'when all parameters are valid' do + let(:params) do + { + 'address' => 's', + 'private_key' => 'a', + 'public_key' => 'b', + 'preshared_key' => 'c', + 'enable' => false, + 'data' => {} + } + end + + it 'return true' do + expect(validate).to be(true) + end + end + + context 'when parameters are empty' do + let(:params) do + {} + end + + it 'return true' do + expect(validate).to be(true) + end + end + + context 'when all parameters are valid but there are extra ones' do + let(:params) do + { + 'address' => 's', + 'private_key' => 'a', + 'public_key' => 'b', + 'preshared_key' => 'c', + 'enable' => false, + 'data' => {}, + 'extra' => 123 + } + end + + it 'raises a validation error' do + expect { validate }.to raise_error(JSON::Schema::ValidationError) + end + end + + context 'when one parameter is not valid' do + let(:params) do + { + 'enable' => 'false' + } + end + + it 'raises a validation error' do + expect { validate }.to raise_error(JSON::Schema::ValidationError) + end + end + + context 'when there were several parameters and they were valid' do + let(:params) do + { + 'preshared_key' => 'c', + 'enable' => false, + 'data' => {} + } + end + + it 'return true' do + expect(validate).to be(true) + end + end end