File: test_key_manager.rb

package info (click to toggle)
ruby-dalli 3.2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: ruby: 6,552; sh: 20; makefile: 4
file content (340 lines) | stat: -rw-r--r-- 10,762 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# frozen_string_literal: true

require_relative 'helper'

describe 'KeyManager' do
  describe 'options' do
    let(:key_manager) { Dalli::KeyManager.new(options) }

    describe 'digest_class' do
      describe 'when there is no explicit digest_class parameter provided' do
        let(:options) { {} }

        it 'uses Digest::MD5 as a default' do
          assert_equal Digest::MD5, key_manager.digest_class
        end
      end

      describe 'when there is an explicit digest_class parameter provided' do
        describe 'and the class implements hexdigest' do
          let(:options) { { digest_class: Digest::SHA2 } }

          it 'uses the specified argument' do
            assert_equal Digest::SHA2, key_manager.digest_class
          end
        end

        describe 'and the class does not implement hexdigest' do
          let(:options) { { digest_class: Object.new } }

          it 'raises an argument error' do
            err = assert_raises ArgumentError do
              key_manager
            end

            assert_equal 'The digest_class object must respond to the hexdigest method', err.message
          end
        end
      end
    end

    describe 'namespace' do
      describe 'when there is no explicit namespace parameter provided' do
        let(:options) { {} }

        it 'the namespace is nil' do
          assert_nil key_manager.namespace
        end
      end

      describe 'when there is an explicit String provided as a namespace parameter' do
        let(:options) { { namespace: namespace_as_s } }
        let(:namespace_as_s) { SecureRandom.hex(5) }

        it 'the namespace is the string' do
          assert_equal namespace_as_s, key_manager.namespace
        end
      end

      describe 'when there is an explicit symbol provided as a namespace parameter' do
        let(:options) { { namespace: namespace_as_symbol } }
        let(:namespace_as_symbol) { namespace_as_s.to_sym }
        let(:namespace_as_s) { SecureRandom.hex(5) }

        it 'the namespace is the stringified symbol' do
          assert_equal namespace_as_s, key_manager.namespace
        end
      end

      describe 'when there is a Proc provided as a namespace parameter' do
        let(:options) { { namespace: namespace_as_proc } }
        let(:namespace_as_proc) { proc { namespace_as_symbol } }
        let(:namespace_as_symbol) { namespace_as_s.to_sym }
        let(:namespace_as_s) { SecureRandom.hex(5) }

        it 'the namespace is the proc' do
          assert_equal namespace_as_proc, key_manager.namespace
        end

        it 'the evaluated namespace is the stringified symbol' do
          assert_equal namespace_as_s, key_manager.evaluate_namespace
        end
      end

      describe 'when the namespace Proc returns dynamic results' do
        count = 0

        let(:options) { { namespace: namespace_as_proc } }
        let(:namespace_as_proc) do
          proc { count += 1 }
        end

        it 'evaluates the namespace proc every time we need it' do
          assert_equal 0, count
          assert_equal '1', key_manager.evaluate_namespace
          assert_equal(/\A2:/, key_manager.namespace_regexp)
          assert_equal '3', key_manager.evaluate_namespace
          assert_equal '4:test', key_manager.key_with_namespace('test')
        end
      end
    end
  end

  describe 'validate_key' do
    subject { key_manager.validate_key(key) }

    describe 'when there is no namespace' do
      let(:key_manager) { Dalli::KeyManager.new(options) }
      let(:options) { {} }

      describe 'when the key is nil' do
        let(:key) { nil }

        it 'raises an error' do
          err = assert_raises ArgumentError do
            subject
          end

          assert_equal 'key cannot be blank', err.message
        end
      end

      describe 'when the key is empty' do
        let(:key) { '' }

        it 'raises an error' do
          err = assert_raises ArgumentError do
            subject
          end

          assert_equal 'key cannot be blank', err.message
        end
      end

      describe 'when the key is blank, but not empty' do
        let(:keylen) { rand(1..5) }
        let(:key) { Array.new(keylen) { [' ', '\t', '\n'].sample }.join }

        it 'returns the key' do
          assert_equal key, subject
        end
      end

      describe 'when the key is shorter than 250 characters' do
        let(:keylen) { rand(1..250) }
        let(:alphanum) { [('a'..'z').to_a, ('A'..'Z').to_a, ('0'..'9').to_a].flatten }
        let(:key) { Array.new(keylen) { alphanum.sample }.join }

        it 'returns the key' do
          assert_equal keylen, key.length
          assert_equal key, subject
        end
      end

      describe 'when the key is longer than 250 characters' do
        let(:keylen) { rand(251..500) }
        let(:alphanum) { [('a'..'z').to_a, ('A'..'Z').to_a, ('0'..'9').to_a].flatten }
        let(:key) { Array.new(keylen) { alphanum.sample }.join }

        describe 'when there is no digest_class parameter' do
          let(:truncated_key) { "#{key[0, 212]}:md5:#{Digest::MD5.hexdigest(key)}" }

          it 'returns the truncated key' do
            assert_equal 249, subject.length
            assert_equal truncated_key, subject
          end
        end

        describe 'when there is a custom digest_class parameter' do
          let(:options) { { digest_class: Digest::SHA2 } }
          let(:truncated_key) { "#{key[0, 180]}:md5:#{Digest::SHA2.hexdigest(key)}" }

          it 'returns the truncated key' do
            assert_equal 249, subject.length
            assert_equal truncated_key, subject
          end
        end
      end
    end

    describe 'when there is a namespace' do
      let(:key_manager) { Dalli::KeyManager.new(options) }
      let(:half_namespace_len) { rand(1..5) }
      let(:namespace_as_s) { SecureRandom.hex(half_namespace_len) }
      let(:options) { { namespace: namespace_as_s } }

      describe 'when the key is nil' do
        let(:key) { nil }

        it 'raises an error' do
          err = assert_raises ArgumentError do
            subject
          end

          assert_equal 'key cannot be blank', err.message
        end
      end

      describe 'when the key is empty' do
        let(:key) { '' }

        it 'raises an error' do
          err = assert_raises ArgumentError do
            subject
          end

          assert_equal 'key cannot be blank', err.message
        end
      end

      describe 'when the key is blank, but not empty' do
        let(:keylen) { rand(1..5) }
        let(:key) { Array.new(keylen) { [' ', '\t', '\n'].sample }.join }

        it 'returns the key' do
          assert_equal "#{namespace_as_s}:#{key}", subject
        end
      end

      describe 'when the key with namespace is shorter than 250 characters' do
        let(:keylen) { rand(250 - (half_namespace_len * 2)) + 1 }
        let(:alphanum) { [('a'..'z').to_a, ('A'..'Z').to_a, ('0'..'9').to_a].flatten }
        let(:key) { Array.new(keylen) { alphanum.sample }.join }

        it 'returns the key' do
          assert_equal keylen, key.length
          assert_equal "#{namespace_as_s}:#{key}", subject
        end
      end

      describe 'when the key with namespace is longer than 250 characters' do
        let(:keylen) { rand(251..500) - (half_namespace_len * 2) }
        let(:alphanum) { [('a'..'z').to_a, ('A'..'Z').to_a, ('0'..'9').to_a].flatten }
        let(:key) { Array.new(keylen) { alphanum.sample }.join }

        describe 'when there is no digest_class parameter' do
          let(:key_prefix) { key[0, 212 - (half_namespace_len * 2)] }
          let(:truncated_key) do
            "#{namespace_as_s}:#{key_prefix}:md5:#{Digest::MD5.hexdigest("#{namespace_as_s}:#{key}")}"
          end

          it 'returns the truncated key' do
            assert_equal 250, subject.length
            assert_equal truncated_key, subject
          end
        end

        describe 'when there is a custom digest_class parameter' do
          let(:options) { { digest_class: Digest::SHA2, namespace: namespace_as_s } }
          let(:key_prefix) { key[0, 180 - (half_namespace_len * 2)] }
          let(:truncated_key) do
            "#{namespace_as_s}:#{key_prefix}:md5:#{Digest::SHA2.hexdigest("#{namespace_as_s}:#{key}")}"
          end

          it 'returns the truncated key' do
            assert_equal 250, subject.length
            assert_equal truncated_key, subject
          end
        end
      end
    end
  end

  describe 'key_with_namespace' do
    let(:raw_key) { SecureRandom.hex(10) }
    let(:key_manager) { Dalli::KeyManager.new(options) }
    subject { key_manager.key_with_namespace(raw_key) }

    describe 'without namespace' do
      let(:options) { {} }

      it 'returns the argument' do
        assert_equal raw_key, subject
      end
    end

    describe 'with namespace' do
      let(:namespace_as_s) { SecureRandom.hex(5) }
      let(:options) { { namespace: namespace_as_s } }

      it 'returns the argument with the namespace prepended' do
        assert_equal "#{namespace_as_s}:#{raw_key}", subject
      end
    end
  end

  describe 'key_without_namespace' do
    let(:key_manager) { Dalli::KeyManager.new(options) }
    subject { key_manager.key_without_namespace(raw_key) }

    describe 'without namespace' do
      let(:options) { {} }

      describe 'when the key has no colon' do
        let(:raw_key) { SecureRandom.hex(10) }

        it 'returns the argument' do
          assert_equal raw_key, subject
        end
      end

      describe 'when the key has a colon' do
        let(:raw_key) { "#{SecureRandom.hex(5)}:#{SecureRandom.hex(10)}" }

        it 'returns the argument' do
          assert_equal raw_key, subject
        end
      end
    end

    describe 'with namespace' do
      let(:namespace_as_s) { SecureRandom.hex(5) }
      let(:options) { { namespace: namespace_as_s } }

      describe 'when the argument starts with the namespace' do
        let(:key_wout_namespace) { SecureRandom.hex(5) }
        let(:raw_key) { "#{namespace_as_s}:#{key_wout_namespace}" }

        it 'strips the namespace' do
          assert_equal key_wout_namespace, subject
        end
      end

      describe 'when the argument includes the namespace in a position other than the start' do
        let(:raw_key) { "#{SecureRandom.hex(5)}#{namespace_as_s}:#{SecureRandom.hex(5)}" }

        it 'returns the argument' do
          assert_equal raw_key, subject
        end
      end

      describe 'when the argument does not include the namespace' do
        let(:raw_key) { SecureRandom.hex(10) }

        it 'returns the argument' do
          assert_equal raw_key, subject
        end
      end
    end
  end
end