1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-30 08:53:05 +00:00
mpw/test/test_mpw.rb

137 lines
2.9 KiB
Ruby
Raw Normal View History

2014-12-29 14:45:25 +00:00
#!/usr/bin/ruby
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
wallet_file = 'default.gpg'
key = 'test@example.com'
password = 'password'
if defined?(I18n.enforce_available_locales)
I18n.enforce_available_locales = false
end
@mpw = MPW::MPW.new(key, wallet_file, password)
2017-05-11 20:24:05 +00:00
@fixtures = YAML.load_file('./test/files/fixtures.yml')
end
def test_00_decrypt_empty_file
@mpw.read_data
assert_equal(0, @mpw.list.length)
end
def test_01_encrypt_empty_file
@mpw.read_data
@mpw.write_data
end
def test_02_add_item
2017-05-11 20:24:05 +00:00
data = {
2017-05-13 10:28:42 +00:00
group: @fixtures['add']['group'],
host: @fixtures['add']['host'],
protocol: @fixtures['add']['protocol'],
user: @fixtures['add']['user'],
port: @fixtures['add']['port'],
comment: @fixtures['add']['comment']
2017-05-11 20:24:05 +00:00
}
item = MPW::Item.new(data)
assert(!item.nil?)
assert(!item.empty?)
@mpw.read_data
@mpw.add(item)
2017-05-13 10:28:42 +00:00
@mpw.set_password(item.id, @fixtures['add']['password'])
assert_equal(1, @mpw.list.length)
item = @mpw.list[0]
2017-05-13 10:28:42 +00:00
@fixtures['add'].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
@mpw.write_data
end
def test_03_decrypt_file
@mpw.read_data
assert_equal(1, @mpw.list.length)
item = @mpw.list[0]
2017-05-13 10:28:42 +00:00
@fixtures['add'].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
end
def test_04_delete_item
@mpw.read_data
assert_equal(1, @mpw.list.length)
2017-05-11 20:24:05 +00:00
@mpw.list.each(&:delete)
assert_equal(0, @mpw.list.length)
@mpw.write_data
end
def test_05_search
@mpw.read_data
@fixtures.each_value do |v|
2017-05-11 20:24:05 +00:00
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
assert_equal(3, @mpw.list.length)
2017-05-13 10:28:42 +00:00
assert_equal(1, @mpw.list(group: @fixtures['add']['group']).length)
assert_equal(1, @mpw.list(pattern: 'gogole').length)
assert_equal(2, @mpw.list(pattern: 'example[2\.]').length)
end
def test_06_add_gpg_key
@mpw.read_data
@mpw.add_key('test2@example.com')
assert_equal(2, @mpw.list_keys.length)
@mpw.write_data
end
def test_07_delete_gpg_key
@mpw.read_data
assert_equal(2, @mpw.list_keys.length)
@mpw.delete_key('test2@example.com')
assert_equal(1, @mpw.list_keys.length)
@mpw.write_data
end
2014-12-07 15:24:31 +00:00
end