Tenants API Tenant Model - Role Tests Parity
1. Refactored the tenants-api tenant responses model. 2. Fixed serialization/deserialization issues for role tests parity. 3. Added and tested _obj_to_json methods in tenant response model. Change-Id: I9642f0bef9844b30225cd3ace7842ab7d5e78f4a
This commit is contained in:
@@ -35,7 +35,7 @@ class Tenants(BaseIdentityListModel):
|
||||
|
||||
@classmethod
|
||||
def _list_to_obj(cls, list_):
|
||||
ret = {'tenants': [Tenant(**tenant) for tenant in list_]}
|
||||
ret = {'tenants': [Tenant._dict_to_obj(tenant) for tenant in list_]}
|
||||
return Tenants(**ret)
|
||||
|
||||
@classmethod
|
||||
@@ -55,19 +55,29 @@ class Tenants(BaseIdentityListModel):
|
||||
|
||||
class Tenant(BaseIdentityModel):
|
||||
def __init__(self, id_=None, name=None, description=None,
|
||||
enabled=None, created=None):
|
||||
enabled=None):
|
||||
"""
|
||||
An object that represents a tenant response object.
|
||||
"""
|
||||
super(Tenant, self).__init__()
|
||||
self.id_ = id_
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.enabled = enabled
|
||||
self.created = created
|
||||
|
||||
def _obj_to_json(self):
|
||||
json_dict = {"tenant": {"id": self.id_,
|
||||
"name": self.name,
|
||||
"description": self.description,
|
||||
"enabled": self.enabled}}
|
||||
return json.dumps(json_dict)
|
||||
|
||||
@classmethod
|
||||
def _dict_to_obj(cls, dic):
|
||||
return Tenant(**dic)
|
||||
return Tenant(id_=dic.get("id"),
|
||||
name=dic.get("name"),
|
||||
description=dic.get("description"),
|
||||
enabled=dic.get("enabled"))
|
||||
|
||||
@classmethod
|
||||
def _json_to_obj(cls, serialized_str):
|
||||
@@ -122,13 +132,22 @@ class TenantsLinks(BaseIdentityListModel):
|
||||
|
||||
class TenantsLink(BaseIdentityModel):
|
||||
def __init__(self, href=None, type_=None, rel=None):
|
||||
super(TenantsLink, self).__init__()
|
||||
self.href = href
|
||||
self.type = type_
|
||||
self.type_ = type_
|
||||
self.rel = rel
|
||||
|
||||
def _obj_to_json(self):
|
||||
json_dict = {"tenantsLink": {"href": self.href,
|
||||
"type": self.type_,
|
||||
"rel": self.rel}}
|
||||
return json.dumps(json_dict)
|
||||
|
||||
@classmethod
|
||||
def _dict_to_obj(cls, tenant_dict):
|
||||
return TenantsLink(**tenant_dict)
|
||||
return TenantsLink(href=tenant_dict.get("href"),
|
||||
type_=tenant_dict.get("type"),
|
||||
rel=tenant_dict.get("rel"))
|
||||
|
||||
@classmethod
|
||||
def _json_to_obj(cls, serialized_str):
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
from unittest import TestCase
|
||||
from cloudcafe.identity.v2_0.tenants_api.models.responses.tenant import \
|
||||
Tenant, TenantsLink, Tenants
|
||||
@@ -11,35 +12,48 @@ class TenantTest(TestCase):
|
||||
self.tenant_enabled = False
|
||||
|
||||
self.tenant_dict = {
|
||||
"id_": self.tenant_id,
|
||||
"id": self.tenant_id,
|
||||
"name": self.tenant_name,
|
||||
"description": self.tenant_description,
|
||||
"enabled": self.tenant_enabled}
|
||||
|
||||
self.href = "HREF"
|
||||
self.type = "TYPE"
|
||||
self.rel = "REL"
|
||||
self.tenants_link_dict = {"href": self.href,
|
||||
"type": self.type,
|
||||
"rel": self.rel}
|
||||
|
||||
self.expected_tenant = Tenant(id_=self.tenant_id,
|
||||
name=self.tenant_name,
|
||||
description=self.tenant_description,
|
||||
enabled=self.tenant_enabled)
|
||||
|
||||
self.tenant_serialized_str = '{"tenant": ' \
|
||||
'{"id_": "TENANT_ID", ' \
|
||||
'{"id": "TENANT_ID", ' \
|
||||
'"enabled": false, ' \
|
||||
'"name": "TENANT_NAME", ' \
|
||||
'"description": "TENANT_DESCRIPTION"}}'
|
||||
|
||||
self.expected_tenant_link = TenantsLink(href="HREF", type_="TYPE",
|
||||
rel="REL")
|
||||
self.expected_tenant_link = TenantsLink(href=self.href,
|
||||
type_=self.type,
|
||||
rel=self.rel)
|
||||
self.tenant_list_dict = [self.tenant_dict]
|
||||
self.expected_tenants = Tenants(tenants=[self.expected_tenant])
|
||||
|
||||
self.tenants_serialized_str = '{"tenants": ' \
|
||||
'[{"id_": "TENANT_ID", ' \
|
||||
'[{"id": "TENANT_ID", ' \
|
||||
'"enabled": false, ' \
|
||||
'"name": "TENANT_NAME", ' \
|
||||
'"description": "TENANT_DESCRIPTION"}]}'
|
||||
self.expected_tenant_json = json.dumps({"tenant": self.tenant_dict})
|
||||
self.expected_tenants_link_json = \
|
||||
json.dumps({"tenantsLink": self.tenants_link_dict})
|
||||
|
||||
def test_dict_to_obj(self):
|
||||
assert self.expected_tenant == Tenant._dict_to_obj(self.tenant_dict)
|
||||
assert self.expected_tenant_link == TenantsLink._dict_to_obj(
|
||||
self.tenants_link_dict)
|
||||
|
||||
def test_json_to_obj(self):
|
||||
assert self.expected_tenant == Tenant._json_to_obj(
|
||||
@@ -49,4 +63,9 @@ class TenantTest(TestCase):
|
||||
|
||||
def test_list_to_obj(self):
|
||||
assert self.expected_tenants == Tenants._list_to_obj(
|
||||
self.tenant_list_dict)
|
||||
self.tenant_list_dict)
|
||||
|
||||
def test_ojb_to_json(self):
|
||||
assert self.expected_tenant_json == self.expected_tenant._obj_to_json()
|
||||
assert self.expected_tenants_link_json == \
|
||||
self.expected_tenant_link._obj_to_json()
|
||||
|
||||
Reference in New Issue
Block a user