# -*- coding: utf-8 -*-

require 'rspec/expectations'
require 'chefspec'
require 'chefspec/berkshelf'

ChefSpec::Coverage.start! { add_filter 'proxychains' }

require 'chef/application'

describe 'proxychains::default' do
  context 'with custom attributes' do
    let(:subject) do
      ChefSpec::SoloRunner.new(platform: 'debian', version: '9.0') do |node|
        node.override['proxychains']['chain_type'] = 'random'
        node.override['proxychains']['chain_len'] = 42
        node.override['proxychains']['quiet_mode'] = true
        node.override['proxychains']['proxy_dns'] = true
        node.override['proxychains']['tcp_read_time_out'] = 1337
        node.override['proxychains']['tcp_connect_time_out'] = 42
        node.override['proxychains']['proxy_list'] = ['socks4 127.0.0.1 9050',
                                                      'http 192.168.13.37 8080']
      end.converge(described_recipe)
    end

    it 'does install proxychains package' do
      expect(subject).to install_package('proxychains')
    end

    it 'does create proxychains configuration file' do
      config_file = '/etc/proxychains.conf'
      matches = [start_with('# Generated by Chef!'),
                 /^random_chain$/,
                 /^chain_len = 42$/,
                 /^quiet_mode$/,
                 /^proxy_dns$/,
                 /^tcp_read_time_out 1337$/,
                 /^tcp_connect_time_out 42$/,
                 /^socks4 127.0.0.1 9050$/,
                 /^http 192.168.13.37 8080$/]

      expect(subject).to create_template(config_file)
        .with(source: 'proxychains.conf.erb')

      matches.each do |m|
        expect(subject).to render_file(config_file).with_content(m)
      end
    end
  end
end
