1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-12-05 02:23:04 +00:00
mpw/test/test_mpw.rb

141 lines
2.8 KiB
Ruby
Raw Normal View History

2014-12-29 14:45:25 +00:00
#!/usr/bin/ruby
2014-12-07 15:24:31 +00:00
2016-11-10 22:57:16 +00:00
require 'mpw/mpw'
require 'mpw/item'
2014-12-09 21:53:49 +00:00
require 'test/unit'
require 'yaml'
2014-12-28 17:18:45 +00:00
require 'csv'
2014-12-07 15:24:31 +00:00
class TestMPW < Test::Unit::TestCase
def setup
2016-11-10 22:57:16 +00:00
fixture_file = './test/files/fixtures.yml'
2014-12-28 17:48:37 +00:00
2016-11-10 22:57:16 +00:00
wallet_file = 'default.gpg'
key = 'test@example.com'
password = 'password'
2014-12-07 15:24:31 +00:00
if defined?(I18n.enforce_available_locales)
I18n.enforce_available_locales = false
end
2016-11-10 22:57:16 +00:00
@mpw = MPW::MPW.new(key, wallet_file, password)
2015-02-12 22:03:01 +00:00
@fixtures = YAML.load_file(fixture_file)
2014-12-07 15:24:31 +00:00
end
2014-12-29 14:36:06 +00:00
2016-11-10 22:57:16 +00:00
def test_00_decrypt_empty_file
@mpw.read_data
assert_equal(0, @mpw.list.length)
2014-12-29 14:36:06 +00:00
end
2016-11-10 22:57:16 +00:00
def test_01_encrypt_empty_file
@mpw.read_data
@mpw.write_data
2014-12-29 14:36:06 +00:00
end
2014-12-07 15:24:31 +00:00
2016-11-10 22:57:16 +00:00
def test_02_add_item
data = { group: @fixtures['add_new']['group'],
host: @fixtures['add_new']['host'],
protocol: @fixtures['add_new']['protocol'],
user: @fixtures['add_new']['user'],
port: @fixtures['add_new']['port'],
comment: @fixtures['add_new']['comment'],
2015-02-12 22:03:01 +00:00
}
item = MPW::Item.new(data)
2014-12-07 15:24:31 +00:00
2015-02-12 22:03:01 +00:00
assert(!item.nil?)
assert(!item.empty?)
2014-12-09 21:53:49 +00:00
2016-11-10 22:57:16 +00:00
@mpw.read_data
@mpw.add(item)
@mpw.set_password(item.id, @fixtures['add_new']['password'])
2014-12-09 21:53:49 +00:00
2015-02-12 22:03:01 +00:00
assert_equal(1, @mpw.list.length)
2014-12-28 17:48:37 +00:00
2015-02-12 22:03:01 +00:00
item = @mpw.list[0]
2016-11-10 22:57:16 +00:00
@fixtures['add_new'].each do |k,v|
if k == 'password'
assert_equal(v, @mpw.get_password(item.id))
else
assert_equal(v, item.send(k).to_s)
end
end
2014-12-30 08:22:41 +00:00
2016-11-10 22:57:16 +00:00
@mpw.write_data
2014-12-30 08:22:41 +00:00
end
2016-11-10 22:57:16 +00:00
def test_03_decrypt_file
@mpw.read_data
assert_equal(1, @mpw.list.length)
2014-12-30 08:22:41 +00:00
2016-11-10 22:57:16 +00:00
item = @mpw.list[0]
@fixtures['add_new'].each do |k,v|
if k == 'password'
assert_equal(v, @mpw.get_password(item.id))
else
assert_equal(v, item.send(k).to_s)
end
end
2014-12-30 08:22:41 +00:00
end
2016-11-10 22:57:16 +00:00
def test_04_delete_item
@mpw.read_data
2014-12-30 08:55:31 +00:00
2016-11-10 22:57:16 +00:00
assert_equal(1, @mpw.list.length)
2014-12-30 08:55:31 +00:00
2016-11-10 22:57:16 +00:00
@mpw.list.each do |item|
item.delete
end
2014-12-31 10:30:30 +00:00
2016-11-10 22:57:16 +00:00
assert_equal(0, @mpw.list.length)
2015-02-12 22:03:01 +00:00
2016-11-10 22:57:16 +00:00
@mpw.write_data
2014-12-31 10:30:30 +00:00
end
2015-05-11 17:15:33 +00:00
2016-11-10 22:57:16 +00:00
def test_05_search
@mpw.read_data
@fixtures.each_value do |v|
data = { group: v['group'],
host: v['host'],
protocol: v['protocol'],
user: v['user'],
port: v['port'],
comment: v['comment'],
}
item = MPW::Item.new(data)
assert(!item.nil?)
assert(!item.empty?)
@mpw.add(item)
@mpw.set_password(item.id, v['password'])
end
2015-05-11 17:15:33 +00:00
2016-11-10 22:57:16 +00:00
assert_equal(3, @mpw.list.length)
2015-05-11 17:15:33 +00:00
assert_equal(1, @mpw.list(group: @fixtures['add_new']['group']).length)
2016-11-10 22:57:16 +00:00
assert_equal(1, @mpw.list(pattern: 'existing').length)
assert_equal(2, @mpw.list(pattern: 'host_[eu]').length)
2015-05-11 17:15:33 +00:00
end
def test_06_add_gpg_key
@mpw.read_data
@mpw.add_key('test2@example.com')
2017-02-11 12:47:22 +00:00
assert_equal(2, @mpw.list_keys.length)
@mpw.write_data
end
2017-02-11 12:43:29 +00:00
def test_07_delete_gpg_key
@mpw.read_data
2017-02-11 12:47:22 +00:00
assert_equal(2, @mpw.list_keys.length)
@mpw.delete_key('test2@example.com')
2017-02-11 12:47:22 +00:00
assert_equal(1, @mpw.list_keys.length)
@mpw.write_data
end
2014-12-07 15:24:31 +00:00
end