Browse Source

Better way to manage status

new-scoring
aeris 2 years ago
parent
commit
13fa9541d6

+ 1
- 0
lib/cryptcheck.rb View File

@@ -32,6 +32,7 @@ module CryptCheck
32 32
 	end
33 33
 
34 34
 	autoload :Status, 'cryptcheck/status'
35
+	autoload :Statused, 'cryptcheck/statused'
35 36
 	autoload :Logger, 'cryptcheck/logger'
36 37
 	autoload :Tls, 'cryptcheck/tls'
37 38
 	module Tls

+ 4
- 0
lib/cryptcheck/status.rb View File

@@ -9,6 +9,10 @@ module CryptCheck
9 9
 			LEVELS.each &block
10 10
 		end
11 11
 
12
+		def self.empty
13
+			self.collect { |s| [s, []] }.to_h
14
+		end
15
+
12 16
 		def self.status(statuses)
13 17
 			statuses = self.convert statuses
14 18
 			self.min LEVELS, statuses

+ 48
- 0
lib/cryptcheck/statused.rb View File

@@ -0,0 +1,48 @@
1
+module CryptCheck
2
+	module Statused
3
+		def status
4
+			@status ||= calculate_status
5
+		end
6
+
7
+		private
8
+		def merge(statuses)
9
+			Status.collect do |s|
10
+				status = statuses.collect { |ss| ss[s] }
11
+				status = status.inject &:+
12
+				[s, status.uniq]
13
+			end.to_h
14
+		end
15
+
16
+		def checks
17
+			[]
18
+		end
19
+
20
+		def children
21
+			[]
22
+		end
23
+
24
+		def perform_check(check)
25
+			name, check, level = check
26
+			result             = check.call self
27
+			return nil unless result
28
+			level ||= result
29
+			[level, name]
30
+		end
31
+
32
+		def personal_status
33
+			states = Status.empty
34
+			checks.each do |check|
35
+				level, name = perform_check check
36
+				next unless level
37
+				states[level] << name
38
+			end
39
+			states
40
+		end
41
+
42
+		def calculate_status
43
+			children_statuses = children.collect(&:status)
44
+			statuses = [personal_status] + children_statuses
45
+			merge statuses
46
+		end
47
+	end
48
+end

+ 1
- 1
lib/cryptcheck/tls/grade.rb View File

@@ -124,7 +124,7 @@ module CryptCheck
124 124
 			end
125 125
 
126 126
 			def calculate_states
127
-				states = Status.collect { |s| [s, []] }.to_h
127
+				states = Status.empty
128 128
 				@checks.each do |name, check, status|
129 129
 					result = check.call @server
130 130
 					if result

+ 82
- 0
spec/cryptcheck/statused_spec.rb View File

@@ -0,0 +1,82 @@
1
+describe CryptCheck::Statused do
2
+	def match_status(actual, **expected)
3
+		expected = ::CryptCheck::Status.empty.merge expected
4
+		expect(actual.status).to eq expected
5
+	end
6
+
7
+	describe '::status' do
8
+		it 'must return empty if no check nor child' do
9
+			statused = Class.new do
10
+				include ::CryptCheck::Statused
11
+			end.new
12
+			match_status statused
13
+		end
14
+
15
+		it 'must return personal status if no child' do
16
+			statused = Class.new do
17
+				include ::CryptCheck::Statused
18
+
19
+				def checks
20
+					[
21
+							[:foo, -> (_) { true }, :critical],
22
+							[:bar, -> (_) { :error }],
23
+							[:baz, -> (_) { false }]
24
+					]
25
+				end
26
+			end.new
27
+			match_status statused, critical: %i(foo), error: %i(bar)
28
+		end
29
+
30
+		it 'must return personal and children statuses' do
31
+			child  = Class.new do
32
+				include ::CryptCheck::Statused
33
+
34
+				def checks
35
+					[[:bar, -> (_) { :error }]]
36
+				end
37
+			end.new
38
+			parent = Class.new do
39
+				include ::CryptCheck::Statused
40
+
41
+				def initialize(child)
42
+					@child = child
43
+				end
44
+
45
+				def checks
46
+					[[:foo, -> (_) { :critical }]]
47
+				end
48
+
49
+				def children
50
+					[@child]
51
+				end
52
+			end.new(child)
53
+			match_status parent, critical: %i(foo), error: %i(bar)
54
+		end
55
+
56
+		it 'must return remove duplicated status' do
57
+			child  = Class.new do
58
+				include ::CryptCheck::Statused
59
+
60
+				def checks
61
+					[[:foo, -> (_) { :critical }]]
62
+				end
63
+			end.new
64
+			parent = Class.new do
65
+				include ::CryptCheck::Statused
66
+
67
+				def initialize(child)
68
+					@child = child
69
+				end
70
+
71
+				def checks
72
+					[[:foo, -> (_) { :critical }]]
73
+				end
74
+
75
+				def children
76
+					[@child]
77
+				end
78
+			end.new(child)
79
+			match_status parent, critical: %i(foo)
80
+		end
81
+	end
82
+end

Loading…
Cancel
Save