diff --git a/pillar/application/modules/nodes.py b/pillar/application/modules/nodes.py index 0fcafad3..065fa307 100644 --- a/pillar/application/modules/nodes.py +++ b/pillar/application/modules/nodes.py @@ -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): diff --git a/tests/test_nodes.py b/tests/test_nodes.py index bfe6a683..e593c22e 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -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)