Nodes: embed 'short_link' when 'short_code' is present and not empty.

This prevents calls to /nodes/<node-id>/share to get the short link.
This commit is contained in:
Sybren A. Stüvel 2016-07-15 11:10:54 +02:00
parent 2d5a538ad6
commit 9b3e75b9b9
2 changed files with 26 additions and 0 deletions

View File

@ -321,6 +321,10 @@ def before_returning_node(node):
# Run validation process, since GET on nodes entry point is public
check_permissions('nodes', node, 'GET', append_allowed_methods=True)
# Embed short_link_info if the node has a short_code.
short_code = node.get('short_code')
if short_code:
node['short_link'] = short_link_info(short_code)['short_link']
def before_returning_nodes(nodes):

View File

@ -358,3 +358,25 @@ class NodeSharingTest(AbstractPillarTest):
self._check_share_data(share_data)
self.assertEqual(3, create_short_link.call_count)
def test_projections(self):
"""Projecting short_code should get us short_link as well."""
# Share the node
resp = self.post('/nodes/%s/share' % self.node_id, auth_token='token',
expected_status=201)
share_data = resp.json()
# Get the node with short_code
resp = self.get('/nodes/%s' % self.node_id,
json={'projection': {'short_code': 1}})
node = resp.json()
self.assertEqual(node['short_code'], share_data['short_code'])
self.assertTrue(node['short_link'].endswith(share_data['short_code']))
# Get the node without short_code
resp = self.get('/nodes/%s' % self.node_id,
qs={'projection': {'short_code': 0}})
node = resp.json()
self.assertNotIn('short_code', node)
self.assertNotIn('short_link', node)