Exemplo n.º 1
0
    def test_no_tags_url_has_no_query_string(self):
        self._create_activity_log_page()

        page = CFGOVPage(title='test')
        self.root.add_child(instance=page)

        self.assertEqual(page.generate_view_more_url(self.request),
                         '/activity-log/')
Exemplo n.º 2
0
 def test_author_with_middle_name(self):
     page = CFGOVPage()
     page.authors.add('Jess Schafer', 'Richa Something Agarwal',
                      'Sarah Simpson')
     expected_result = [
         'Richa Something Agarwal', 'Jess Schafer', 'Sarah Simpson'
     ]
     author_names = [a.name for a in page.alphabetize_authors()]
     self.assertEquals(author_names, expected_result)
Exemplo n.º 3
0
 def test_alphabetize_authors_by_last_name(self):
     page = CFGOVPage()
     page.authors.add('Ross Karchner', 'Richa Agarwal', 'Andy Chosak',
                      'Will Barton')
     expected_result = [
         'Richa Agarwal', 'Will Barton', 'Andy Chosak', 'Ross Karchner'
     ]
     author_names = [a.name for a in page.alphabetize_authors()]
     self.assertEquals(author_names, expected_result)
Exemplo n.º 4
0
    def test_tags_get_appended_as_query_string(self):
        self._create_activity_log_page()

        page = CFGOVPage(title='test')
        self.root.add_child(instance=page)
        page.tags.add('bar')
        page.tags.add('foo')

        self.assertEqual(page.generate_view_more_url(self.request),
                         '/activity-log/?topics=bar&topics=foo')
Exemplo n.º 5
0
    def test_no_tags_url_has_no_query_string(self):
        self._create_activity_log_page()

        page = CFGOVPage(title='test')
        self.root.add_child(instance=page)

        self.assertEqual(
            page.generate_view_more_url(self.request),
            '/activity-log/'
        )
Exemplo n.º 6
0
    def test_tags_get_appended_as_query_string(self):
        self._create_activity_log_page()

        page = CFGOVPage(title='test')
        self.root.add_child(instance=page)
        page.tags.add('bar')
        page.tags.add('foo')

        self.assertEqual(
            page.generate_view_more_url(self.request),
            '/activity-log/?topics=bar&topics=foo'
        )
Exemplo n.º 7
0
 def setUp(self):
     self.resource1 = Resource.objects.create(title='Test resource 1')
     self.resource1.tags.add(u'tagA')
     self.resource2 = Resource.objects.create(title='Test resource 2')
     self.resource2.tags.add(u'tagB')
     self.page1 = CFGOVPage(title='live', slug='test')
     self.page1.tags.add(u'tagC')
Exemplo n.º 8
0
    def test_forward_mapper_explicit_path_pas_link(self):
        page = CFGOVPage(
            title='Privacy Act statement',
            slug='privacy-act-statement',
        )
        save_new_page(page)

        data = {
            'text':
            u'Our email newsletter has tips and info to help you ...',
            'gd_code':
            u'USCFPB_127',
            'heading':
            u'Buying a home?',
            'form_field': [{
                'info': u'<p><a href="/privacy-act-statement/">PAS</a></p>',
                'inline_info': True,
                'required': True,
                'label': u'Email address',
                'btn_text': u'Sign up',
                'placeholder': u'*****@*****.**',
                'type': u'email'
            }],
            'default_heading':
            False
        }

        migrated = self.migration.forward_mapper('unused param', data)

        self.assertEqual(
            migrated, {
                'heading':
                u'Buying a home?',
                'default_heading':
                False,
                'text':
                u'Our email newsletter has tips and info to help you ...',
                'gd_code':
                u'USCFPB_127',
                'disclaimer_page':
                page.pk,
                'form_field': [{
                    'type': u'email',
                    'inline_info': True,
                    'btn_text': u'Sign up',
                    'info':
                    u'<p><a href="/privacy-act-statement/">PAS</a></p>',
                    'label': u'Email address',
                    'required': True,
                    'placeholder': u'*****@*****.**'
                }]
            })
Exemplo n.º 9
0
    def setUp(self):
        """
        Create a new page, a User, and a couple of mock objects that
        simulate the context and request. There are also a couple of
        side effect functions for accessing the mocks as dictionaries.
        Also an object that is not a page and doesn't do anything, but
        has a `.specific` member variable.
        """

        self.page = CFGOVPage(title='a very nice cfgov page')
        self.page.shared = False
        self.page.live = False
        self.not_a_page = mock.MagicMock()
        self.not_a_page.specific = mock.MagicMock()

        helpers.save_new_page(self.page)

        self.user = User(first_name='A', last_name='User')
        self.user.save()

        self.context = mock.MagicMock()
        self.request = mock.MagicMock()

        self.context_dict = {}

        def context_getitem(name):
            return self.context_dict[name]

        def context_setitem(name, value):
            self.context_dict[name] = value

        self.context.__getitem__.side_effect = context_getitem
        self.context.__setitem__.side_effect = context_setitem

        self.request.user = self.user
        self.site_objects = Site.objects.all()
Exemplo n.º 10
0
    def setUp(self):
        """
        Create a new page, a User, and a couple of mock objects that
        simulate the context and request. There are also a couple of
        side effect functions for accessing the mocks as dictionaries.
        Also an object that is not a page and doesn't do anything, but
        has a `.specific` member variable.
        """

        self.page = CFGOVPage(title='a very nice cfgov page')
        self.page.shared = False
        self.page.live = False
        self.not_a_page = mock.MagicMock()
        self.not_a_page.specific = mock.MagicMock()

        helpers.save_new_page(self.page)

        self.user = User(first_name='A', last_name='User')
        self.user.save()

        self.context = mock.MagicMock()
        self.request = mock.MagicMock()

        self.context_dict = {}

        def context_getitem(name):
            return self.context_dict[name]

        def context_setitem(name, value):
            self.context_dict[name] = value

        self.context.__getitem__.side_effect = context_getitem
        self.context.__setitem__.side_effect = context_setitem

        self.request.user = self.user
        self.site_objects = Site.objects.all()
Exemplo n.º 11
0
 def test_authors_get_sorted_alphabetically_by_default(self):
     with patch.object(CFGOVPage, 'alphabetize_authors') as mock:
         CFGOVPage().get_authors()
     mock.assert_called_with()
Exemplo n.º 12
0
 def test_live_and_shared(self):
     page = CFGOVPage(live=True, shared=True, has_unpublished_changes=True)
     self.assertEqual(page.status_string, 'live + shared')
