1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-27 07:33:05 +00:00

add new unit test

This commit is contained in:
nishiki 2014-12-07 16:24:31 +01:00
parent e5529e6a7b
commit 140819dbb5
3 changed files with 107 additions and 47 deletions

View file

@ -8,6 +8,7 @@ require 'gpgme'
require 'csv' require 'csv'
require 'i18n' require 'i18n'
require 'fileutils' require 'fileutils'
require 'yaml'
module MPW module MPW
class MPW class MPW
@ -15,7 +16,7 @@ module MPW
attr_accessor :error_msg attr_accessor :error_msg
# Constructor # Constructor
def initialize(file_gpg, key=nil, share_keys='') def initialize(file_gpg, key, share_keys='')
@error_msg = nil @error_msg = nil
@file_gpg = file_gpg @file_gpg = file_gpg
@key = key @key = key
@ -33,7 +34,6 @@ module MPW
@data = YAML.load(data_decrypt) if not data_decrypt.to_s.empty? @data = YAML.load(data_decrypt) if not data_decrypt.to_s.empty?
end end
puts @data.class
return true return true
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.gpg_file.decrypt')}\n#{e}" @error_msg = "#{I18n.t('error.gpg_file.decrypt')}\n#{e}"

View file

@ -1,45 +0,0 @@
# File: tc_simple_number.rb
require_relative "../lib/MPW"
require "test/unit"
class TestMPW < Test::Unit::TestCase
def test_initialize
File.delete('test.gpg') if File.exist?('test.gpg')
end
def test_load_empty_file
mpw = MPW::MPW.new('test.cfg')
mpw.decrypt
assert_equal(0, mpw.search.length)
end
def test_add
mpw = MPW::MPW.new('test.cfg')
name = 'test_name'
group = 'test_group'
host = 'test_host'
protocol = 'test_protocol'
login = 'test_login'
password = 'test_password'
port = '42'
comment = 'test_comment'
mpw.update(name, group, host, protocol, login, password, port, comment)
assert_equal(1, mpw.search.length)
assert_equal('test_name', mpw.search[0]['name'])
assert_equal('test_group', mpw.search[0]['group'])
assert_equal('test_host', mpw.search[0]['host'])
assert_equal('test_protocol', mpw.search[0]['protocol'])
assert_equal('test_login', mpw.search[0]['login'])
assert_equal('test_password', mpw.search[0]['password'])
assert_equal(42, mpw.search[0]['port'])
assert_equal('test_comment', mpw.search[0]['comment'])
end
end

105
test/test_mpw.rb Normal file
View file

@ -0,0 +1,105 @@
# File: tc_simple_number.rb
require_relative "../lib/MPW"
require "test/unit"
class TestMPW < Test::Unit::TestCase
def setup
@file_gpg = 'test.gpg'
@key = 'test-mpw@test-mpw.local'
if defined?(I18n.enforce_available_locales)
I18n.enforce_available_locales = false
end
File.delete(@file_gpg) if File.exist?(@file_gpg)
@mpw = MPW::MPW.new(@file_gpg, @key)
end
def test_load_empty_file
assert(@mpw.decrypt)
assert_equal(0, @mpw.search.length)
end
def test_add
name = 'test_name'
group = 'test_group'
host = 'test_host'
protocol = 'test_protocol'
login = 'test_login'
password = 'test_password'
port = '42'
comment = 'test_comment'
assert(@mpw.update(name, group, host, protocol, login, password, port, comment))
assert_equal(1, @mpw.search.length)
result = @mpw.search[0]
assert_equal('test_name', result['name'])
assert_equal('test_group', result['group'])
assert_equal('test_host', result['host'])
assert_equal('test_protocol', result['protocol'])
assert_equal('test_login', result['login'])
assert_equal('test_password', result['password'])
assert_equal(42, result['port'])
assert_equal('test_comment', result['comment'])
assert(@mpw.update(name, group, host, protocol, login, password, port, comment))
assert_equal(2, @mpw.search.length)
end
def test_add_empty_name
name = ''
group = 'test_group'
host = 'test_host'
protocol = 'test_protocol'
login = 'test_login'
password = 'test_password'
port = '42'
comment = 'test_comment'
assert(!@mpw.update(name, group, host, protocol, login, password, port, comment))
assert_equal(0, @mpw.search.length)
end
def test_update
name = 'test_name'
group = 'test_group'
host = 'test_host'
protocol = 'test_protocol'
login = 'test_login'
password = 'test_password'
port = '42'
comment = 'test_comment'
assert(@mpw.update(name, group, host, protocol, login, password, port, comment))
id = @mpw.search[0]['id']
name = 'test_name_update'
group = 'test_group_update'
host = 'test_host_update'
protocol = 'test_protocol_update'
login = 'test_login_update'
password = 'test_password_update'
port = '43'
comment = 'test_comment_update'
assert(@mpw.update(name, group, host, protocol, login, password, port, comment, id))
assert_equal(1, @mpw.search.length)
result = @mpw.search_by_id(id)
assert_equal('test_name_update', result['name'])
assert_equal('test_group_update', result['group'])
assert_equal('test_host_update', result['host'])
assert_equal('test_protocol_update', result['protocol'])
assert_equal('test_login_update', result['login'])
assert_equal('test_password_update', result['password'])
assert_equal(43, result['port'])
assert_equal('test_comment_update', result['comment'])
end
end