Skip to content

Commit

Permalink
Tests for the destroy method in the controller (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk authored May 14, 2024
1 parent 7482458 commit 5ad1803
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions spec/app/clients_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,66 @@
end
end
end

describe '#destroy' do
before do
create_conf_file('spec/fixtures/wg0.json')
end

context 'when the necessary config is available' do
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.2',
private_key: 'MJn6fwoyqG8S6wsrJzWrUow4leZuEM9O8s+G+kcXElU=',
public_key: 'LiXk4UOfnScgf4UnkcYNcz4wWeqTOW1UrHKRVhZ1OXg=',
preshared_key: '3UzAMA6mLIGjHOImShNb5tWlkwxsha8LZZP7dm49meQ=',
data: {
lol: 'kek'
}
},
'3' => {
id: 3,
address: '10.8.0.4',
private_key: 'eF3Owsqd5MGAIXjmALGBi8ea8mkFUmAiyh80U3hVXn8=',
public_key: 'bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=',
preshared_key: 'IyVg7fktkSBxJ0uK82j6nlI7Vmo0E53eBmYZ723/45E=',
data: {
key: 'value'
}
}
}
}
end

it 'returns an empty hash as result' do
result = controller.destroy('2')

expect(result).to eq({}.to_json)
end

it 'removes the required config from the configuration file' do
controller.destroy('2')

json_config = File.read(wg_conf_path)

expect(json_config).to eq(JSON.pretty_generate(expected_result))
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.destroy('17') }.to raise_error(Errors::ConfigNotFoundError)
end
end
end
end

0 comments on commit 5ad1803

Please sign in to comment.