Skip to content

Commit

Permalink
Enforce entity attribute check
Browse files Browse the repository at this point in the history
This change enforce type checking for entity attribute in object returned

See #56
  • Loading branch information
antechrestos committed Dec 28, 2019
1 parent 3defa42 commit ee74a94
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 12 deletions.
3 changes: 2 additions & 1 deletion main/cloudfoundry_client/v2/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, target_endpoint: str, client: 'CloudFoundryClient', *args, **
self.target_endpoint = target_endpoint
self.client = client
try:
if 'entity' not in self:
if not (isinstance(self.get('entity'), dict)):
raise InvalidEntity(**self)

for attribute, value in list(self['entity'].items()):
Expand Down Expand Up @@ -48,6 +48,7 @@ def __init__(self, target_endpoint: str, client: 'CloudFoundryClient', *args, **

PaginateEntities = Generator[Entity, None, None]


class EntityManager(object):
list_query_parameters = ['page', 'results-per-page', 'order-direction']

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"metadata": {
"guid": "any-id",
"url": "/fake/any-id",
"created_at": "2015-11-30T23:38:33Z",
"updated_at": "2015-11-30T23:38:33Z"
},
"entity": []
}
9 changes: 9 additions & 0 deletions test/fixtures/fake/GET_invalid_entity_with_null_entity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"metadata": {
"guid": "any-id",
"url": "/fake/any-id",
"created_at": "2015-11-30T23:38:33Z",
"updated_at": "2015-11-30T23:38:33Z"
},
"entity": null
}
8 changes: 8 additions & 0 deletions test/fixtures/fake/GET_invalid_entity_without_entity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"metadata": {
"guid": "any-id",
"url": "/fake/any-id",
"created_at": "2015-11-30T23:38:33Z",
"updated_at": "2015-11-30T23:38:33Z"
}
}
64 changes: 53 additions & 11 deletions test/v2/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,79 @@
from http import HTTPStatus
from unittest.mock import MagicMock, call

from cloudfoundry_client.errors import InvalidEntity
from cloudfoundry_client.v2.entities import EntityManager
from fake_requests import TARGET_ENDPOINT, mock_response


class TestEntities(unittest.TestCase):

def test_invalid_entity_without_entity_attribute(self):
client = MagicMock()
entity_manager = EntityManager(TARGET_ENDPOINT, client, '/fake/anyone')

client.get.return_value = mock_response(
'/fake/anyone/any-id',
HTTPStatus.OK,
None,
'fake', 'GET_invalid_entity_without_entity.json')

self.assertRaises(InvalidEntity, lambda: entity_manager['any-id'])

def test_invalid_entity_with_null_entity(self):
client = MagicMock()
entity_manager = EntityManager(TARGET_ENDPOINT, client, '/fake/anyone')

client.get.return_value = mock_response(
'/fake/anyone/any-id',
HTTPStatus.OK,
None,
'fake', 'GET_invalid_entity_with_null_entity.json')

self.assertRaises(InvalidEntity, lambda: entity_manager['any-id'])

def test_invalid_entity_with_invalid_entity_type(self):
client = MagicMock()
entity_manager = EntityManager(TARGET_ENDPOINT, client, '/fake/anyone')

client.get.return_value = mock_response(
'/fake/anyone/any-id',
HTTPStatus.OK,
None,
'fake', 'GET_invalid_entity_with_invalid_entity_type.json')

self.assertRaises(InvalidEntity, lambda: entity_manager['any-id'])

def test_query(self):
url = EntityManager('http://cf.api', None, '/v2/apps')._get_url_filtered('/v2/apps', **{'results-per-page': 20,
'order-direction': 'asc',
'page': 1,
'space_guid': 'id',
'order-by': 'id'})
'order-direction': 'asc',
'page': 1,
'space_guid': 'id',
'order-by': 'id'})
self.assertEqual('/v2/apps?order-by=id&order-direction=asc&page=1&results-per-page=20&q=space_guid%3Aid', url)

def test_query_multi_order_by(self):
url = EntityManager('http://cf.api', None, '/v2/apps')._get_url_filtered('/v2/apps', **{'order-by': ['timestamp', 'id']})
url = EntityManager('http://cf.api', None, '/v2/apps')._get_url_filtered('/v2/apps',
**{'order-by': ['timestamp', 'id']})
self.assertEqual('/v2/apps?order-by=timestamp&order-by=id', url)

def test_query_single_order_by(self):
url = EntityManager('http://cf.api', None, '/v2/apps')._get_url_filtered('/v2/apps', **{'order-by': 'timestamp'})
url = EntityManager('http://cf.api', None, '/v2/apps')._get_url_filtered('/v2/apps',
**{'order-by': 'timestamp'})
self.assertEqual('/v2/apps?order-by=timestamp', url)

def test_query_in(self):
url = EntityManager('http://cf.api', None, '/v2/apps')._get_url_filtered('/v2/apps', **{'results-per-page': 20,
'order-direction': 'asc',
'page': 1,
'space_guid': ['id1', 'id2']})
'order-direction': 'asc',
'page': 1,
'space_guid': ['id1',
'id2']})
self.assertEqual('/v2/apps?order-direction=asc&page=1&results-per-page=20&q=space_guid%20IN%20id1%2Cid2', url)

def test_multi_query(self):
url = EntityManager('http://cf.api', None, '/v2/events')._get_url_filtered('/v2/events', **{'type': ['create', 'update'],
'organization_guid': 'org-id'})
url = EntityManager('http://cf.api', None, '/v2/events')._get_url_filtered('/v2/events',
**{'type': ['create', 'update'],
'organization_guid': 'org-id'})
self.assertEqual('/v2/events?q=organization_guid%3Aorg-id&q=type%20IN%20create%2Cupdate', url)

def test_list(self):
Expand Down

0 comments on commit ee74a94

Please sign in to comment.