Status helper

new-scoring
aeris 2017-01-02 17:21:18 +01:00
parent 52a19f8c35
commit d0160f1067
3 changed files with 150 additions and 0 deletions

View File

@ -31,6 +31,7 @@ module CryptCheck
end
end
autoload :Status, 'cryptcheck/status'
autoload :Logger, 'cryptcheck/logger'
autoload :Tls, 'cryptcheck/tls'
module Tls

View File

@ -0,0 +1,28 @@
module CryptCheck
module Status
LEVELS = %i(critical error warning good perfect best).freeze
PROBLEMS = %i(critical error warning).freeze
def self.status(statuses)
statuses = self.collect statuses
self.select LEVELS, statuses
end
def self.problem(statuses)
statuses = self.collect statuses
self.select PROBLEMS, statuses
end
private
def self.collect(statuses)
first = statuses.first
statuses = statuses.collect &:status if first.respond_to? :status
statuses
end
def self.select(levels, statuses)
return nil if statuses.empty?
(levels & statuses).first
end
end
end

View File

@ -0,0 +1,121 @@
require 'ostruct'
describe CryptCheck::Status do
describe '::status' do
it 'must handle empty list' do
expect(CryptCheck::Status.status []).to be_nil
end
it 'must answer correctly' do
{
[:critical, :critical] => :critical,
[:critical, :error] => :critical,
[:critical, :warning] => :critical,
[:critical, :good] => :critical,
[:critical, :perfect] => :critical,
[:critical, :best] => :critical,
[:error, :critical] => :critical,
[:error, :error] => :error,
[:error, :warning] => :error,
[:error, :good] => :error,
[:error, :perfect] => :error,
[:error, :best] => :error,
[:warning, :critical] => :critical,
[:warning, :error] => :error,
[:warning, :warning] => :warning,
[:warning, :good] => :warning,
[:warning, :perfect] => :warning,
[:warning, :best] => :warning,
[:good, :critical] => :critical,
[:good, :error] => :error,
[:good, :warning] => :warning,
[:good, :good] => :good,
[:good, :perfect] => :good,
[:good, :best] => :good,
[:perfect, :critical] => :critical,
[:perfect, :error] => :error,
[:perfect, :warning] => :warning,
[:perfect, :good] => :good,
[:perfect, :perfect] => :perfect,
[:perfect, :best] => :perfect,
[:best, :critical] => :critical,
[:best, :error] => :error,
[:best, :warning] => :warning,
[:best, :good] => :good,
[:best, :perfect] => :perfect,
[:best, :best] => :best
}.each do |levels, result|
got = CryptCheck::Status.status levels
expect(got).to be(result), "#{levels} : expected #{result.inspect}, got #{got.inspect}"
end
end
it 'must handle object list' do
critical = OpenStruct.new status: :critical
warning = OpenStruct.new status: :warning
expect(CryptCheck::Status.status [critical, warning]).to be :critical
end
end
describe '::problem' do
it 'must answer correctly' do
{
[:critical, :critical] => :critical,
[:critical, :error] => :critical,
[:critical, :warning] => :critical,
[:critical, :good] => :critical,
[:critical, :perfect] => :critical,
[:critical, :best] => :critical,
[:error, :critical] => :critical,
[:error, :error] => :error,
[:error, :warning] => :error,
[:error, :good] => :error,
[:error, :perfect] => :error,
[:error, :best] => :error,
[:warning, :critical] => :critical,
[:warning, :error] => :error,
[:warning, :warning] => :warning,
[:warning, :good] => :warning,
[:warning, :perfect] => :warning,
[:warning, :best] => :warning,
[:good, :critical] => :critical,
[:good, :error] => :error,
[:good, :warning] => :warning,
[:good, :good] => nil,
[:good, :perfect] => nil,
[:good, :best] => nil,
[:perfect, :critical] => :critical,
[:perfect, :error] => :error,
[:perfect, :warning] => :warning,
[:perfect, :good] => nil,
[:perfect, :perfect] => nil,
[:perfect, :best] => nil,
[:best, :critical] => :critical,
[:best, :error] => :error,
[:best, :warning] => :warning,
[:best, :good] => nil,
[:best, :perfect] => nil,
[:best, :best] => nil
}.each do |levels, result|
got = CryptCheck::Status.problem levels
expect(got).to be(result), "#{levels} : expected #{result.inspect}, got #{got.inspect}"
end
end
it 'must handle object list' do
critical = OpenStruct.new status: :critical
warning = OpenStruct.new status: :warning
expect(CryptCheck::Status.problem [critical, warning]).to be :critical
end
end
end