Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk committed May 19, 2024
1 parent d90b669 commit 1d63fc7
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion app/clients_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class ClientsValidator
'preshared_key' => { 'type' => 'string' },
'enable' => { 'type' => 'boolean' },
'data' => { 'type' => 'object' }
}
},
'additionalProperties' => false
}.freeze

def initialize(params)
Expand Down
113 changes: 113 additions & 0 deletions spec/app/clients_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
72 changes: 72 additions & 0 deletions spec/app/clients_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1d63fc7

Please sign in to comment.