Coverage for tests/admin/test_authorize_signer.py: 100%

61 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-07-10 13:43 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2021 RSK Labs Ltd 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy of 

6# this software and associated documentation files (the "Software"), to deal in 

7# the Software without restriction, including without limitation the rights to 

8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 

9# of the Software, and to permit persons to whom the Software is furnished to do 

10# so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in all 

13# copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23from types import SimpleNamespace 

24from unittest import TestCase 

25from unittest.mock import patch, call 

26from admin.authorize_signer import do_authorize_signer 

27from admin.misc import AdminError 

28 

29import logging 

30 

31logging.disable(logging.CRITICAL) 

32 

33 

34@patch("admin.authorize_signer.SignerAuthorization") 

35@patch("admin.authorize_signer.do_unlock") 

36@patch("admin.authorize_signer.get_hsm") 

37@patch("admin.authorize_signer.dispose_hsm") 

38class TestAuthorizeSigner(TestCase): 

39 def test_ok(self, dispose_hsm_mock, get_hsm_mock, do_unlock_mock, sa_mock): 

40 with patch("sys.stdout"): 

41 options = SimpleNamespace( 

42 signer_authorization_file_path="/a/file/path", 

43 another="option", 

44 andfinally="anotherone", 

45 verbose="is-verbose", 

46 ) 

47 do_authorize_signer(options) 

48 

49 self.assertEqual([call("/a/file/path")], 

50 sa_mock.from_jsonfile.call_args_list) 

51 self.assertEqual([call(options, label=False, exit=False)], 

52 do_unlock_mock.call_args_list) 

53 self.assertEqual([call("is-verbose")], get_hsm_mock.call_args_list) 

54 self.assertEqual([call(sa_mock.from_jsonfile.return_value)], 

55 get_hsm_mock.return_value.authorize_signer.call_args_list) 

56 self.assertEqual([call(get_hsm_mock.return_value)], 

57 dispose_hsm_mock.call_args_list) 

58 

59 def test_jsonfile_error(self, dispose_hsm_mock, get_hsm_mock, do_unlock_mock, 

60 sa_mock): 

61 sa_mock.from_jsonfile.side_effect = Exception("loading-jsonfile") 

62 

63 with patch("sys.stdout"): 

64 options = SimpleNamespace( 

65 signer_authorization_file_path="/a/file/path", 

66 another="option", 

67 andfinally="anotherone", 

68 verbose="is-verbose", 

69 ) 

70 with self.assertRaises(AdminError): 

71 do_authorize_signer(options) 

72 

73 self.assertEqual([call("/a/file/path")], 

74 sa_mock.from_jsonfile.call_args_list) 

75 self.assertEqual([], do_unlock_mock.call_args_list) 

76 self.assertEqual([], get_hsm_mock.call_args_list) 

77 self.assertEqual([], dispose_hsm_mock.call_args_list) 

78 

79 def test_unlock_error(self, dispose_hsm_mock, get_hsm_mock, do_unlock_mock, sa_mock): 

80 do_unlock_mock.side_effect = Exception("unlocking") 

81 

82 with patch("sys.stdout"): 

83 options = SimpleNamespace( 

84 signer_authorization_file_path="/a/file/path", 

85 another="option", 

86 andfinally="anotherone", 

87 verbose="is-verbose", 

88 ) 

89 with self.assertRaises(AdminError): 

90 do_authorize_signer(options) 

91 

92 self.assertEqual([call("/a/file/path")], 

93 sa_mock.from_jsonfile.call_args_list) 

94 self.assertEqual([call(options, label=False, exit=False)], 

95 do_unlock_mock.call_args_list) 

96 self.assertEqual([], get_hsm_mock.call_args_list) 

97 self.assertEqual([], dispose_hsm_mock.call_args_list) 

98 

99 def test_get_hsm_error(self, dispose_hsm_mock, get_hsm_mock, do_unlock_mock, sa_mock): 

100 get_hsm_mock.side_effect = Exception("connecting-to-hsm") 

101 

102 with patch("sys.stdout"): 

103 options = SimpleNamespace( 

104 signer_authorization_file_path="/a/file/path", 

105 another="option", 

106 andfinally="anotherone", 

107 verbose="is-verbose", 

108 ) 

109 with self.assertRaises(AdminError): 

110 do_authorize_signer(options) 

111 

112 self.assertEqual([call("/a/file/path")], 

113 sa_mock.from_jsonfile.call_args_list) 

114 self.assertEqual([call(options, label=False, exit=False)], 

115 do_unlock_mock.call_args_list) 

116 self.assertEqual([call("is-verbose")], get_hsm_mock.call_args_list) 

117 self.assertEqual([call(None)], dispose_hsm_mock.call_args_list) 

118 

119 def test_auth_error(self, dispose_hsm_mock, get_hsm_mock, do_unlock_mock, sa_mock): 

120 get_hsm_mock.return_value.authorize_signer.side_effect = \ 

121 Exception("authorising-signer") 

122 

123 with patch("sys.stdout"): 

124 options = SimpleNamespace( 

125 signer_authorization_file_path="/a/file/path", 

126 another="option", 

127 andfinally="anotherone", 

128 verbose="is-verbose", 

129 ) 

130 with self.assertRaises(AdminError): 

131 do_authorize_signer(options) 

132 

133 self.assertEqual([call("/a/file/path")], 

134 sa_mock.from_jsonfile.call_args_list) 

135 self.assertEqual([call(options, label=False, exit=False)], 

136 do_unlock_mock.call_args_list) 

137 self.assertEqual([call("is-verbose")], get_hsm_mock.call_args_list) 

138 self.assertEqual([call(get_hsm_mock.return_value)], 

139 dispose_hsm_mock.call_args_list)