Exemplo n.º 13
0
    def setUp(self):

        self.hostname = 'localhost'

        # add some authors to a CFGOV page and give it some tags

        self.author1 = 'Some Author'
        self.author2 = 'Another Person'
        self.author3 = 'A Third Author'
        self.page_with_authors = CFGOVPage(title='a cfgov page with authors')
        helpers.save_new_page(self.page_with_authors)

        self.page_with_authors.authors.add(self.author1)
        self.page_with_authors.authors.add(self.author2)
        self.page_with_authors.authors.add(self.author3)

        self.page_with_authors.tags.add('tag 1')
        self.page_with_authors.tags.add('tag 2')

        # set up parent pages for the different types of related
        # posts we can have

        self.blog_parent = CFGOVPage(slug='blog', title='blog parent')
        self.newsroom_parent = CFGOVPage(slug='newsroom', title='newsroom parent')
        self.events_parent = CFGOVPage(slug='events', title='events parent')
        self.archive_events_parent = CFGOVPage(slug='archive-past-events', title='archive past events parent')

        helpers.save_new_page(self.blog_parent)
        helpers.save_new_page(self.newsroom_parent)
        helpers.save_new_page(self.events_parent)
        helpers.save_new_page(self.archive_events_parent)

        # set up children of the parent pages and give them some tags
        # and some categories

        self.blog_child1 = AbstractFilterPage(title='blog child 1', date_published=dt.date(2016, 9, 1))
        self.blog_child1.tags.add('tag 1')
        self.blog_child1.categories.add(CFGOVPageCategory(name='info-for-consumers'))

        self.blog_child2 = AbstractFilterPage(title='blog child 2', date_published=dt.date(2016, 9, 2))
        self.blog_child2.tags.add('tag 2')
        self.blog_child2.categories.add(CFGOVPageCategory(name='policy_compliance'))

        self.newsroom_child1 = AbstractFilterPage(title='newsroom child 1', date_published=dt.date(2016, 9, 2))
        self.newsroom_child1.tags.add('tag 1')
        self.newsroom_child1.tags.add('tag 2')
        self.newsroom_child1.categories.add(CFGOVPageCategory(name='op-ed'))

        self.newsroom_child2 = AbstractFilterPage(title='newsroom child 2', date_published=dt.date(2016, 9, 3))
        self.newsroom_child2.tags.add('tag 2')
        self.newsroom_child2.categories.add(CFGOVPageCategory(name='some-other-category'))

        self.events_child1 = AbstractFilterPage(title='events child 1', date_published=dt.date(2016, 9, 7))
        self.events_child1.tags.add('tag 1')

        self.events_child2 = AbstractFilterPage(title='events child 2', date_published=dt.date(2016, 9, 5))
        self.events_child2.tags.add('tag 2')

        helpers.save_new_page(self.blog_child1, self.blog_parent)
        helpers.save_new_page(self.blog_child2, self.blog_parent)
        helpers.save_new_page(self.newsroom_child1, self.newsroom_parent)
        helpers.save_new_page(self.newsroom_child2, self.newsroom_parent)
        helpers.save_new_page(self.events_child1, self.events_parent)

        # mock a stream block that dictates how to retrieve the related posts
        # note that because of the way that the related_posts_category_lookup function
        # works i.e. by consulting a hard-coded object, the specific_categories
        # slot of the dict has to be something that it can actually find.

        self.block = mock.MagicMock()
        self.block.block_type = 'related_posts'
        self.block.value = dict({
            'limit': 3,
            'show_heading': True,
            'header_title': 'Further reading',
            'relate_posts': False,
            'relate_newsroom': False,
            'relate_events': False,
            'specific_categories': []
        })
Exemplo n.º 14
0
 def test_same_last_names(self):
     page = CFGOVPage()
     page.authors.add('Mary Smith', 'Vic Kumar', 'John Smith')
     expected_result = ['Vic Kumar', 'John Smith', 'Mary Smith']
     author_names = [a.name for a in page.alphabetize_authors()]
     self.assertEquals(author_names, expected_result)
Exemplo n.º 15
0
 def test_no_authors(self):
     page = CFGOVPage()
     self.assertEquals(page.alphabetize_authors(), [])
Exemplo n.º 16
0
 def test_expired(self):
     page = CFGOVPage(expired=True)
     self.assertEqual(page.status_string, 'expired')
Exemplo n.º 17
0
    def test_no_activity_log_page_raises_does_not_exist(self):
        page = CFGOVPage(title='test')
        self.root.add_child(instance=page)

        with self.assertRaises(Page.DoesNotExist):
            page.generate_view_more_url(self.request)
Exemplo n.º 18
0
class TemplatetagsShareTestCase(TestCase):
    def setUp(self):
        """
        Create a new page, a User, and a couple of mock objects that
        simulate the context and request. There are also a couple of
        side effect functions for accessing the mocks as dictionaries.
        Also an object that is not a page and doesn't do anything, but
        has a `.specific` member variable.
        """

        self.page = CFGOVPage(title='a very nice cfgov page')
        self.page.shared = False
        self.page.live = False
        self.not_a_page = mock.MagicMock()
        self.not_a_page.specific = mock.MagicMock()

        helpers.save_new_page(self.page)

        self.user = User(first_name='A', last_name='User')
        self.user.save()

        self.context = mock.MagicMock()
        self.request = mock.MagicMock()

        self.context_dict = {}

        def context_getitem(name):
            return self.context_dict[name]

        def context_setitem(name, value):
            self.context_dict[name] = value

        self.context.__getitem__.side_effect = context_getitem
        self.context.__setitem__.side_effect = context_setitem

        self.request.user = self.user
        self.site_objects = Site.objects.all()

    def tearDown(self):

        self.user.delete()

    def test_is_shared(self):
        """
        Test if the `is_shared` function properly reports a page as shared.
        Something that is not an instance of the Page class should return None
        while otherwise the `.shared` property controls whether a page is
        shared or not.
        """

        result = share.is_shared(self.not_a_page)

        self.assertIsNone(result)

        self.page.shared = True
        self.page.save()
        result = share.is_shared(self.page)

        self.assertTrue(result)

        self.page.shared = False
        self.page.save()
        result = share.is_shared(self.page)

        self.assertFalse(result)

    def test_get_page_state_url_non_live_non_shared(self):
        """
        If a page is neither live nor shared, the url returned
        should be None.
        """

        self.page.live = False
        self.page.shared = False
        self.page.save()

        result = share.get_page_state_url(None, self.page)
        self.assertIsNone(result)

    def test_get_page_state_url_staged(self):
        """
        If the page is shared and staged, return the url with the hostname
        replaced by the hostname of the staging server. Here we check against
        the replaced version of the page.url because the final url of the
        saved page can vary depending on the order in which objects are saved,
        so there's no hard-coded "ground truth" to compare to.
        """
        self.page.live = False
        self.page.shared = True
        self.page.save()

        result = share.get_page_state_url(None, self.page)
        self.assertEqual(
            result,
            self.page.url.replace('http://localhost',
                                  'http://content.localhost'))

    def test_get_page_state_url_live(self):
        """
        If the page is live and shared, and attached to the host root, but not
        staged, then the result url should be just the same as the page.url.
        """
        self.page.live = True
        self.page.shared = False
        self.page.save()

        result = share.get_page_state_url(None, self.page)
        self.assertEqual(result, self.page.url)

    def test_v1page_permissions_no_permissions_in_context(self):
        """
        If the key 'user_page_permissions' is not present in the context dictionary,
        use the CFGOVUserPagePermissionsProxy object for the user to retrieve the
        CFGOVPagePermissionTester object. This test just checks that such an object
        is returned.
        """
        self.context_dict = {'request': self.request}

        result = share.v1page_permissions(self.context, self.page)

        self.assertIsInstance(result, CFGOVPagePermissionTester)

    def test_v1page_permissions_permissions_in_context(self):
        """
        Like the above test, but this time 'user_page_permissions' is present, and
        is set by CFGOVUserPagePermissionsProxy for the user ahead of time. Checks
        to see if the CFGOVPagePermissionTester is returned.
        """

        self.context_dict = {
            'request': self.request,
            'user_page_permissions': CFGOVUserPagePermissionsProxy(self.user)
        }

        result = share.v1page_permissions(self.context, self.page)

        self.assertIsInstance(result, CFGOVPagePermissionTester)

    def test_get_page_state_url_no_url(self):
        """
        If a page is live and/or shared, but is not connected to a site root,
        return None. In order for this to work, we temporarily delete all Site
        objects but then we just restore them again.
        """

        self.page.shared = True
        self.page.live = True
        self.page.save()

        site_objects = Site.objects.all()
        for site in site_objects:
            site.delete()

        result = share.get_page_state_url(None, self.page)

        for site in site_objects:
            site.save()

        self.assertIsNone(result)
