|
| 1 | +require File.expand_path('../../../spec_helper', __FILE__) |
| 2 | + |
| 3 | +module Bcdatabase::Commands |
| 4 | + describe Encrypt do |
| 5 | + let(:pprod_path) { File.join(ENV['BCDATABASE_PATH'], 'pprod.yaml') } |
| 6 | + |
| 7 | + before do |
| 8 | + enable_fake_cipherment |
| 9 | + ENV['BCDATABASE_PATH'] = (tmpdir + 'base').tap { |d| d.mkpath } |
| 10 | + |
| 11 | + temporary_yaml 'pprod', { |
| 12 | + 'wh' => { |
| 13 | + 'password' => 'zanzibar' |
| 14 | + }, |
| 15 | + 'app' => { |
| 16 | + 'epassword' => 'etalocohc' |
| 17 | + } |
| 18 | + } |
| 19 | + end |
| 20 | + |
| 21 | + after do |
| 22 | + ENV['BCDATABASE_PATH'] = nil |
| 23 | + disable_fake_cipherment |
| 24 | + end |
| 25 | + |
| 26 | + def run_with(inputfile, outputfile=nil) |
| 27 | + capture_std do |
| 28 | + Encrypt.new(inputfile, outputfile).run |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def run_with_input(*lines) |
| 33 | + replace_stdin(*lines) do |
| 34 | + capture_std do |
| 35 | + Encrypt.new.run |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + shared_examples 'general behavior' do |
| 41 | + let(:yaml_output) { YAML.load(output) } |
| 42 | + |
| 43 | + it 'reads from the appropriate input' do |
| 44 | + output.should =~ /wh:/ |
| 45 | + end |
| 46 | + |
| 47 | + it 'encrypts the password' do |
| 48 | + yaml_output['wh']['epassword'].should == 'rabiznaz' |
| 49 | + end |
| 50 | + |
| 51 | + it 'removes the unencrypted password' do |
| 52 | + yaml_output['wh'].should_not have_key('password') |
| 53 | + end |
| 54 | + |
| 55 | + it 'leaves any existing epasswords alone' do |
| 56 | + yaml_output['app']['epassword'].should == 'etalocohc' |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + describe 'with zero arguments' do |
| 61 | + subject { run_with_input('wh:', ' password: zanzibar', 'app:', ' epassword: etalocohc') } |
| 62 | + |
| 63 | + let(:output) { subject[:out] } |
| 64 | + |
| 65 | + include_examples 'general behavior' |
| 66 | + end |
| 67 | + |
| 68 | + describe 'with one argument' do |
| 69 | + subject { run_with(pprod_path) } |
| 70 | + |
| 71 | + let(:output) { subject[:out] } |
| 72 | + |
| 73 | + include_examples 'general behavior' |
| 74 | + end |
| 75 | + |
| 76 | + describe 'with two arguments' do |
| 77 | + subject { run_with(pprod_path, out_path) } |
| 78 | + |
| 79 | + let(:out_path) { tmpdir + 'encrypted.yml' } |
| 80 | + let(:output) { subject; File.read(out_path) } |
| 81 | + |
| 82 | + include_examples 'general behavior' |
| 83 | + end |
| 84 | + end |
| 85 | +end |
0 commit comments