Skip to content

Commit

Permalink
In contentfactories view, test if add_view is None on all content fac…
Browse files Browse the repository at this point in the history
…tories (#512)

* Fix a bug when TypeInfo.add_view on a content type is None

* For TypeInfo.addable, test if add_view is None

* Better tests
  • Loading branch information
tiberiuichim authored and disko committed Oct 31, 2016
1 parent 037f18a commit da2c065
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Change History
1.3.1.dev0 - unreleased
-----------------------

- No changes yet.
- Bugfix: when showing addable content in the menu, check if the factory has
a defined add_view. This avoids a hard crash with, for example, a content
type derived from Content that has no add_view defined.

1.3.0 - 2016-10-10
------------------
Expand Down
2 changes: 2 additions & 0 deletions kotti/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ def addable(self, context, request):
:rtype: Boolean
"""

if self.add_view is None:
return False
if context.type_info.name in self.addable_to:
return view_permitted(context, request, self.add_view)
else:
Expand Down
11 changes: 11 additions & 0 deletions kotti/tests/test_node.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

from mock import Mock
from mock import patch
from pyramid.security import ALL_PERMISSIONS
from pyramid.security import Allow
from pyramid.security import Deny
Expand Down Expand Up @@ -475,3 +477,12 @@ def test_type_info_add_permission_custom(self):
from kotti.resources import TypeInfo
type_info = TypeInfo(add_permission='customadd')
assert type_info.add_permission == 'customadd'

def test_type_info_unaddable(self):
from kotti.resources import TypeInfo
type_info = TypeInfo(add_view=None, addable_to=['Document'])
context = Mock(type_info=Mock())
context.type_info.name = 'Document'
with patch('kotti.resources.view_permitted') as vp:
res = type_info.addable(context, None)
assert vp.call_count == 0
40 changes: 40 additions & 0 deletions kotti/tests/test_util_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,46 @@ def test_has_permission(self, db_session):
api.has_permission('drink')
has_permission.assert_called_with('drink', api.root)

def test_contenttypefactories_add_links(self, config):
from kotti.views.edit.actions import content_type_factories
from kotti.resources import Document, File
from kotti.views.edit import content

config.include(content)

res = content_type_factories(Document(''), DummyRequest())
assert res['factories'] == [Document, File]

def test_contenttypefactories_with_invalid_add_link(self, config):
from kotti.resources import Content, Document, File
from kotti.resources import default_type_info
from kotti.views.edit import content
from kotti.views.edit.actions import content_type_factories
from sqlalchemy import Column, Integer, ForeignKey

class TestContent(object):
type_info = default_type_info.copy(
name=u'TestContent',
title=u'Test Content',
add_view=None,
addable_to=[u'Document'],
)

config.include(content)
req = DummyRequest()
root = Document('')

with patch('kotti.views.edit.actions.get_settings') as gs:
gs.return_value = {'kotti.available_types':
[TestContent, Document, File]}
res = content_type_factories(root, req)

assert res['factories'] == [Document, File]

TestContent.type_info.add_view = 'add_document'
res = content_type_factories(root, req)
assert res['factories'] == [TestContent, Document, File]

def test_edit_links(self, config, db_session):
from kotti import views
from kotti.views.edit import actions, content, default_views
Expand Down

0 comments on commit da2c065

Please sign in to comment.