def setUp(self):
        from v1.models import HomePage
        self.ROOT_PAGE = HomePage.objects.get(slug='cfgov')
        self.landing_page = LandingPage(
            title='Landing', slug='landing')
        self.ROOT_PAGE.add_child(instance=self.landing_page)

        User = get_user_model()

        days_91 = timezone.now() - timedelta(days=91)
        days_90 = timezone.now() - timedelta(days=90)
        days_89 = timezone.now() - timedelta(days=89)
        days_61 = timezone.now() - timedelta(days=61)

        # This user is clearly inactive at 91 days
        self.user_1 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_91,
                                          date_joined=days_91)

        # This user is inactive because it's the 90th day
        self.user_2 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_90,
                                          date_joined=days_91)

        # This user is not inactive because it's been 89 days
        # This user will receive a warning email
        self.user_3 = User.objects.create(username='******',
                                          email='ü[email protected]',
                                          last_login=days_89,
                                          date_joined=days_91)

        # This user has never logged in, joined 91 days ago
        self.user_4 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          date_joined=days_91)

        # This user has never logged in, joined today.
        self.user_5 = User.objects.create(username='******',
                                          email='*****@*****.**')

        # This user last logged on 61 days ago, will be warned.
        self.user_6 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_61,
                                          date_joined=days_91)

        # This user hasn't logged in for 91 days but has since edited a page.
        self.page_editor = User.objects.create(username='******',
                                               email='*****@*****.**',
                                               last_login=days_91,
                                               date_joined=days_91)
        self.landing_page.save_revision(user=self.page_editor)

        self.stdout = StringIO()
 def test_well(self):
     """Well content correctly displays on a Landing Page"""
     landing_page = LandingPage(
         title='Landing Page',
         slug='landing',
     )
     landing_page.content = StreamValue(landing_page.content.stream_block,
                                        [atomic.well], True)
     publish_page(child=landing_page)
     response = django_client.get('/landing/')
     self.assertContains(response, 'this is well content')
 def test_info_unit_group(self):
     """Info Unit Group correctly displays on a Landing Page"""
     landing_page = LandingPage(
         title='Landing Page',
         slug='landing',
     )
     landing_page.content = StreamValue(landing_page.content.stream_block,
                                        [atomic.info_unit_group], True)
     publish_page(child=landing_page)
     response = django_client.get('/landing/')
     self.assertContains(response, 'Info Unit Group')
Пример #4
0
 def test_get_context_sets_meta_description_from_header_text_introduction_intro(
         self):  # noqa
     expected = 'Correct Meta Description'
     self.page = LandingPage(title='test',
                             header=json.dumps([
                                 {
                                     "type": "text_introduction",
                                     "value": {
                                         "intro": expected
                                     },
                                 },
                                 {
                                     "type": "text_introduction",
                                     "value": {
                                         "intro":
                                         'Incorrect Meta Description'
                                     },
                                 },
                             ]),
                             content=json.dumps([{
                                 "type": "text_introduction",
                                 "value": {
                                     "intro": 'Incorrect Meta Description'
                                 }
                             }]))
     test_context = self.page.get_context(self.request)
     result = test_context['meta_description']
     self.assertEqual(expected, result)
