Skip to content

Commit

Permalink
Do not allow tag and badge to have same value (#106)
Browse files Browse the repository at this point in the history
* Do not allow tag and badge to have same value

* Update

* Added unit test
  • Loading branch information
jinhyukchang authored Jan 31, 2020
1 parent 83a165a commit 6c0dc13
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
12 changes: 11 additions & 1 deletion metadata_service/api/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ def put(self, table_uri: str, tag: str) -> Iterable[Union[Mapping, int, None]]:
args = self.parser.parse_args()
# use tag_type to distinguish between tag and badge
tag_type = args.get('tag_type', 'default')

whitelist_badges = app.config.get('WHITELIST_BADGES', [])
if tag_type == 'badge':
# need to check whether the badge is part of the whitelist:
whitelist_badges = app.config.get('WHITELIST_BADGES', [])
if tag not in whitelist_badges:
return \
{'message': 'The tag {} for table_uri {} with type {} '
Expand All @@ -211,6 +212,15 @@ def put(self, table_uri: str, tag: str) -> Iterable[Union[Mapping, int, None]]:
table_uri,
tag_type)}, \
HTTPStatus.NOT_FOUND
else:
if tag in whitelist_badges:
return \
{'message': 'The tag {} for table_uri {} with type {} '
'is not added successfully as tag '
'for it is reserved for badge'.format(tag,
table_uri,
tag_type)}, \
HTTPStatus.CONFLICT

try:
self.client.add_tag(table_uri=table_uri,
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/api/table/test_table_badge_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import unittest
from http import HTTPStatus

from mock import patch, Mock

from tests.unit.test_basics import BasicTestCase

TABLE_NAME = 'magic'
BADGE_NAME = 'foo'
TAG_NAME = 'bar'


class TableTagAPI(BasicTestCase):
def setUp(self) -> None:
super().setUp()

self.mock_client = patch('metadata_service.api.table.get_proxy_client')
self.mock_proxy = self.mock_client.start().return_value = Mock()

def tearDown(self):
super().tearDown()

self.mock_client.stop()

def test_block_tag_on_reserved_badge_value(self) -> None:
self.app.config['WHITELIST_BADGES'] = [BADGE_NAME]
response = self.app.test_client().put(f'/table/{TABLE_NAME}/tag/{BADGE_NAME}')

self.assertEqual(response.status_code, HTTPStatus.CONFLICT)

def test_tag_on_unreserved_badge_value(self) -> None:
self.app.config['WHITELIST_BADGES'] = [BADGE_NAME]
response = self.app.test_client().put(f'/table/{TABLE_NAME}/tag/{TAG_NAME}')

self.assertEqual(response.status_code, HTTPStatus.OK)

def test_badge_on_reserved_badge_value(self) -> None:
self.app.config['WHITELIST_BADGES'] = [BADGE_NAME]
response = self.app.test_client().put(f'/table/{TABLE_NAME}/tag/{BADGE_NAME}?tag_type=badge')

self.assertEqual(response.status_code, HTTPStatus.OK)

def test_badge_on_unreserved_badge_value(self) -> None:
self.app.config['WHITELIST_BADGES'] = [BADGE_NAME]
response = self.app.test_client().put(f'/table/{TABLE_NAME}/tag/{TAG_NAME}?tag_type=badge')

self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)


if __name__ == '__main__':
unittest.main()

0 comments on commit 6c0dc13

Please sign in to comment.