keystok 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,119 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under GitDock Oy license terms.
3
+ # See https://bitbucket.org/keystok/keystok-client-ruby/raw/master/LICENSE.txt
4
+ # for details.
5
+
6
+ require 'spec_helper'
7
+ require 'stringio'
8
+ require 'tempfile'
9
+
10
+ describe Keystok::Cache do
11
+ let(:dummy_class) do
12
+ Class.new do
13
+ attr_accessor :config
14
+ include Keystok::AESCrypto
15
+ include Keystok::Cache
16
+ end
17
+ end
18
+ let(:dummy) { dummy_class.new }
19
+ let(:iostream) { StringIO.new }
20
+ let(:test_data) { 'data_' * 10 }
21
+ let(:tmp_dir) { File.expand_path('../../../tmp', __FILE__) }
22
+ let(:tmp_filepath) { File.join(tmp_dir, 'keystok_cache.data') }
23
+
24
+ before do
25
+ Keystok.logger = Logger.new(nil)
26
+ end
27
+
28
+ describe 'cache_file_path' do
29
+ it 'works with dir passed as an argument' do
30
+ path = dummy.cache_file_path('/some_dir')
31
+ expect(path).to eq('/some_dir/keystok_cache.data')
32
+ end
33
+
34
+ it 'works without dir argument or config' do
35
+ path = dummy.cache_file_path
36
+ expect(path).to eq('./keystok_cache.data')
37
+ end
38
+
39
+ it 'works with dir in @config' do
40
+ dummy.config = { tmp_dir: '/another_dir' }
41
+ path = dummy.cache_file_path
42
+ expect(path).to eq('/another_dir/keystok_cache.data')
43
+ end
44
+ end
45
+
46
+ describe 'cache_file_exists?' do
47
+ before do
48
+ dummy.config = { tmp_dir: tmp_dir }
49
+ end
50
+
51
+ it 'returns true if file exists' do
52
+ FileUtils.touch(tmp_filepath)
53
+ expect(dummy.cache_file_exists?).to eq(true)
54
+ end
55
+
56
+ it 'returns false if file not exists' do
57
+ FileUtils.rm_f(tmp_filepath)
58
+ expect(dummy.cache_file_exists?).to eq(false)
59
+ end
60
+ end
61
+
62
+ describe 'cache_stream' do
63
+ before do
64
+ dummy.config = { tmp_dir: tmp_dir }
65
+ FileUtils.touch(tmp_filepath)
66
+ end
67
+
68
+ after do
69
+ FileUtils.rm_f(tmp_filepath)
70
+ end
71
+
72
+ it 'is opening file for read when mode is :read' do
73
+ expect { dummy.cache_stream(:read).read }.to_not raise_error
74
+ end
75
+
76
+ it 'is opening file for write when mode is :write' do
77
+ expect { dummy.cache_stream(:write).write('dummy') }.to_not raise_error
78
+ end
79
+
80
+ it 'default mode is :read' do
81
+ expect { dummy.cache_stream.read }.to_not raise_error
82
+ end
83
+ end
84
+
85
+ describe 'load_cache' do
86
+ before do
87
+ iostream.write('some dummy data')
88
+ iostream.seek(0)
89
+ end
90
+
91
+ it "logs warning when it's loading data" do
92
+ logger = double(:logger)
93
+ Keystok.logger = logger
94
+ expect(logger).to receive(:warn).once
95
+ .with('Loading data from cache')
96
+ begin
97
+ dummy.load_cache(iostream)
98
+ # rubocop:disable HandleExceptions
99
+ rescue Keystok::Error::CacheCorrupted
100
+ # rubocop:enable HandleExceptions
101
+ # Suppressing because we just want to check logger message
102
+ end
103
+ end
104
+ end
105
+
106
+ describe 'write_cache' do
107
+ it 'works' do
108
+ expect { dummy.write_cache(test_data, iostream) }.to_not raise_error
109
+ end
110
+ end
111
+
112
+ describe 'sanity check' do
113
+ it 'saving and loading is not changing data' do
114
+ dummy.write_cache(test_data, iostream)
115
+ iostream.seek(0)
116
+ expect(dummy.load_cache(iostream)).to eq(test_data)
117
+ end
118
+ end
119
+ end