Пример #5
0
def run():
    default_site = Site.objects.get(is_default_site=True)
    root_page = default_site.root_page

    try:
        about_us = Page.objects.get(slug='about-us')
    except Page.DoesNotExist:
        logger.info('Creating page: About Us')
        about_us = LandingPage(title='About Us', slug='about-us', live=False)
        save_new_page(about_us, root=root_page)

    try:
        careers = Page.objects.get(slug='careers')
    except Page.DoesNotExist:
        logger.info('Creating page: Careers')
        careers = SublandingPage(title='Careers', slug='careers', live=False)
        save_new_page(careers, root=about_us)

    child_pages = [
        ('Working at the CFPB', 'working-at-cfpb'),
        ('Job Application Process', 'application-process'),
        ('Students and Graduates', 'students-and-graduates'),
        ('Current Openings', 'current-openings'),
    ]

    for title, slug in child_pages:
        try:
            child_page = Page.objects.get(slug=slug)
        except Page.DoesNotExist:
            logger.info('Creating page: {}'.format(title))
            child_page = BrowsePage(title=title, slug=slug, live=False)
            save_new_page(child_page, careers)
    def setUp(self):
        from v1.models import HomePage
        self.ROOT_PAGE = HomePage.objects.get(slug='cfgov')
        self.landing_page = LandingPage(
            title='Landing', slug='landing')
        self.ROOT_PAGE.add_child(instance=self.landing_page)

        User = get_user_model()

        days_91 = timezone.now() - timedelta(days=91)
        days_90 = timezone.now() - timedelta(days=90)
        days_89 = timezone.now() - timedelta(days=89)
        days_61 = timezone.now() - timedelta(days=61)

        # This user is clearly inactive at 91 days
        self.user_1 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_91,
                                          date_joined=days_91)

        # This user is inactive because it's the 90th day
        self.user_2 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_90,
                                          date_joined=days_91)

        # This user is not inactive because it's been 89 days
        # This user will receive a warning email
        self.user_3 = User.objects.create(username='******',
                                          email='ü[email protected]',
                                          last_login=days_89,
                                          date_joined=days_91)

        # This user has never logged in, joined 91 days ago
        self.user_4 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          date_joined=days_91)

        # This user has never logged in, joined today.
        self.user_5 = User.objects.create(username='******',
                                          email='*****@*****.**')

        # This user last logged on 61 days ago, will be warned.
        self.user_6 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_61,
                                          date_joined=days_91)

        # This user hasn't logged in for 91 days but has since edited a page.
        self.page_editor = User.objects.create(username='******',
                                               email='*****@*****.**',
                                               last_login=days_91,
                                               date_joined=days_91)
        self.landing_page.save_revision(user=self.page_editor)

        self.stdout = StringIO()
 def test_sidebar_contact_info(self):
     """Sidebar contact info correctly displays on a Landing Page"""
     landing_page = LandingPage(
         title='Landing Page',
         slug='landing',
     )
     contact = self.get_contact()
     landing_page.sidefoot = StreamValue(
         landing_page.sidefoot.stream_block,
         [atomic.sidebar_contact(contact.id)], True)
     publish_page(child=landing_page)
     response = django_client.get('/landing/')
     self.assertContains(response, '*****@*****.**')
     self.assertContains(response, '(515) 123-4567')
     self.assertContains(response, 'Ext. 1234')
     self.assertContains(response, '123 abc street')
     self.assertContains(response, 'this is a heading')
     self.assertContains(response, 'this is a body')
     self.assertContains(
         response, 'Contact Information')  # This is specific to sidebar
Пример #8
0
 def test_get_context_sets_meta_description_from_search_description(self):
     result = 'Correct Meta Description'
     self.page = LandingPage(
         title='test',
         search_description=result,
         header=json.dumps([{
             "type": "hero",
             "value": {
                 "body": 'Incorrect Meta Description'
             },
         }]),
     )
     test_context = self.page.get_context(self.request)
     self.assertEqual(test_context['meta_description'], result)
