| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from keystoneauth1 import exceptions as ksa_exceptions |
| 14 |
import testresources |
| 15 |
from testtools import matchers |
| 16 |
|
| 17 |
|
| 18 |
from keystoneclient import exceptions as ksc_exceptions |
| 19 |
from keystoneclient.tests.unit import client_fixtures |
| 20 |
from keystoneclient.tests.unit import utils as test_utils |
| 21 |
from keystoneclient import utils |
| 22 |
|
| 23 |
|
| 24 |
class FakeResource(object): |
| 25 |
pass |
| 26 |
|
| 27 |
|
| 28 |
class FakeManager(object): |
| 29 |
|
| 30 |
resource_class = FakeResource |
| 31 |
|
| 32 |
resources = { |
| 33 |
'1234': {'name': 'entity_one'}, |
| 34 |
'8e8ec658-c7b0-4243-bdf8-6f7f2952c0d0': {'name': 'entity_two'}, |
| 35 |
'\xe3\x82\xbdtest': {'name': '\u30bdtest'}, |
| 36 |
'5678': {'name': '9876'} |
| 37 |
} |
| 38 |
|
| 39 |
def get(self, resource_id): |
| 40 |
try: |
| 41 |
return self.resources[str(resource_id)] |
| 42 |
except KeyError: |
| 43 |
raise ksa_exceptions.NotFound(resource_id) |
| 44 |
|
| 45 |
def find(self, name=None): |
| 46 |
if name == '9999': |
| 47 |
|
| 48 |
raise ksc_exceptions.NoUniqueMatch() |
| 49 |
for resource_id, resource in self.resources.items(): |
| 50 |
if resource['name'] == str(name): |
| 51 |
return resource |
| 52 |
raise ksa_exceptions.NotFound(name) |
| 53 |
|
| 54 |
|
| 55 |
class FindResourceTestCase(test_utils.TestCase): |
| 56 |
|
| 57 |
def setUp(self): |
| 58 |
super(FindResourceTestCase, self).setUp() |
| 59 |
self.manager = FakeManager() |
| 60 |
|
| 61 |
def test_find_none(self): |
| 62 |
self.assertRaises(ksc_exceptions.CommandError, |
| 63 |
utils.find_resource, |
| 64 |
self.manager, |
| 65 |
'asdf') |
| 66 |
|
| 67 |
def test_find_by_integer_id(self): |
| 68 |
output = utils.find_resource(self.manager, 1234) |
| 69 |
self.assertEqual(output, self.manager.resources['1234']) |
| 70 |
|
| 71 |
def test_find_by_str_id(self): |
| 72 |
output = utils.find_resource(self.manager, '1234') |
| 73 |
self.assertEqual(output, self.manager.resources['1234']) |
| 74 |
|
| 75 |
def test_find_by_uuid(self): |
| 76 |
uuid = '8e8ec658-c7b0-4243-bdf8-6f7f2952c0d0' |
| 77 |
output = utils.find_resource(self.manager, uuid) |
| 78 |
self.assertEqual(output, self.manager.resources[uuid]) |
| 79 |
|
| 80 |
def test_find_by_unicode(self): |
| 81 |
name = '\xe3\x82\xbdtest' |
| 82 |
output = utils.find_resource(self.manager, name) |
| 83 |
self.assertEqual(output, self.manager.resources[name]) |
| 84 |
|
| 85 |
def test_find_by_str_name(self): |
| 86 |
output = utils.find_resource(self.manager, 'entity_one') |
| 87 |
self.assertEqual(output, self.manager.resources['1234']) |
| 88 |
|
| 89 |
def test_find_by_int_name(self): |
| 90 |
output = utils.find_resource(self.manager, 9876) |
| 91 |
self.assertEqual(output, self.manager.resources['5678']) |
| 92 |
|
| 93 |
def test_find_no_unique_match(self): |
| 94 |
self.assertRaises(ksc_exceptions.CommandError, |
| 95 |
utils.find_resource, |
| 96 |
self.manager, |
| 97 |
9999) |
| 98 |
|
| 99 |
|
| 100 |
class FakeObject(object): |
| 101 |
def __init__(self, name): |
| 102 |
self.name = name |
| 103 |
|
| 104 |
|
| 105 |
class HashSignedTokenTestCase(test_utils.TestCase, |
| 106 |
testresources.ResourcedTestCase): |
| 107 |
"""Unit tests for utils.hash_signed_token().""" |
| 108 |
|
| 109 |
resources = [('examples', client_fixtures.EXAMPLES_RESOURCE)] |
| 110 |
|
| 111 |
def test_default_md5(self): |
| 112 |
"""The default hash method is md5.""" |
| 113 |
token = self.examples.SIGNED_TOKEN_SCOPED |
| 114 |
token = token.encode('utf-8') |
| 115 |
token_id_default = utils.hash_signed_token(token) |
| 116 |
token_id_md5 = utils.hash_signed_token(token, mode='md5') |
| 117 |
self.assertThat(token_id_default, matchers.Equals(token_id_md5)) |
| 118 |
|
| 119 |
self.assertThat(token_id_default, matchers.HasLength(32)) |
| 120 |
|
| 121 |
def test_sha256(self): |
| 122 |
"""Can also hash with sha256.""" |
| 123 |
token = self.examples.SIGNED_TOKEN_SCOPED |
| 124 |
token = token.encode('utf-8') |
| 125 |
token_id = utils.hash_signed_token(token, mode='sha256') |
| 126 |
|
| 127 |
self.assertThat(token_id, matchers.HasLength(64)) |
| 128 |
|
| 129 |
|
| 130 |
def load_tests(loader, tests, pattern): |
| 131 |
return testresources.OptimisingTestSuite(tests) |