14
14
__metaclass__ = type
19
from maastesting.factory import factory
17
20
from maastesting.testcase import MAASTestCase
18
21
from provisioningserver.rpc import arguments
22
from testtools import ExpectedException
19
23
from testtools.matchers import (
29
class TestBytes(MAASTestCase):
31
def test_round_trip(self):
32
argument = arguments.Bytes()
33
example = factory.getRandomBytes()
34
encoded = argument.toString(example)
35
self.assertThat(encoded, IsInstance(bytes))
36
decoded = argument.fromString(encoded)
37
self.assertThat(decoded, Equals(example))
39
def test_error_when_input_is_not_a_byte_string(self):
40
with ExpectedException(TypeError, "^Not a byte string: <.*"):
41
arguments.Bytes().toString(object())
44
class TestChoice(MAASTestCase):
46
def test_round_trip(self):
48
factory.make_name("name"): factory.getRandomBytes()
51
argument = arguments.Choice(choices)
52
choice = random.choice(list(choices))
53
encoded = argument.toString(choice)
54
self.assertThat(encoded, IsInstance(bytes))
55
decoded = argument.fromString(encoded)
56
self.assertThat(decoded, Equals(choice))
58
def test_error_when_input_is_not_in_choices(self):
59
with ExpectedException(KeyError, "^<object .*"):
60
arguments.Choice({}).toString(object())
62
def test_error_when_choices_is_not_mapping(self):
63
with ExpectedException(TypeError, "^Not a mapping: \[\]"):
66
def test_error_when_choices_values_are_not_byte_strings(self):
67
with ExpectedException(TypeError, "^Not byte strings: 12345, u'foo'"):
68
arguments.Choice({object(): 12345, object(): u'foo'})
25
71
class TestStructureAsJSON(MAASTestCase):
36
82
self.assertThat(encoded, IsInstance(bytes))
37
83
decoded = argument.fromString(encoded)
38
84
self.assertThat(decoded, Equals(self.example))
87
class TestParsedURL(MAASTestCase):
89
def test_round_trip(self):
90
argument = arguments.ParsedURL()
91
example = factory.make_parsed_url()
92
encoded = argument.toString(example)
93
self.assertThat(encoded, IsInstance(bytes))
94
decoded = argument.fromString(encoded)
95
self.assertThat(decoded.geturl(), Equals(example.geturl()))
97
def test_error_when_input_is_not_a_url_object(self):
98
with ExpectedException(TypeError, "^Not a URL-like object: <.*"):
99
arguments.ParsedURL().toString(object())
101
def test_netloc_containing_non_ascii_characters_is_encoded_to_idna(self):
102
argument = arguments.ParsedURL()
103
example = factory.make_parsed_url()._replace(
104
netloc=u'\u24b8\u211d\U0001d538\u24b5\U0001d502')
105
encoded = argument.toString(example)
106
self.assertThat(encoded, IsInstance(bytes))
107
decoded = argument.fromString(encoded)
108
# The non-ASCII netloc was encoded using IDNA.
109
expected = example._replace(netloc="cra(z)y")
110
self.assertThat(decoded.geturl(), Equals(expected.geturl()))