|
| 1 | +# frozen_string_literal: false |
| 2 | + |
| 3 | +require 'minitest/autorun' |
| 4 | +require 'pass_station' |
| 5 | +require 'pass_station/output' |
| 6 | + |
| 7 | +# Create a pseudo-Boolean class |
| 8 | +module Boolean; end |
| 9 | +class TrueClass; include Boolean; end |
| 10 | +class FalseClass; include Boolean; end |
| 11 | + |
| 12 | +class PassStattionTest < Minitest::Test |
| 13 | + def setup |
| 14 | + @ps = PassStation::DB.new |
| 15 | + @ps.parse |
| 16 | + end |
| 17 | + |
| 18 | + def test_DB_attributes_defaults |
| 19 | + ps = PassStation::DB.new |
| 20 | + assert_equal('data/', ps.storage_location) |
| 21 | + assert_equal('DefaultCreds-Cheat-Sheet.csv', ps.database_name) |
| 22 | + assert_nil(ps.data) |
| 23 | + end |
| 24 | + |
| 25 | + def test_DB_check_for_update |
| 26 | + assert_kind_of(Boolean, PassStation::DB.check_for_update) |
| 27 | + end |
| 28 | + |
| 29 | + def test_DB_download_upstream |
| 30 | + assert_instance_of(String, PassStation::DB.download_upstream(Dir.mktmpdir)) |
| 31 | + end |
| 32 | + |
| 33 | + def test_DB_parse |
| 34 | + assert_instance_of(Array, @ps.parse) |
| 35 | + assert_instance_of(CSV::Row, @ps.parse[0]) |
| 36 | + assert_instance_of(Array, @ps.parse(:username)) |
| 37 | + assert_instance_of(CSV::Row, @ps.parse(:password)[0]) |
| 38 | + end |
| 39 | + |
| 40 | + def test_DB_search |
| 41 | + # exists |
| 42 | + assert_instance_of(Array, @ps.search('zyxel', :productvendor)) |
| 43 | + assert_instance_of(CSV::Row, @ps.search('zyxel', :productvendor)[0]) |
| 44 | + # Doesn't exist |
| 45 | + assert_empty(@ps.search('xdshfiuds6567557657', :username)) |
| 46 | + end |
| 47 | + |
| 48 | + def test_DB_output |
| 49 | + # Types |
| 50 | + assert_instance_of(Array, @ps.output('table', @ps.data)) |
| 51 | + assert_instance_of(Array, @ps.output('pretty-table', @ps.data)) |
| 52 | + assert_instance_of(String, @ps.output('table', @ps.data)[0]) |
| 53 | + assert_instance_of(String, @ps.output('pretty-table', @ps.data)[0]) |
| 54 | + # Headers |
| 55 | + assert(@ps.output('table', @ps.data)[0].match?(/productvendor\s+username\s+password\s+/)) |
| 56 | + assert(@ps.output('pretty-table', @ps.data)[0].match?(/\+-+\+-+\+-+\+/)) |
| 57 | + assert(@ps.output('pretty-table', @ps.data)[1].match?(/|\sproductvendor\s+|\susername\+|\spassword\s+|/)) |
| 58 | + end |
| 59 | + |
| 60 | + def test_DB_output_list |
| 61 | + assert_instance_of(Array, @ps.output_list('table')) |
| 62 | + assert_instance_of(Array, @ps.output_list('pretty-table')) |
| 63 | + assert_instance_of(String, @ps.output_list('table')[0]) |
| 64 | + assert_instance_of(String, @ps.output_list('pretty-table')[0]) |
| 65 | + end |
| 66 | + |
| 67 | + def test_DB_output_search |
| 68 | + @ps.search('cisco', :all, sensitive: true) |
| 69 | + assert_instance_of(Array, @ps.output_search('table')) |
| 70 | + assert_instance_of(Array, @ps.output_search('pretty-table')) |
| 71 | + assert_instance_of(String, @ps.output_search('table')[0]) |
| 72 | + assert_instance_of(String, @ps.output_search('pretty-table')[0]) |
| 73 | + end |
| 74 | + |
| 75 | + def test_DB_highlight_found |
| 76 | + # String |
| 77 | + @ps.search('Apache', :all, sensitive: true) |
| 78 | + output = @ps.output_search('table') |
| 79 | + assert_instance_of(Array, @ps.highlight_found('Apache', output, sensitive: true)) |
| 80 | + assert_instance_of(String, @ps.highlight_found('Apache', output, sensitive: true)[0]) |
| 81 | + # Regexp |
| 82 | + @ps.search('password[0-9]+', :password) |
| 83 | + output = @ps.output_search('table') |
| 84 | + assert_instance_of(Array, @ps.highlight_found('password[0-9]+', output, sensitive: false)) |
| 85 | + assert_instance_of(String, @ps.highlight_found('password[0-9]+', output, sensitive: false)[0]) |
| 86 | + end |
| 87 | + |
| 88 | + def test_Output_Table_format |
| 89 | + assert_instance_of(Array, PassStation::Output::Table.format(@ps.data)) |
| 90 | + assert_instance_of(String, PassStation::Output::Table.format(@ps.data)[0]) |
| 91 | + end |
| 92 | + |
| 93 | + def test_Output_PrettyTable_format |
| 94 | + assert_instance_of(Array, PassStation::Output::PrettyTable.format(@ps.data)) |
| 95 | + assert_instance_of(String, PassStation::Output::PrettyTable.format(@ps.data)[0]) |
| 96 | + end |
| 97 | +end |
0 commit comments