class InactiveUsersTestCase(TestCase):

    def setUp(self):
        from v1.models import HomePage
        self.ROOT_PAGE = HomePage.objects.get(slug='cfgov')
        self.landing_page = LandingPage(
            title='Landing', slug='landing')
        self.ROOT_PAGE.add_child(instance=self.landing_page)

        User = get_user_model()

        days_91 = timezone.now() - timedelta(days=91)
        days_90 = timezone.now() - timedelta(days=90)
        days_89 = timezone.now() - timedelta(days=89)
        days_61 = timezone.now() - timedelta(days=61)

        # This user is clearly inactive at 91 days
        self.user_1 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_91,
                                          date_joined=days_91)

        # This user is inactive because it's the 90th day
        self.user_2 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_90,
                                          date_joined=days_91)

        # This user is not inactive because it's been 89 days
        # This user will receive a warning email
        self.user_3 = User.objects.create(username='******',
                                          email='ü[email protected]',
                                          last_login=days_89,
                                          date_joined=days_91)

        # This user has never logged in, joined 91 days ago
        self.user_4 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          date_joined=days_91)

        # This user has never logged in, joined today.
        self.user_5 = User.objects.create(username='******',
                                          email='*****@*****.**')

        # This user last logged on 61 days ago, will be warned.
        self.user_6 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_61,
                                          date_joined=days_91)

        # This user hasn't logged in for 91 days but has since edited a page.
        self.page_editor = User.objects.create(username='******',
                                               email='*****@*****.**',
                                               last_login=days_91,
                                               date_joined=days_91)
        self.landing_page.save_revision(user=self.page_editor)

        self.stdout = StringIO()

    def get_stdout(self):
        if six.PY2:  # pragma: no cover
            return self.stdout.getvalue().decode('utf-8')
        else:  # pragma: no cover
            return self.stdout.getvalue()

    def test_format_inactive_users_last_login(self):
        short_date = date_format(self.user_1.last_login,
                                 "SHORT_DATETIME_FORMAT")
        self.assertEqual(
            Command().format_inactive_users([self.user_1]),
            '\tuser_1: {}\n'.format(short_date)
        )

    def test_format_inactive_users_never_logged_in(self):
        self.assertEqual(
            Command().format_inactive_users([self.user_5]),
            '\tuser_5: never\n'
        )

    def test_get_inactive_users(self):
        """ Test that three users are listed for the default 90 period
        including one that last logged in 90 days ago. """
        call_command('inactive_users', stdout=self.stdout)
        self.assertIn("user_1", self.get_stdout())
        self.assertIn("user_2", self.get_stdout())
        self.assertIn("user_4", self.get_stdout())
        self.assertNotIn("üser_3", self.get_stdout())
        self.assertNotIn("user_5", self.get_stdout())
        self.assertNotIn("user_6", self.get_stdout())

    def test_get_inactive_users_87_days(self):
        """ Test that four users are listed for a custom 87 day period """
        call_command('inactive_users', period=87, stdout=self.stdout)
        self.assertIn("user_1", self.get_stdout())
        self.assertIn("user_2", self.get_stdout())
        self.assertIn("üser_3", self.get_stdout())
        self.assertIn("user_4", self.get_stdout())
        self.assertNotIn("user_5", self.get_stdout())
        self.assertNotIn("user_6", self.get_stdout())

    def test_get_inactive_users_92_days(self):
        """ Test that no users are listed for a custom 92 day period """
        call_command('inactive_users', period=92, stdout=self.stdout)
        self.assertNotIn("user_1", self.get_stdout())
        self.assertNotIn("user_2", self.get_stdout())
        self.assertNotIn("üser_3", self.get_stdout())
        self.assertNotIn("user_4", self.get_stdout())
        self.assertNotIn("user_5", self.get_stdout())
        self.assertNotIn("user_6", self.get_stdout())
        self.assertIn("No users are inactive", self.get_stdout())

    @override_settings(EMAIL_SUBJECT_PREFIX='[Prefix]')
    def test_sends_email(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     emails=['*****@*****.**'],
                     stdout=self.stdout)
        # Outbox will have one system-owner email
        self.assertEqual(len(mail.outbox), 1)

        # Test the first (summary) email for inactive users only
        email = mail.outbox[0]
        self.assertEqual(email.to, ['*****@*****.**'])
        self.assertEqual(email.from_email, 'webmaster@localhost')
        self.assertIn('[Prefix]', email.subject)
        self.assertIn('Inactive users as of', email.subject)

        message = email.message().as_string()
        self.assertIn("user_1", message)
        self.assertIn("user_2", message)
        self.assertIn("user_4", message)
        self.assertNotIn("üser_3", message)
        self.assertNotIn("user_5", message)
        self.assertIn("*****@*****.**", self.get_stdout())

    def test_sends_email_when_warning(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     '--warn-users',
                     stdout=self.stdout)
        # Should see 5 warnings since we're not passing `--deactivate`.
        self.assertEqual(len(mail.outbox), 5)

    def test_sends_email_when_deactivating(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     '--deactivate-users',
                     stdout=self.stdout)
        # Outbox will have three inactive user emails
        self.assertEqual(len(mail.outbox), 3)

    def test_sends_email_when_deactivating_and_warning(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     '--deactivate-users',
                     '--warn-users',
                     stdout=self.stdout)
        # Outbox will have five user emails
        self.assertEqual(len(mail.outbox), 5)

    def test_users_properly_deactivated(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     '--deactivate-users',
                     stdout=self.stdout)

        # Test that users were actually deactivated
        # May need to re-fetch users
        # inactive_users = User.objects.all()
        User = get_user_model()
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)

    def test_users_properly_deactivated_when_deactivating_and_warning(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     '--deactivate-users',
                     '--warn-users',
                     stdout=self.stdout)

        # Test that users were actually deactivated
        User = get_user_model()
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)

        # No warnings if we give the remaining users a save_revision pass.
        mail.outbox = []
        self.landing_page.save_revision(user=self.user_3)
        self.landing_page.save_revision(user=self.user_6)

        call_command('inactive_users',
                     '--warn-users',
                     stdout=self.stdout)
        self.assertEqual(len(mail.outbox), 0)
