|
| 1 | +# coding=utf-8 |
| 2 | +from __future__ import absolute_import, division, print_function, \ |
| 3 | + unicode_literals |
| 4 | + |
| 5 | +from unittest import TestCase |
| 6 | + |
| 7 | +import filters as f |
| 8 | +from filters.test import BaseFilterTestCase |
| 9 | + |
| 10 | +from iota import Iota, TransactionHash, TryteString |
| 11 | +from iota.adapter import MockAdapter |
| 12 | +from iota.commands.core.check_consistency import CheckConsistencyCommand |
| 13 | +from iota.filters import Trytes |
| 14 | + |
| 15 | + |
| 16 | +class CheckConsistencyRequestFilterTestCase(BaseFilterTestCase): |
| 17 | + filter_type = CheckConsistencyCommand(MockAdapter()).get_request_filter |
| 18 | + skip_value_check = True |
| 19 | + |
| 20 | + # noinspection SpellCheckingInspection |
| 21 | + def setUp(self): |
| 22 | + super(CheckConsistencyRequestFilterTestCase, self).setUp() |
| 23 | + |
| 24 | + self.hash1 = ( |
| 25 | + 'TESTVALUE9DONTUSEINPRODUCTION99999DXSCAD' |
| 26 | + 'YBVDCTTBLHFYQATFZPYPCBG9FOUKIGMYIGLHM9NEZ' |
| 27 | + ) |
| 28 | + |
| 29 | + self.hash2 = ( |
| 30 | + 'TESTVALUE9DONTUSEINPRODUCTION99999EMFYSM' |
| 31 | + 'HWODIAPUTTFDLQRLYIDAUIPJXXEXZZSBVKZEBWGAN' |
| 32 | + ) |
| 33 | + |
| 34 | + def test_pass_happy_path(self): |
| 35 | + """ |
| 36 | + Request is valid. |
| 37 | + """ |
| 38 | + request = { |
| 39 | + # Raw trytes are extracted to match the IRI's JSON protocol. |
| 40 | + 'tails': [self.hash1, self.hash2], |
| 41 | + } |
| 42 | + |
| 43 | + filter_ = self._filter(request) |
| 44 | + |
| 45 | + self.assertFilterPasses(filter_) |
| 46 | + self.assertDictEqual(filter_.cleaned_data, request) |
| 47 | + |
| 48 | + def test_pass_compatible_types(self): |
| 49 | + """ |
| 50 | + Request contains values that can be converted to the expected |
| 51 | + types. |
| 52 | + """ |
| 53 | + filter_ = self._filter({ |
| 54 | + 'tails': [ |
| 55 | + # Any TrytesCompatible value can be used here. |
| 56 | + TransactionHash(self.hash1), |
| 57 | + bytearray(self.hash2.encode('ascii')), |
| 58 | + ], |
| 59 | + }) |
| 60 | + |
| 61 | + self.assertFilterPasses(filter_) |
| 62 | + self.assertDictEqual( |
| 63 | + filter_.cleaned_data, |
| 64 | + |
| 65 | + { |
| 66 | + # Raw trytes are extracted to match the IRI's JSON protocol. |
| 67 | + 'tails': [self.hash1, self.hash2], |
| 68 | + }, |
| 69 | + ) |
| 70 | + |
| 71 | + def test_fail_empty(self): |
| 72 | + """ |
| 73 | + Request is empty. |
| 74 | + """ |
| 75 | + self.assertFilterErrors( |
| 76 | + {}, |
| 77 | + |
| 78 | + { |
| 79 | + 'tails': [f.FilterMapper.CODE_MISSING_KEY], |
| 80 | + }, |
| 81 | + ) |
| 82 | + |
| 83 | + def test_fail_unexpected_parameters(self): |
| 84 | + """ |
| 85 | + Request contains unexpected parameters. |
| 86 | + """ |
| 87 | + self.assertFilterErrors( |
| 88 | + { |
| 89 | + 'tails': [TransactionHash(self.hash1)], |
| 90 | + 'foo': 'bar', |
| 91 | + }, |
| 92 | + |
| 93 | + { |
| 94 | + 'foo': [f.FilterMapper.CODE_EXTRA_KEY], |
| 95 | + }, |
| 96 | + ) |
| 97 | + |
| 98 | + def test_fail_tails_null(self): |
| 99 | + """ |
| 100 | + ``tails`` is null. |
| 101 | + """ |
| 102 | + self.assertFilterErrors( |
| 103 | + { |
| 104 | + 'tails': None, |
| 105 | + }, |
| 106 | + |
| 107 | + { |
| 108 | + 'tails': [f.Required.CODE_EMPTY], |
| 109 | + }, |
| 110 | + ) |
| 111 | + |
| 112 | + def test_fail_tails_wrong_type(self): |
| 113 | + """ |
| 114 | + ``tails`` is not an array. |
| 115 | + """ |
| 116 | + self.assertFilterErrors( |
| 117 | + { |
| 118 | + # It's gotta be an array, even if there's only one hash. |
| 119 | + 'tails': TransactionHash(self.hash1), |
| 120 | + }, |
| 121 | + |
| 122 | + { |
| 123 | + 'tails': [f.Type.CODE_WRONG_TYPE], |
| 124 | + }, |
| 125 | + ) |
| 126 | + |
| 127 | + def test_fail_tails_empty(self): |
| 128 | + """ |
| 129 | + ``tails`` is an array, but it is empty. |
| 130 | + """ |
| 131 | + self.assertFilterErrors( |
| 132 | + { |
| 133 | + 'tails': [], |
| 134 | + }, |
| 135 | + |
| 136 | + { |
| 137 | + 'tails': [f.Required.CODE_EMPTY], |
| 138 | + }, |
| 139 | + ) |
| 140 | + |
| 141 | + def test_fail_tails_contents_invalid(self): |
| 142 | + """ |
| 143 | + ``tails`` is a non-empty array, but it contains invalid values. |
| 144 | + """ |
| 145 | + self.assertFilterErrors( |
| 146 | + { |
| 147 | + 'tails': [ |
| 148 | + b'', |
| 149 | + True, |
| 150 | + None, |
| 151 | + b'not valid trytes', |
| 152 | + |
| 153 | + # This is actually valid; I just added it to make sure the |
| 154 | + # filter isn't cheating! |
| 155 | + TryteString(self.hash1), |
| 156 | + |
| 157 | + 2130706433, |
| 158 | + b'9' * 82, |
| 159 | + ], |
| 160 | + }, |
| 161 | + |
| 162 | + { |
| 163 | + 'tails.0': [f.Required.CODE_EMPTY], |
| 164 | + 'tails.1': [f.Type.CODE_WRONG_TYPE], |
| 165 | + 'tails.2': [f.Required.CODE_EMPTY], |
| 166 | + 'tails.3': [Trytes.CODE_NOT_TRYTES], |
| 167 | + 'tails.5': [f.Type.CODE_WRONG_TYPE], |
| 168 | + 'tails.6': [Trytes.CODE_WRONG_FORMAT], |
| 169 | + }, |
| 170 | + ) |
| 171 | + |
| 172 | + |
| 173 | +class CheckConsistencyCommandTestCase(TestCase): |
| 174 | + # noinspection SpellCheckingInspection |
| 175 | + def setUp(self): |
| 176 | + super(CheckConsistencyCommandTestCase, self).setUp() |
| 177 | + |
| 178 | + self.adapter = MockAdapter() |
| 179 | + self.command = CheckConsistencyCommand(self.adapter) |
| 180 | + |
| 181 | + # Define some tryte sequences that we can re-use across tests. |
| 182 | + self.milestone =\ |
| 183 | + TransactionHash( |
| 184 | + b'TESTVALUE9DONTUSEINPRODUCTION99999W9KDIH' |
| 185 | + b'BALAYAFCADIDU9HCXDKIXEYDNFRAKHN9IEIDZFWGJ' |
| 186 | + ) |
| 187 | + |
| 188 | + self.hash1 =\ |
| 189 | + TransactionHash( |
| 190 | + b'TESTVALUE9DONTUSEINPRODUCTION99999TBPDM9' |
| 191 | + b'ADFAWCKCSFUALFGETFIFG9UHIEFE9AYESEHDUBDDF' |
| 192 | + ) |
| 193 | + |
| 194 | + self.hash2 =\ |
| 195 | + TransactionHash( |
| 196 | + b'TESTVALUE9DONTUSEINPRODUCTION99999CIGCCF' |
| 197 | + b'KIUFZF9EP9YEYGQAIEXDTEAAUGAEWBBASHYCWBHDX' |
| 198 | + ) |
| 199 | + |
| 200 | + def test_wireup(self): |
| 201 | + """ |
| 202 | + Verify that the command is wired up correctly. |
| 203 | + """ |
| 204 | + self.assertIsInstance( |
| 205 | + Iota(self.adapter).checkConsistency, |
| 206 | + CheckConsistencyCommand, |
| 207 | + ) |
| 208 | + |
| 209 | + def test_happy_path(self): |
| 210 | + """ |
| 211 | + Successfully checking consistency. |
| 212 | + """ |
| 213 | + |
| 214 | + self.adapter.seed_response('checkConsistency', { |
| 215 | + 'state': True, |
| 216 | + }) |
| 217 | + |
| 218 | + response = self.command(tails=[self.hash1, self.hash2]) |
| 219 | + |
| 220 | + self.assertDictEqual( |
| 221 | + response, |
| 222 | + |
| 223 | + { |
| 224 | + 'state': True, |
| 225 | + } |
| 226 | + ) |
| 227 | + |
| 228 | + def test_info_with_false_state(self): |
| 229 | + """ |
| 230 | + `info` field exists when `state` is False. |
| 231 | + """ |
| 232 | + |
| 233 | + self.adapter.seed_response('checkConsistency', { |
| 234 | + 'state': False, |
| 235 | + 'info': 'Additional information', |
| 236 | + }) |
| 237 | + |
| 238 | + response = self.command(tails=[self.hash1, self.hash2]) |
| 239 | + |
| 240 | + self.assertDictEqual( |
| 241 | + response, |
| 242 | + |
| 243 | + { |
| 244 | + 'state': False, |
| 245 | + 'info': 'Additional information', |
| 246 | + } |
| 247 | + ) |
0 commit comments