public
Rubygem
Description: ActiveRecord macro that helps encrypt passwords and generate api tokens before_save.
Homepage:
Clone URL: git://github.com/matthewtodd/has_digest.git
has_digest / test / has_digest_test.rb
100644 136 lines (109 sloc) 3.846 kb
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
128
129
130
131
132
133
134
135
136
require File.join(File.dirname(__FILE__), 'test_helper')
 
class HasDigestTest < Test::Unit::TestCase
  context 'Model with a standalone digest' do
    setup do
      @klass = model_with_attributes(:token) do
        has_digest :token
      end
    end
 
    context 'instance' do
      setup { @instance = @klass.new }
 
      context 'save' do
        setup { @instance.save }
        should_change '@instance.token', :to => /^\w{40}$/
 
        context 'save again' do
          setup { @instance.save }
          should_not_change '@instance.token'
        end
      end
 
      should 'not rely on the default date format' do
        default = Time::DATE_FORMATS[:default]
        Time::DATE_FORMATS[:default] = '.'
        begin
          assert_unique((1..1000).collect { @instance.digest })
        ensure
          Time::DATE_FORMATS[:default] = default
        end
      end
    end
  end
 
  context 'Model with a single-attribute-based digest' do
    setup do
      @klass = model_with_attributes(:encrypted_password) do
        has_digest :encrypted_password, :depends => :password
      end
    end
 
    context 'saved instance' do
      setup { @instance = @klass.create(:password => 'PASSWORD') }
 
      should 'have digested attribute' do
        assert_not_nil @instance.encrypted_password
      end
 
      context 'saved again' do
        setup { @instance.save }
        should_not_change '@instance.encrypted_password'
      end
 
      context 'updated' do
        setup { @instance.update_attributes(:password => 'NEW PASSWORD') }
        should_change '@instance.encrypted_password'
      end
 
      context 'loaded completely fresh' do
        setup { @instance = @klass.find(@instance.id) }
 
        context 'and saved' do
          setup { @instance.save }
          should_not_change '@instance.encrypted_password'
        end
      end
    end
  end
 
  context 'Model with a multiple-attribute-based digest' do
    setup do
      @klass = model_with_attributes(:login, :remember_me_token, :remember_me_until) do
        has_digest :remember_me_token, :depends => [:login, :remember_me_until]
      end
    end
 
    context 'saved instance' do
      setup { @instance = @klass.create(:login => 'bob', :remember_me_until => 2.weeks.from_now) }
 
      should 'have digested attribute' do
        assert_not_nil @instance.remember_me_token
      end
 
      context 'update one attribute' do
        setup { @instance.update_attributes(:remember_me_until => 3.weeks.from_now) }
        should_change '@instance.remember_me_token'
      end
 
      context 'update one attribute to nil' do
        setup { @instance.update_attributes(:remember_me_until => nil) }
 
        should 'change digested attribute to nil' do
          assert_nil @instance.remember_me_token
        end
      end
    end
  end
 
  context 'Model with a magic salt column' do
    setup { @klass = model_with_attributes(:salt) }
 
    context 'saved instance' do
      setup { @instance = @klass.create }
 
      should 'not have digested salt attribute' do
        assert_nil @instance.salt
      end
    end
  end
 
  context 'Model with a magic salt column and an attribute-based digest' do
    setup do
      @klass = model_with_attributes(:salt, :encrypted_password) do
        has_digest :encrypted_password, :depends => :password
      end
    end
 
    context 'saved instance' do
      setup { @instance = @klass.create(:password => 'PASSWORD') }
 
      should 'have digested salt attribute' do
        assert_not_nil @instance.salt
      end
 
      should 'have digested encrypted_password attribute' do
        assert_not_nil @instance.encrypted_password
      end
 
      should 'have used salt to digest encrypted password' do
        assert_equal @instance.digest(@instance.salt, 'PASSWORD'), @instance.encrypted_password
      end
    end
  end
end