class InactiveUsersTestCase(TestCase):

    def setUp(self):
        from v1.models import HomePage
        self.ROOT_PAGE = HomePage.objects.get(slug='cfgov')
        self.landing_page = LandingPage(
            title='Landing', slug='landing')
        self.ROOT_PAGE.add_child(instance=self.landing_page)

        User = get_user_model()

        days_91 = timezone.now() - timedelta(days=91)
        days_90 = timezone.now() - timedelta(days=90)
        days_89 = timezone.now() - timedelta(days=89)
        days_61 = timezone.now() - timedelta(days=61)

        # This user is clearly inactive at 91 days
        self.user_1 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_91,
                                          date_joined=days_91)

        # This user is inactive because it's the 90th day
        self.user_2 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_90,
                                          date_joined=days_91)

        # This user is not inactive because it's been 89 days
        # This user will receive a warning email
        self.user_3 = User.objects.create(username='******',
                                          email='ü[email protected]',
                                          last_login=days_89,
                                          date_joined=days_91)

        # This user has never logged in, joined 91 days ago
        self.user_4 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          date_joined=days_91)

        # This user has never logged in, joined today.
        self.user_5 = User.objects.create(username='******',
                                          email='*****@*****.**')

        # This user last logged on 61 days ago, will be warned.
        self.user_6 = User.objects.create(username='******',
                                          email='*****@*****.**',
                                          last_login=days_61,
                                          date_joined=days_91)

        # This user hasn't logged in for 91 days but has since edited a page.
        self.page_editor = User.objects.create(username='******',
                                               email='*****@*****.**',
                                               last_login=days_91,
                                               date_joined=days_91)
        self.landing_page.save_revision(user=self.page_editor)

        self.stdout = StringIO()

    def get_stdout(self):
        return self.stdout.getvalue()

    def test_format_inactive_users_last_login(self):
        short_date = date_format(self.user_1.last_login,
                                 "SHORT_DATETIME_FORMAT")
        self.assertEqual(
            Command().format_inactive_users([self.user_1]),
            '\tuser_1: {}\n'.format(short_date)
        )

    def test_format_inactive_users_never_logged_in(self):
        self.assertEqual(
            Command().format_inactive_users([self.user_5]),
            '\tuser_5: never\n'
        )

    def test_get_inactive_users(self):
        """ Test that three users are listed for the default 90 period
        including one that last logged in 90 days ago. """
        call_command('inactive_users', stdout=self.stdout)
        self.assertIn("user_1", self.get_stdout())
        self.assertIn("user_2", self.get_stdout())
        self.assertIn("user_4", self.get_stdout())
        self.assertNotIn("üser_3", self.get_stdout())
        self.assertNotIn("user_5", self.get_stdout())
        self.assertNotIn("user_6", self.get_stdout())

    def test_get_inactive_users_87_days(self):
        """ Test that four users are listed for a custom 87 day period """
        call_command('inactive_users', period=87, stdout=self.stdout)
        self.assertIn("user_1", self.get_stdout())
        self.assertIn("user_2", self.get_stdout())
        self.assertIn("üser_3", self.get_stdout())
        self.assertIn("user_4", self.get_stdout())
        self.assertNotIn("user_5", self.get_stdout())
        self.assertNotIn("user_6", self.get_stdout())

    def test_get_inactive_users_92_days(self):
        """ Test that no users are listed for a custom 92 day period """
        call_command('inactive_users', period=92, stdout=self.stdout)
        self.assertNotIn("user_1", self.get_stdout())
        self.assertNotIn("user_2", self.get_stdout())
        self.assertNotIn("üser_3", self.get_stdout())
        self.assertNotIn("user_4", self.get_stdout())
        self.assertNotIn("user_5", self.get_stdout())
        self.assertNotIn("user_6", self.get_stdout())
        self.assertIn("No users are inactive", self.get_stdout())

    @override_settings(EMAIL_SUBJECT_PREFIX='[Prefix]')
    def test_sends_email(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     emails=['*****@*****.**'],
                     stdout=self.stdout)
        # Outbox will have one system-owner email
        self.assertEqual(len(mail.outbox), 1)

        # Test the first (summary) email for inactive users only
        email = mail.outbox[0]
        self.assertEqual(email.to, ['*****@*****.**'])
        self.assertEqual(email.from_email, 'webmaster@localhost')
        self.assertIn('[Prefix]', email.subject)
        self.assertIn('Inactive users as of', email.subject)

        message = email.message().as_string()
        self.assertIn("user_1", message)
        self.assertIn("user_2", message)
        self.assertIn("user_4", message)
        self.assertNotIn("üser_3", message)
        self.assertNotIn("user_5", message)
        self.assertIn("*****@*****.**", self.get_stdout())

    def test_sends_email_when_warning(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     '--warn-users',
                     stdout=self.stdout)
        # Should see 5 warnings since we're not passing `--deactivate`.
        self.assertEqual(len(mail.outbox), 5)

    def test_sends_email_when_deactivating(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     '--deactivate-users',
                     stdout=self.stdout)
        # Outbox will have three inactive user emails
        self.assertEqual(len(mail.outbox), 3)

    def test_sends_email_when_deactivating_and_warning(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     '--deactivate-users',
                     '--warn-users',
                     stdout=self.stdout)
        # Outbox will have five user emails
        self.assertEqual(len(mail.outbox), 5)

    def test_users_properly_deactivated(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     '--deactivate-users',
                     stdout=self.stdout)

        # Test that users were actually deactivated
        # May need to re-fetch users
        # inactive_users = User.objects.all()
        User = get_user_model()
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)

    def test_users_properly_deactivated_when_deactivating_and_warning(self):
        """ Test that mail.EmailMessage is called with the appropriate
        list of users """

        mail.outbox = []
        call_command('inactive_users',
                     '--deactivate-users',
                     '--warn-users',
                     stdout=self.stdout)

        # Test that users were actually deactivated
        User = get_user_model()
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)
        self.assertFalse(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)
        self.assertTrue(User.objects.get(username="******").is_active)

        # No warnings if we give the remaining users a save_revision pass.
        mail.outbox = []
        self.landing_page.save_revision(user=self.user_3)
        self.landing_page.save_revision(user=self.user_6)

        call_command('inactive_users',
                     '--warn-users',
                     stdout=self.stdout)
        self.assertEqual(len(mail.outbox), 0)