Permalink
Browse files

Refactor Password Verification

Change-Id: Ib61dfca7675f9f41d2c1ce7f018f06e6b6e19e6b
  • Loading branch information...
1 parent 349a8be commit d81c0238c2c6975cc0fa77dc16be79bb0995b941 Max Brunsfeld and Tim Labeeuw committed Jan 18, 2013
@@ -6,6 +6,24 @@ class Base < CLI
def precondition
check_logged_in
end
+
+ private
+
+ def validate_password!(password)
+ validate_password_verified!(password)
+ validate_password_strength!(password)
+ end
+
+ def validate_password_verified!(password)
+ fail "Passwords do not match." unless force? || password == input[:verify]
+ end
+
+ def validate_password_strength!(password)
+ strength = client.base.uaa.password_score(password)
+ msg = "Your password strength is: #{strength}"
+ fail msg if strength == :weak
+ line msg
+ end
end
end
end
@@ -20,27 +20,15 @@ def passwd
user = input[:user]
password = input[:password] if v2?
new_password = input[:new_password]
- verify = input[:verify]
- if new_password != verify
- fail "Passwords do not match."
- end
-
- 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
+ validate_password! new_password
- with_progress("Changing password") do
- if v2?
- user.change_password!(new_password, password)
- else
- user.password = new_password
- user.update!
- end
+ with_progress("Changing password") do
+ if v2?
+ user.change_password!(new_password, password)
+ else
+ user.password = new_password
+ user.update!
end
end
end
@@ -1,38 +1,27 @@
-require "vmc/cli/start/base"
+require "vmc/cli/user/base"
-module VMC::Start
+module VMC::User
class Register < Base
+ def precondition; end
+
desc "Create a user and log in"
- group :start, :hidden => true
+ group :admin, :user, :hidden => true
input :email, :desc => "Desired email", :argument => :optional
input :password, :desc => "Desired password"
input :verify, :desc => "Repeat password"
input :login, :desc => "Automatically log in?", :default => true
def register
- show_context
-
email = input[:email]
password = input[:password]
- if !force? && password != input[:verify]
- fail "Passwords do not match."
- end
-
- pw_strength = client.base.uaa.password_score(password)
- msg = "Your password strength is: #{pw_strength}"
-
- if pw_strength == :weak
- fail msg
- else
- line msg
+ validate_password!(input[:password])
- with_progress("Creating user") do
- client.register(email, password)
- end
+ with_progress("Creating user") do
+ client.register(email, password)
+ end
- if input[:login]
- invoke :login, :username => email, :password => password
- end
+ if input[:login]
+ invoke :login, :username => email, :password => password
end
end
@@ -46,7 +46,7 @@
subject do
capture_output do
- VMC::CLI.start %W(passwd --password #{old_password} --new-password #{new_password} --verify #{verify_password} --force --debug)
+ VMC::CLI.start %W(passwd --password #{old_password} --new-password #{new_password} --verify #{verify_password} --no-force --debug)
end
end
@@ -1,6 +1,6 @@
require 'spec_helper'
-describe VMC::Start::Register do
+describe VMC::User::Register do
describe 'metadata' do
let(:command) { Mothership.commands[:register] }
@@ -64,7 +64,7 @@
it "doesn't log in or register" do
dont_allow(client).register
- any_instance_of VMC::Start::Register do |register|
+ any_instance_of(described_class) do |register|
dont_allow(register).invoke
end
subject
@@ -98,7 +98,7 @@
let(:login) { true }
it 'logs in' do
- any_instance_of VMC::Start::Register do |register|
+ any_instance_of(described_class) do |register|
mock(register).invoke(:login, :username => email, :password => password)
end
subject
@@ -107,7 +107,7 @@
context 'and the login flag is false' do
it "doesn't log in" do
- any_instance_of VMC::Start::Register do |register|
+ any_instance_of(described_class) do |register|
dont_allow(register).invoke(:login, :username => email, :password => password)
end
subject
@@ -132,7 +132,7 @@
end
it "doesn't log in" do
- any_instance_of VMC::Start::Register do |register|
+ any_instance_of(described_class) do |register|
dont_allow(register).invoke(:login, :username => email, :password => password)
end
subject

0 comments on commit d81c023

Please sign in to comment.