Permalink
Browse files

Added password strength for password change

Change-Id: Ib5b900a3302951464efc6f0832d4265b1aa2cba1
  • Loading branch information...
vito committed Dec 18, 2012
1 parent 291db91 commit 13d2fc73fcb555af6aa578a6747e1953ebde3ff5
Showing with 125 additions and 9 deletions.
  1. +16 −7 lib/vmc/cli/user/passwd.rb
  2. +105 −0 spec/vmc/cli/user/passwd_spec.rb
  3. +4 −2 spec/vmc/cli_spec.rb
View
@@ -23,15 +23,24 @@ def passwd
verify = input[:verify]
if new_password != verify
- fail "Passwords don't match."
+ fail "Passwords do not match."
end
- with_progress("Changing password") do
- if v2?
- user.change_password!(new_password, password)
- else
- user.password = new_password
- user.update!
+ pw_strength = client.base.uaa.password_score(new_password)
+ msg = "Your password strength is: #{pw_strength}"
+
+ if pw_strength == :weak
+ fail msg
+ else
+ line msg
+
+ with_progress("Changing password") do
+ if v2?
+ user.change_password!(new_password, password)
+ else
+ user.password = new_password
+ user.update!
+ end
end
end
end
@@ -0,0 +1,105 @@
+require 'spec_helper'
+
+describe VMC::User::Passwd do
+ describe 'metadata' do
+ let(:command) { Mothership.commands[:passwd] }
+
+ describe 'command' do
+ subject { command }
+ its(:description) { should eq "Update a user's password" }
+ it { expect(Mothership::Help.group(:admin, :user)).to include(subject) }
+ end
+
+ include_examples 'inputs must have descriptions'
+
+ describe 'arguments' do
+ subject { command.arguments }
+ it 'have the correct commands (with inconsistent user instead of email)' do
+ should eq [{:type => :optional, :value => nil, :name => :user}]
+ end
+ end
+ end
+
+ describe '#passwd' do
+ let(:client) { FactoryGirl.build(:client) }
+ let(:output) { StringIO.new }
+ let(:out) { output.string }
+ let(:old_password) { "old" }
+ let(:new_password) { "password" }
+ let(:verify_password) { new_password }
+ let(:score) { :strong }
+ let(:user) { FactoryGirl.build(:user) }
+
+ before do
+ stub(VMC::CLI).exit { |code| code }
+ any_instance_of(VMC::CLI) do |cli|
+ stub(cli).client { client }
+ stub(cli).precondition { nil }
+ end
+ stub(client).current_user { user }
+ stub(client).register
+ stub(client).base.stub!.uaa.stub!.password_score(new_password) { score }
+ end
+
+ subject do
+ with_output_to output do
+ VMC::CLI.start %W(passwd --password #{old_password} --new-password #{new_password} --verify #{verify_password} --force --debug)
+ end
+ end
+
+ context 'when the passwords dont match' do
+ let(:verify_password) { "other_password" }
+
+ it { should eq 1 }
+
+ it 'fails' do
+ subject
+ expect(out).to include "Passwords do not match."
+ end
+
+ it "doesn't print out the score" do
+ subject
+ expect(out).not_to include "strength"
+ end
+
+ it "doesn't log in or register" do
+ dont_allow(user).change_password!
+ subject
+ end
+ end
+
+ context 'when the password is good or strong' do
+ before do
+ stub(user).change_password!
+ end
+
+ it { should eq 0 }
+
+ it 'prints out the password score' do
+ subject
+ expect(out).to include "Your password strength is: strong"
+ end
+
+ it 'changes the password' do
+ mock(user).change_password!(new_password, old_password)
+ subject
+ end
+ end
+
+ context 'when the password is weak' do
+ let(:score) { :weak }
+
+ it { should eq 1 }
+
+ it 'prints out the password score' do
+ subject
+ expect(out).to include "Your password strength is: weak"
+ end
+
+ it "doesn't change the password" do
+ dont_allow(user).change_password!
+ subject
+ end
+ end
+ end
+end
View
@@ -7,8 +7,10 @@
let(:inputs) { {} }
subject do
- stub(cmd).input { inputs }
- cmd.execute(nil, [])
+ with_output_to do
+ stub(cmd).input { inputs }
+ cmd.execute(nil, [])
+ end
end
it 'wraps Timeout::Error with a more friendly message' do

0 comments on commit 13d2fc7

Please sign in to comment.