Node 'user' property now defaults to the current user ID.

This commit is contained in:
Sybren A. Stüvel 2016-07-06 15:19:28 +02:00
parent 447473303a
commit d95004e62e
3 changed files with 53 additions and 2 deletions

View File

@ -1,7 +1,7 @@
import logging import logging
from bson import ObjectId from bson import ObjectId
from flask import current_app from flask import current_app, g
from werkzeug.exceptions import UnprocessableEntity from werkzeug.exceptions import UnprocessableEntity
from application.modules import file_storage from application.modules import file_storage
@ -116,6 +116,9 @@ def before_inserting_nodes(items):
if project: if project:
item['project'] = project['_id'] item['project'] = project['_id']
# Default the 'user' property to the current user.
item.setdefault('user', g.current_user['user_id'])
def after_inserting_nodes(items): def after_inserting_nodes(items):
for item in items: for item in items:

View File

@ -304,7 +304,6 @@ nodes_schema = {
}, },
'user': { 'user': {
'type': 'objectid', 'type': 'objectid',
'required': True,
'data_relation': { 'data_relation': {
'resource': 'users', 'resource': 'users',
'field': '_id', 'field': '_id',

View File

@ -183,3 +183,52 @@ class NodeContentTypeTest(AbstractPillarTest):
}, },
'name': 'Texture node'}, 'name': 'Texture node'},
file_id_image_bump) file_id_image_bump)
class NodeOwnerTest(AbstractPillarTest):
def setUp(self, **kwargs):
AbstractPillarTest.setUp(self, **kwargs)
self.user_id = self.create_user()
self.create_valid_auth_token(self.user_id, 'token')
self.project_id, _ = self.ensure_project_exists(
project_overrides={'permissions': {
'users': [
{'user': self.user_id,
'methods': ['GET', 'PUT', 'POST', 'DELETE']}
]
}}
)
def test_create_with_explicit_owner(self):
test_node = {
'project': self.project_id,
'node_type': 'asset',
'name': 'test with user',
'user': self.user_id,
'properties': {},
}
self._test_user(test_node)
def test_create_with_implicit_owner(self):
test_node = {
'project': self.project_id,
'node_type': 'asset',
'name': 'test with user',
'properties': {},
}
self._test_user(test_node)
def _test_user(self, test_node):
from application.utils import dumps
resp = self.client.post('/nodes', data=dumps(test_node),
headers={'Authorization': self.make_header('token'),
'Content-Type': 'application/json'})
self.assertEqual(201, resp.status_code, resp.data)
created = json.loads(resp.data)
resp = self.client.get('/nodes/%s' % created['_id'],
headers={'Authorization': self.make_header('token')})
self.assertEqual(200, resp.status_code, resp.data)
json_node = json.loads(resp.data)
self.assertEqual(str(self.user_id), json_node['user'])