From 033309696b4be431761cc4f5d5793f8c4efc039b Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Mon, 20 Oct 2014 14:53:17 +0200 Subject: [PATCH] Add tests --- Rakefile | 7 ++++++ test/test_helper.rb | 2 ++ test/twik/python_test.rb | 52 ++++++++++++++++++++++++++++++++++++++++ test/twik_test.rb | 33 +++++++++++++++++++++++++ twik.gemspec | 2 ++ 5 files changed, 96 insertions(+) create mode 100644 test/test_helper.rb create mode 100644 test/twik/python_test.rb create mode 100644 test/twik_test.rb diff --git a/Rakefile b/Rakefile index 809eb56..19cb6b8 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,9 @@ require "bundler/gem_tasks" +require 'rake/testtask' +Rake::TestTask.new do |t| + t.libs << "test" + t.test_files = Dir['test/**/*_test.rb'] +end + +task :default => :test diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..990195e --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,2 @@ +require 'minitest/autorun' +require 'twik' diff --git a/test/twik/python_test.rb b/test/twik/python_test.rb new file mode 100644 index 0000000..428961e --- /dev/null +++ b/test/twik/python_test.rb @@ -0,0 +1,52 @@ +require 'test_helper' + +# Port of the tests from Twik Python version +class Twik::PythonTest < Minitest::Test + PRIVATE_KEY = 'TFCY2AJI-NBPU-V01E-F7CP-PJIZNRKPF25W' + MASTER_KEY = 'foobar' + TAG = 'tag' + + VALUES = { + alphanumeric_and_special_chars: { + 4 => 'm3/I', + 8 => 'mb/5AsJ9', + 12 => 'mb/5AsJ9Uon7', + 22 => 'mb15As*9Uon7ZzvcsXMjpV', + 26 => 'mb15AsJ9&on7ZzvcsXMjpVLTqQ', + }, + alphanumeric: { + 4 => 'm31I', + 8 => 'mb15AsJ9', + 12 => 'mb15AsJ9Uon7', + 22 => 'mb15AsJ9Uon7ZzvcsXMjpV', + 26 => 'mb15AsJ9Uon7ZzvcsXMjpVLTqQ', + }, + numeric: { + 4 => '4315', + 8 => '43154099', + 12 => '431540992657', + 22 => '4315409926570734032171', + 26 => '43154099265707340321711986', + } + } + + def setup + @twik = Twik.new(PRIVATE_KEY) + end + + VALUES.keys.each do |type| + define_method(:"test_password_#{type}") do + @twik.type = type + + VALUES[type].each do |length,value| + assert_equal value, @twik.generate(TAG, MASTER_KEY, length: length) + end + end + end + + def test_size + (1..26).each do |length| + assert_equal length, @twik.generate(TAG, MASTER_KEY, length: length).length + end + end +end diff --git a/test/twik_test.rb b/test/twik_test.rb new file mode 100644 index 0000000..3d09ba2 --- /dev/null +++ b/test/twik_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' + +class TwikTest < Minitest::Test + def setup + @twik = Twik.new('TEST') + end + + def test_that_generates_password + @twik.length = 16 + + assert_equal 'N4eCufjtnKRM+8dK', @twik.generate('test', 'test', type: :alphanumeric_and_special_chars) + assert_equal 'N4eCufjtnKRMK8dK', @twik.generate('test', 'test', type: :alphanumeric) + assert_equal '4483382261839821', @twik.generate('test', 'test', type: :numeric) + end + + def test_that_length_is_correct + assert_equal 1, @twik.generate('test', 'test', length: 1).length + assert_equal 26, @twik.generate('test', 'test', length: 26).length + assert_equal 8, @twik.generate('test', 'test', length: 8).length + end + + def test_that_type_is_numeric + assert_match(/\A[0-9]+\z/, @twik.generate('test', 'test', type: :numeric)) + end + + def test_that_type_is_alphanumeric + assert_match(/\A[A-Za-z0-9]+\z/, @twik.generate('test', 'test', type: :alphanumeric)) + end + + def test_that_type_is_alphanumeric_and_special_chars + refute_match(/\A[A-Za-z0-9]+\z/, @twik.generate('test', 'test', type: :alphanumeric_and_special_chars)) + end +end diff --git a/twik.gemspec b/twik.gemspec index f9c088a..5c9567e 100644 --- a/twik.gemspec +++ b/twik.gemspec @@ -20,4 +20,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.7" spec.add_development_dependency "rake", "~> 10.0" + + spec.add_development_dependency 'minitest', '~> 5.4' end