def test_empty_database(self): # Call users with a config file but an empty database sys.argv = ['notused', self.conf_file_path] sys.stdout = StringIO() result = users() self.assertEqual(result, None) stdout = sys.stdout.getvalue() self.assertEqual(stdout, '')
def test_no_arguments(self): # Replace sys argv and stdout sys.argv = [] sys.stdout = StringIO() # Call users with no arguments result = users() self.assertEqual(result, 2) stdout = sys.stdout.getvalue() self.assertEqual(stdout, 'You must provide at least one argument\n')
def test_users(self): # Save sys values old_args = sys.argv[:] old_stdout = sys.stdout # Replace sys argv and stdout sys.argv = [] sys.stdout = StringIO() # Call users with no arguments result = users() self.assertEqual(result, 2) stdout = sys.stdout.getvalue() self.assertEqual(stdout, 'You must provide at least one argument\n') # Call users with a config file but an empty database sys.argv = ['notused', self.conf_file_path] sys.stdout = StringIO() result = users() 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': '*****@*****.**', }) u2_id = self.db.users.insert({ 'first_name': 'John2', 'last_name': 'Doe2', 'email': '*****@*****.**', 'email_verified': True, 'twitter_id': '1234', }) self.db.passwords.insert({ 'service': 'service1', 'secret': 's3cr3t', 'owner': u2_id, }) u3_id = self.db.users.insert({ 'first_name': 'John3', 'last_name': 'Doe3', 'email': '*****@*****.**', 'email_verified': True, 'twitter_id': '1234', 'facebook_id': '5678', 'google_id': 'abcd', 'date_joined': datetime.datetime(2012, 12, 12, 12, 12, 12), 'last_login': datetime.datetime(2012, 12, 12, 12, 12, 12), }) self.db.passwords.insert({ 'service': 'service1', 'secret': 's3cr3t', 'owner': u3_id, }) self.db.passwords.insert({ 'service': 'service2', 'secret': 's3cr3t', 'owner': u3_id, }) sys.argv = ['notused', self.conf_file_path] sys.stdout = StringIO() result = users() self.assertEqual(result, None) stdout = sys.stdout.getvalue() expected_output = """John Doe <*****@*****.**> (%s) Passwords: 0 Providers: Verified: False Date joined: Unknown Last login: Unknown John2 Doe2 <*****@*****.**> (%s) Passwords: 1 Providers: twitter Verified: True Date joined: Unknown Last login: Unknown John3 Doe3 <*****@*****.**> (%s) Passwords: 2 Providers: facebook, google, twitter Verified: True Date joined: 2012-12-12 12:12:12+00:00 Last login: 2012-12-12 12:12:12+00:00 """ % (u1_id, u2_id, u3_id) self.assertEqual(stdout, expected_output) # Restore sys.values sys.argv = old_args sys.stdout = old_stdout
def test_non_empty_database(self): d = datetime.datetime with transaction.manager: user1 = User(first_name='John', last_name='Doe', creation=d(2012, 12, 12, 12, 12, 12), last_login=d(2012, 12, 12, 12, 12, 12), email='*****@*****.**') Session.add(user1) user2 = User(first_name='John2', last_name='Doe2', creation=d(2012, 12, 12, 12, 12, 12), last_login=d(2012, 12, 12, 12, 12, 12), email='*****@*****.**', email_verified=True) identity2 = ExternalIdentity(user=user2, provider='twitter', external_id='1234') Session.add(user2) Session.add(identity2) self.add_passwords(user2, 1) user3 = User(first_name='John3', last_name='Doe3', creation=d(2012, 12, 12, 12, 12, 12), last_login=d(2012, 12, 12, 12, 12, 12), email='*****@*****.**', email_verified=True) identity3_1 = ExternalIdentity(user=user3, provider='twitter', external_id='1234') identity3_2 = ExternalIdentity(user=user3, provider='facebook', external_id='5678') identity3_3 = ExternalIdentity(user=user3, provider='google', external_id='abcd') Session.add(user3) Session.add(identity3_1) Session.add(identity3_2) Session.add(identity3_3) self.add_passwords(user3, 2) Session.flush() u1_id = user1.id u2_id = user2.id u3_id = user3.id sys.argv = ['notused', self.conf_file_path] sys.stdout = StringIO() result = users() self.assertEqual(result, None) stdout = sys.stdout.getvalue() context = { 'u1': u1_id, 'u2': u2_id, 'u3': u3_id, 'tab': '\t', } expected_output = """John Doe <*****@*****.**> (%(u1)s) %(tab)sPasswords: 0 %(tab)sProviders: %(tab)sVerified: False %(tab)sDate joined: 2012-12-12 12:12:12 %(tab)sLast login: 2012-12-12 12:12:12 John2 Doe2 <*****@*****.**> (%(u2)s) %(tab)sPasswords: 1 %(tab)sProviders: twitter %(tab)sVerified: True %(tab)sDate joined: 2012-12-12 12:12:12 %(tab)sLast login: 2012-12-12 12:12:12 John3 Doe3 <*****@*****.**> (%(u3)s) %(tab)sPasswords: 2 %(tab)sProviders: facebook, google, twitter %(tab)sVerified: True %(tab)sDate joined: 2012-12-12 12:12:12 %(tab)sLast login: 2012-12-12 12:12:12 """ % context self.assertEqual(stdout, expected_output)