Skip to content

Commit

Permalink
👷 Refactoring and more tests (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk authored May 12, 2024
1 parent be5942a commit a471dc5
Show file tree
Hide file tree
Showing 15 changed files with 347 additions and 39 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@ on:
branches: [ "master" ]

jobs:
test:
rubocop:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'
bundler-cache: true
- name: Install dependencies
run: bundle install
- name: Run cops
run: bundle exec rubocop

specs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'
bundler-cache: true
- name: Install dependencies
run: bundle install
- name: Run specs
run: bundle exec rspec
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.byebug_history
etc
.rspec_status
tmp
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ group :development do
gem 'rspec', '~> 3.13'
gem 'rubocop', '~> 1.63'
gem 'rubocop-rspec', '~> 2.29', '>= 2.29.2'
gem 'super_diff', '~> 0.12.1'
end
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
attr_extras (7.1.0)
base64 (0.2.0)
byebug (11.1.3)
config (5.4.0)
Expand All @@ -15,10 +16,13 @@ GEM
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
nio4r (2.7.2)
optimist (3.1.0)
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
patience_diff (1.2.0)
optimist (~> 3.0)
puma (6.4.2)
nio4r (~> 2.0)
racc (1.7.3)
Expand Down Expand Up @@ -76,6 +80,10 @@ GEM
rack-protection (= 4.0.0)
rack-session (>= 2.0.0, < 3)
tilt (~> 2.0)
super_diff (0.12.1)
attr_extras (>= 6.2.4)
diff-lcs
patience_diff
tilt (2.3.0)
unicode-display_width (2.5.0)

Expand All @@ -93,6 +101,7 @@ DEPENDENCIES
rubocop (~> 1.63)
rubocop-rspec (~> 2.29, >= 2.29.2)
sinatra (~> 4.0)
super_diff (~> 0.12.1)

BUNDLED WITH
2.5.9
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,22 @@ Returns an array with all clients on the server
Example response:

```json
{
"6": {
"id": 6,
"address": "10.8.0.7",
[
{
"id": 15,
"server_public_key": "server_public_key",
"address": "10.8.0.16/24",
"private_key": "private_key",
"public_key": "public_key",
"preshared_key": "preshared_key",
"allowed_ips": "0.0.0.0/0, ::/0",
"dns": "1.1.1.1",
"persistent_keepalive": 0,
"endpoint": "0.0.0.0:51820",
"data": {
"params": "value"
"params1": "value1"
}
}
}
]
```

### POST /clients
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.1.4
v0.1.5
2 changes: 1 addition & 1 deletion app/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize
end

def index
wire_guard.all_configs.to_json
ClientsSerializer.each_serialize(wire_guard.all_configs, wire_guard.server_public_key)
end

def create(params)
Expand Down
43 changes: 43 additions & 0 deletions app/clients_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,60 @@ def self.serialize(client_config, server_public_key)
new(client_config, server_public_key).client.to_json
end

def self.each_serialize(client_config, server_public_key)
new(client_config, server_public_key).clients.to_json
end

# NOTE: If an instance of a class is created through the each_serialize method,
# then the client_config variable will contain a hash with configs of the type:
# {
# '1' => {
# id: 1,
# address: '10.8.0.2',
# private_key: '1',
# public_key: '2',
# preshared_key: '3',
# data: {}
# },
# '2' => {
# id: 2,
# address: '10.8.0.3',
# private_key: '1',
# public_key: '2',
# preshared_key: '3',
# data: {}
# }
# }
def initialize(client_config, server_public_key)
@client_config = stringify_keys(client_config)
@server_public_key = server_public_key
end

def clients # rubocop:disable Metrics/MethodLength
client_config.map do |_config_id, config|
{
id: config['id'],
server_public_key:,
address: "#{config['address']}/24",
private_key: config['private_key'],
public_key: config['public_key'],
preshared_key: config['preshared_key'],
allowed_ips: WG_ALLOWED_IPS,
dns: DNS,
persistent_keepalive: WG_PERSISTENT_KEEPALIVE,
endpoint: "#{WG_HOST}:#{WG_PORT}",
data: config['data']
}
end
end

def client # rubocop:disable Metrics/MethodLength
{
id: client_config['id'],
server_public_key:,
address: "#{client_config['address']}/24",
private_key: client_config['private_key'],
public_key: client_config['public_key'],
preshared_key: client_config['preshared_key'],
allowed_ips: WG_ALLOWED_IPS,
dns: DNS,
Expand Down
2 changes: 1 addition & 1 deletion config/settings/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wg_path: 'etc'
wg_path: 'tmp/etc'
wg_device: 'eth0'
wg_default_address: '10.8.0.x'
wg_allowed_ips: '0.0.0.0/0, ::/0'
Expand Down
1 change: 1 addition & 0 deletions lib/wire_guard/server.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'fileutils'
require_relative 'config_builder'
require_relative 'key_generator'
require_relative 'config_updater'
Expand Down
115 changes: 115 additions & 0 deletions spec/app/clients_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# frozen_string_literal: true

RSpec.describe ClientsController do
subject(:controller) { described_class.new }

let(:wg_conf_path) { "#{Settings.wg_path}/wg0.json" }

before do
allow(WireGuard::ConfigUpdater).to receive(:update)
allow(WireGuard::KeyGenerator).to receive_messages(wg_genkey: 'wg_genkey', wg_pubkey: 'wg_pubkey',
wg_genpsk: 'wg_genpsk')
end

after do
FileUtils.rm_rf(wg_conf_path)
end

describe '#index' do
context 'when there is no configuration file' do
let(:expected_result) do
{
server: {
private_key: 'wg_genkey',
public_key: 'wg_pubkey',
address: '10.8.0.1'
},
configs: {
last_id: 0,
last_address: '10.8.0.1'
}
}
end

it 'creates a configuration file and returns an empty array' do # rubocop:disable RSpec/MultipleExpectations
result = controller.index

expect(result).to eq([].to_json)

config = File.read(wg_conf_path)

expect(config).to eq(JSON.pretty_generate(expected_result))
end
end

context 'when there is already a configuration file without clients' do
before do
FileUtils.cp('spec/fixtures/empty_wg0.json', wg_conf_path)
end

it 'returns an empty array' do
expect(controller.index).to eq([].to_json)
end
end

context 'when there is already a configuration file with clients' do
before do
FileUtils.cp('spec/fixtures/wg0.json', wg_conf_path)
end

let(:expected_result) do
[
{
id: 1,
server_public_key: 'uygGKpQt7gOwrP+bqkiXytafHiM+XqFGc0jtZVJ5bnw=',
address: '10.8.0.2/24',
private_key: 'MJn6fwoyqG8S6wsrJzWrUow4leZuEM9O8s+G+kcXElU=',
public_key: 'LiXk4UOfnScgf4UnkcYNcz4wWeqTOW1UrHKRVhZ1OXg=',
preshared_key: '3UzAMA6mLIGjHOImShNb5tWlkwxsha8LZZP7dm49meQ=',
allowed_ips: '0.0.0.0/0, ::/0',
dns: '1.1.1.1',
persistent_keepalive: 0,
endpoint: '2.2.2.2:51820',
data: {
lol: 'kek'
}
},
{
id: 2,
server_public_key: 'uygGKpQt7gOwrP+bqkiXytafHiM+XqFGc0jtZVJ5bnw=',
address: '10.8.0.3/24',
private_key: 'aN7ye98FKrmydwfA6tHgHE1PbiidWzUJ9cltnies8F4=',
public_key: 'hvIyIW2o8JROVKuY2yYFdUn0oA+43aLuT8KCy0YbORE=',
preshared_key: 'dVW/5kF8wnsx0zAwR4uPIa06btACxpQ/rHBL1B3qPnk=',
allowed_ips: '0.0.0.0/0, ::/0',
dns: '1.1.1.1',
persistent_keepalive: 0,
endpoint: '2.2.2.2:51820',
data: {
cheburek: 'hah'
}
},
{
id: 3,
server_public_key: 'uygGKpQt7gOwrP+bqkiXytafHiM+XqFGc0jtZVJ5bnw=',
address: '10.8.0.4/24',
private_key: 'eF3Owsqd5MGAIXjmALGBi8ea8mkFUmAiyh80U3hVXn8=',
public_key: 'bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=',
preshared_key: 'IyVg7fktkSBxJ0uK82j6nlI7Vmo0E53eBmYZ723/45E=',
allowed_ips: '0.0.0.0/0, ::/0',
dns: '1.1.1.1',
persistent_keepalive: 0,
endpoint: '2.2.2.2:51820',
data: {
key: 'value'
}
}
]
end

it 'returns a serialized array with all clients' do
expect(controller.index).to eq(expected_result.to_json)
end
end
end
end
Loading

0 comments on commit a471dc5

Please sign in to comment.