Exemplo n.º 19
0
 def test_live_with_another_live_page(self):
     page = CFGOVPage(title='test', slug='test', live=True)
     save_new_page(page)
     self.check_live_counts(on_live_host=2)
Exemplo n.º 20
0
 def setUp(self):
     self.page = CFGOVPage(title='Test', slug='test')
     self.factory = RequestFactory()
     self.request = self.factory.get('/')
Exemplo n.º 21
0
class TestCFGOVPage(TestCase):
    def setUp(self):
        self.page = CFGOVPage(title='Test', slug='test')
        self.factory = RequestFactory()
        self.request = self.factory.get('/')

    @mock.patch('__builtin__.super')
    @mock.patch('v1.models.base.hooks')
    def test_get_context_calls_get_context(self, mock_hooks, mock_super):
        self.page.get_context(self.request)
        mock_super.assert_called_with(CFGOVPage, self.page)
        mock_super().get_context.assert_called_with(self.request)

    @mock.patch('__builtin__.super')
    @mock.patch('v1.models.base.hooks')
    def test_get_context_calls_get_hooks(self, mock_hooks, mock_super):
        self.page.get_context(self.request)
        mock_hooks.get_hooks.assert_called_with('cfgovpage_context_handlers')

    @mock.patch('__builtin__.super')
    @mock.patch('v1.models.base.hooks')
    def test_get_context_calls_hook_functions(self, mock_hooks, mock_super):
        fn = mock.Mock()
        mock_hooks.get_hooks.return_value = [fn]
        self.page.get_context(self.request)
        fn.assert_called_with(
            self.page, self.request, mock_super().get_context()
        )

    @mock.patch('__builtin__.super')
    @mock.patch('v1.models.base.hooks')
    def test_get_context_returns_context(self, mock_hooks, mock_super):
        result = self.page.get_context(self.request)
        self.assertEqual(result, mock_super().get_context())

    @mock.patch('__builtin__.super')
    def test_serve_calls_super_on_non_ajax_request(self, mock_super):
        self.page.serve(self.request)
        mock_super.assert_called_with(CFGOVPage, self.page)
        mock_super().serve.assert_called_with(self.request)

    @mock.patch('__builtin__.super')
    @mock.patch('v1.models.base.CFGOVPage.serve_post')
    def test_serve_calls_serve_post_on_post_request(
            self, mock_serve_post, mock_super):
        self.request = self.factory.post('/')
        self.page.serve(self.request)
        mock_serve_post.assert_called_with(self.request)

    def test_serve_post_returns_failed_JSON_response_for_no_form_id(self):
        self.request = self.factory.post(
            '/', HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        response = self.page.serve_post(self.request)
        self.assertEqual(response.content, '{"result": "error"}')
        self.assertEqual(response.status_code, 400)

    @mock.patch('v1.models.base.HttpResponseBadRequest')
    def test_serve_post_calls_messages_and_bad_request_for_no_form_id(
            self, mock_bad_request):
        self.request = self.factory.post('/')
        self.page.serve_post(self.request)
        mock_bad_request.assert_called_with(self.page.url)

    @mock.patch('v1.models.base.HttpResponseBadRequest')
    def test_serve_post_returns_bad_request_for_no_form_id(
            self, mock_bad_request):
        self.request = self.factory.post('/')
        self.page.serve_post(self.request)
        self.assertTrue(mock_bad_request.called)

    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_gets_streamfield_from_page_using_form_id(
            self,
            mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response):
        mock_getattr.return_value = mock.MagicMock()
        mock_get_context.return_value = mock.MagicMock()
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        self.page.serve_post(self.request)
        mock_getattr.assert_called_with(self.page, 'content')

    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_calls_module_get_result(
            self,
            mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response):
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        self.page.serve_post(self.request)
        module = mock_getattr()[0]
        module.block.get_result.assert_called_with(
            self.page,
            self.request,
            module.value,
            True
        )

    @mock.patch('v1.models.base.isinstance')
    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_returns_result_if_response(
            self, mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response,
            mock_isinstance):
        mock_getattr.return_value = mock.MagicMock()
        mock_get_context.return_value = mock.MagicMock()
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        result = self.page.serve_post(self.request)
        module = mock_getattr()[0]
        self.assertEqual(result, module.block.get_result())

    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_calls_get_context(
            self,
            mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response):
        mock_getattr.return_value = mock.MagicMock()
        mock_get_context.return_value = mock.MagicMock()
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        self.page.serve_post(self.request)
        self.assertTrue(mock_get_context.called)

    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_sets_context(
            self,
            mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response):
        context = {'form_modules': {'content': {}}}
        mock_getattr.return_value = mock.MagicMock()
        mock_get_context.return_value = context
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        self.page.serve_post(self.request)
        module = mock_getattr()[0]
        self.assertIn(0, context['form_modules']['content'])
        self.assertEqual(
            module.block.get_result(), context['form_modules']['content'][0]
        )

    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_returns_template_response_if_result_not_response(
            self,
            mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response):
        mock_getattr.return_value = mock.MagicMock()
        mock_get_context.return_value = mock.MagicMock()
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        result = self.page.serve_post(self.request)
        mock_response.assert_called_with(
            self.request, mock_get_template(), mock_get_context()
        )
        self.assertEqual(result, mock_response())
Exemplo n.º 22
0
class TemplatetagsShareTestCase(TestCase):

    def setUp(self):
        """
        Create a new page, a User, and a couple of mock objects that
        simulate the context and request. There are also a couple of
        side effect functions for accessing the mocks as dictionaries.
        Also an object that is not a page and doesn't do anything, but
        has a `.specific` member variable.
        """

        self.page = CFGOVPage(title='a very nice cfgov page')
        self.page.shared = False
        self.page.live = False
        self.not_a_page = mock.MagicMock()
        self.not_a_page.specific = mock.MagicMock()

        helpers.save_new_page(self.page)

        self.user = User(first_name='A', last_name='User')
        self.user.save()

        self.context = mock.MagicMock()
        self.request = mock.MagicMock()

        self.context_dict = {}

        def context_getitem(name):
            return self.context_dict[name]

        def context_setitem(name, value):
            self.context_dict[name] = value

        self.context.__getitem__.side_effect = context_getitem
        self.context.__setitem__.side_effect = context_setitem

        self.request.user = self.user
        self.site_objects = Site.objects.all()

    def tearDown(self):

        self.user.delete()

    def test_is_shared(self):
        """
        Test if the `is_shared` function properly reports a page as shared.
        Something that is not an instance of the Page class should return None
        while otherwise the `.shared` property controls whether a page is
        shared or not.
        """

        result = share.is_shared(self.not_a_page)

        self.assertIsNone(result)

        self.page.shared = True
        self.page.save()
        result = share.is_shared(self.page)

        self.assertTrue(result)

        self.page.shared = False
        self.page.save()
        result = share.is_shared(self.page)

        self.assertFalse(result)

    def test_get_page_state_url_non_live_non_shared(self):
        """
        If a page is neither live nor shared, the url returned
        should be None.
        """

        self.page.live = False
        self.page.shared = False
        self.page.save()

        result = share.get_page_state_url(None, self.page)
        self.assertIsNone(result)

    def test_get_page_state_url_staged(self):
        """
        If the page is shared and staged, return the url with the hostname
        replaced by the hostname of the staging server. Here we check against
        the replaced version of the page.url because the final url of the
        saved page can vary depending on the order in which objects are saved,
        so there's no hard-coded "ground truth" to compare to.
        """
        self.page.live = False
        self.page.shared = True
        self.page.save()

        result = share.get_page_state_url(None, self.page)
        self.assertEqual(result, self.page.url.replace('http://localhost',
                                                       'http://content.localhost'))

    def test_get_page_state_url_live(self):
        """
        If the page is live and shared, and attached to the host root, but not
        staged, then the result url should be just the same as the page.url.
        """
        self.page.live = True
        self.page.shared = False
        self.page.save()

        result = share.get_page_state_url(None, self.page)
        self.assertEqual(result, self.page.url)

    def test_v1page_permissions_no_permissions_in_context(self):
        """
        If the key 'user_page_permissions' is not present in the context dictionary,
        use the CFGOVUserPagePermissionsProxy object for the user to retrieve the
        CFGOVPagePermissionTester object. This test just checks that such an object
        is returned.
        """
        self.context_dict = {'request': self.request}

        result = share.v1page_permissions(self.context, self.page)

        self.assertIsInstance(result, CFGOVPagePermissionTester)

    def test_v1page_permissions_permissions_in_context(self):
        """
        Like the above test, but this time 'user_page_permissions' is present, and
        is set by CFGOVUserPagePermissionsProxy for the user ahead of time. Checks
        to see if the CFGOVPagePermissionTester is returned.
        """

        self.context_dict = {'request': self.request,
                             'user_page_permissions': CFGOVUserPagePermissionsProxy(self.user)}

        result = share.v1page_permissions(self.context, self.page)

        self.assertIsInstance(result, CFGOVPagePermissionTester)

    def test_get_page_state_url_no_url(self):
        """
        If a page is live and/or shared, but is not connected to a site root,
        return None. In order for this to work, we temporarily delete all Site
        objects but then we just restore them again.
        """

        self.page.shared = True
        self.page.live = True
        self.page.save()

        site_objects = Site.objects.all()
        for site in site_objects:
            site.delete()

        result = share.get_page_state_url(None, self.page)

        for site in site_objects:
            site.save()

        self.assertIsNone(result)
Exemplo n.º 23
0
 def test_draft(self):
     page = CFGOVPage(live=False, shared=False)
     self.assertEqual(page.status_string, 'draft')
Exemplo n.º 24
0
class RelatedPostsTestCase(TestCase):

    def setUp(self):

        self.hostname = 'localhost'

        # add some authors to a CFGOV page and give it some tags

        self.author1 = 'Some Author'
        self.author2 = 'Another Person'
        self.author3 = 'A Third Author'
        self.page_with_authors = CFGOVPage(title='a cfgov page with authors')
        helpers.save_new_page(self.page_with_authors)

        self.page_with_authors.authors.add(self.author1)
        self.page_with_authors.authors.add(self.author2)
        self.page_with_authors.authors.add(self.author3)

        self.page_with_authors.tags.add('tag 1')
        self.page_with_authors.tags.add('tag 2')

        # set up parent pages for the different types of related
        # posts we can have

        self.blog_parent = CFGOVPage(slug='blog', title='blog parent')
        self.newsroom_parent = CFGOVPage(slug='newsroom', title='newsroom parent')
        self.events_parent = CFGOVPage(slug='events', title='events parent')
        self.archive_events_parent = CFGOVPage(slug='archive-past-events', title='archive past events parent')

        helpers.save_new_page(self.blog_parent)
        helpers.save_new_page(self.newsroom_parent)
        helpers.save_new_page(self.events_parent)
        helpers.save_new_page(self.archive_events_parent)

        # set up children of the parent pages and give them some tags
        # and some categories

        self.blog_child1 = AbstractFilterPage(title='blog child 1', date_published=dt.date(2016, 9, 1))
        self.blog_child1.tags.add('tag 1')
        self.blog_child1.categories.add(CFGOVPageCategory(name='info-for-consumers'))

        self.blog_child2 = AbstractFilterPage(title='blog child 2', date_published=dt.date(2016, 9, 2))
        self.blog_child2.tags.add('tag 2')
        self.blog_child2.categories.add(CFGOVPageCategory(name='policy_compliance'))

        self.newsroom_child1 = AbstractFilterPage(title='newsroom child 1', date_published=dt.date(2016, 9, 2))
        self.newsroom_child1.tags.add('tag 1')
        self.newsroom_child1.tags.add('tag 2')
        self.newsroom_child1.categories.add(CFGOVPageCategory(name='op-ed'))

        self.newsroom_child2 = AbstractFilterPage(title='newsroom child 2', date_published=dt.date(2016, 9, 3))
        self.newsroom_child2.tags.add('tag 2')
        self.newsroom_child2.categories.add(CFGOVPageCategory(name='some-other-category'))

        self.events_child1 = AbstractFilterPage(title='events child 1', date_published=dt.date(2016, 9, 7))
        self.events_child1.tags.add('tag 1')

        self.events_child2 = AbstractFilterPage(title='events child 2', date_published=dt.date(2016, 9, 5))
        self.events_child2.tags.add('tag 2')

        helpers.save_new_page(self.blog_child1, self.blog_parent)
        helpers.save_new_page(self.blog_child2, self.blog_parent)
        helpers.save_new_page(self.newsroom_child1, self.newsroom_parent)
        helpers.save_new_page(self.newsroom_child2, self.newsroom_parent)
        helpers.save_new_page(self.events_child1, self.events_parent)

        # mock a stream block that dictates how to retrieve the related posts
        # note that because of the way that the related_posts_category_lookup function
        # works i.e. by consulting a hard-coded object, the specific_categories
        # slot of the dict has to be something that it can actually find.

        self.block = mock.MagicMock()
        self.block.block_type = 'related_posts'
        self.block.value = dict({
            'limit': 3,
            'show_heading': True,
            'header_title': 'Further reading',
            'relate_posts': False,
            'relate_newsroom': False,
            'relate_events': False,
            'specific_categories': []
        })

    def tearDown(self):

        # you don't need to delete the children independently,
        # you can just delete the parent and it cascades

        self.blog_parent.delete()
        self.newsroom_parent.delete()
        self.events_parent.delete()
        self.archive_events_parent.delete()

        self.page_with_authors.delete()

    def test_related_posts_blog(self):
        """
        Tests whether related posts from the blog from the supplied specific
        categories are retrieved. We expect there to be two such posts from
        the blog because we added two such posts in the setup. There should
        be no other posts in either of the other categories.
        """

        self.block.value['relate_posts'] = True
        self.block.value['relate_newsroom'] = False
        self.block.value['relate_events'] = False
        self.block.value['specific_categories'] = ['Info for Consumers', 'Policy &amp; Compliance']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Blog', related_posts)
        self.assertEqual(len(related_posts['Blog']), 2)
        self.assertEqual(related_posts['Blog'][0], self.blog_child2)
        self.assertEqual(related_posts['Blog'][1], self.blog_child1)
        self.assertNotIn('Newsroom', related_posts)
        self.assertNotIn('Events', related_posts)

    def test_related_posts_blog_limit(self):
        """
        Tests whether related posts from the blog from the supplied specific
        categories are retrieved, subject to supplied limit. We expect there
        to be one such post from the blog, that post should be the one with
        the most recent publication date, and no other categories
        (newsroom, events) to have, any posts in them.
        """

        self.block.value['relate_posts'] = True
        self.block.value['relate_newsroom'] = False
        self.block.value['relate_events'] = False
        self.block.value['limit'] = 1
        self.block.value['specific_categories'] = ['Info for Consumers', 'Policy &amp; Compliance']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Blog', related_posts)
        self.assertEqual(len(related_posts['Blog']), 1)
        self.assertEqual(related_posts['Blog'][0], self.blog_child2)
        self.assertNotIn('Newsroom', related_posts)
        self.assertNotIn('Events', related_posts)

    def test_related_posts_newsroom(self):
        """
        Tests whether related posts from the newsroom for the supplied specific
        categories are retrieved. We expect there to be one such post from
        the newsroom; we added two newsroom children but one of them should not
        match. We also expect that no other categories (newsroom, events) have
        any posts in them.
        """

        self.block.value['relate_posts'] = False
        self.block.value['relate_newsroom'] = True
        self.block.value['relate_events'] = False
        self.block.value['specific_categories'] = ['Op-Ed']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Newsroom', related_posts)
        self.assertEqual(len(related_posts['Newsroom']), 1)
        self.assertEqual(related_posts['Newsroom'][0], self.newsroom_child1)
        self.assertNotIn('Blog', related_posts)
        self.assertNotIn('Events', related_posts)

    def test_related_posts_events(self):
        """
        Tests whether related posts from events are retrieved. Events have
        no specific categories associated with them so it doesn't matter what
        that value is set to. We expect there to be one such post from
        events because we added one child to the events parent. We also expect
        that no other categories (newsroom, blog) have any posts in them.
        """

        self.block.value['relate_posts'] = False
        self.block.value['relate_newsroom'] = False
        self.block.value['relate_events'] = True
        self.block.value['specific_categories'] = ['anything', 'can', 'be', 'here']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Events', related_posts)
        self.assertEqual(len(related_posts), 1)
        self.assertEqual(related_posts['Events'][0], self.events_child1)
        self.assertNotIn('Blog', related_posts)
        self.assertNotIn('Newsroom', related_posts)

    def test_related_posts_events_archive(self):
        """
        Tests whether related posts from archived events are retrieved.
        Events have no specific categories associated with them so it
        doesn't matter what that value is set to. Here, we save an
        archived event child, and thus we expect that we should retrieve
        both the original event child and the archive event child.
        We also expect that no other categories (newsroom, blog) have
        any posts in them.
        """
        helpers.save_new_page(self.events_child2, self.archive_events_parent)

        self.block.value['relate_posts'] = False
        self.block.value['relate_newsroom'] = False
        self.block.value['relate_events'] = True
        self.block.value['specific_categories'] = ['anything', 'can', 'be', 'here']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Events', related_posts)
        self.assertEqual(len(related_posts['Events']), 2)
        self.assertNotIn('Blog', related_posts)
        self.assertNotIn('Newsroom', related_posts)
        self.assertEqual(related_posts['Events'][0], self.events_child1)
        self.assertEqual(related_posts['Events'][1], self.events_child2)

    def test_related_posts_all(self):
        """
        We test whether all the different posts are retrieved. This is
        basically the logical AND of the blog, newsroom, and events
        tests. We expect to retrieve all the blog, newsroom, and events
        posts (two, one, and one of each, respectively).
        """

        self.block.value['relate_posts'] = True
        self.block.value['relate_newsroom'] = True
        self.block.value['relate_events'] = True
        self.block.value['specific_categories'] = ['Info for Consumers',
                                                   'Policy &amp; Compliance',
                                                   'Op-Ed']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Blog', related_posts)
        self.assertIn('Newsroom', related_posts)
        self.assertIn('Events', related_posts)

        self.assertEqual(len(related_posts['Blog']), 2)
        self.assertEqual(len(related_posts['Events']), 1)
        self.assertEqual(len(related_posts['Newsroom']), 1)

        self.assertEqual(related_posts['Blog'][0], self.blog_child2)
        self.assertEqual(related_posts['Blog'][1], self.blog_child1)
        self.assertEqual(related_posts['Events'][0], self.events_child1)
        self.assertEqual(related_posts['Newsroom'][0], self.newsroom_child1)
Exemplo n.º 25
0
 def _create_activity_log_page(self):
     activity_log = CFGOVPage(title='Activity log', slug='activity-log')
     self.root.add_child(instance=activity_log)
Exemplo n.º 26
0
class TestCFGOVPage(TestCase):
    def setUp(self):
        self.page = CFGOVPage(title='Test', slug='test')
        self.factory = RequestFactory()
        self.request = self.factory.get('/')

    @mock.patch('__builtin__.super')
    @mock.patch('v1.models.base.hooks')
    def test_get_context_calls_get_context(self, mock_hooks, mock_super):
        self.page.get_context(self.request)
        mock_super.assert_called_with(CFGOVPage, self.page)
        mock_super().get_context.assert_called_with(self.request)

    @mock.patch('__builtin__.super')
    @mock.patch('v1.models.base.hooks')
    def test_get_context_calls_get_hooks(self, mock_hooks, mock_super):
        self.page.get_context(self.request)
        mock_hooks.get_hooks.assert_called_with('cfgovpage_context_handlers')

    @mock.patch('__builtin__.super')
    @mock.patch('v1.models.base.hooks')
    def test_get_context_calls_hook_functions(self, mock_hooks, mock_super):
        fn = mock.Mock()
        mock_hooks.get_hooks.return_value = [fn]
        self.page.get_context(self.request)
        fn.assert_called_with(
            self.page, self.request, mock_super().get_context()
        )

    @mock.patch('__builtin__.super')
    @mock.patch('v1.models.base.hooks')
    def test_get_context_returns_context(self, mock_hooks, mock_super):
        result = self.page.get_context(self.request)
        self.assertEqual(result, mock_super().get_context())

    @mock.patch('__builtin__.super')
    def test_serve_calls_super_on_non_ajax_request(self, mock_super):
        self.page.serve(self.request)
        mock_super.assert_called_with(CFGOVPage, self.page)
        mock_super().serve.assert_called_with(self.request)

    @mock.patch('__builtin__.super')
    @mock.patch('v1.models.base.CFGOVPage.serve_post')
    def test_serve_calls_serve_post_on_post_request(
            self, mock_serve_post, mock_super):
        self.request = self.factory.post('/')
        self.page.serve(self.request)
        mock_serve_post.assert_called_with(self.request)

    def test_serve_post_returns_failed_JSON_response_for_no_form_id(self):
        self.request = self.factory.post(
            '/', HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        response = self.page.serve_post(self.request)
        self.assertEqual(response.content, '{"result": "error"}')
        self.assertEqual(response.status_code, 400)

    @mock.patch('v1.models.base.HttpResponseBadRequest')
    def test_serve_post_calls_messages_and_bad_request_for_no_form_id(
            self, mock_bad_request):
        self.request = self.factory.post('/')
        self.page.serve_post(self.request)
        mock_bad_request.assert_called_with(self.page.url)

    @mock.patch('v1.models.base.HttpResponseBadRequest')
    def test_serve_post_returns_bad_request_for_no_form_id(
            self, mock_bad_request):
        self.request = self.factory.post('/')
        self.page.serve_post(self.request)
        self.assertTrue(mock_bad_request.called)

    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_gets_streamfield_from_page_using_form_id(
            self,
            mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response):
        mock_getattr.return_value = mock.MagicMock()
        mock_get_context.return_value = mock.MagicMock()
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        self.page.serve_post(self.request)
        mock_getattr.assert_called_with(self.page, 'content')

    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_calls_module_get_result(
            self,
            mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response):
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        self.page.serve_post(self.request)
        module = mock_getattr()[0]
        module.block.get_result.assert_called_with(
            self.page,
            self.request,
            module.value,
            True
        )

    @mock.patch('v1.models.base.isinstance')
    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_returns_result_if_response(
            self, mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response,
            mock_isinstance):
        mock_getattr.return_value = mock.MagicMock()
        mock_get_context.return_value = mock.MagicMock()
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        result = self.page.serve_post(self.request)
        module = mock_getattr()[0]
        self.assertEqual(result, module.block.get_result())

    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_calls_get_context(
            self,
            mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response):
        mock_getattr.return_value = mock.MagicMock()
        mock_get_context.return_value = mock.MagicMock()
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        self.page.serve_post(self.request)
        self.assertTrue(mock_get_context.called)

    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_sets_context(
            self,
            mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response):
        context = {'form_modules': {'content': {}}}
        mock_getattr.return_value = mock.MagicMock()
        mock_get_context.return_value = context
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        self.page.serve_post(self.request)
        module = mock_getattr()[0]
        self.assertIn(0, context['form_modules']['content'])
        self.assertEqual(
            module.block.get_result(), context['form_modules']['content'][0]
        )

    @mock.patch('v1.models.base.TemplateResponse')
    @mock.patch('v1.models.base.CFGOVPage.get_template')
    @mock.patch('v1.models.base.CFGOVPage.get_context')
    @mock.patch('v1.models.base.getattr')
    def test_serve_post_returns_template_response_if_result_not_response(
            self,
            mock_getattr,
            mock_get_context,
            mock_get_template,
            mock_response):
        mock_getattr.return_value = mock.MagicMock()
        mock_get_context.return_value = mock.MagicMock()
        self.request = self.factory.post(
            '/',
            {'form_id': 'form-content-0'},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        result = self.page.serve_post(self.request)
        mock_response.assert_called_with(
            self.request, mock_get_template(), mock_get_context()
        )
        self.assertEqual(result, mock_response())
Exemplo n.º 27
0
 def test_live(self):
     page = CFGOVPage(live=True)
     self.assertEqual(page.status_string, 'live')
Exemplo n.º 28
0
 def test_live_shared_with_another_live_shared_page(self):
     page = CFGOVPage(title='test', slug='test', live=True, shared=True)
     publish_page(page)
     self.check_live_shared_counts(on_live_host=2, on_staging_host=2)
Exemplo n.º 29
0
 def test_alphabetize_authors_by_last_name(self):
     page = CFGOVPage()
     page.authors.add('Ross Karchner', 'Richa Agarwal', 'Andy Chosak', 'Will Barton')
     expected_result = ['Richa Agarwal', 'Will Barton', 'Andy Chosak', 'Ross Karchner']
     author_names = [a.name for a in page.alphabetize_authors()]
     self.assertEquals(author_names, expected_result)
Exemplo n.º 30
0
 def test_no_authors(self):
     page = CFGOVPage()
     self.assertEquals(page.alphabetize_authors(), [])
Exemplo n.º 31
0
 def test_author_with_middle_name(self):
     page = CFGOVPage()
     page.authors.add('Jess Schafer', 'Richa Something Agarwal', 'Sarah Simpson')
     expected_result = ['Richa Something Agarwal', 'Jess Schafer', 'Sarah Simpson']
     author_names = [a.name for a in page.alphabetize_authors()]
     self.assertEquals(author_names, expected_result)
Exemplo n.º 32
0
 def test_shared(self):
     page = CFGOVPage(live=False, shared=True)
     self.assertEqual(page.status_string, 'shared')
Exemplo n.º 33
0
class RelatedPostsTestCase(TestCase):

    def setUp(self):

        self.hostname = 'localhost'

        # add some authors to a CFGOV page and give it some tags

        self.author1 = 'Some Author'
        self.author2 = 'Another Person'
        self.author3 = 'A Third Author'
        self.page_with_authors = CFGOVPage(title='a cfgov page with authors')
        helpers.save_new_page(self.page_with_authors)

        self.page_with_authors.authors.add(self.author1)
        self.page_with_authors.authors.add(self.author2)
        self.page_with_authors.authors.add(self.author3)

        self.page_with_authors.tags.add('tag 1')
        self.page_with_authors.tags.add('tag 2')

        # set up parent pages for the different types of related
        # posts we can have

        self.blog_parent = CFGOVPage(slug='blog', title='blog parent')
        self.newsroom_parent = CFGOVPage(slug='newsroom', title='newsroom parent')
        self.events_parent = CFGOVPage(slug='events', title='events parent')
        self.archive_events_parent = CFGOVPage(slug='archive-past-events', title='archive past events parent')

        helpers.save_new_page(self.blog_parent)
        helpers.save_new_page(self.newsroom_parent)
        helpers.save_new_page(self.events_parent)
        helpers.save_new_page(self.archive_events_parent)

        # set up children of the parent pages and give them some tags
        # and some categories

        self.blog_child1 = AbstractFilterPage(title='blog child 1', date_published=dt.date(2016, 9, 1))
        self.blog_child1.tags.add('tag 1')
        self.blog_child1.categories.add(CFGOVPageCategory(name='info-for-consumers'))

        self.blog_child2 = AbstractFilterPage(title='blog child 2', date_published=dt.date(2016, 9, 2))
        self.blog_child2.tags.add('tag 2')
        self.blog_child2.categories.add(CFGOVPageCategory(name='policy_compliance'))

        self.newsroom_child1 = AbstractFilterPage(title='newsroom child 1', date_published=dt.date(2016, 9, 2))
        self.newsroom_child1.tags.add('tag 1')
        self.newsroom_child1.tags.add('tag 2')
        self.newsroom_child1.categories.add(CFGOVPageCategory(name='op-ed'))

        self.newsroom_child2 = AbstractFilterPage(title='newsroom child 2', date_published=dt.date(2016, 9, 3))
        self.newsroom_child2.tags.add('tag 2')
        self.newsroom_child2.categories.add(CFGOVPageCategory(name='some-other-category'))

        self.events_child1 = AbstractFilterPage(title='events child 1', date_published=dt.date(2016, 9, 7))
        self.events_child1.tags.add('tag 1')

        self.events_child2 = AbstractFilterPage(title='events child 2', date_published=dt.date(2016, 9, 5))
        self.events_child2.tags.add('tag 2')

        helpers.save_new_page(self.blog_child1, self.blog_parent)
        helpers.save_new_page(self.blog_child2, self.blog_parent)
        helpers.save_new_page(self.newsroom_child1, self.newsroom_parent)
        helpers.save_new_page(self.newsroom_child2, self.newsroom_parent)
        helpers.save_new_page(self.events_child1, self.events_parent)

        # mock a stream block that dictates how to retrieve the related posts
        # note that because of the way that the related_posts_category_lookup function
        # works i.e. by consulting a hard-coded object, the specific_categories
        # slot of the dict has to be something that it can actually find.

        self.block = mock.MagicMock()
        self.block.block_type = 'related_posts'
        self.block.value = dict({
            'limit': 3,
            'show_heading': True,
            'header_title': 'Further reading',
            'relate_posts': False,
            'relate_newsroom': False,
            'relate_events': False,
            'specific_categories': []
        })

    def tearDown(self):

        # you don't need to delete the children independently,
        # you can just delete the parent and it cascades

        self.blog_parent.delete()
        self.newsroom_parent.delete()
        self.events_parent.delete()
        self.archive_events_parent.delete()

        self.page_with_authors.delete()

    def test_related_posts_blog(self):
        """
        Tests whether related posts from the blog from the supplied specific
        categories are retrieved. We expect there to be two such posts from
        the blog because we added two such posts in the setup. There should
        be no other posts in either of the other categories.
        """

        self.block.value['relate_posts'] = True
        self.block.value['relate_newsroom'] = False
        self.block.value['relate_events'] = False
        self.block.value['specific_categories'] = ['Info for Consumers', 'Policy &amp; Compliance']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Blog', related_posts)
        self.assertEqual(len(related_posts['Blog']), 2)
        self.assertEqual(related_posts['Blog'][0], self.blog_child2)
        self.assertEqual(related_posts['Blog'][1], self.blog_child1)
        self.assertNotIn('Newsroom', related_posts)
        self.assertNotIn('Events', related_posts)

    def test_related_posts_blog_limit(self):
        """
        Tests whether related posts from the blog from the supplied specific
        categories are retrieved, subject to supplied limit. We expect there
        to be one such post from the blog, that post should be the one with
        the most recent publication date, and no other categories
        (newsroom, events) to have, any posts in them.
        """

        self.block.value['relate_posts'] = True
        self.block.value['relate_newsroom'] = False
        self.block.value['relate_events'] = False
        self.block.value['limit'] = 1
        self.block.value['specific_categories'] = ['Info for Consumers', 'Policy &amp; Compliance']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Blog', related_posts)
        self.assertEqual(len(related_posts['Blog']), 1)
        self.assertEqual(related_posts['Blog'][0], self.blog_child2)
        self.assertNotIn('Newsroom', related_posts)
        self.assertNotIn('Events', related_posts)

    def test_related_posts_newsroom(self):
        """
        Tests whether related posts from the newsroom for the supplied specific
        categories are retrieved. We expect there to be one such post from
        the newsroom; we added two newsroom children but one of them should not
        match. We also expect that no other categories (newsroom, events) have
        any posts in them.
        """

        self.block.value['relate_posts'] = False
        self.block.value['relate_newsroom'] = True
        self.block.value['relate_events'] = False
        self.block.value['specific_categories'] = ['Op-Ed']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Newsroom', related_posts)
        self.assertEqual(len(related_posts['Newsroom']), 1)
        self.assertEqual(related_posts['Newsroom'][0], self.newsroom_child1)
        self.assertNotIn('Blog', related_posts)
        self.assertNotIn('Events', related_posts)

    def test_related_posts_events(self):
        """
        Tests whether related posts from events are retrieved. Events have
        no specific categories associated with them so it doesn't matter what
        that value is set to. We expect there to be one such post from
        events because we added one child to the events parent. We also expect
        that no other categories (newsroom, blog) have any posts in them.
        """

        self.block.value['relate_posts'] = False
        self.block.value['relate_newsroom'] = False
        self.block.value['relate_events'] = True
        self.block.value['specific_categories'] = ['anything', 'can', 'be', 'here']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Events', related_posts)
        self.assertEqual(len(related_posts), 1)
        self.assertEqual(related_posts['Events'][0], self.events_child1)
        self.assertNotIn('Blog', related_posts)
        self.assertNotIn('Newsroom', related_posts)

    def test_related_posts_events_archive(self):
        """
        Tests whether related posts from archived events are retrieved.
        Events have no specific categories associated with them so it
        doesn't matter what that value is set to. Here, we save an
        archived event child, and thus we expect that we should retrieve
        both the original event child and the archive event child.
        We also expect that no other categories (newsroom, blog) have
        any posts in them.
        """
        helpers.save_new_page(self.events_child2, self.archive_events_parent)

        self.block.value['relate_posts'] = False
        self.block.value['relate_newsroom'] = False
        self.block.value['relate_events'] = True
        self.block.value['specific_categories'] = ['anything', 'can', 'be', 'here']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Events', related_posts)
        self.assertEqual(len(related_posts['Events']), 2)
        self.assertNotIn('Blog', related_posts)
        self.assertNotIn('Newsroom', related_posts)
        self.assertEqual(related_posts['Events'][0], self.events_child1)
        self.assertEqual(related_posts['Events'][1], self.events_child2)

    def test_related_posts_all(self):
        """
        We test whether all the different posts are retrieved. This is
        basically the logical AND of the blog, newsroom, and events
        tests. We expect to retrieve all the blog, newsroom, and events
        posts (two, one, and one of each, respectively).
        """

        self.block.value['relate_posts'] = True
        self.block.value['relate_newsroom'] = True
        self.block.value['relate_events'] = True
        self.block.value['specific_categories'] = ['Info for Consumers',
                                                   'Policy &amp; Compliance',
                                                   'Op-Ed']

        related_posts = self.page_with_authors.related_posts(self.block, self.hostname)

        self.assertIn('Blog', related_posts)
        self.assertIn('Newsroom', related_posts)
        self.assertIn('Events', related_posts)

        self.assertEqual(len(related_posts['Blog']), 2)
        self.assertEqual(len(related_posts['Events']), 1)
        self.assertEqual(len(related_posts['Newsroom']), 1)

        self.assertEqual(related_posts['Blog'][0], self.blog_child2)
        self.assertEqual(related_posts['Blog'][1], self.blog_child1)
        self.assertEqual(related_posts['Events'][0], self.events_child1)
        self.assertEqual(related_posts['Newsroom'][0], self.newsroom_child1)
Exemplo n.º 34
0
 def test_same_last_names(self):
     page = CFGOVPage()
     page.authors.add('Mary Smith', 'Vic Kumar', 'John Smith')
     expected_result = ['Vic Kumar', 'John Smith', 'Mary Smith']
     author_names = [a.name for a in page.alphabetize_authors()]
     self.assertEquals(author_names, expected_result)
Exemplo n.º 35
0
    def setUp(self):

        self.hostname = 'localhost'

        # add some authors to a CFGOV page and give it some tags

        self.author1 = 'Some Author'
        self.author2 = 'Another Person'
        self.author3 = 'A Third Author'
        self.page_with_authors = CFGOVPage(title='a cfgov page with authors')
        helpers.save_new_page(self.page_with_authors)

        self.page_with_authors.authors.add(self.author1)
        self.page_with_authors.authors.add(self.author2)
        self.page_with_authors.authors.add(self.author3)

        self.page_with_authors.tags.add('tag 1')
        self.page_with_authors.tags.add('tag 2')

        # set up parent pages for the different types of related
        # posts we can have

        self.blog_parent = CFGOVPage(slug='blog', title='blog parent')
        self.newsroom_parent = CFGOVPage(slug='newsroom', title='newsroom parent')
        self.events_parent = CFGOVPage(slug='events', title='events parent')
        self.archive_events_parent = CFGOVPage(slug='archive-past-events', title='archive past events parent')

        helpers.save_new_page(self.blog_parent)
        helpers.save_new_page(self.newsroom_parent)
        helpers.save_new_page(self.events_parent)
        helpers.save_new_page(self.archive_events_parent)

        # set up children of the parent pages and give them some tags
        # and some categories

        self.blog_child1 = AbstractFilterPage(title='blog child 1', date_published=dt.date(2016, 9, 1))
        self.blog_child1.tags.add('tag 1')
        self.blog_child1.categories.add(CFGOVPageCategory(name='info-for-consumers'))

        self.blog_child2 = AbstractFilterPage(title='blog child 2', date_published=dt.date(2016, 9, 2))
        self.blog_child2.tags.add('tag 2')
        self.blog_child2.categories.add(CFGOVPageCategory(name='policy_compliance'))

        self.newsroom_child1 = AbstractFilterPage(title='newsroom child 1', date_published=dt.date(2016, 9, 2))
        self.newsroom_child1.tags.add('tag 1')
        self.newsroom_child1.tags.add('tag 2')
        self.newsroom_child1.categories.add(CFGOVPageCategory(name='op-ed'))

        self.newsroom_child2 = AbstractFilterPage(title='newsroom child 2', date_published=dt.date(2016, 9, 3))
        self.newsroom_child2.tags.add('tag 2')
        self.newsroom_child2.categories.add(CFGOVPageCategory(name='some-other-category'))

        self.events_child1 = AbstractFilterPage(title='events child 1', date_published=dt.date(2016, 9, 7))
        self.events_child1.tags.add('tag 1')

        self.events_child2 = AbstractFilterPage(title='events child 2', date_published=dt.date(2016, 9, 5))
        self.events_child2.tags.add('tag 2')

        helpers.save_new_page(self.blog_child1, self.blog_parent)
        helpers.save_new_page(self.blog_child2, self.blog_parent)
        helpers.save_new_page(self.newsroom_child1, self.newsroom_parent)
        helpers.save_new_page(self.newsroom_child2, self.newsroom_parent)
        helpers.save_new_page(self.events_child1, self.events_parent)

        # mock a stream block that dictates how to retrieve the related posts
        # note that because of the way that the related_posts_category_lookup function
        # works i.e. by consulting a hard-coded object, the specific_categories
        # slot of the dict has to be something that it can actually find.

        self.block = mock.MagicMock()
        self.block.block_type = 'related_posts'
        self.block.value = dict({
            'limit': 3,
            'show_heading': True,
            'header_title': 'Further reading',
            'relate_posts': False,
            'relate_newsroom': False,
            'relate_events': False,
            'specific_categories': []
        })
Exemplo n.º 36
0
    def test_no_activity_log_page_raises_does_not_exist(self):
        page = CFGOVPage(title='test')
        self.root.add_child(instance=page)

        with self.assertRaises(Page.DoesNotExist):
            page.generate_view_more_url(self.request)
Exemplo n.º 37
0
 def setUp(self):
     self.page = CFGOVPage(title='Test', slug='test')
     self.factory = RequestFactory()
     self.request = self.factory.get('/')
Exemplo n.º 38
0
 def test_live_shared_with_another_shared_page(self):
     page = CFGOVPage(title='test', slug='test', live=False, shared=True)
     save_new_page(page)
     self.check_live_shared_counts(on_live_host=1, on_staging_host=2)