2014-12-29 14:45:25 +00:00
|
|
|
#!/usr/bin/ruby
|
2014-12-07 15:24:31 +00:00
|
|
|
|
2014-12-09 21:53:49 +00:00
|
|
|
require_relative '../lib/MPW'
|
2015-02-12 22:03:01 +00:00
|
|
|
require_relative '../lib/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
|
2015-02-12 22:03:01 +00:00
|
|
|
fixture_file = 'files/fixtures.yml'
|
2014-12-28 17:48:37 +00:00
|
|
|
|
2014-12-09 21:53:49 +00:00
|
|
|
file_gpg = 'test.gpg'
|
2014-12-30 08:32:14 +00:00
|
|
|
key = ENV['MPW_TEST_KEY']
|
2014-12-30 08:22:41 +00:00
|
|
|
|
|
|
|
puts
|
2014-12-07 15:24:31 +00:00
|
|
|
|
|
|
|
if defined?(I18n.enforce_available_locales)
|
|
|
|
I18n.enforce_available_locales = false
|
|
|
|
end
|
|
|
|
|
2014-12-09 21:53:49 +00:00
|
|
|
File.delete(file_gpg) if File.exist?(file_gpg)
|
2015-02-12 22:03:01 +00:00
|
|
|
|
|
|
|
@mpw = MPW::MPW.new(file_gpg, key)
|
|
|
|
@fixtures = YAML.load_file(fixture_file)
|
2014-12-07 15:24:31 +00:00
|
|
|
end
|
2014-12-29 14:36:06 +00:00
|
|
|
|
2014-12-30 08:22:41 +00:00
|
|
|
def test_01_import_yaml
|
2014-12-30 07:57:45 +00:00
|
|
|
import_file = 'files/test_import.yml'
|
2014-12-29 14:36:06 +00:00
|
|
|
|
|
|
|
assert(@mpw.import(import_file, :yaml))
|
2015-02-12 22:03:01 +00:00
|
|
|
assert_equal(2, @mpw.list.length)
|
|
|
|
|
|
|
|
item = @mpw.list[0]
|
|
|
|
assert_equal(@fixtures['add_new']['name'], item.name)
|
|
|
|
assert_equal(@fixtures['add_new']['group'], item.group)
|
|
|
|
assert_equal(@fixtures['add_new']['host'], item.host)
|
|
|
|
assert_equal(@fixtures['add_new']['protocol'], item.protocol)
|
|
|
|
assert_equal(@fixtures['add_new']['user'], item.user)
|
|
|
|
assert_equal(@fixtures['add_new']['password'], item.password)
|
|
|
|
assert_equal(@fixtures['add_new']['port'].to_i, item.port)
|
|
|
|
assert_equal(@fixtures['add_new']['comment'], item.comment)
|
2014-12-29 14:36:06 +00:00
|
|
|
end
|
|
|
|
|
2014-12-30 08:22:41 +00:00
|
|
|
def test_02_export_yaml
|
2015-02-12 22:03:01 +00:00
|
|
|
import_file = 'files/test_import.yml'
|
2014-12-29 14:36:06 +00:00
|
|
|
export_file = 'test_export.yml'
|
|
|
|
|
2015-02-12 22:03:01 +00:00
|
|
|
assert(@mpw.import(import_file))
|
|
|
|
assert_equal(2, @mpw.list.length)
|
2014-12-29 14:36:06 +00:00
|
|
|
assert(@mpw.export(export_file, :yaml))
|
|
|
|
export = YAML::load_file(export_file)
|
|
|
|
assert_equal(2, export.length)
|
|
|
|
|
|
|
|
result = export.values[0]
|
2015-02-12 22:03:01 +00:00
|
|
|
assert_equal(@fixtures['add_new']['name'], result['name'])
|
|
|
|
assert_equal(@fixtures['add_new']['group'], result['group'])
|
|
|
|
assert_equal(@fixtures['add_new']['host'], result['host'])
|
|
|
|
assert_equal(@fixtures['add_new']['protocol'], result['protocol'])
|
|
|
|
assert_equal(@fixtures['add_new']['user'], result['user'])
|
|
|
|
assert_equal(@fixtures['add_new']['password'], result['password'])
|
|
|
|
assert_equal(@fixtures['add_new']['port'].to_i, result['port'])
|
|
|
|
assert_equal(@fixtures['add_new']['comment'], result['comment'])
|
2014-12-29 14:36:06 +00:00
|
|
|
|
|
|
|
File.unlink(export_file)
|
|
|
|
end
|
|
|
|
|
2014-12-30 08:22:41 +00:00
|
|
|
def test_03_import_csv
|
2014-12-30 07:57:45 +00:00
|
|
|
import_file = 'files/test_import.csv'
|
2014-12-29 14:36:06 +00:00
|
|
|
|
|
|
|
assert(@mpw.import(import_file, :csv))
|
2015-02-12 22:03:01 +00:00
|
|
|
assert_equal(2, @mpw.list.length)
|
2014-12-29 14:36:06 +00:00
|
|
|
|
|
|
|
import = CSV.parse(File.read(import_file), headers: true)
|
|
|
|
|
2015-02-12 22:03:01 +00:00
|
|
|
item = @mpw.list[0]
|
|
|
|
assert_equal(import[0]['name'], item.name)
|
|
|
|
assert_equal(import[0]['group'], item.group)
|
|
|
|
assert_equal(import[0]['host'], item.host)
|
|
|
|
assert_equal(import[0]['protocol'], item.protocol)
|
|
|
|
assert_equal(import[0]['user'], item.user)
|
|
|
|
assert_equal(import[0]['password'], item.password)
|
|
|
|
assert_equal(import[0]['port'].to_i, item.port)
|
|
|
|
assert_equal(import[0]['comment'], item.comment)
|
2014-12-29 14:36:06 +00:00
|
|
|
end
|
|
|
|
|
2014-12-30 08:22:41 +00:00
|
|
|
def test_04_export_csv
|
2015-02-12 22:03:01 +00:00
|
|
|
import_file = 'files/test_import.csv'
|
2014-12-29 14:36:06 +00:00
|
|
|
export_file = 'test_export.csv'
|
2015-02-12 22:03:01 +00:00
|
|
|
|
|
|
|
assert(@mpw.import(import_file, :csv))
|
|
|
|
assert_equal(2, @mpw.list.length)
|
2014-12-29 14:36:06 +00:00
|
|
|
assert(@mpw.export(export_file, :csv))
|
|
|
|
export = CSV.parse(File.read(export_file), headers: true)
|
|
|
|
assert_equal(2, export.length)
|
|
|
|
|
|
|
|
result = export[0]
|
2015-02-12 22:03:01 +00:00
|
|
|
assert_equal(@fixtures['add_new']['name'], result['name'])
|
|
|
|
assert_equal(@fixtures['add_new']['group'], result['group'])
|
|
|
|
assert_equal(@fixtures['add_new']['host'], result['host'])
|
|
|
|
assert_equal(@fixtures['add_new']['protocol'], result['protocol'])
|
|
|
|
assert_equal(@fixtures['add_new']['user'], result['user'])
|
|
|
|
assert_equal(@fixtures['add_new']['password'], result['password'])
|
|
|
|
assert_equal(@fixtures['add_new']['port'], result['port'])
|
|
|
|
assert_equal(@fixtures['add_new']['comment'], result['comment'])
|
2014-12-29 14:36:06 +00:00
|
|
|
|
|
|
|
File.unlink(export_file)
|
|
|
|
end
|
2014-12-07 15:24:31 +00:00
|
|
|
|
2015-02-12 22:03:01 +00:00
|
|
|
def test_05_add_item
|
|
|
|
data = {name: @fixtures['add_new']['name'],
|
|
|
|
group: @fixtures['add_new']['group'],
|
|
|
|
host: @fixtures['add_new']['host'],
|
|
|
|
protocol: @fixtures['add_new']['protocol'],
|
|
|
|
user: @fixtures['add_new']['user'],
|
|
|
|
password: @fixtures['add_new']['password'],
|
|
|
|
port: @fixtures['add_new']['port'],
|
|
|
|
comment: @fixtures['add_new']['comment'],
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-02-12 22:03:01 +00:00
|
|
|
assert(@mpw.add(item))
|
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]
|
|
|
|
assert_equal(@fixtures['add_new']['name'], item.name)
|
|
|
|
assert_equal(@fixtures['add_new']['group'], item.group)
|
|
|
|
assert_equal(@fixtures['add_new']['host'], item.host)
|
|
|
|
assert_equal(@fixtures['add_new']['protocol'], item.protocol)
|
|
|
|
assert_equal(@fixtures['add_new']['user'], item.user)
|
|
|
|
assert_equal(@fixtures['add_new']['password'], item.password)
|
|
|
|
assert_equal(@fixtures['add_new']['port'].to_i, item.port)
|
|
|
|
assert_equal(@fixtures['add_new']['comment'], item.comment)
|
2014-12-29 14:45:25 +00:00
|
|
|
end
|
2014-12-30 08:22:41 +00:00
|
|
|
|
|
|
|
def test_11_encrypt_empty_file
|
|
|
|
assert(@mpw.encrypt)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_12_encrypt
|
2015-02-12 22:03:01 +00:00
|
|
|
import_file = 'files/test_import.yml'
|
|
|
|
|
|
|
|
assert(@mpw.import(import_file, :yaml))
|
|
|
|
assert_equal(2, @mpw.list.length)
|
2014-12-30 08:22:41 +00:00
|
|
|
|
|
|
|
assert(@mpw.encrypt)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_13_decrypt_empty_file
|
|
|
|
assert(@mpw.decrypt)
|
2015-02-12 22:03:01 +00:00
|
|
|
assert_equal(0, @mpw.list.length)
|
2014-12-30 08:22:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_14_decrypt
|
2014-12-30 08:55:31 +00:00
|
|
|
import_file = 'files/test_import.yml'
|
|
|
|
|
2015-02-12 22:03:01 +00:00
|
|
|
assert(@mpw.import(import_file, :yaml))
|
|
|
|
assert_equal(2, @mpw.list.length)
|
2014-12-30 08:55:31 +00:00
|
|
|
|
2015-02-12 22:03:01 +00:00
|
|
|
assert(@mpw.encrypt)
|
2014-12-31 10:30:30 +00:00
|
|
|
|
2015-02-12 22:03:01 +00:00
|
|
|
assert(@mpw.decrypt)
|
|
|
|
assert_equal(2, @mpw.list.length)
|
|
|
|
|
|
|
|
item = @mpw.list[0]
|
|
|
|
assert_equal(@fixtures['add_new']['name'], item.name)
|
|
|
|
assert_equal(@fixtures['add_new']['group'], item.group)
|
|
|
|
assert_equal(@fixtures['add_new']['host'], item.host)
|
|
|
|
assert_equal(@fixtures['add_new']['protocol'], item.protocol)
|
|
|
|
assert_equal(@fixtures['add_new']['user'], item.user)
|
|
|
|
assert_equal(@fixtures['add_new']['password'], item.password)
|
|
|
|
assert_equal(@fixtures['add_new']['port'].to_i, item.port)
|
|
|
|
assert_equal(@fixtures['add_new']['comment'], item.comment)
|
2014-12-31 10:30:30 +00:00
|
|
|
end
|
2015-05-11 17:15:33 +00:00
|
|
|
|
|
|
|
def test_15_search
|
|
|
|
import_file = 'files/test_import.yml'
|
|
|
|
|
|
|
|
assert(@mpw.import(import_file, :yaml))
|
|
|
|
assert_equal(2, @mpw.list.length)
|
|
|
|
|
|
|
|
assert_equal(1, @mpw.list(group: @fixtures['add_new']['group']).length)
|
|
|
|
assert_equal(1, @mpw.list(protocol: @fixtures['add_new']['protocol']).length)
|
|
|
|
assert_equal(2, @mpw.list(search: @fixtures['add_new']['name'][0..-2]).length)
|
|
|
|
end
|
2014-12-07 15:24:31 +00:00
|
|
|
end
|