Skip to content

Commit

Permalink
Merge pull request #1336 from tracim/develop
Browse files Browse the repository at this point in the history
Fix for html.document migration from tracim_v1 to tracim_v2
  • Loading branch information
Tracim committed Jan 18, 2019
2 parents d290519 + 641ae84 commit bb148ab
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 14 deletions.
15 changes: 12 additions & 3 deletions backend/tracim_backend/app_models/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,22 @@ def restricted_allowed_types_slug(self) -> typing.List[str]:
allowed_type_slug = [contents_type.slug for contents_type in self._content_types] # nopep8
return allowed_type_slug

def endpoint_allowed_types_slug(self) -> typing.List[str]:
def endpoint_allowed_types(self) -> typing.List[ContentType]:
"""
Same as restricted_allowed_types_slug but with special content_type
included like comments.
Same as restricted_allowed_types_slug but return
ContentType instead of str slug
and add special content_type included like comments.
"""
content_types = self._content_types
content_types.extend(self._special_contents_types)
return content_types

def endpoint_allowed_types_slug(self) -> typing.List[str]:
"""
same as endpoint_allowed_types but return str slug
instead of ContentType
"""
content_types = self.endpoint_allowed_types()
allowed_type_slug = [contents_type.slug for contents_type in content_types] # nopep8
return allowed_type_slug

Expand Down
33 changes: 24 additions & 9 deletions backend/tracim_backend/lib/core/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,20 @@ def move(self,
)
item.revision_type = ActionDescription.MOVE

def _get_allowed_content_type(
self,
allowed_content_dict: typing.Dict[str, str]
) -> typing.List[ContentType]:
allowed_content_type = [] # type: typing.List[ContentType]
for slug, value in allowed_content_dict.items():
if value:
try:
content_type = content_type_list.get_one_by_slug(slug)
allowed_content_type.append(content_type)
except ContentTypeNotExist:
pass
return allowed_content_type

