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

31 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 unittest import TestCase 

24from unittest.mock import Mock, patch 

25from admin.misc import get_ud_value_for_attestation, RskClientError, AdminError 

26 

27import logging 

28 

29logging.disable(logging.CRITICAL) 

30 

31 

32class TestGetUdValueForAttestation(TestCase): 

33 def test_hex_string(self): 

34 self.assertEqual("aa"*32, get_ud_value_for_attestation("aa"*32)) 

35 self.assertEqual("aa"*32, get_ud_value_for_attestation("0x"+"aa"*32)) 

36 

37 @patch("admin.misc.RskClient") 

38 def test_ud_source_ok(self, RskClient): 

39 rsk_client = Mock() 

40 RskClient.return_value = rsk_client 

41 rsk_client.get_best_block_number.return_value = "the-best-block-number" 

42 rsk_client.get_block_by_number.return_value = {"hash": "0x" + "bb"*32} 

43 

44 self.assertEqual("bb"*32, get_ud_value_for_attestation("a-ud-source")) 

45 

46 RskClient.assert_called_with("a-ud-source") 

47 rsk_client.get_best_block_number.assert_called() 

48 rsk_client.get_block_by_number.assert_called_with("the-best-block-number") 

49 

50 @patch("admin.misc.RskClient") 

51 def test_ud_source_client_error(self, RskClient): 

52 rsk_client = Mock() 

53 RskClient.return_value = rsk_client 

54 rsk_client.get_best_block_number.return_value = "the-best-block-number" 

55 rsk_client.get_block_by_number.side_effect = RskClientError("an-error") 

56 

57 with self.assertRaises(AdminError) as e: 

58 get_ud_value_for_attestation("a-ud-source") 

59 self.assertIn("an-error", str(e.exception)) 

60 

61 RskClient.assert_called_with("a-ud-source") 

62 rsk_client.get_best_block_number.assert_called() 

63 rsk_client.get_block_by_number.assert_called_with("the-best-block-number")