|
| 1 | +require_relative "../../../spec_helper" |
| 2 | +require 'kontena/cli/vault/update_command' |
| 3 | + |
| 4 | +describe Kontena::Cli::Vault::UpdateCommand do |
| 5 | + |
| 6 | + include RequirementsHelper |
| 7 | + include ClientHelpers |
| 8 | + |
| 9 | + let(:subject) { described_class.new(File.basename($0)) } |
| 10 | + |
| 11 | + describe '#execute' do |
| 12 | + |
| 13 | + context 'without value parameter' do |
| 14 | + |
| 15 | + let(:stdin) { double(:stdin) } |
| 16 | + |
| 17 | + before(:each) do |
| 18 | + @old_stdin = $stdin |
| 19 | + $stdin = stdin |
| 20 | + end |
| 21 | + |
| 22 | + after(:each) { $stdin = @old_stdin } |
| 23 | + |
| 24 | + context 'without tty' do |
| 25 | + before(:each) { allow(stdin).to receive(:tty?).and_return(false) } |
| 26 | + after(:each) { $stdin = @old_stdin } |
| 27 | + |
| 28 | + context 'nothing in stdin' do |
| 29 | + before(:each) do |
| 30 | + allow(stdin).to receive(:eof?).and_return(true) |
| 31 | + end |
| 32 | + |
| 33 | + it 'returns error if value not provided' do |
| 34 | + expect{subject.run(['mysql_password'])}.to exit_with_error.and output(/Missing/).to_stderr |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context 'value in stdin' do |
| 39 | + it 'sends update request' do |
| 40 | + expect(stdin).to receive(:eof?).and_return(false) |
| 41 | + expect(stdin).to receive(:read).and_return('secret') |
| 42 | + expect(client).to receive(:put).with('secrets/test-grid/mysql_password', { name: 'mysql_password', value: 'secret', upsert: false}) |
| 43 | + expect{subject.run(['mysql_password'])}.not_to exit_with_error |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + context 'with tty' do |
| 49 | + before(:each) do |
| 50 | + allow(stdin).to receive(:tty?).and_return(true) |
| 51 | + end |
| 52 | + |
| 53 | + context 'when value not given' do |
| 54 | + let(:prompt) { double(:prompt) } |
| 55 | + it 'prompts for value' do |
| 56 | + expect(subject).to receive(:prompt).and_return(prompt) |
| 57 | + expect(prompt).to receive(:mask).once.and_return('very-secret') |
| 58 | + expect(client).to receive(:put).with('secrets/test-grid/mysql_password', { name: 'mysql_password', value: 'very-secret', upsert: false}) |
| 59 | + expect{subject.run(['mysql_password'])}.not_to exit_with_error |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + context 'with value parameter' do |
| 66 | + it 'sends update request' do |
| 67 | + expect(client).to receive(:put).with('secrets/test-grid/mysql_password', { name: 'mysql_password', value: 'secret', upsert: false}) |
| 68 | + expect{subject.run(['mysql_password', 'secret'])}.not_to exit_with_error |
| 69 | + end |
| 70 | + |
| 71 | + context 'when giving --upsert flag' do |
| 72 | + it 'sets upsert true' do |
| 73 | + expect(client).to receive(:put).with('secrets/test-grid/mysql_password', { name: 'mysql_password', value: 'secret', upsert: true}) |
| 74 | + subject.run(['-u', 'mysql_password', 'secret']) |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | +end |
0 commit comments