From 9146a541ff96b92f4f14c6292307b68dc4673097 Mon Sep 17 00:00:00 2001 From: mh Date: Wed, 27 Jul 2011 18:43:55 +0200 Subject: initial release of trocla --- spec/data/.keep | 0 spec/spec_helper.rb | 30 +++++++++++ spec/trocla/util_spec.rb | 19 +++++++ spec/trocla_spec.rb | 127 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 spec/data/.keep create mode 100644 spec/spec_helper.rb create mode 100644 spec/trocla/util_spec.rb create mode 100644 spec/trocla_spec.rb (limited to 'spec') diff --git a/spec/data/.keep b/spec/data/.keep new file mode 100644 index 0000000..e69de29 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..64ce0de --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,30 @@ +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +$LOAD_PATH.unshift(File.dirname(__FILE__)) +require 'rspec' +require 'mocha' +require 'trocla' + +# Requires supporting files with custom matchers and macros, etc, +# in ./support/ and its subdirectories. +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} + +RSpec.configure do |config| + +end + +def default_config + @default_config ||= YAML.load(File.read(File.expand_path(base_dir+'/lib/trocla/default_config.yaml'))) +end + +def test_config + return @config unless @config.nil? + @config = default_config + yaml_path = File.expand_path(base_dir+'/spec/data/test_config.yaml') + File.unlink(yaml_path) if File.exists?(yaml_path) + @config['adapter_options'][:path] = yaml_path + @config +end + +def base_dir + File.dirname(__FILE__)+'/../' +end diff --git a/spec/trocla/util_spec.rb b/spec/trocla/util_spec.rb new file mode 100644 index 0000000..4e02d97 --- /dev/null +++ b/spec/trocla/util_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe "Trocla::Util" do + describe "random_str" do + it "should be random" do + Trocla::Util.random_str.should_not eql(Trocla::Util.random_str) + end + + it "should default to length 12" do + Trocla::Util.random_str.length.should == 12 + end + + it "should be possible to change length" do + Trocla::Util.random_str(8).length.should == 8 + Trocla::Util.random_str(32).length.should == 32 + Trocla::Util.random_str(1).length.should == 1 + end + end +end \ No newline at end of file diff --git a/spec/trocla_spec.rb b/spec/trocla_spec.rb new file mode 100644 index 0000000..c6cb94c --- /dev/null +++ b/spec/trocla_spec.rb @@ -0,0 +1,127 @@ +require File.expand_path(File.dirname(__FILE__) + '/spec_helper') + +describe "Trocla" do + + before(:each) do + Trocla.any_instance.expects(:read_config).returns(test_config) + @trocla = Trocla.new + end + + describe "password" do + it "should generate random passwords by default" do + @trocla.password('random1','plain').should_not eql(@trocla.password('random2','plain')) + end + + it "should generate passwords of length #{default_config['options']['length']}" do + @trocla.password('random1','plain').length.should eql(default_config['options']['length']) + end + + Trocla::Formats.all.each do |format| + describe "#{format} password format" do + it "should return a password hashed in the #{format} format" do + @trocla.password('some_test',format,format_options[format]).should_not be_empty + end + + it "should return the same hashed for the #{format} format on multiple invocations" do + (round1=@trocla.password('some_test',format,format_options[format])).should_not be_empty + @trocla.password('some_test',format,format_options[format]).should eql(round1) + end + + it "should also store the plain password by default" do + pwd = @trocla.password('some_test','plain') + pwd.should_not be_empty + pwd.length.should eql(12) + end + end + end + + Trocla::Formats.all.reject{|f| f == 'plain' }.each do |format| + it "should raise an exception if not a random password is asked but plain password is not present for format #{format}" do + lambda{ @trocla.password('not_random',format, 'random' => false) }.should raise_error + end + end + end + + describe "set_password" do + it "should reset hashed passwords on a new plain password" do + @trocla.password('reset_test','mysql').should_not be_empty + @trocla.get_password('reset_test','mysql').should_not be_nil + (old_plain=@trocla.password('reset_test','mysql')).should_not be_empty + + @trocla.set_password('reset_test','plain','foobar').should_not eql(old_plain) + @trocla.get_password('reset_test','mysql').should be_nil + end + + it "should otherwise only update the hash" do + (mysql = @trocla.password('reset_test2','mysql')).should_not be_empty + (md5crypt = @trocla.password('reset_test2','md5crypt')).should_not be_empty + (plain = @trocla.get_password('reset_test2','plain')).should_not be_empty + + (new_mysql = @trocla.set_password('reset_test2','mysql','foo')).should_not eql(mysql) + @trocla.get_password('reset_test2','mysql').should eql(new_mysql) + @trocla.get_password('reset_test2','md5crypt').should eql(md5crypt) + @trocla.get_password('reset_test2','plain').should eql(plain) + end + end + + describe "reset_password" do + it "should reset a password" do + plain1 = @trocla.password('reset_pwd','plain') + plain2 = @trocla.reset_password('reset_pwd','plain') + + plain1.should_not eql(plain2) + end + + it "should not reset other formats" do + (mysql = @trocla.password('reset_pwd2','mysql')).should_not be_empty + (md5crypt1 = @trocla.password('reset_pwd2','md5crypt')).should_not be_empty + + (md5crypt2 = @trocla.reset_password('reset_pwd2','md5crypt')).should_not be_empty + md5crypt2.should_not eql(md5crypt1) + + @trocla.get_password('reset_pwd2','mysql').should eql(mysql) + end + end + + describe "delete_password" do + it "should delete all passwords if no format is given" do + @trocla.password('delete_test1','mysql').should_not be_nil + @trocla.get_password('delete_test1','plain').should_not be_nil + + @trocla.delete_password('delete_test1') + @trocla.get_password('delete_test1','plain').should be_nil + @trocla.get_password('delete_test1','mysql').should be_nil + end + + it "should delete only a given format" do + @trocla.password('delete_test2','mysql').should_not be_nil + @trocla.get_password('delete_test2','plain').should_not be_nil + + @trocla.delete_password('delete_test2','plain') + @trocla.get_password('delete_test2','plain').should be_nil + @trocla.get_password('delete_test2','mysql').should_not be_nil + end + + it "should delete only a given non-plain format" do + @trocla.password('delete_test3','mysql').should_not be_nil + @trocla.get_password('delete_test3','plain').should_not be_nil + + @trocla.delete_password('delete_test3','mysql') + @trocla.get_password('delete_test3','mysql').should be_nil + @trocla.get_password('delete_test3','plain').should_not be_nil + end + end + + describe "VERSION" do + it "should return a version" do + Trocla::VERSION::STRING.should_not be_empty + end + end + + def format_options + @format_options ||= Hash.new({}).merge({ + 'pgsql' => { 'username' => 'test' } + }) + end + +end -- cgit v1.2.3