def test_pages_ordering(self): [category] = self.create_categories(self.workspace, count=1) page1 = Page({ 'title': 'title 1', 'language': 'eng_GB', 'position': 3, 'primary_category': category.uuid}) page2 = Page({ 'title': 'title 2', 'language': 'eng_GB', 'position': 0, 'primary_category': category.uuid}) page3 = Page({ 'title': 'title 3', 'language': 'eng_GB', 'position': 1, 'primary_category': category.uuid}) page4 = Page({ 'title': 'title 4', 'language': 'eng_GB', 'position': 2, 'primary_category': category.uuid}) self.workspace.save(page1, 'Update position') self.workspace.save(page2, 'Update position') self.workspace.save(page3, 'Update position') self.workspace.save(page4, 'Update position') self.workspace.refresh_index() request = self.mk_request(matchdict={'category': category.uuid}) views = CmsViews(request) cat = views.category() p1, p2, p3, p4 = cat['pages'] self.assertEqual(p1.uuid, page2.uuid) self.assertEqual(p2.uuid, page3.uuid) self.assertEqual(p3.uuid, page4.uuid) self.assertEqual(p4.uuid, page1.uuid)
def test_localisation(self): loc = Localisation({ 'locale': 'eng_GB', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/' }) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request({'_LOCALE_': 'eng_GB'}) self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'eng_GB') self.assertEqual(localisation.image, 'sample-uuid-000000-0001') self.assertEqual(localisation.image_host, 'http://some.site.com/') # Test fallbacks request = self.mk_request({'_LOCALE_': 'eng_UK'}) self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'eng_GB') self.assertEqual(localisation.image, 'sample-uuid-000000-0001') self.assertEqual(localisation.image_host, 'http://some.site.com/')
def test_image_url(self): self.views = CmsViews(self.mk_request()) self.assertEqual( self.views.get_image_url('http://some.site.com', 'sample-uuid-000000-0001'), 'http://some.site.com/' '1bzRPrcuQPXBECF9mHxFVr11viY=/sample-uuid-000000-0001') self.assertEqual( self.views.get_image_url('http://some.site.com', 'sample-uuid-000000-0001', 300, 200), 'http://some.site.com/' '8Ko7ZiKCwOv8zDovqScWL5Lgrc8=/300x200/sample-uuid-000000-0001') self.assertEqual( self.views.get_image_url('http://some.site.com', 'sample-uuid-000000-0001', 300), 'http://some.site.com/' 'LUyVe1umwB1caELC5m3LWu1HxvI=/300x0/sample-uuid-000000-0001') self.assertEqual( self.views.get_image_url('http://some.site.com', 'sample-uuid-000000-0001', height=150), 'http://some.site.com/' '4kS9gT_mYqVhnheDCuQhsahI_dU=/0x150/sample-uuid-000000-0001') self.assertEqual(self.views.get_image_url('', ''), '')
def setUp(self): self.workspace = self.mk_workspace() settings = self.get_settings( self.workspace, **{'unicorecomments.host': 'http://localhost/commentservice'}) self.config = testing.setUp(settings=settings) self.config.include('cms') set_cache_regions_from_settings(settings) self.views = CmsViews(self.mk_request()) self.app_id = settings['unicorehub.app_id'] self.app = self.mk_app(self.workspace, settings=settings) [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages(self.workspace, count=1, description='description', primary_category=category.uuid) self.page = page self.stream = self.mk_comment_stream(page.uuid, state='open', offset=10, limit=5) self.patch_lazy_comment_page = mock.patch.object( LazyCommentPage, 'data', new=mock.PropertyMock(return_value=self.stream)) self.patch_lazy_comment_page.start()
def setUp(self): self.workspace = self.mk_workspace() self.workspace.setup_custom_mapping( Page, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping( Category, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', }, 'position': { 'type': 'long' } } }) self.workspace.setup_custom_mapping( Localisation, { 'properties': { 'locale': { 'type': 'string', 'index': 'not_analyzed', } } }) languages = ("[('eng_GB', 'English'), ('swa_KE', 'Swahili')," "('spa_ES', 'Spanish'), ('fre_FR', 'French')," "('hin_IN', 'Hindi'), ('ind_ID', 'Bahasa')," "('per_IR', 'Persian'), ('grn_PY', 'Guarani')]") featured_langs = "[('spa_ES', 'Spanish'), ('eng_GB', 'English')]" settings = self.get_settings(self.workspace, available_languages=languages, featured_languages=featured_langs) self.config = testing.setUp(settings=settings) self.config.include('pyramid_chameleon') set_cache_regions_from_settings(settings) self.config.set_locale_negotiator(locale_negotiator_with_fallbacks) self.views = CmsViews(self.mk_request()) self.app = self.mk_app(self.workspace, settings=settings)
def render_logo(locale, default_src=None): request = self.mk_request(locale_name=locale) self.views = CmsViews(request) if default_src: define = 'tal:define="img_attrs view.get_logo_attributes' \ '(default_image_src=\'%s\')"' % default_src else: define = '' template = '<div metal:use-macro="view.logo_template" %s></div>' template = PageTemplate(template % define) return template.render(view=self.views, request=request)
def test_flatpage_markdown_rendering(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages(self.workspace, count=1, content='**strong**', description='_emphasised_') request = self.mk_request(matchdict={'slug': page.slug}) self.views = CmsViews(request) response = self.views.flatpage() self.assertEqual(response['content'], '<p><strong>strong</strong></p>') self.assertEqual(response['description'], '<p><em>emphasised</em></p>')
def test_content_linked_pages_none(self): [category] = self.create_categories(self.workspace, count=1) [page1] = self.create_pages(self.workspace, linked_pages=None, count=1, content='', description='', primary_category=category.uuid) request = self.mk_request(matchdict={'uuid': page1.uuid}) self.views = CmsViews(request) response = self.views.content() self.assertEqual(list(response['linked_pages']), [])
def test_get_top_nav(self): category1, category2 = self.create_categories(self.workspace) category3, category4 = self.create_categories(self.workspace, language='swa_KE', featured_in_navbar=True) self.assertEqual([], list(self.views.get_top_nav)) # Change language self.views = CmsViews(self.mk_request(locale_name='swa_KE')) cat1, cat2 = self.views.get_top_nav self.assertEqual(set([cat1.language, cat2.language]), set(['swa_KE', 'swa_KE']))
def test_get_flatpage_using_old_english_code(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages(self.workspace, count=1, content='Sample page in english', description='_emphasised_', language='eng_GB') request = self.mk_request({'_LOCALE_': 'eng_UK'}, matchdict={'slug': page.slug}) self.views = CmsViews(request) response = self.views.flatpage() self.assertEqual(response['content'], '<p>Sample page in english</p>')
def test_localisation_not_found(self): loc = Localisation({ 'locale': 'eng_GB', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/' }) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request({'_LOCALE_': 'spa_ES'}) self.views = CmsViews(request) self.assertIsNone(self.views.get_localisation())
def test_rtl(self): loc = Localisation({ 'locale': 'urd_IN', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/' }) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request(locale_name='urd_IN') self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'urd_IN')
def test_category_view(self): [category] = self.create_categories( self.workspace, count=1, language='swa_KE') [page] = self.create_pages( self.workspace, count=1, language='swa_KE', primary_category=category.uuid) request = self.mk_request( matchdict={'category': category.uuid}, locale_name='swa_KE') views = CmsViews(request) response = views.category() self.assertEqual(response['category'].uuid, category.uuid) self.assertEqual( [p.uuid for p in response['pages']], [page.uuid])
def test_category_view(self): [category] = self.create_categories(self.workspace, count=1, language='swa_KE') [page] = self.create_pages(self.workspace, count=1, language='swa_KE', primary_category=category.uuid) request = self.mk_request(matchdict={'category': category.uuid}, locale_name='swa_KE') views = CmsViews(request) response = views.category() self.assertEqual(response['category'].uuid, category.uuid) self.assertEqual([p.uuid for p in response['pages']], [page.uuid])
def test_category_view_does_not_exist(self): request = self.mk_request(locale_name='swa_KE') views = CmsViews(request) for does_not_exist in (None, 'abcd'): request.matchdict['category'] = does_not_exist self.assertRaises(HTTPNotFound, views.category)
def test_get_categories(self): category1, category2 = self.create_categories(self.workspace, language='eng_GB') category3, category4 = self.create_categories(self.workspace, language='swa_KE') cat1, cat2 = self.views.get_categories() self.assertEqual( set([category1.uuid, category2.uuid]), set([category.uuid for category in self.views.get_categories()])) # Change language self.views = CmsViews(self.mk_request(locale_name='swa_KE')) cat1, cat2 = self.views.get_categories() self.assertEqual(set([cat1.language, cat2.language]), set(['swa_KE', 'swa_KE']))
def setUp(self): self.workspace = self.mk_workspace() settings = self.get_settings(self.workspace, **{ 'unicorecomments.host': 'http://localhost/commentservice'}) self.config = testing.setUp(settings=settings) self.config.include('cms') set_cache_regions_from_settings(settings) self.views = CmsViews(self.mk_request()) self.app_id = settings['unicorehub.app_id'] self.app = self.mk_app(self.workspace, settings=settings) [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages( self.workspace, count=1, description='description', primary_category=category.uuid) self.page = page self.stream = self.mk_comment_stream( page.uuid, state='open', offset=10, limit=5) self.patch_lazy_comment_page = mock.patch.object( LazyCommentPage, 'data', new=mock.PropertyMock(return_value=self.stream)) self.patch_lazy_comment_page.start()
def test_get_featured_category_pages_swahili(self): self.views = CmsViews(self.mk_request(locale_name='swa_KE')) [category_eng] = self.create_categories( self.workspace, language='eng_GB', count=1) self.create_pages( self.workspace, count=10, language='eng_GB', primary_category=category_eng.uuid) self.create_pages( self.workspace, count=2, language='eng_GB', featured_in_category=True, primary_category=category_eng.uuid) [category_swh] = self.create_categories( self.workspace, language='swa_KE', count=1) self.create_pages( self.workspace, count=10, language='swa_KE', primary_category=category_swh.uuid) pages_swh_featured = self.create_pages( self.workspace, count=2, language='swa_KE', featured_in_category=True, primary_category=category_swh.uuid) # Assert english content not returned since language is swahili self.assertEqual( [], list(self.views.get_featured_category_pages(category_eng.uuid))) # Assert we get back featured pages for Swahili self.assertEqual( set([page.uuid for page in pages_swh_featured]), set([page.uuid for page in self.views.get_featured_category_pages( category_swh.uuid)]))
def test_unsupported_locales(self): request = self.mk_request(locale_name='grn_PY') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual( langs, [('grn_PY', u'Guarani'), ('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')])
def test_locales_displayed(self): langs = self.views.get_display_languages() self.assertEqual(langs, [('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')]) request = self.mk_request(locale_name='fre_FR') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual(langs, [('fre_FR', u'fran\xe7ais'), ('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')]) request = self.mk_request(locale_name='spa_ES') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual(langs, [('spa_ES', u'espa\xf1ol'), ('eng_GB', 'English')])
def setUp(self): self.workspace = self.mk_workspace() self.workspace.setup_custom_mapping( Page, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping( Category, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping( Localisation, { 'properties': { 'locale': { 'type': 'string', 'index': 'not_analyzed', } } }) settings = self.get_settings(self.workspace) self.config = testing.setUp(settings=settings) set_cache_regions_from_settings(settings) self.config.set_locale_negotiator(locale_negotiator_with_fallbacks) self.views = CmsViews(self.mk_request()) self.app = self.mk_app(self.workspace, settings=settings)
def test_locales_displayed(self): langs = self.views.get_display_languages() self.assertEqual( langs, [('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')]) request = self.mk_request(locale_name='fre_FR') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual( langs, [('fre_FR', u'fran\xe7ais'), ('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')]) request = self.mk_request(locale_name='spa_ES') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual( langs, [('spa_ES', u'espa\xf1ol'), ('eng_GB', 'English')])
def test_format_date_helper(self): views = CmsViews(self.mk_request()) self.assertEqual( views.format_date('2014-10-10T09:10:17+00:00'), '10 October 2014') self.assertEqual( views.format_date('2014-10-10T09:10:17+00:00', '%d-%b-%y'), '10-Oct-14') self.assertEqual( views.format_date('some invalid date'), 'some invalid date') dt = datetime(year=2014, month=10, day=10, hour=9, minute=10, second=17, tzinfo=pytz.utc) self.assertEqual( views.format_date(dt), '10 October 2014')
def setUp(self): self.workspace = self.mk_workspace() self.workspace.setup_custom_mapping(Page, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping(Category, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', }, 'position': { 'type': 'long' } } }) self.workspace.setup_custom_mapping(Localisation, { 'properties': { 'locale': { 'type': 'string', 'index': 'not_analyzed', } } }) languages = ("[('eng_GB', 'English'), ('swa_KE', 'Swahili')," "('spa_ES', 'Spanish'), ('fre_FR', 'French')," "('hin_IN', 'Hindi'), ('ind_ID', 'Bahasa')," "('per_IR', 'Persian'), ('grn_PY', 'Guarani')]") featured_langs = "[('spa_ES', 'Spanish'), ('eng_GB', 'English')]" settings = self.get_settings( self.workspace, available_languages=languages, featured_languages=featured_langs) self.config = testing.setUp(settings=settings) self.config.include('pyramid_chameleon') set_cache_regions_from_settings(settings) self.config.set_locale_negotiator(locale_negotiator_with_fallbacks) self.views = CmsViews(self.mk_request()) self.app = self.mk_app(self.workspace, settings=settings)
def setUp(self): self.workspace = self.mk_workspace() settings = self.get_settings(self.workspace) self.config = testing.setUp(settings=settings) self.config.include('cms') set_cache_regions_from_settings(settings) self.views = CmsViews(self.mk_request()) self.app = self.mk_app(self.workspace, settings=settings)
def test_get_featured_category_pages_swahili(self): self.views = CmsViews(self.mk_request(locale_name='swa_KE')) [category_eng] = self.create_categories(self.workspace, language='eng_GB', count=1) self.create_pages(self.workspace, count=10, language='eng_GB', primary_category=category_eng.uuid) self.create_pages(self.workspace, count=2, language='eng_GB', featured_in_category=True, primary_category=category_eng.uuid) [category_swh] = self.create_categories(self.workspace, language='swa_KE', count=1) self.create_pages(self.workspace, count=10, language='swa_KE', primary_category=category_swh.uuid) pages_swh_featured = self.create_pages( self.workspace, count=2, language='swa_KE', featured_in_category=True, primary_category=category_swh.uuid) # Assert english content not returned since language is swahili self.assertEqual( [], list(self.views.get_featured_category_pages(category_eng.uuid))) # Assert we get back featured pages for Swahili self.assertEqual( set([page.uuid for page in pages_swh_featured]), set([ page.uuid for page in self.views.get_featured_category_pages( category_swh.uuid) ]))
def test_content_linked_pages_none(self): [category] = self.create_categories(self.workspace, count=1) [page1] = self.create_pages( self.workspace, linked_pages=None, count=1, content='', description='', primary_category=category.uuid) request = self.mk_request(matchdict={'uuid': page1.uuid}) self.views = CmsViews(request) response = self.views.content() self.assertEqual(list(response['linked_pages']), [])
def test_pages_ordering(self): [category] = self.create_categories(self.workspace, count=1) page1 = Page({ 'title': 'title 1', 'language': 'eng_GB', 'position': 3, 'primary_category': category.uuid }) page2 = Page({ 'title': 'title 2', 'language': 'eng_GB', 'position': 0, 'primary_category': category.uuid }) page3 = Page({ 'title': 'title 3', 'language': 'eng_GB', 'position': 1, 'primary_category': category.uuid }) page4 = Page({ 'title': 'title 4', 'language': 'eng_GB', 'position': 2, 'primary_category': category.uuid }) self.workspace.save(page1, 'Update position') self.workspace.save(page2, 'Update position') self.workspace.save(page3, 'Update position') self.workspace.save(page4, 'Update position') self.workspace.refresh_index() request = self.mk_request(matchdict={'category': category.uuid}) views = CmsViews(request) cat = views.category() p1, p2, p3, p4 = cat['pages'] self.assertEqual(p1.uuid, page2.uuid) self.assertEqual(p2.uuid, page3.uuid) self.assertEqual(p3.uuid, page4.uuid) self.assertEqual(p4.uuid, page1.uuid)
def test_localisation_not_found(self): loc = Localisation({ 'locale': 'eng_GB', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/'}) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request({'_LOCALE_': 'spa_ES'}) self.views = CmsViews(request) self.assertIsNone(self.views.get_localisation())
def test_get_flatpage_using_old_english_code(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages( self.workspace, count=1, content='Sample page in english', description='_emphasised_', language='eng_GB') request = self.mk_request( {'_LOCALE_': 'eng_UK'}, matchdict={'slug': page.slug}) self.views = CmsViews(request) response = self.views.flatpage() self.assertEqual( response['content'], '<p>Sample page in english</p>')
def test_rtl(self): loc = Localisation({ 'locale': 'urd_IN', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/'}) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request(locale_name='urd_IN') self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'urd_IN')
def test_get_top_nav(self): category1, category2 = self.create_categories(self.workspace) category3, category4 = self.create_categories( self.workspace, language='swa_KE', featured_in_navbar=True) self.assertEqual([], list(self.views.get_top_nav)) # Change language self.views = CmsViews(self.mk_request(locale_name='swa_KE')) cat1, cat2 = self.views.get_top_nav self.assertEqual( set([cat1.language, cat2.language]), set(['swa_KE', 'swa_KE']))
def test_flatpage_markdown_rendering(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages( self.workspace, count=1, content='**strong**', description='_emphasised_') request = self.mk_request(matchdict={'slug': page.slug}) self.views = CmsViews(request) response = self.views.flatpage() self.assertEqual( response['content'], '<p><strong>strong</strong></p>') self.assertEqual( response['description'], '<p><em>emphasised</em></p>')
def test_localisation(self): loc = Localisation({ 'locale': 'eng_GB', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/'}) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request({'_LOCALE_': 'eng_GB'}) self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'eng_GB') self.assertEqual(localisation.image, 'sample-uuid-000000-0001') self.assertEqual(localisation.image_host, 'http://some.site.com/') # Test fallbacks request = self.mk_request({'_LOCALE_': 'eng_UK'}) self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'eng_GB') self.assertEqual(localisation.image, 'sample-uuid-000000-0001') self.assertEqual(localisation.image_host, 'http://some.site.com/')
def test_get_categories(self): category1, category2 = self.create_categories( self.workspace, language='eng_GB') category3, category4 = self.create_categories( self.workspace, language='swa_KE') cat1, cat2 = self.views.get_categories() self.assertEqual( set([category1.uuid, category2.uuid]), set([category.uuid for category in self.views.get_categories()]) ) # Change language self.views = CmsViews(self.mk_request(locale_name='swa_KE')) cat1, cat2 = self.views.get_categories() self.assertEqual( set([cat1.language, cat2.language]), set(['swa_KE', 'swa_KE']))
def setUp(self): self.workspace = self.mk_workspace() self.workspace.setup_custom_mapping(Page, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping(Category, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping(Localisation, { 'properties': { 'locale': { 'type': 'string', 'index': 'not_analyzed', } } }) settings = self.get_settings(self.workspace) self.config = testing.setUp(settings=settings) set_cache_regions_from_settings(settings) self.config.set_locale_negotiator(locale_negotiator_with_fallbacks) self.views = CmsViews(self.mk_request()) self.app = self.mk_app(self.workspace, settings=settings)
def test_image_url(self): self.views = CmsViews(self.mk_request()) self.assertEqual(self.views.get_image_url( 'http://some.site.com', 'sample-uuid-000000-0001'), 'http://some.site.com/' '1bzRPrcuQPXBECF9mHxFVr11viY=/sample-uuid-000000-0001') self.assertEqual(self.views.get_image_url( 'http://some.site.com', 'sample-uuid-000000-0001', 300, 200), 'http://some.site.com/' '8Ko7ZiKCwOv8zDovqScWL5Lgrc8=/300x200/sample-uuid-000000-0001') self.assertEqual(self.views.get_image_url( 'http://some.site.com', 'sample-uuid-000000-0001', 300), 'http://some.site.com/' 'LUyVe1umwB1caELC5m3LWu1HxvI=/300x0/sample-uuid-000000-0001') self.assertEqual(self.views.get_image_url( 'http://some.site.com', 'sample-uuid-000000-0001', height=150), 'http://some.site.com/' '4kS9gT_mYqVhnheDCuQhsahI_dU=/0x150/sample-uuid-000000-0001') self.assertEqual(self.views.get_image_url('', ''), '')
def test_format_date_helper(self): views = CmsViews(self.mk_request()) self.assertEqual(views.format_date('2014-10-10T09:10:17+00:00'), '10 October 2014') self.assertEqual( views.format_date('2014-10-10T09:10:17+00:00', '%d-%b-%y'), '10-Oct-14') self.assertEqual(views.format_date('some invalid date'), 'some invalid date') dt = datetime(year=2014, month=10, day=10, hour=9, minute=10, second=17, tzinfo=pytz.utc) self.assertEqual(views.format_date(dt), '10 October 2014')
def test_unsupported_locales(self): request = self.mk_request(locale_name='grn_PY') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual(langs, [('grn_PY', u'Guarani'), ('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')])
class TestViews(UnicoreTestCase): def setUp(self): self.workspace = self.mk_workspace() self.workspace.setup_custom_mapping( Page, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping( Category, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', }, 'position': { 'type': 'long' } } }) self.workspace.setup_custom_mapping( Localisation, { 'properties': { 'locale': { 'type': 'string', 'index': 'not_analyzed', } } }) languages = ("[('eng_GB', 'English'), ('swa_KE', 'Swahili')," "('spa_ES', 'Spanish'), ('fre_FR', 'French')," "('hin_IN', 'Hindi'), ('ind_ID', 'Bahasa')," "('per_IR', 'Persian'), ('grn_PY', 'Guarani')]") featured_langs = "[('spa_ES', 'Spanish'), ('eng_GB', 'English')]" settings = self.get_settings(self.workspace, available_languages=languages, featured_languages=featured_langs) self.config = testing.setUp(settings=settings) self.config.include('pyramid_chameleon') set_cache_regions_from_settings(settings) self.config.set_locale_negotiator(locale_negotiator_with_fallbacks) self.views = CmsViews(self.mk_request()) self.app = self.mk_app(self.workspace, settings=settings) def tearDown(self): testing.tearDown() def test_get_featured_pages(self): featured_pages = self.create_pages(self.workspace, count=2, featured=True) self.create_pages(self.workspace, count=2) self.assertEqual( set([p.uuid for p in featured_pages]), set([p.uuid for p in self.views.get_featured_pages(limit=10)])) def test_get_pages_count(self): self.create_pages(self.workspace, count=10) pages = self.views.get_pages(limit=7) self.assertEqual(len(pages), 7) def test_get_pages_order_by(self): self.create_pages(self.workspace, count=10) pages = self.views.get_pages(limit=2, order_by=('title', )) self.assertEqual([p.title for p in pages], ['Test Page 0', 'Test Page 1']) def test_get_pages_reversed(self): self.create_pages(self.workspace, count=10, timestamp_cb=lambda i: (arrow.utcnow() - timedelta(days=i)).isoformat()) pages = self.views.get_pages(limit=2, order_by=('-modified_at', )) self.assertEqual([p.title for p in pages], ['Test Page 0', 'Test Page 1']) def test_get_available_languages(self): languages = self.views.get_available_languages self.assertEqual(languages[1][0], 'eng_GB') self.assertEqual(languages[7][0], 'swa_KE') self.assertEqual(languages[7][1], 'Kiswahili') def test_get_featured_category_pages(self): category1, category2 = self.create_categories(self.workspace) self.create_pages(self.workspace, count=10, primary_category=category1.uuid) featured_pages = self.create_pages(self.workspace, count=2, primary_category=category1.uuid, featured_in_category=True) page1, page2 = self.views.get_featured_category_pages(category1.uuid) self.assertEqual(set([page1.uuid, page2.uuid]), set([fp.uuid for fp in featured_pages])) self.assertEqual(set([page1.language, page2.language]), set(['eng_GB', 'eng_GB'])) self.assertEqual( [], list(self.views.get_featured_category_pages(category2.uuid))) def test_get_featured_category_pages_swahili(self): self.views = CmsViews(self.mk_request(locale_name='swa_KE')) [category_eng] = self.create_categories(self.workspace, language='eng_GB', count=1) self.create_pages(self.workspace, count=10, language='eng_GB', primary_category=category_eng.uuid) self.create_pages(self.workspace, count=2, language='eng_GB', featured_in_category=True, primary_category=category_eng.uuid) [category_swh] = self.create_categories(self.workspace, language='swa_KE', count=1) self.create_pages(self.workspace, count=10, language='swa_KE', primary_category=category_swh.uuid) pages_swh_featured = self.create_pages( self.workspace, count=2, language='swa_KE', featured_in_category=True, primary_category=category_swh.uuid) # Assert english content not returned since language is swahili self.assertEqual( [], list(self.views.get_featured_category_pages(category_eng.uuid))) # Assert we get back featured pages for Swahili self.assertEqual( set([page.uuid for page in pages_swh_featured]), set([ page.uuid for page in self.views.get_featured_category_pages( category_swh.uuid) ])) def test_get_page_by_slug(self): self.create_pages(self.workspace, count=5, language='eng_GB') self.create_pages(self.workspace, count=5, language='swa_KE') p = self.views.get_page(None, 'test-page-1', 'eng_GB') self.assertEqual(p.title, 'Test Page 1') self.assertEqual(p.language, 'eng_GB') p = self.views.get_page(None, 'test-page-1', 'swa_KE') self.assertEqual(p.language, 'swa_KE') self.assertEqual(self.views.get_page(None, 'invalid-slug'), None) def test_content_linked_pages(self): [category] = self.create_categories(self.workspace, count=1) [page1] = self.create_pages(self.workspace, count=1, content='', description='', primary_category=category.uuid) [page2] = self.create_pages(self.workspace, count=1, content='', description='', linked_pages=[page1.uuid], primary_category=category.uuid) request = self.mk_request(matchdict={'uuid': page2.uuid}) self.views = CmsViews(request) response = self.views.content() [linked_page] = response['linked_pages'] self.assertEqual(linked_page, page1) def test_content_linked_pages_none(self): [category] = self.create_categories(self.workspace, count=1) [page1] = self.create_pages(self.workspace, linked_pages=None, count=1, content='', description='', primary_category=category.uuid) request = self.mk_request(matchdict={'uuid': page1.uuid}) self.views = CmsViews(request) response = self.views.content() self.assertEqual(list(response['linked_pages']), []) def test_content_markdown_rendering(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages(self.workspace, count=1, content='**strong**', description='_emphasised_', primary_category=category.uuid) request = self.mk_request(matchdict={'uuid': page.uuid}) self.views = CmsViews(request) response = self.views.content() self.assertEqual(response['content'], '<p><strong>strong</strong></p>') self.assertEqual(response['description'], '<p><em>emphasised</em></p>') def test_views_no_primary_category(self): [page] = self.create_pages(self.workspace, linked_pages=None, count=1, content='', description='', primary_category=None) # checks that relevant views don't generate exceptions self.app.get('/') self.app.get('/spice/') self.app.get('/content/detail/%s/' % page.uuid) self.app.get('/spice/content/detail/%s/' % page.uuid) self.app.get('/content/comments/%s/' % page.uuid) def test_flatpage_markdown_rendering(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages(self.workspace, count=1, content='**strong**', description='_emphasised_') request = self.mk_request(matchdict={'slug': page.slug}) self.views = CmsViews(request) response = self.views.flatpage() self.assertEqual(response['content'], '<p><strong>strong</strong></p>') self.assertEqual(response['description'], '<p><em>emphasised</em></p>') def test_get_categories(self): category1, category2 = self.create_categories(self.workspace, language='eng_GB') category3, category4 = self.create_categories(self.workspace, language='swa_KE') cat1, cat2 = self.views.get_categories() self.assertEqual( set([category1.uuid, category2.uuid]), set([category.uuid for category in self.views.get_categories()])) # Change language self.views = CmsViews(self.mk_request(locale_name='swa_KE')) cat1, cat2 = self.views.get_categories() self.assertEqual(set([cat1.language, cat2.language]), set(['swa_KE', 'swa_KE'])) def test_categories_ordering(self): category1 = Category({ 'title': 'title 1', 'language': 'eng_GB', 'position': 3 }) category2 = Category({ 'title': 'title 2', 'language': 'eng_GB', 'position': 0 }) category3 = Category({ 'title': 'title 3', 'language': 'eng_GB', 'position': 1 }) category4 = Category({ 'title': 'title 4', 'language': 'eng_GB', 'position': 2 }) self.workspace.save(category1, 'Update position') self.workspace.save(category2, 'Update position') self.workspace.save(category3, 'Update position') self.workspace.save(category4, 'Update position') self.workspace.refresh_index() cat1, cat2, cat3, cat4 = self.views.get_categories() self.assertEqual(cat1.uuid, category2.uuid) self.assertEqual(cat2.uuid, category3.uuid) self.assertEqual(cat3.uuid, category4.uuid) self.assertEqual(cat4.uuid, category1.uuid) def test_get_category(self): request = self.mk_request(locale_name='swa_KE') views = CmsViews(request) for does_not_exist in (None, 'abcd'): self.assertIs(views.get_category(does_not_exist), None) def test_pagination_first_page(self): [category] = self.create_categories(self.workspace, count=1, language='eng_GB') self.create_pages(self.workspace, count=15, content='baby', primary_category=category.uuid) resp = self.app.get('/content/list/%s/' % category.uuid, params={'p': '0'}, status=200) self.assertFalse('Previous' in resp.body) self.assertTrue('Next' in resp.body) def test_pagination_last_page(self): [category] = self.create_categories(self.workspace, count=1, language='eng_GB') self.create_pages(self.workspace, count=30, content='baby', primary_category=category.uuid) resp = self.app.get('/content/list/%s/' % category.uuid, params={'p': '3'}, status=200) self.assertTrue('Previous' in resp.body) self.assertFalse('Next' in resp.body) def test_pagination_middle_page(self): [category] = self.create_categories(self.workspace, count=1, language='eng_GB') self.create_pages(self.workspace, count=40, content='baby', primary_category=category.uuid) resp = self.app.get('/content/list/%s/' % category.uuid, params={'p': '2'}, status=200) self.assertTrue('Previous' in resp.body) self.assertTrue('Next' in resp.body) def test_pagination_results_per_page_configurable(self): settings = self.config.registry.settings.copy() settings["results_per_page"] = '5' app = self.mk_app(self.workspace, settings=settings) [category] = self.create_categories(self.workspace, count=1, language='eng_GB') self.create_pages(self.workspace, count=8, content='baby', primary_category=category.uuid) resp = app.get('/content/list/%s/' % category.uuid, params={'p': '0'}, status=200) self.assertTrue('Previous' not in resp.body) self.assertTrue('Next' in resp.body) def test_pagination_results_per_page_configurable_last_page(self): settings = self.config.registry.settings.copy() settings["results_per_page"] = '5' app = self.mk_app(self.workspace, settings=settings) [category] = self.create_categories(self.workspace, count=1, language='eng_GB') self.create_pages(self.workspace, count=8, content='baby', primary_category=category.uuid) resp = app.get('/content/list/%s/' % category.uuid, params={'p': '5'}, status=200) self.assertTrue('Previous' in resp.body) self.assertTrue('Next' not in resp.body) # check that we're on page 2 self.assertTrue('<b>2</b>' in resp.body) def test_category_view(self): [category] = self.create_categories(self.workspace, count=1, language='swa_KE') [page] = self.create_pages(self.workspace, count=1, language='swa_KE', primary_category=category.uuid) request = self.mk_request(matchdict={'category': category.uuid}, locale_name='swa_KE') views = CmsViews(request) response = views.category() self.assertEqual(response['category'].uuid, category.uuid) self.assertEqual([p.uuid for p in response['pages']], [page.uuid]) def test_category_view_does_not_exist(self): request = self.mk_request(locale_name='swa_KE') views = CmsViews(request) for does_not_exist in (None, 'abcd'): request.matchdict['category'] = does_not_exist self.assertRaises(HTTPNotFound, views.category) def test_pages_ordering(self): [category] = self.create_categories(self.workspace, count=1) page1 = Page({ 'title': 'title 1', 'language': 'eng_GB', 'position': 3, 'primary_category': category.uuid }) page2 = Page({ 'title': 'title 2', 'language': 'eng_GB', 'position': 0, 'primary_category': category.uuid }) page3 = Page({ 'title': 'title 3', 'language': 'eng_GB', 'position': 1, 'primary_category': category.uuid }) page4 = Page({ 'title': 'title 4', 'language': 'eng_GB', 'position': 2, 'primary_category': category.uuid }) self.workspace.save(page1, 'Update position') self.workspace.save(page2, 'Update position') self.workspace.save(page3, 'Update position') self.workspace.save(page4, 'Update position') self.workspace.refresh_index() request = self.mk_request(matchdict={'category': category.uuid}) views = CmsViews(request) cat = views.category() p1, p2, p3, p4 = cat['pages'] self.assertEqual(p1.uuid, page2.uuid) self.assertEqual(p2.uuid, page3.uuid) self.assertEqual(p3.uuid, page4.uuid) self.assertEqual(p4.uuid, page1.uuid) def test_get_top_nav(self): category1, category2 = self.create_categories(self.workspace) category3, category4 = self.create_categories(self.workspace, language='swa_KE', featured_in_navbar=True) self.assertEqual([], list(self.views.get_top_nav)) # Change language self.views = CmsViews(self.mk_request(locale_name='swa_KE')) cat1, cat2 = self.views.get_top_nav self.assertEqual(set([cat1.language, cat2.language]), set(['swa_KE', 'swa_KE'])) def test_format_date_helper(self): views = CmsViews(self.mk_request()) self.assertEqual(views.format_date('2014-10-10T09:10:17+00:00'), '10 October 2014') self.assertEqual( views.format_date('2014-10-10T09:10:17+00:00', '%d-%b-%y'), '10-Oct-14') self.assertEqual(views.format_date('some invalid date'), 'some invalid date') dt = datetime(year=2014, month=10, day=10, hour=9, minute=10, second=17, tzinfo=pytz.utc) self.assertEqual(views.format_date(dt), '10 October 2014') def test_get_flatpage_using_old_swahili_code(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages(self.workspace, count=1, content='Sample page in swahili', description='_emphasised_', language='swa_KE') request = self.mk_request({'_LOCALE_': 'swh_KE'}, matchdict={'slug': page.slug}) self.views = CmsViews(request) response = self.views.flatpage() self.assertEqual(response['content'], '<p>Sample page in swahili</p>') def test_get_flatpage_using_old_english_code(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages(self.workspace, count=1, content='Sample page in english', description='_emphasised_', language='eng_GB') request = self.mk_request({'_LOCALE_': 'eng_UK'}, matchdict={'slug': page.slug}) self.views = CmsViews(request) response = self.views.flatpage() self.assertEqual(response['content'], '<p>Sample page in english</p>') def test_image_url(self): self.views = CmsViews(self.mk_request()) self.assertEqual( self.views.get_image_url('http://some.site.com', 'sample-uuid-000000-0001'), 'http://some.site.com/' '1bzRPrcuQPXBECF9mHxFVr11viY=/sample-uuid-000000-0001') self.assertEqual( self.views.get_image_url('http://some.site.com', 'sample-uuid-000000-0001', 300, 200), 'http://some.site.com/' '8Ko7ZiKCwOv8zDovqScWL5Lgrc8=/300x200/sample-uuid-000000-0001') self.assertEqual( self.views.get_image_url('http://some.site.com', 'sample-uuid-000000-0001', 300), 'http://some.site.com/' 'LUyVe1umwB1caELC5m3LWu1HxvI=/300x0/sample-uuid-000000-0001') self.assertEqual( self.views.get_image_url('http://some.site.com', 'sample-uuid-000000-0001', height=150), 'http://some.site.com/' '4kS9gT_mYqVhnheDCuQhsahI_dU=/0x150/sample-uuid-000000-0001') self.assertEqual(self.views.get_image_url('', ''), '') def test_localisation(self): loc = Localisation({ 'locale': 'eng_GB', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/' }) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request({'_LOCALE_': 'eng_GB'}) self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'eng_GB') self.assertEqual(localisation.image, 'sample-uuid-000000-0001') self.assertEqual(localisation.image_host, 'http://some.site.com/') # Test fallbacks request = self.mk_request({'_LOCALE_': 'eng_UK'}) self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'eng_GB') self.assertEqual(localisation.image, 'sample-uuid-000000-0001') self.assertEqual(localisation.image_host, 'http://some.site.com/') def test_localisation_not_found(self): loc = Localisation({ 'locale': 'eng_GB', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/' }) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request({'_LOCALE_': 'spa_ES'}) self.views = CmsViews(request) self.assertIsNone(self.views.get_localisation()) def test_localised_logo(self): self.create_localisation(self.workspace, locale='eng_GB', logo_text='logo_text_foo', logo_description='logo_description_foo', logo_image='sample-uuid-000000-0002', logo_image_host='http://some.site.com/') def render_logo(locale, default_src=None): request = self.mk_request(locale_name=locale) self.views = CmsViews(request) if default_src: define = 'tal:define="img_attrs view.get_logo_attributes' \ '(default_image_src=\'%s\')"' % default_src else: define = '' template = '<div metal:use-macro="view.logo_template" %s></div>' template = PageTemplate(template % define) return template.render(view=self.views, request=request) localised_logo = render_logo('eng_GB') non_localised_logo = render_logo('spa_ES', '/default/logo.png') no_logo = render_logo('spa_ES') self.assertTrue('http://some.site.com/' in localised_logo) self.assertTrue('sample-uuid-000000-0002' in localised_logo) self.assertTrue('logo_description_foo' in localised_logo) self.assertFalse('logo-container' in no_logo) self.assertTrue('/default/logo.png' in non_localised_logo) def test_locale_cookie(self): [category1] = self.create_categories(self.workspace, count=1, locale='eng_GB', title='English Category') [category2] = self.create_categories(self.workspace, count=1, locale='spa_ES', title='Spanish Category') self.app.get('/locale/?language=eng_GB', status=302) resp = self.app.get('/', status=200) self.assertTrue('English Category' in resp.body) self.assertFalse('Spanish Category' in resp.body) self.app.get('/locale/?language=spa_ES', status=302) resp = self.app.get('/', status=200) self.assertTrue('Spanish Category' in resp.body) self.assertFalse('English Category' in resp.body) self.app.get('/locale/eng_GB/', status=302) resp = self.app.get('/', status=200) self.assertTrue('English Category' in resp.body) self.assertFalse('Spanish Category' in resp.body) self.app.get('/locale/spa_ES/', status=302) resp = self.app.get('/', status=200) self.assertTrue('Spanish Category' in resp.body) self.assertFalse('English Category' in resp.body) def test_locales_displayed(self): langs = self.views.get_display_languages() self.assertEqual(langs, [('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')]) request = self.mk_request(locale_name='fre_FR') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual(langs, [('fre_FR', u'fran\xe7ais'), ('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')]) request = self.mk_request(locale_name='spa_ES') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual(langs, [('spa_ES', u'espa\xf1ol'), ('eng_GB', 'English')]) def test_unsupported_locales(self): request = self.mk_request(locale_name='grn_PY') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual(langs, [('grn_PY', u'Guarani'), ('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')]) def test_change_locale_page(self): resp = self.app.get('/locale/change/') self.assertTrue(u'<a href="/locale/spa_ES/">espa\xf1ol</a>' in resp.body.decode('utf-8')) self.assertTrue(u'<a href="/locale/eng_GB/">English</a>' in resp.body.decode('utf-8')) self.assertTrue(u'<a href="/locale/swa_KE/">Kiswahili</a>' in resp.body.decode('utf-8')) self.assertTrue( u'<a href="/locale/per_IR/">\u0641\u0627\u0631\u0633\u06cc</a>' in resp.body.decode('utf-8')) def test_404_page(self): resp = self.app.get('/;jsdafjahs;dfjas;', expect_errors=True) self.assertTrue('class="page-not-found"' in resp.body) self.assertEqual(resp.status_int, 404) def test_health(self): resp = self.app.get('/health/', status=200) data = json.loads(resp.body) self.assertEqual(data, { 'version': None, 'id': None, }) def test_health_with_env_vars(self): os.environ['MARATHON_APP_ID'] = 'the-app-id' os.environ['MARATHON_APP_VERSION'] = 'the-app-version' resp = self.app.get('/health/', status=200) data = json.loads(resp.body) self.assertEqual(data, { 'version': 'the-app-version', 'id': 'the-app-id', }) os.environ.pop('MARATHON_APP_ID') os.environ.pop('MARATHON_APP_VERSION')
class TestCommentViews(UnicoreTestCase): def setUp(self): self.workspace = self.mk_workspace() settings = self.get_settings(self.workspace, **{ 'unicorecomments.host': 'http://localhost/commentservice'}) self.config = testing.setUp(settings=settings) self.config.include('cms') set_cache_regions_from_settings(settings) self.views = CmsViews(self.mk_request()) self.app_id = settings['unicorehub.app_id'] self.app = self.mk_app(self.workspace, settings=settings) [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages( self.workspace, count=1, description='description', primary_category=category.uuid) self.page = page self.stream = self.mk_comment_stream( page.uuid, state='open', offset=10, limit=5) self.patch_lazy_comment_page = mock.patch.object( LazyCommentPage, 'data', new=mock.PropertyMock(return_value=self.stream)) self.patch_lazy_comment_page.start() def tearDown(self): testing.tearDown() self.patch_lazy_comment_page.stop() def mk_comment(self, content_uuid, **fields): data = { 'uuid': uuid.uuid4().hex, 'user_uuid': uuid.uuid4().hex, 'content_uuid': content_uuid, 'app_uuid': self.app_id, 'comment': 'this is a comment', 'user_name': 'foo', 'submit_datetime': datetime.now(pytz.utc).isoformat(), 'content_type': 'page', 'content_title': 'I Am A Page', 'content_url': 'http://example.com/page/', 'locale': 'eng_ZA', 'flag_count': '0', 'is_removed': 'False', 'moderation_state': 'visible', 'ip_address': '192.168.1.1' } data.update(fields) return data def mk_comment_stream(self, content_uuid, limit=10, offset=0, state='open', total=100, **comment_fields): return { 'start': offset, 'end': offset + limit, 'total': total, 'count': limit, 'objects': [self.mk_comment(content_uuid, **comment_fields) for i in range(limit)], 'metadata': {'state': state} } @mock.patch.object(LazyCommentPage, '__init__') def test_get_comments_for_content(self, mock_init): content_uuid = 'content_uuid' mock_init.return_value = None page_obj = self.views.get_comments_for_content(content_uuid) self.assertIsInstance(page_obj, LazyCommentPage) mock_init.assert_called_with( self.config.registry.commentclient, content_uuid=content_uuid, app_uuid=self.app_id, limit=COMMENTS_PER_PAGE) self.views.request.GET['c_after'] = 'after_uuid' page_obj = self.views.get_comments_for_content( content_uuid, limit=1) self.assertIsInstance(page_obj, LazyCommentPage) mock_init.assert_called_with( self.config.registry.commentclient, content_uuid=content_uuid, app_uuid=self.app_id, limit=1, after='after_uuid') del self.views.request.GET['c_after'] self.views.request.GET['c_before'] = 'before_uuid' page_obj = self.views.get_comments_for_content(content_uuid) self.assertIsInstance(page_obj, LazyCommentPage) mock_init.assert_called_with( self.config.registry.commentclient, content_uuid=content_uuid, app_uuid=self.app_id, limit=COMMENTS_PER_PAGE, before='before_uuid') self.views.request.registry.commentclient = None page_obj = self.views.get_comments_for_content(content_uuid) self.assertIs(page_obj, None) def check_comment_list(self, root): uuid = self.stream['objects'][0]['content_uuid'] # header header = root.select('.comments-header')[0] self.assertIn('%d comments' % self.stream['total'], header.string) # pagination prev, nxt = root.select('.comments-pagination > a') self.assertIn('Previous', prev.string) self.assertIn( '/content/comments/%s/?c_after' % uuid, prev['href']) self.assertIn('Next', nxt.string) self.assertIn( '/content/comments/%s/?c_before' % uuid, nxt['href']) # comments li = root.select('.comment-list > .comment') self.assertEqual(len(li), len(self.stream['objects'])) comment_obj = self.stream['objects'][0] comment_el = li[0] self.assertTrue(comment_el.select('.comment-author')) self.assertEqual( comment_el.select('.comment-author')[0].string, comment_obj['user_name']) self.assertTrue(comment_el.select('.comment-meta > .date')) self.assertTrue( comment_el.select('.comment-meta > .date')[0].string) self.assertTrue(comment_el.select('p')) self.assertEqual( comment_el.select('p')[0].string, comment_obj['comment']) def check_comment_flag_logged_in(self, root): comment_el = root.select('.comment-list > .comment')[0] comment_obj = self.stream['objects'][0] flag = comment_el.select('.comment-actions > .comment-flag > a') self.assertTrue(flag) self.assertIn('Report', flag[0].string) self.assertIn( '/comments/flag/%s/' % comment_obj['uuid'], flag[0]['href']) def check_comment_flag_logged_out(self, root): comment_el = root.select('.comment-list > .comment')[0] flag = comment_el.select('.comment-actions > .comment-flag > a') self.assertFalse(flag) def check_comment_form(self, root): uuid = self.stream['objects'][0]['content_uuid'] form = root.select('#add-comment > form') self.assertTrue(form) form = form[0] self.assertIn('/content/comments/%s/' % uuid, form['action']) self.assertEqual(len(form.select('input')), 5) self.assertEqual(len(form.select('textarea')), 1) def test_comments_404(self): response = self.app.get( '/content/comments/%s/' % uuid.uuid4().hex, expect_errors=True) self.assertEqual(response.status_int, 404) response = self.app.get( '/content/comments/%s/?_LOCALE_=swa_KE' % self.page.uuid, expect_errors=True) self.assertEqual(response.status_int, 404) def test_comments_open_stream_logged_out(self): response = self.app.get('/content/comments/%s/' % self.page.uuid) self.check_comment_list(response.html) not_signed_in = response.html.select('#add-comment > p') self.assertTrue(not_signed_in) self.assertIn( 'You have to be signed in to add a comment', not_signed_in[0].text) self.assertFalse(response.html.select('#add-comment > form')) self.check_comment_flag_logged_out(response.html) def test_comments_open_stream_logged_in(self): response = self.app.get( '/content/comments/%s/' % self.page.uuid, headers=self.mk_session()[1]) self.check_comment_form(response.html) self.check_comment_flag_logged_in(response.html) def test_comments_closed_stream(self): self.stream['metadata']['state'] = 'closed' response = self.app.get('/content/comments/%s/' % self.page.uuid) not_open = response.html.select('#add-comment > p') self.assertTrue(not_open) self.assertIn( 'Commenting has been closed', not_open[0].string) self.assertFalse(response.html.select('#add-comment > form')) def test_comments_disabled_stream(self): for meta in ({'state': 'disabled'}, {}): self.stream['metadata'] = meta response = self.app.get('/content/comments/%s/' % self.page.uuid) for selector in ('#add-comment *', '.comment-list', '.comments-header', '.comments-pagination'): self.assertFalse(response.html.select(selector)) def test_content_with_comments(self): # NOTE: Chameleon templates don't have commenting nor # will commenting be added to them response = self.app.get( '/spice/content/detail/%s/' % self.page.uuid, headers=self.mk_session()[1]) self.check_comment_list(response.html) self.check_comment_form(response.html) def test_post_comment(self): mock_create_comment = mock.Mock() patch_client = mock.patch.object( self.app.app.registry.commentclient, 'create_comment', new=mock_create_comment) patch_client.start() self.stream['objects'] = [] self.stream['start'] = None self.stream['end'] = None self.stream['total'] = 0 session_cookie = self.mk_session()[1] response = self.app.get( '/content/comments/%s/' % self.page.uuid, headers=session_cookie) form = response.forms[0] form['comment'] = 'foo bar' # no issues response = form.submit('submit', headers=session_cookie) self.assertEqual(response.status_int, 302) self.assertEqual( response.location, 'http://localhost/content/comments/%s/' % self.page.uuid) self.assertGreaterEqual(mock_create_comment.call_count, 1) data = mock_create_comment.call_args[0][0] self.assertEqual(data.get('comment'), 'foo bar') # missing field form['comment'] = '' response = form.submit('submit', headers=session_cookie) self.assertEqual(response.status_int, 200) self.assertIn( 'Required', response.html.select('#add-comment > form')[0].text) # banned user form['comment'] = 'foo bar' mock_create_comment.side_effect = UserBanned(mock.MagicMock()) response = form.submit('submit', headers=session_cookie) self.assertEqual(response.status_int, 200) self.assertIn( 'You have been banned from commenting', response.html.select('#add-comment > form')[0].text) # closed stream mock_create_comment.side_effect = CommentStreamNotOpen( mock.MagicMock()) response = form.submit('submit', headers=session_cookie) self.assertEqual(response.status_int, 302) # ValueError (CSRF failure, no authenticated user, etc) mock_create_comment.side_effect = ValueError response = form.submit( 'submit', headers=session_cookie, expect_errors=True) self.assertEqual(response.status_int, 400) def test_removed_comments(self): for i, mod_state in enumerate(('removed_by_moderator', 'removed_by_community', 'removed_for_profanity')): self.stream['objects'][i]['is_removed'] = 'True' self.stream['objects'][i]['moderation_state'] = mod_state response = self.app.get('/content/comments/%s/' % self.page.uuid) comments = response.html.select('.comment-list > .comment') for i, mod_msg in enumerate(('by a moderator', 'by the community', 'because it contains profanity')): comment = comments[i] self.assertIn( 'This comment has been removed %s.' % mod_msg, comment.select('p')[0].string) self.assertFalse(comment.select('.comment-meta')) self.assertFalse(comment.select('.comment-actions')) def test_flag_comment_logged_in(self): mock_create_flag = mock.Mock(return_value=True) patch_client = mock.patch.object( self.app.app.registry.commentclient, 'create_flag', new=mock_create_flag) patch_client.start() comment_obj = self.stream['objects'][0] session_cookie = self.mk_session()[1] def check_response(referer, referer_is_next): mock_create_flag.reset_mock() headers = session_cookie.copy() if referer is not None: headers['Referer'] = referer response = self.app.get( '/comments/flag/%s/' % comment_obj['uuid'], headers=headers) self.assertEqual(response.status_int, 302) if referer_is_next: self.assertEqual( 'http://localhost/comments/flag/%s/success/?%s' % ( comment_obj['uuid'], urlencode({'next': referer})), response.location) else: self.assertEqual( 'http://localhost/comments/flag/%s/success/' % comment_obj['uuid'], response.location) self.assertGreaterEqual(mock_create_flag.call_count, 1) flag_data = mock_create_flag.call_args[0][0] self.assertIsInstance(flag_data.get('submit_datetime'), basestring) self.assertIsInstance(flag_data.get('user_uuid'), basestring) self.assertEqual(flag_data.get('app_uuid'), self.app_id) self.assertEqual( flag_data.get('comment_uuid'), comment_obj['uuid']) check_response( referer='http://localhost/content/comments/%s/' % self.page.uuid, referer_is_next=True) check_response( referer='http://external.domain.com', referer_is_next=False) check_response( referer=None, referer_is_next=False) # 404 mock_create_flag.side_effect = CommentServiceException( mock.MagicMock(status_code=404)) response = self.app.get( '/comments/flag/commentuuid/', headers=session_cookie, expect_errors=True) self.assertEqual(response.status_int, 404) patch_client.stop() def test_flag_comment_logged_out(self): mock_create_flag = mock.Mock(return_value=True) patch_client = mock.patch.object( self.app.app.registry.commentclient, 'create_flag', new=mock_create_flag) patch_client.start() comment_obj = self.stream['objects'][0] response = self.app.get( '/comments/flag/%s/' % comment_obj['uuid'], expect_errors=True) mock_create_flag.assert_not_called() self.assertEqual(response.status_int, 404) patch_client.stop() def test_flag_comment_success(self): comment_obj = self.stream['objects'][0] session_cookie = self.mk_session()[1] def check_response(next_url, valid_next): qs = '?%s' % urlencode({'next': next_url}) if next_url else '' response = self.app.get( '/comments/flag/%s/success/%s' % (comment_obj['uuid'], qs), headers=session_cookie) self.assertEqual(response.status_int, 200) content = response.html.select('.comment-flagged > p') self.assertTrue(content) self.assertIn( 'You have successfully reported a comment', content[0].text) go_back = content[0].select('a') if valid_next: self.assertTrue(go_back) self.assertIn('Go back', go_back[0].string) self.assertEqual(go_back[0]['href'], next_url) else: self.assertFalse(go_back) # logged in check_response( next_url='http://localhost/content/comments/%s/' % self.page.uuid, valid_next=True) check_response( next_url='http://external.domain.com', valid_next=False) check_response( next_url=None, valid_next=False) # logged out response = self.app.get( '/comments/flag/%s/success/' % comment_obj['uuid'], expect_errors=True) self.assertEqual(response.status_int, 404)
class TestViews(UnicoreTestCase): def setUp(self): self.workspace = self.mk_workspace() self.workspace.setup_custom_mapping(Page, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping(Category, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', }, 'position': { 'type': 'long' } } }) self.workspace.setup_custom_mapping(Localisation, { 'properties': { 'locale': { 'type': 'string', 'index': 'not_analyzed', } } }) languages = ("[('eng_GB', 'English'), ('swa_KE', 'Swahili')," "('spa_ES', 'Spanish'), ('fre_FR', 'French')," "('hin_IN', 'Hindi'), ('ind_ID', 'Bahasa')," "('per_IR', 'Persian'), ('grn_PY', 'Guarani')]") featured_langs = "[('spa_ES', 'Spanish'), ('eng_GB', 'English')]" settings = self.get_settings( self.workspace, available_languages=languages, featured_languages=featured_langs) self.config = testing.setUp(settings=settings) self.config.include('pyramid_chameleon') set_cache_regions_from_settings(settings) self.config.set_locale_negotiator(locale_negotiator_with_fallbacks) self.views = CmsViews(self.mk_request()) self.app = self.mk_app(self.workspace, settings=settings) def tearDown(self): testing.tearDown() def test_get_featured_pages(self): featured_pages = self.create_pages( self.workspace, count=2, featured=True) self.create_pages(self.workspace, count=2) self.assertEqual( set([p.uuid for p in featured_pages]), set([p.uuid for p in self.views.get_featured_pages(limit=10)])) def test_get_pages_count(self): self.create_pages(self.workspace, count=10) pages = self.views.get_pages(limit=7) self.assertEqual(len(pages), 7) def test_get_pages_order_by(self): self.create_pages(self.workspace, count=10) pages = self.views.get_pages(limit=2, order_by=('title',)) self.assertEqual( [p.title for p in pages], ['Test Page 0', 'Test Page 1']) def test_get_pages_reversed(self): self.create_pages( self.workspace, count=10, timestamp_cb=lambda i: ( arrow.utcnow() - timedelta(days=i)).isoformat()) pages = self.views.get_pages(limit=2, order_by=('-modified_at',)) self.assertEqual( [p.title for p in pages], ['Test Page 0', 'Test Page 1']) def test_get_available_languages(self): languages = self.views.get_available_languages self.assertEqual(languages[1][0], 'eng_GB') self.assertEqual(languages[7][0], 'swa_KE') self.assertEqual(languages[7][1], 'Kiswahili') def test_get_featured_category_pages(self): category1, category2 = self.create_categories(self.workspace) self.create_pages( self.workspace, count=10, primary_category=category1.uuid) featured_pages = self.create_pages( self.workspace, count=2, primary_category=category1.uuid, featured_in_category=True) page1, page2 = self.views.get_featured_category_pages(category1.uuid) self.assertEqual( set([page1.uuid, page2.uuid]), set([fp.uuid for fp in featured_pages])) self.assertEqual( set([page1.language, page2.language]), set(['eng_GB', 'eng_GB'])) self.assertEqual( [], list(self.views.get_featured_category_pages(category2.uuid))) def test_get_featured_category_pages_swahili(self): self.views = CmsViews(self.mk_request(locale_name='swa_KE')) [category_eng] = self.create_categories( self.workspace, language='eng_GB', count=1) self.create_pages( self.workspace, count=10, language='eng_GB', primary_category=category_eng.uuid) self.create_pages( self.workspace, count=2, language='eng_GB', featured_in_category=True, primary_category=category_eng.uuid) [category_swh] = self.create_categories( self.workspace, language='swa_KE', count=1) self.create_pages( self.workspace, count=10, language='swa_KE', primary_category=category_swh.uuid) pages_swh_featured = self.create_pages( self.workspace, count=2, language='swa_KE', featured_in_category=True, primary_category=category_swh.uuid) # Assert english content not returned since language is swahili self.assertEqual( [], list(self.views.get_featured_category_pages(category_eng.uuid))) # Assert we get back featured pages for Swahili self.assertEqual( set([page.uuid for page in pages_swh_featured]), set([page.uuid for page in self.views.get_featured_category_pages( category_swh.uuid)])) def test_get_page_by_slug(self): self.create_pages(self.workspace, count=5, language='eng_GB') self.create_pages(self.workspace, count=5, language='swa_KE') p = self.views.get_page(None, 'test-page-1', 'eng_GB') self.assertEqual(p.title, 'Test Page 1') self.assertEqual(p.language, 'eng_GB') p = self.views.get_page(None, 'test-page-1', 'swa_KE') self.assertEqual(p.language, 'swa_KE') self.assertEqual(self.views.get_page(None, 'invalid-slug'), None) def test_content_linked_pages(self): [category] = self.create_categories(self.workspace, count=1) [page1] = self.create_pages( self.workspace, count=1, content='', description='', primary_category=category.uuid) [page2] = self.create_pages( self.workspace, count=1, content='', description='', linked_pages=[page1.uuid], primary_category=category.uuid) request = self.mk_request(matchdict={'uuid': page2.uuid}) self.views = CmsViews(request) response = self.views.content() [linked_page] = response['linked_pages'] self.assertEqual(linked_page, page1) def test_content_linked_pages_none(self): [category] = self.create_categories(self.workspace, count=1) [page1] = self.create_pages( self.workspace, linked_pages=None, count=1, content='', description='', primary_category=category.uuid) request = self.mk_request(matchdict={'uuid': page1.uuid}) self.views = CmsViews(request) response = self.views.content() self.assertEqual(list(response['linked_pages']), []) def test_content_markdown_rendering(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages( self.workspace, count=1, content='**strong**', description='_emphasised_', primary_category=category.uuid) request = self.mk_request(matchdict={'uuid': page.uuid}) self.views = CmsViews(request) response = self.views.content() self.assertEqual( response['content'], '<p><strong>strong</strong></p>') self.assertEqual( response['description'], '<p><em>emphasised</em></p>') def test_views_no_primary_category(self): [page] = self.create_pages( self.workspace, linked_pages=None, count=1, content='', description='', primary_category=None) # checks that relevant views don't generate exceptions self.app.get('/') self.app.get('/spice/') self.app.get('/content/detail/%s/' % page.uuid) self.app.get('/spice/content/detail/%s/' % page.uuid) self.app.get('/content/comments/%s/' % page.uuid) def test_flatpage_markdown_rendering(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages( self.workspace, count=1, content='**strong**', description='_emphasised_') request = self.mk_request(matchdict={'slug': page.slug}) self.views = CmsViews(request) response = self.views.flatpage() self.assertEqual( response['content'], '<p><strong>strong</strong></p>') self.assertEqual( response['description'], '<p><em>emphasised</em></p>') def test_get_categories(self): category1, category2 = self.create_categories( self.workspace, language='eng_GB') category3, category4 = self.create_categories( self.workspace, language='swa_KE') cat1, cat2 = self.views.get_categories() self.assertEqual( set([category1.uuid, category2.uuid]), set([category.uuid for category in self.views.get_categories()]) ) # Change language self.views = CmsViews(self.mk_request(locale_name='swa_KE')) cat1, cat2 = self.views.get_categories() self.assertEqual( set([cat1.language, cat2.language]), set(['swa_KE', 'swa_KE'])) def test_categories_ordering(self): category1 = Category( {'title': 'title 1', 'language': 'eng_GB', 'position': 3}) category2 = Category( {'title': 'title 2', 'language': 'eng_GB', 'position': 0}) category3 = Category( {'title': 'title 3', 'language': 'eng_GB', 'position': 1}) category4 = Category( {'title': 'title 4', 'language': 'eng_GB', 'position': 2}) self.workspace.save(category1, 'Update position') self.workspace.save(category2, 'Update position') self.workspace.save(category3, 'Update position') self.workspace.save(category4, 'Update position') self.workspace.refresh_index() cat1, cat2, cat3, cat4 = self.views.get_categories() self.assertEqual(cat1.uuid, category2.uuid) self.assertEqual(cat2.uuid, category3.uuid) self.assertEqual(cat3.uuid, category4.uuid) self.assertEqual(cat4.uuid, category1.uuid) def test_get_category(self): request = self.mk_request(locale_name='swa_KE') views = CmsViews(request) for does_not_exist in (None, 'abcd'): self.assertIs(views.get_category(does_not_exist), None) def test_pagination_first_page(self): [category] = self.create_categories( self.workspace, count=1, language='eng_GB') self.create_pages(self.workspace, count=15, content='baby', primary_category=category.uuid) resp = self.app.get( '/content/list/%s/' % category.uuid, params={'p': '0'}, status=200) self.assertFalse('Previous' in resp.body) self.assertTrue('Next' in resp.body) def test_pagination_last_page(self): [category] = self.create_categories( self.workspace, count=1, language='eng_GB') self.create_pages(self.workspace, count=30, content='baby', primary_category=category.uuid) resp = self.app.get( '/content/list/%s/' % category.uuid, params={'p': '3'}, status=200) self.assertTrue('Previous' in resp.body) self.assertFalse('Next' in resp.body) def test_pagination_middle_page(self): [category] = self.create_categories( self.workspace, count=1, language='eng_GB') self.create_pages(self.workspace, count=40, content='baby', primary_category=category.uuid) resp = self.app.get( '/content/list/%s/' % category.uuid, params={'p': '2'}, status=200) self.assertTrue('Previous' in resp.body) self.assertTrue('Next' in resp.body) def test_pagination_results_per_page_configurable(self): settings = self.config.registry.settings.copy() settings["results_per_page"] = '5' app = self.mk_app(self.workspace, settings=settings) [category] = self.create_categories( self.workspace, count=1, language='eng_GB') self.create_pages(self.workspace, count=8, content='baby', primary_category=category.uuid) resp = app.get( '/content/list/%s/' % category.uuid, params={'p': '0'}, status=200) self.assertTrue('Previous' not in resp.body) self.assertTrue('Next' in resp.body) def test_pagination_results_per_page_configurable_last_page(self): settings = self.config.registry.settings.copy() settings["results_per_page"] = '5' app = self.mk_app(self.workspace, settings=settings) [category] = self.create_categories( self.workspace, count=1, language='eng_GB') self.create_pages(self.workspace, count=8, content='baby', primary_category=category.uuid) resp = app.get( '/content/list/%s/' % category.uuid, params={'p': '5'}, status=200) self.assertTrue('Previous' in resp.body) self.assertTrue('Next' not in resp.body) # check that we're on page 2 self.assertTrue('<b>2</b>' in resp.body) def test_category_view(self): [category] = self.create_categories( self.workspace, count=1, language='swa_KE') [page] = self.create_pages( self.workspace, count=1, language='swa_KE', primary_category=category.uuid) request = self.mk_request( matchdict={'category': category.uuid}, locale_name='swa_KE') views = CmsViews(request) response = views.category() self.assertEqual(response['category'].uuid, category.uuid) self.assertEqual( [p.uuid for p in response['pages']], [page.uuid]) def test_category_view_does_not_exist(self): request = self.mk_request(locale_name='swa_KE') views = CmsViews(request) for does_not_exist in (None, 'abcd'): request.matchdict['category'] = does_not_exist self.assertRaises(HTTPNotFound, views.category) def test_pages_ordering(self): [category] = self.create_categories(self.workspace, count=1) page1 = Page({ 'title': 'title 1', 'language': 'eng_GB', 'position': 3, 'primary_category': category.uuid}) page2 = Page({ 'title': 'title 2', 'language': 'eng_GB', 'position': 0, 'primary_category': category.uuid}) page3 = Page({ 'title': 'title 3', 'language': 'eng_GB', 'position': 1, 'primary_category': category.uuid}) page4 = Page({ 'title': 'title 4', 'language': 'eng_GB', 'position': 2, 'primary_category': category.uuid}) self.workspace.save(page1, 'Update position') self.workspace.save(page2, 'Update position') self.workspace.save(page3, 'Update position') self.workspace.save(page4, 'Update position') self.workspace.refresh_index() request = self.mk_request(matchdict={'category': category.uuid}) views = CmsViews(request) cat = views.category() p1, p2, p3, p4 = cat['pages'] self.assertEqual(p1.uuid, page2.uuid) self.assertEqual(p2.uuid, page3.uuid) self.assertEqual(p3.uuid, page4.uuid) self.assertEqual(p4.uuid, page1.uuid) def test_get_top_nav(self): category1, category2 = self.create_categories(self.workspace) category3, category4 = self.create_categories( self.workspace, language='swa_KE', featured_in_navbar=True) self.assertEqual([], list(self.views.get_top_nav)) # Change language self.views = CmsViews(self.mk_request(locale_name='swa_KE')) cat1, cat2 = self.views.get_top_nav self.assertEqual( set([cat1.language, cat2.language]), set(['swa_KE', 'swa_KE'])) def test_format_date_helper(self): views = CmsViews(self.mk_request()) self.assertEqual( views.format_date('2014-10-10T09:10:17+00:00'), '10 October 2014') self.assertEqual( views.format_date('2014-10-10T09:10:17+00:00', '%d-%b-%y'), '10-Oct-14') self.assertEqual( views.format_date('some invalid date'), 'some invalid date') dt = datetime(year=2014, month=10, day=10, hour=9, minute=10, second=17, tzinfo=pytz.utc) self.assertEqual( views.format_date(dt), '10 October 2014') def test_get_flatpage_using_old_swahili_code(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages( self.workspace, count=1, content='Sample page in swahili', description='_emphasised_', language='swa_KE') request = self.mk_request( {'_LOCALE_': 'swh_KE'}, matchdict={'slug': page.slug}) self.views = CmsViews(request) response = self.views.flatpage() self.assertEqual( response['content'], '<p>Sample page in swahili</p>') def test_get_flatpage_using_old_english_code(self): [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages( self.workspace, count=1, content='Sample page in english', description='_emphasised_', language='eng_GB') request = self.mk_request( {'_LOCALE_': 'eng_UK'}, matchdict={'slug': page.slug}) self.views = CmsViews(request) response = self.views.flatpage() self.assertEqual( response['content'], '<p>Sample page in english</p>') def test_image_url(self): self.views = CmsViews(self.mk_request()) self.assertEqual(self.views.get_image_url( 'http://some.site.com', 'sample-uuid-000000-0001'), 'http://some.site.com/' '1bzRPrcuQPXBECF9mHxFVr11viY=/sample-uuid-000000-0001') self.assertEqual(self.views.get_image_url( 'http://some.site.com', 'sample-uuid-000000-0001', 300, 200), 'http://some.site.com/' '8Ko7ZiKCwOv8zDovqScWL5Lgrc8=/300x200/sample-uuid-000000-0001') self.assertEqual(self.views.get_image_url( 'http://some.site.com', 'sample-uuid-000000-0001', 300), 'http://some.site.com/' 'LUyVe1umwB1caELC5m3LWu1HxvI=/300x0/sample-uuid-000000-0001') self.assertEqual(self.views.get_image_url( 'http://some.site.com', 'sample-uuid-000000-0001', height=150), 'http://some.site.com/' '4kS9gT_mYqVhnheDCuQhsahI_dU=/0x150/sample-uuid-000000-0001') self.assertEqual(self.views.get_image_url('', ''), '') def test_localisation(self): loc = Localisation({ 'locale': 'eng_GB', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/'}) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request({'_LOCALE_': 'eng_GB'}) self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'eng_GB') self.assertEqual(localisation.image, 'sample-uuid-000000-0001') self.assertEqual(localisation.image_host, 'http://some.site.com/') # Test fallbacks request = self.mk_request({'_LOCALE_': 'eng_UK'}) self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'eng_GB') self.assertEqual(localisation.image, 'sample-uuid-000000-0001') self.assertEqual(localisation.image_host, 'http://some.site.com/') def test_localisation_not_found(self): loc = Localisation({ 'locale': 'eng_GB', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/'}) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request({'_LOCALE_': 'spa_ES'}) self.views = CmsViews(request) self.assertIsNone(self.views.get_localisation()) def test_localised_logo(self): self.create_localisation( self.workspace, locale='eng_GB', logo_text='logo_text_foo', logo_description='logo_description_foo', logo_image='sample-uuid-000000-0002', logo_image_host='http://some.site.com/') def render_logo(locale, default_src=None): request = self.mk_request(locale_name=locale) self.views = CmsViews(request) if default_src: define = 'tal:define="img_attrs view.get_logo_attributes' \ '(default_image_src=\'%s\')"' % default_src else: define = '' template = '<div metal:use-macro="view.logo_template" %s></div>' template = PageTemplate(template % define) return template.render(view=self.views, request=request) localised_logo = render_logo('eng_GB') non_localised_logo = render_logo('spa_ES', '/default/logo.png') no_logo = render_logo('spa_ES') self.assertTrue('http://some.site.com/' in localised_logo) self.assertTrue('sample-uuid-000000-0002' in localised_logo) self.assertTrue('logo_description_foo' in localised_logo) self.assertFalse('logo-container' in no_logo) self.assertTrue('/default/logo.png' in non_localised_logo) def test_locale_cookie(self): [category1] = self.create_categories( self.workspace, count=1, locale='eng_GB', title='English Category') [category2] = self.create_categories( self.workspace, count=1, locale='spa_ES', title='Spanish Category') self.app.get('/locale/?language=eng_GB', status=302) resp = self.app.get('/', status=200) self.assertTrue('English Category' in resp.body) self.assertFalse('Spanish Category' in resp.body) self.app.get('/locale/?language=spa_ES', status=302) resp = self.app.get('/', status=200) self.assertTrue('Spanish Category' in resp.body) self.assertFalse('English Category' in resp.body) self.app.get('/locale/eng_GB/', status=302) resp = self.app.get('/', status=200) self.assertTrue('English Category' in resp.body) self.assertFalse('Spanish Category' in resp.body) self.app.get('/locale/spa_ES/', status=302) resp = self.app.get('/', status=200) self.assertTrue('Spanish Category' in resp.body) self.assertFalse('English Category' in resp.body) def test_locales_displayed(self): langs = self.views.get_display_languages() self.assertEqual( langs, [('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')]) request = self.mk_request(locale_name='fre_FR') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual( langs, [('fre_FR', u'fran\xe7ais'), ('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')]) request = self.mk_request(locale_name='spa_ES') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual( langs, [('spa_ES', u'espa\xf1ol'), ('eng_GB', 'English')]) def test_unsupported_locales(self): request = self.mk_request(locale_name='grn_PY') self.views = CmsViews(request) langs = self.views.get_display_languages() self.assertEqual( langs, [('grn_PY', u'Guarani'), ('eng_GB', 'English'), ('spa_ES', u'espa\xf1ol')]) def test_change_locale_page(self): resp = self.app.get('/locale/change/') self.assertTrue( u'<a href="/locale/spa_ES/">espa\xf1ol</a>' in resp.body.decode('utf-8')) self.assertTrue( u'<a href="/locale/eng_GB/">English</a>' in resp.body.decode('utf-8')) self.assertTrue( u'<a href="/locale/swa_KE/">Kiswahili</a>' in resp.body.decode('utf-8')) self.assertTrue( u'<a href="/locale/per_IR/">\u0641\u0627\u0631\u0633\u06cc</a>' in resp.body.decode('utf-8')) def test_404_page(self): resp = self.app.get('/;jsdafjahs;dfjas;', expect_errors=True) self.assertTrue('class="page-not-found"'in resp.body) self.assertEqual(resp.status_int, 404) def test_health(self): resp = self.app.get('/health/', status=200) data = json.loads(resp.body) self.assertEqual(data, { 'version': None, 'id': None, }) def test_health_with_env_vars(self): os.environ['MARATHON_APP_ID'] = 'the-app-id' os.environ['MARATHON_APP_VERSION'] = 'the-app-version' resp = self.app.get('/health/', status=200) data = json.loads(resp.body) self.assertEqual(data, { 'version': 'the-app-version', 'id': 'the-app-id', }) os.environ.pop('MARATHON_APP_ID') os.environ.pop('MARATHON_APP_VERSION')
class TestDirectionality(UnicoreTestCase): def setUp(self): self.workspace = self.mk_workspace() self.workspace.setup_custom_mapping( Page, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping( Category, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping( Localisation, { 'properties': { 'locale': { 'type': 'string', 'index': 'not_analyzed', } } }) settings = self.get_settings(self.workspace) self.config = testing.setUp(settings=settings) set_cache_regions_from_settings(settings) self.config.set_locale_negotiator(locale_negotiator_with_fallbacks) self.views = CmsViews(self.mk_request()) self.app = self.mk_app(self.workspace, settings=settings) def test_ltr(self): [category1] = self.create_categories(self.workspace, count=1, locale='eng_GB', title='English Category') resp = self.app.get('/', status=200) self.assertTrue('dir="ltr"' in resp.body) def test_rtl(self): loc = Localisation({ 'locale': 'urd_IN', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/' }) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request(locale_name='urd_IN') self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'urd_IN')
def test_get_category(self): request = self.mk_request(locale_name='swa_KE') views = CmsViews(request) for does_not_exist in (None, 'abcd'): self.assertIs(views.get_category(does_not_exist), None)
class TestCommentViews(UnicoreTestCase): def setUp(self): self.workspace = self.mk_workspace() settings = self.get_settings( self.workspace, **{'unicorecomments.host': 'http://localhost/commentservice'}) self.config = testing.setUp(settings=settings) self.config.include('cms') set_cache_regions_from_settings(settings) self.views = CmsViews(self.mk_request()) self.app_id = settings['unicorehub.app_id'] self.app = self.mk_app(self.workspace, settings=settings) [category] = self.create_categories(self.workspace, count=1) [page] = self.create_pages(self.workspace, count=1, description='description', primary_category=category.uuid) self.page = page self.stream = self.mk_comment_stream(page.uuid, state='open', offset=10, limit=5) self.patch_lazy_comment_page = mock.patch.object( LazyCommentPage, 'data', new=mock.PropertyMock(return_value=self.stream)) self.patch_lazy_comment_page.start() def tearDown(self): testing.tearDown() self.patch_lazy_comment_page.stop() def mk_comment(self, content_uuid, **fields): data = { 'uuid': uuid.uuid4().hex, 'user_uuid': uuid.uuid4().hex, 'content_uuid': content_uuid, 'app_uuid': self.app_id, 'comment': 'this is a comment', 'user_name': 'foo', 'submit_datetime': datetime.now(pytz.utc).isoformat(), 'content_type': 'page', 'content_title': 'I Am A Page', 'content_url': 'http://example.com/page/', 'locale': 'eng_ZA', 'flag_count': '0', 'is_removed': 'False', 'moderation_state': 'visible', 'ip_address': '192.168.1.1' } data.update(fields) return data def mk_comment_stream(self, content_uuid, limit=10, offset=0, state='open', total=100, **comment_fields): return { 'start': offset, 'end': offset + limit, 'total': total, 'count': limit, 'objects': [ self.mk_comment(content_uuid, **comment_fields) for i in range(limit) ], 'metadata': { 'state': state } } @mock.patch.object(LazyCommentPage, '__init__') def test_get_comments_for_content(self, mock_init): content_uuid = 'content_uuid' mock_init.return_value = None page_obj = self.views.get_comments_for_content(content_uuid) self.assertIsInstance(page_obj, LazyCommentPage) mock_init.assert_called_with(self.config.registry.commentclient, content_uuid=content_uuid, app_uuid=self.app_id, limit=COMMENTS_PER_PAGE) self.views.request.GET['c_after'] = 'after_uuid' page_obj = self.views.get_comments_for_content(content_uuid, limit=1) self.assertIsInstance(page_obj, LazyCommentPage) mock_init.assert_called_with(self.config.registry.commentclient, content_uuid=content_uuid, app_uuid=self.app_id, limit=1, after='after_uuid') del self.views.request.GET['c_after'] self.views.request.GET['c_before'] = 'before_uuid' page_obj = self.views.get_comments_for_content(content_uuid) self.assertIsInstance(page_obj, LazyCommentPage) mock_init.assert_called_with(self.config.registry.commentclient, content_uuid=content_uuid, app_uuid=self.app_id, limit=COMMENTS_PER_PAGE, before='before_uuid') self.views.request.registry.commentclient = None page_obj = self.views.get_comments_for_content(content_uuid) self.assertIs(page_obj, None) def check_comment_list(self, root): uuid = self.stream['objects'][0]['content_uuid'] # header header = root.select('.comments-header')[0] self.assertIn('%d comments' % self.stream['total'], header.string) # pagination prev, nxt = root.select('.comments-pagination > a') self.assertIn('Previous', prev.string) self.assertIn('/content/comments/%s/?c_after' % uuid, prev['href']) self.assertIn('Next', nxt.string) self.assertIn('/content/comments/%s/?c_before' % uuid, nxt['href']) # comments li = root.select('.comment-list > .comment') self.assertEqual(len(li), len(self.stream['objects'])) comment_obj = self.stream['objects'][0] comment_el = li[0] self.assertTrue(comment_el.select('.comment-author')) self.assertEqual( comment_el.select('.comment-author')[0].string, comment_obj['user_name']) self.assertTrue(comment_el.select('.comment-meta > .date')) self.assertTrue(comment_el.select('.comment-meta > .date')[0].string) self.assertTrue(comment_el.select('p')) self.assertEqual( comment_el.select('p')[0].string, comment_obj['comment']) def check_comment_flag_logged_in(self, root): comment_el = root.select('.comment-list > .comment')[0] comment_obj = self.stream['objects'][0] flag = comment_el.select('.comment-actions > .comment-flag > a') self.assertTrue(flag) self.assertIn('Report', flag[0].string) self.assertIn('/comments/flag/%s/' % comment_obj['uuid'], flag[0]['href']) def check_comment_flag_logged_out(self, root): comment_el = root.select('.comment-list > .comment')[0] flag = comment_el.select('.comment-actions > .comment-flag > a') self.assertFalse(flag) def check_comment_form(self, root): uuid = self.stream['objects'][0]['content_uuid'] form = root.select('#add-comment > form') self.assertTrue(form) form = form[0] self.assertIn('/content/comments/%s/' % uuid, form['action']) self.assertEqual(len(form.select('input')), 5) self.assertEqual(len(form.select('textarea')), 1) def test_comments_404(self): response = self.app.get('/content/comments/%s/' % uuid.uuid4().hex, expect_errors=True) self.assertEqual(response.status_int, 404) response = self.app.get('/content/comments/%s/?_LOCALE_=swa_KE' % self.page.uuid, expect_errors=True) self.assertEqual(response.status_int, 404) def test_comments_open_stream_logged_out(self): response = self.app.get('/content/comments/%s/' % self.page.uuid) self.check_comment_list(response.html) not_signed_in = response.html.select('#add-comment > p') self.assertTrue(not_signed_in) self.assertIn('You have to be signed in to add a comment', not_signed_in[0].text) self.assertFalse(response.html.select('#add-comment > form')) self.check_comment_flag_logged_out(response.html) def test_comments_open_stream_logged_in(self): response = self.app.get('/content/comments/%s/' % self.page.uuid, headers=self.mk_session()[1]) self.check_comment_form(response.html) self.check_comment_flag_logged_in(response.html) def test_comments_closed_stream(self): self.stream['metadata']['state'] = 'closed' response = self.app.get('/content/comments/%s/' % self.page.uuid) not_open = response.html.select('#add-comment > p') self.assertTrue(not_open) self.assertIn('Commenting has been closed', not_open[0].string) self.assertFalse(response.html.select('#add-comment > form')) def test_comments_disabled_stream(self): for meta in ({'state': 'disabled'}, {}): self.stream['metadata'] = meta response = self.app.get('/content/comments/%s/' % self.page.uuid) for selector in ('#add-comment *', '.comment-list', '.comments-header', '.comments-pagination'): self.assertFalse(response.html.select(selector)) def test_content_with_comments(self): # NOTE: Chameleon templates don't have commenting nor # will commenting be added to them response = self.app.get('/spice/content/detail/%s/' % self.page.uuid, headers=self.mk_session()[1]) self.check_comment_list(response.html) self.check_comment_form(response.html) def test_post_comment(self): mock_create_comment = mock.Mock() patch_client = mock.patch.object(self.app.app.registry.commentclient, 'create_comment', new=mock_create_comment) patch_client.start() self.stream['objects'] = [] self.stream['start'] = None self.stream['end'] = None self.stream['total'] = 0 session_cookie = self.mk_session()[1] response = self.app.get('/content/comments/%s/' % self.page.uuid, headers=session_cookie) form = response.forms[0] form['comment'] = 'foo bar' # no issues response = form.submit('submit', headers=session_cookie) self.assertEqual(response.status_int, 302) self.assertEqual( response.location, 'http://localhost/content/comments/%s/' % self.page.uuid) self.assertGreaterEqual(mock_create_comment.call_count, 1) data = mock_create_comment.call_args[0][0] self.assertEqual(data.get('comment'), 'foo bar') # missing field form['comment'] = '' response = form.submit('submit', headers=session_cookie) self.assertEqual(response.status_int, 200) self.assertIn('Required', response.html.select('#add-comment > form')[0].text) # banned user form['comment'] = 'foo bar' mock_create_comment.side_effect = UserBanned(mock.MagicMock()) response = form.submit('submit', headers=session_cookie) self.assertEqual(response.status_int, 200) self.assertIn('You have been banned from commenting', response.html.select('#add-comment > form')[0].text) # closed stream mock_create_comment.side_effect = CommentStreamNotOpen( mock.MagicMock()) response = form.submit('submit', headers=session_cookie) self.assertEqual(response.status_int, 302) # ValueError (CSRF failure, no authenticated user, etc) mock_create_comment.side_effect = ValueError response = form.submit('submit', headers=session_cookie, expect_errors=True) self.assertEqual(response.status_int, 400) def test_removed_comments(self): for i, mod_state in enumerate( ('removed_by_moderator', 'removed_by_community', 'removed_for_profanity')): self.stream['objects'][i]['is_removed'] = 'True' self.stream['objects'][i]['moderation_state'] = mod_state response = self.app.get('/content/comments/%s/' % self.page.uuid) comments = response.html.select('.comment-list > .comment') for i, mod_msg in enumerate(('by a moderator', 'by the community', 'because it contains profanity')): comment = comments[i] self.assertIn('This comment has been removed %s.' % mod_msg, comment.select('p')[0].string) self.assertFalse(comment.select('.comment-meta')) self.assertFalse(comment.select('.comment-actions')) def test_flag_comment_logged_in(self): mock_create_flag = mock.Mock(return_value=True) patch_client = mock.patch.object(self.app.app.registry.commentclient, 'create_flag', new=mock_create_flag) patch_client.start() comment_obj = self.stream['objects'][0] session_cookie = self.mk_session()[1] def check_response(referer, referer_is_next): mock_create_flag.reset_mock() headers = session_cookie.copy() if referer is not None: headers['Referer'] = referer response = self.app.get('/comments/flag/%s/' % comment_obj['uuid'], headers=headers) self.assertEqual(response.status_int, 302) if referer_is_next: self.assertEqual( 'http://localhost/comments/flag/%s/success/?%s' % (comment_obj['uuid'], urlencode({'next': referer})), response.location) else: self.assertEqual( 'http://localhost/comments/flag/%s/success/' % comment_obj['uuid'], response.location) self.assertGreaterEqual(mock_create_flag.call_count, 1) flag_data = mock_create_flag.call_args[0][0] self.assertIsInstance(flag_data.get('submit_datetime'), basestring) self.assertIsInstance(flag_data.get('user_uuid'), basestring) self.assertEqual(flag_data.get('app_uuid'), self.app_id) self.assertEqual(flag_data.get('comment_uuid'), comment_obj['uuid']) check_response(referer='http://localhost/content/comments/%s/' % self.page.uuid, referer_is_next=True) check_response(referer='http://external.domain.com', referer_is_next=False) check_response(referer=None, referer_is_next=False) # 404 mock_create_flag.side_effect = CommentServiceException( mock.MagicMock(status_code=404)) response = self.app.get('/comments/flag/commentuuid/', headers=session_cookie, expect_errors=True) self.assertEqual(response.status_int, 404) patch_client.stop() def test_flag_comment_logged_out(self): mock_create_flag = mock.Mock(return_value=True) patch_client = mock.patch.object(self.app.app.registry.commentclient, 'create_flag', new=mock_create_flag) patch_client.start() comment_obj = self.stream['objects'][0] response = self.app.get('/comments/flag/%s/' % comment_obj['uuid'], expect_errors=True) mock_create_flag.assert_not_called() self.assertEqual(response.status_int, 404) patch_client.stop() def test_flag_comment_success(self): comment_obj = self.stream['objects'][0] session_cookie = self.mk_session()[1] def check_response(next_url, valid_next): qs = '?%s' % urlencode({'next': next_url}) if next_url else '' response = self.app.get('/comments/flag/%s/success/%s' % (comment_obj['uuid'], qs), headers=session_cookie) self.assertEqual(response.status_int, 200) content = response.html.select('.comment-flagged > p') self.assertTrue(content) self.assertIn('You have successfully reported a comment', content[0].text) go_back = content[0].select('a') if valid_next: self.assertTrue(go_back) self.assertIn('Go back', go_back[0].string) self.assertEqual(go_back[0]['href'], next_url) else: self.assertFalse(go_back) # logged in check_response(next_url='http://localhost/content/comments/%s/' % self.page.uuid, valid_next=True) check_response(next_url='http://external.domain.com', valid_next=False) check_response(next_url=None, valid_next=False) # logged out response = self.app.get('/comments/flag/%s/success/' % comment_obj['uuid'], expect_errors=True) self.assertEqual(response.status_int, 404)
class TestDirectionality(UnicoreTestCase): def setUp(self): self.workspace = self.mk_workspace() self.workspace.setup_custom_mapping(Page, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping(Category, { 'properties': { 'slug': { 'type': 'string', 'index': 'not_analyzed', }, 'language': { 'type': 'string', 'index': 'not_analyzed', } } }) self.workspace.setup_custom_mapping(Localisation, { 'properties': { 'locale': { 'type': 'string', 'index': 'not_analyzed', } } }) settings = self.get_settings(self.workspace) self.config = testing.setUp(settings=settings) set_cache_regions_from_settings(settings) self.config.set_locale_negotiator(locale_negotiator_with_fallbacks) self.views = CmsViews(self.mk_request()) self.app = self.mk_app(self.workspace, settings=settings) def test_ltr(self): [category1] = self.create_categories( self.workspace, count=1, locale='eng_GB', title='English Category') resp = self.app.get('/', status=200) self.assertTrue( 'dir="ltr"' in resp.body) def test_rtl(self): loc = Localisation({ 'locale': 'urd_IN', 'image': 'sample-uuid-000000-0001', 'image_host': 'http://some.site.com/'}) self.workspace.save(loc, 'Add localisation') self.workspace.refresh_index() request = self.mk_request(locale_name='urd_IN') self.views = CmsViews(request) localisation = self.views.get_localisation() self.assertEqual(localisation.locale, 'urd_IN')