You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

status_spec.rb 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. require 'ostruct'
  2. describe CryptCheck::State do
  3. describe '::status' do
  4. it 'must handle empty list' do
  5. expect(CryptCheck::State.status []).to be_nil
  6. end
  7. it 'must answer correctly' do
  8. {
  9. [:critical, :critical] => :critical,
  10. [:critical, :error] => :critical,
  11. [:critical, :warning] => :critical,
  12. [:critical, nil] => :critical,
  13. [:critical, :good] => :critical,
  14. [:critical, :perfect] => :critical,
  15. [:critical, :best] => :critical,
  16. [:error, :critical] => :critical,
  17. [:error, :error] => :error,
  18. [:error, :warning] => :error,
  19. [:error, nil] => :error,
  20. [:error, :good] => :error,
  21. [:error, :perfect] => :error,
  22. [:error, :best] => :error,
  23. [:warning, :critical] => :critical,
  24. [:warning, :error] => :error,
  25. [:warning, :warning] => :warning,
  26. [:warning, nil] => :warning,
  27. [:warning, :good] => :warning,
  28. [:warning, :perfect] => :warning,
  29. [:warning, :best] => :warning,
  30. [:good, :critical] => :critical,
  31. [:good, :error] => :error,
  32. [:good, :warning] => :warning,
  33. [:good, nil] => :good,
  34. [:good, :good] => :good,
  35. [:good, :perfect] => :good,
  36. [:good, :best] => :good,
  37. [:perfect, :critical] => :critical,
  38. [:perfect, :error] => :error,
  39. [:perfect, :warning] => :warning,
  40. [:perfect, nil] => :perfect,
  41. [:perfect, :good] => :good,
  42. [:perfect, :perfect] => :perfect,
  43. [:perfect, :best] => :perfect,
  44. [:best, :critical] => :critical,
  45. [:best, :error] => :error,
  46. [:best, :warning] => :warning,
  47. [:best, nil] => :best,
  48. [:best, :good] => :good,
  49. [:best, :perfect] => :perfect,
  50. [:best, :best] => :best
  51. }.each do |levels, result|
  52. got = CryptCheck::State.status levels
  53. expect(got).to be(result), "#{levels} : expected #{result.inspect}, got #{got.inspect}"
  54. end
  55. end
  56. it 'must handle object list' do
  57. critical = OpenStruct.new status: :critical
  58. warning = OpenStruct.new status: :warning
  59. expect(CryptCheck::State.status [critical, warning]).to be :critical
  60. end
  61. end
  62. describe '::problem' do
  63. it 'must answer correctly' do
  64. {
  65. [:critical, :critical] => :critical,
  66. [:critical, :error] => :critical,
  67. [:critical, :warning] => :critical,
  68. [:critical, nil] => :critical,
  69. [:critical, :good] => :critical,
  70. [:critical, :perfect] => :critical,
  71. [:critical, :best] => :critical,
  72. [:error, :critical] => :critical,
  73. [:error, :error] => :error,
  74. [:error, :warning] => :error,
  75. [:error, nil] => :error,
  76. [:error, :good] => :error,
  77. [:error, :perfect] => :error,
  78. [:error, :best] => :error,
  79. [:warning, :critical] => :critical,
  80. [:warning, :error] => :error,
  81. [:warning, :warning] => :warning,
  82. [:warning, nil] => :warning,
  83. [:warning, :good] => :warning,
  84. [:warning, :perfect] => :warning,
  85. [:warning, :best] => :warning,
  86. [:good, :critical] => :critical,
  87. [:good, :error] => :error,
  88. [:good, :warning] => :warning,
  89. [:good, nil] => nil,
  90. [:good, :good] => nil,
  91. [:good, :perfect] => nil,
  92. [:good, :best] => nil,
  93. [:perfect, :critical] => :critical,
  94. [:perfect, :error] => :error,
  95. [:perfect, :warning] => :warning,
  96. [:perfect, nil] => nil,
  97. [:perfect, :good] => nil,
  98. [:perfect, :perfect] => nil,
  99. [:perfect, :best] => nil,
  100. [:best, :critical] => :critical,
  101. [:best, :error] => :error,
  102. [:best, :warning] => :warning,
  103. [:best, nil] => nil,
  104. [:best, :good] => nil,
  105. [:best, :perfect] => nil,
  106. [:best, :best] => nil
  107. }.each do |levels, result|
  108. got = CryptCheck::State.problem levels
  109. expect(got).to be(result), "#{levels} : expected #{result.inspect}, got #{got.inspect}"
  110. end
  111. end
  112. it 'must handle object list' do
  113. critical = OpenStruct.new status: :critical
  114. warning = OpenStruct.new status: :warning
  115. expect(CryptCheck::State.problem [critical, warning]).to be :critical
  116. end
  117. end
  118. describe '#states' do
  119. def match_states(actual, **expected)
  120. expected = ::CryptCheck::State.empty.merge expected
  121. expect(actual.states).to eq expected
  122. end
  123. let(:empty) do
  124. Class.new do
  125. include ::CryptCheck::State
  126. def checks
  127. []
  128. end
  129. end.new
  130. end
  131. let(:childless) do
  132. Class.new do
  133. include ::CryptCheck::State
  134. def checks
  135. [
  136. [:foo, -> (_) { true }, :critical],
  137. [:bar, -> (_) { :error }],
  138. [:baz, -> (_) { false }]
  139. ]
  140. end
  141. end.new
  142. end
  143. let(:parent) do
  144. child = Class.new do
  145. include ::CryptCheck::State
  146. def checks
  147. [[:bar, -> (_) { :error }]]
  148. end
  149. end.new
  150. Class.new do
  151. include ::CryptCheck::State
  152. def initialize(child)
  153. @child = child
  154. end
  155. def checks
  156. [[:foo, -> (_) { :critical }]]
  157. end
  158. def children
  159. [@child]
  160. end
  161. end.new(child)
  162. end
  163. let(:duplicated) do
  164. child = Class.new do
  165. include ::CryptCheck::State
  166. def checks
  167. [[:foo, -> (_) { :critical }]]
  168. end
  169. end.new
  170. Class.new do
  171. include ::CryptCheck::State
  172. def initialize(child)
  173. @child = child
  174. end
  175. def checks
  176. [[:foo, -> (_) { :critical }]]
  177. end
  178. def children
  179. [@child]
  180. end
  181. end.new(child)
  182. end
  183. it 'must return empty if no check nor child' do
  184. match_states empty
  185. end
  186. it 'must return personal status if no child' do
  187. match_states childless, critical: %i(foo), error: %i(bar)
  188. end
  189. it 'must return personal and children statuses' do
  190. match_states parent, critical: %i(foo), error: %i(bar)
  191. end
  192. it 'must return remove duplicated status' do
  193. match_states duplicated, critical: %i(foo)
  194. end
  195. end
  196. describe '#status' do
  197. it 'must return nil if nothing special' do
  198. empty = Class.new do
  199. include ::CryptCheck::State
  200. def checks
  201. []
  202. end
  203. end.new
  204. expect(empty.status).to be_nil
  205. end
  206. it 'must return the status if only one' do
  207. empty = Class.new do
  208. include ::CryptCheck::State
  209. def checks
  210. [[:foo, -> (_) { :critical }]]
  211. end
  212. end.new
  213. expect(empty.status).to be :critical
  214. end
  215. it 'must return the worst status if multiple' do
  216. empty = Class.new do
  217. include ::CryptCheck::State
  218. def checks
  219. [[:foo, -> (_) { :critical }],
  220. [:bar, -> (_) { :error }]]
  221. end
  222. end.new
  223. expect(empty.status).to be :critical
  224. end
  225. end
  226. end