def _check_valid_content_type_in_dir(self,
content_type: ContentType,
parent: Content,
Expand All @@ -1516,16 +1530,17 @@ def _check_valid_content_type_in_dir(self,
if parent:
assert workspace == parent.workspace
if parent.properties and 'allowed_content' in parent.properties:
if content_type.slug not in parent.properties[ 'allowed_content'] \
or not parent.properties['allowed_content'][content_type.slug]:
raise UnallowedSubContent(
' SubContent of type {subcontent_type} not allowed in content {content_id}'.format(
# nopep8
subcontent_type=content_type.slug,
content_id=parent.content_id,
))
if content_type not in self._get_allowed_content_type(
parent.properties['allowed_content']
):
raise UnallowedSubContent(
' SubContent of type {subcontent_type} not allowed in content {content_id}'.format(
# nopep8
subcontent_type=content_type.slug,
content_id=parent.content_id,
))
if workspace:
if content_type.slug not in workspace.get_allowed_content_types():
if content_type not in workspace.get_allowed_content_types():
raise UnallowedSubContent(
' SubContent of type {subcontent_type} not allowed in workspace {content_id}'.format( # nopep8
subcontent_type=content_type.slug,
Expand Down
5 changes: 3 additions & 2 deletions backend/tracim_backend/models/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from sqlalchemy.types import Unicode

from tracim_backend.app_models.contents import ContentStatus
from tracim_backend.app_models.contents import ContentType
from tracim_backend.app_models.contents import content_status_list
from tracim_backend.app_models.contents import content_type_list
from tracim_backend.exceptions import ContentRevisionUpdateError
Expand Down Expand Up @@ -98,9 +99,9 @@ def get_label(self):
""" this method is for interoperability with Content class"""
return self.label

def get_allowed_content_types(self):
def get_allowed_content_types(self) -> typing.List[ContentType]:
# @see Content.get_allowed_content_types()
return content_type_list.endpoint_allowed_types_slug()
return content_type_list.endpoint_allowed_types()

def get_valid_children(
self,
Expand Down
212 changes: 212 additions & 0 deletions backend/tracim_backend/tests/library/test_content_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
import typing

import pytest
import transaction

from tracim_backend.app_models.contents import content_status_list
from tracim_backend.app_models.contents import ContentType
from tracim_backend.app_models.contents import content_type_list
from tracim_backend.exceptions import ContentInNotEditableState
from tracim_backend.exceptions import ContentFilenameAlreadyUsedInFolder
Expand Down Expand Up @@ -624,6 +627,215 @@ def test_unit__restore_content_default_allowed_content__ok__nominal_case(self):
assert 'allowed_content' in folder.properties
assert folder.properties['allowed_content'] == content_type_list.default_allowed_content_properties(folder.type) # nopep8

def test_unit__get_allowed_content_type__ok__html_document(self):
uapi = UserApi(
session=self.session,
config=self.app_config,
current_user=None,
)
group_api = GroupApi(
current_user=None,
session=self.session,
config=self.app_config,
)
groups = [group_api.get_one(Group.TIM_USER),
group_api.get_one(Group.TIM_MANAGER),
group_api.get_one(Group.TIM_ADMIN)]

user = uapi.create_minimal_user(email='this.is@user',
groups=groups, save_now=True)
workspace = WorkspaceApi(
current_user=user,
session=self.session,
config=self.app_config,
).create_workspace('test workspace', save_now=True)
api = ContentApi(
current_user=user,
session=self.session,
config=self.app_config,
)
allowed_content_type_dict = {'html-document': True, 'file': False}
allowed_content_types = api._get_allowed_content_type(
allowed_content_type_dict
)
assert len(allowed_content_types) == 1
assert allowed_content_types[0] == \
content_type_list.get_one_by_slug('html-document')

def test_unit__get_allowed_content_type__ok__page_legacy_alias(self):
uapi = UserApi(
session=self.session,
config=self.app_config,
current_user=None,
)
group_api = GroupApi(
current_user=None,
session=self.session,
config=self.app_config,
)
groups = [group_api.get_one(Group.TIM_USER),
group_api.get_one(Group.TIM_MANAGER),
group_api.get_one(Group.TIM_ADMIN)]

user = uapi.create_minimal_user(email='this.is@user',
groups=groups, save_now=True)
workspace = WorkspaceApi(
current_user=user,
session=self.session,
config=self.app_config,
).create_workspace('test workspace', save_now=True)
api = ContentApi(
current_user=user,
session=self.session,
config=self.app_config,
)
allowed_content_type_dict = {'page': True, 'file': False}
allowed_content_types = api._get_allowed_content_type(
allowed_content_type_dict
)
assert len(allowed_content_types) == 1
assert allowed_content_types[0] == \
content_type_list.get_one_by_slug('html-document')

def test_unit___check_valid_content_type_in_dir__ok__nominal(self):
uapi = UserApi(
session=self.session,
config=self.app_config,
current_user=None,
)
group_api = GroupApi(
current_user=None,
session=self.session,
config=self.app_config,
)
groups = [group_api.get_one(Group.TIM_USER),
group_api.get_one(Group.TIM_MANAGER),
group_api.get_one(Group.TIM_ADMIN)]

user = uapi.create_minimal_user(email='this.is@user',
groups=groups, save_now=True)
workspace = WorkspaceApi(
current_user=user,
session=self.session,
config=self.app_config,
).create_workspace('test workspace', save_now=True)
api = ContentApi(
current_user=user,
session=self.session,
config=self.app_config,
)
folder = api.create(
content_type_slug=content_type_list.Folder.slug,
workspace=workspace,
parent=None,
label='plop',
do_save=False
)
allowed_content_type_slug_list = [content_type_list.Folder.slug] # nopep8
api.set_allowed_content(
folder,
allowed_content_type_slug_list=allowed_content_type_slug_list
)
api._check_valid_content_type_in_dir(
content_type=content_type_list.Folder,
parent=folder,
workspace=workspace
)

def test_unit___check_valid_content_type_in_dir__err__not_valid_in_folder(self):
uapi = UserApi(
session=self.session,
config=self.app_config,
current_user=None,
)
group_api = GroupApi(
current_user=None,
session=self.session,
config=self.app_config,
)
groups = [group_api.get_one(Group.TIM_USER),
group_api.get_one(Group.TIM_MANAGER),
group_api.get_one(Group.TIM_ADMIN)]

user = uapi.create_minimal_user(email='this.is@user',
groups=groups, save_now=True)
workspace = WorkspaceApi(
current_user=user,
session=self.session,
config=self.app_config,
).create_workspace('test workspace', save_now=True)
api = ContentApi(
current_user=user,
session=self.session,
config=self.app_config,
)
folder = api.create(
content_type_slug=content_type_list.Folder.slug,
workspace=workspace,
parent=None,
label='plop',
do_save=False
)
allowed_content_type_slug_list = [content_type_list.Folder.slug] # nopep8
api.set_allowed_content(
folder,
allowed_content_type_slug_list=allowed_content_type_slug_list
)
with pytest.raises(UnallowedSubContent):
api._check_valid_content_type_in_dir(
content_type=content_type_list.File,
parent=folder,
workspace=workspace
)

def test_unit___check_valid_content_type_in_dir__err__not_valid_in_workspace(self):
uapi = UserApi(
session=self.session,
config=self.app_config,
current_user=None,
)
group_api = GroupApi(
current_user=None,
session=self.session,
config=self.app_config,
)
groups = [group_api.get_one(Group.TIM_USER),
group_api.get_one(Group.TIM_MANAGER),
group_api.get_one(Group.TIM_ADMIN)]

user = uapi.create_minimal_user(email='this.is@user',
groups=groups, save_now=True)
workspace = WorkspaceApi(
current_user=user,
session=self.session,
config=self.app_config,
).create_workspace('test workspace', save_now=True)

# INFO - G.M - 2019-01-16 - override get_allowed_content_types methods
# to allow setting allowed content types of workspaces as tracim doesn't
# support yet to change this.
def fake_get_allowed_content_types() -> typing.List[ContentType]:
return [content_type_list.File]
workspace.get_allowed_content_types = fake_get_allowed_content_types

api = ContentApi(
current_user=user,
session=self.session,
config=self.app_config,
)
api._check_valid_content_type_in_dir(
content_type=content_type_list.File,
parent=None,
workspace=workspace
)
with pytest.raises(UnallowedSubContent):
api._check_valid_content_type_in_dir(
content_type=content_type_list.Folder,
parent=None,
workspace=workspace
)


def test_delete(self):
uapi = UserApi(
session=self.session,
Expand Down

0 comments on commit bb148ab

Please sign in to comment.