Add tests

master
Guillaume Dott 2014-10-20 14:53:17 +02:00
parent e51ed19b8e
commit 033309696b
5 changed files with 96 additions and 0 deletions

View File

@ -1,2 +1,9 @@
require "bundler/gem_tasks" 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

View File

@ -0,0 +1,2 @@
require 'minitest/autorun'
require 'twik'

View File

@ -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

33
test/twik_test.rb 100644
View File

@ -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

View File

@ -20,4 +20,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.7" spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency 'minitest', '~> 5.4'
end end