summaryrefslogtreecommitdiff
path: root/spec/trocla_spec.rb
blob: fe955201b6780c71d357e6cee7e7538c8d9ede49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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('set_test','mysql').should_not be_empty
      @trocla.get_password('set_test','mysql').should_not be_nil
      (old_plain=@trocla.password('set_test','mysql')).should_not be_empty
      
      @trocla.set_password('set_test','plain','foobar').should_not eql(old_plain)
      @trocla.get_password('set_test','mysql').should be_nil
    end
    
    it "should otherwise only update the hash" do
      (mysql = @trocla.password('set_test2','mysql')).should_not be_empty
      (md5crypt = @trocla.password('set_test2','md5crypt')).should_not be_empty
      (plain = @trocla.get_password('set_test2','plain')).should_not be_empty
      
      (new_mysql = @trocla.set_password('set_test2','mysql','foo')).should_not eql(mysql)
      @trocla.get_password('set_test2','mysql').should eql(new_mysql)
      @trocla.get_password('set_test2','md5crypt').should eql(md5crypt)
      @trocla.get_password('set_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