def test_statistics_empty_database(self):
     sys.argv = ['notused', self.conf_file_path]
     sys.stdout = StringIO()
     result = statistics()
     self.assertEqual(result, None)
     stdout = sys.stdout.getvalue()
     self.assertEqual(stdout, '')
    def test_statistics_no_arguments(self):
        # Replace sys argv and stdout
        sys.argv = []
        sys.stdout = StringIO()

        # Call statistics with no arguments
        result = statistics()
        self.assertEqual(result, 2)
        stdout = sys.stdout.getvalue()
        self.assertEqual(stdout, 'You must provide at least one argument\n')
    def test_statistics(self):
        # Save sys values
        old_args = sys.argv[:]
        old_stdout = sys.stdout

        # Replace sys argv and stdout
        sys.argv = []
        sys.stdout = StringIO()

        # Call statistics with no arguments
        result = statistics()
        self.assertEqual(result, 2)
        stdout = sys.stdout.getvalue()
        self.assertEqual(stdout, 'You must provide at least one argument\n')

        # Call statistics with a config file but an empty database
        sys.argv = ['notused', self.conf_file_path]
        sys.stdout = StringIO()
        result = statistics()
        self.assertEqual(result, None)
        stdout = sys.stdout.getvalue()
        self.assertEqual(stdout, '')

        # Add some data to the database
        u1_id = self.db.users.insert({
                'first_name': 'John',
                'last_name': 'Doe',
                'email': '*****@*****.**',
                'email_verified': True,
                'allow_google_analytics': True,
                'google_id': '1',
                })
        self.add_passwords(u1_id, 10)

        u2_id = self.db.users.insert({
                'first_name': 'Peter',
                'last_name': 'Doe',
                'email': '*****@*****.**',
                'email_verified': True,
                'allow_google_analytics': False,
                'twitter_id': '1',
                })
        self.add_passwords(u2_id, 20)

        u3_id = self.db.users.insert({
                'first_name': 'Susan',
                'last_name': 'Doe',
                'email': '*****@*****.**',
                'email_verified': True,
                'allow_google_analytics': False,
                'facebook_id': '1',
                })
        self.add_passwords(u3_id, 15)

        self.db.users.insert({
                'first_name': 'Alice',
                'last_name': 'Doe',
                'email': '',
                'email_verified': False,
                'allow_google_analytics': False,
                'persona_id': '1',
                })

        self.db.users.insert({
                'first_name': 'Bob',
                'last_name': 'Doe',
                'email': '',
                'email_verified': False,
                'allow_google_analytics': False,
                'google_id': '2',
                })
        self.db.users.insert({
                'first_name': 'Kevin',
                'last_name': 'Doe',
                'email': '',
                'email_verified': False,
                'allow_google_analytics': False,
                'google_id': '3',
                })
        self.db.users.insert({
                'first_name': 'Maria',
                'last_name': 'Doe',
                'email': '',
                'email_verified': False,
                'allow_google_analytics': False,
                'google_id': '4',
                })
        self.db.users.insert({
                'first_name': 'Bran',
                'last_name': 'Doe',
                'email': '',
                'email_verified': False,
                'allow_google_analytics': False,
                'twitter_id': '2',
                })
        self.db.users.insert({
                'first_name': 'George',
                'last_name': 'Doe',
                'email': '',
                'email_verified': False,
                'allow_google_analytics': False,
                'twitter_id': '3',
                })
        self.db.users.insert({
                'first_name': 'Travis',
                'last_name': 'Doe',
                'email': '',
                'email_verified': False,
                'allow_google_analytics': False,
                'persona_id': '2',
                })

        sys.argv = ['notused', self.conf_file_path]
        sys.stdout = StringIO()
        result = statistics()
        self.assertEqual(result, None)
        stdout = sys.stdout.getvalue()

        expected_output = """Number of users: 10
Number of passwords: 45
Verified users: 30.00% (3)
Users that allow Google Analytics cookie: 10.00% (1)
Identity providers:
	google: 40.00% (4)
	twitter: 30.00% (3)
	persona: 20.00% (2)
	facebook: 10.00% (1)
Email providers:
	example.com: 66.67% (2)
	Others: 33.33% (1)
Users without email: 70.00% (7)
Most active users:
	Peter Doe <*****@*****.**>: 20
	Susan Doe <*****@*****.**>: 15
	John Doe <*****@*****.**>: 10
Users without passwords: 70.00% (7)
"""
        self.assertEqual(stdout, expected_output)

        # Restore sys.values
        sys.argv = old_args
        sys.stdout = old_stdout
    def test_statistics_non_empty_database(self):
        with transaction.manager:
            user1 = User(first_name='John',
                         last_name='Doe',
                         email='*****@*****.**',
                         email_verified=True,
                         allow_google_analytics=True)
            identity1 = ExternalIdentity(user=user1, provider='google',
                                         external_id='1')
            Session.add(user1)
            Session.add(identity1)
            self.add_passwords(user1, 10)

            user2 = User(first_name='Peter',
                         last_name='Doe',
                         email='*****@*****.**',
                         email_verified=True,
                         allow_google_analytics=False)
            identity2 = ExternalIdentity(user=user1, provider='twitter',
                                         external_id='1')
            Session.add(user2)
            Session.add(identity2)
            self.add_passwords(user2, 20)

            user3 = User(first_name='Susan',
                         last_name='Doe',
                         email='*****@*****.**',
                         email_verified=True,
                         allow_google_analytics=False)
            identity3 = ExternalIdentity(user=user1, provider='facebook',
                                         external_id='1')
            Session.add(user3)
            Session.add(identity3)
            self.add_passwords(user3, 15)

            user4 = User(first_name='Alice',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity4 = ExternalIdentity(user=user1, provider='persona',
                                         external_id='1')
            Session.add(user4)
            Session.add(identity4)

            user5 = User(first_name='Bob',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity5 = ExternalIdentity(user=user1, provider='google',
                                         external_id='2')
            Session.add(user5)
            Session.add(identity5)

            user6 = User(first_name='Kevin',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity6 = ExternalIdentity(user=user1, provider='google',
                                         external_id='3')
            Session.add(user6)
            Session.add(identity6)

            user7 = User(first_name='Maria',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity7 = ExternalIdentity(user=user1, provider='google',
                                         external_id='4')
            Session.add(user7)
            Session.add(identity7)

            user8 = User(first_name='Bran',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity8 = ExternalIdentity(user=user1, provider='twitter',
                                         external_id='2')
            Session.add(user8)
            Session.add(identity8)

            user9 = User(first_name='George',
                         last_name='Doe',
                         email='',
                         email_verified=False,
                         allow_google_analytics=False)
            identity9 = ExternalIdentity(user=user1, provider='twitter',
                                         external_id='3')
            Session.add(user9)
            Session.add(identity9)

            user10 = User(first_name='Travis',
                          last_name='Doe',
                          email='',
                          email_verified=False,
                          allow_google_analytics=False)
            identity10 = ExternalIdentity(user=user1, provider='persona',
                                          external_id='2')
            Session.add(user10)
            Session.add(identity10)
            Session.flush()

        sys.argv = ['notused', self.conf_file_path]
        sys.stdout = StringIO()
        result = statistics()
        self.assertEqual(result, None)
        stdout = sys.stdout.getvalue()

        expected_output = """Number of users: 10
Number of passwords: 45
Verified users: 30.00%% (3)
Users that allow Google Analytics cookie: 10.00%% (1)
Identity providers:
%(tab)sgoogle: 40.00%% (4)
%(tab)stwitter: 30.00%% (3)
%(tab)spersona: 20.00%% (2)
%(tab)sfacebook: 10.00%% (1)
Email providers:
%(tab)sexample.com: 66.67%% (2)
%(tab)sOthers: 33.33%% (1)
Users without email: 70.00%% (7)
Most active users:
%(tab)sPeter Doe <*****@*****.**>: 20
%(tab)sSusan Doe <*****@*****.**>: 15
%(tab)sJohn Doe <*****@*****.**>: 10
Users without passwords: 70.00%% (7)
""" % {'tab': '\t'}
        self.assertEqual(stdout, expected_output)