def test_user_confirm_token(self): User.generate_fake(count=2) user1 = User.query.first() user2 = User.query.filter(User.id != user1.id).first() token1 = user1.generate_confirmation_token(expiration=3600) token2 = user2.generate_confirmation_token(expiration=3600) self.assertTrue(user1.confirm(token1)) self.assertFalse(user1.confirm(token2)) self.assertTrue(user2.confirm(token2)) self.assertFalse(user2.confirm(token1)) self.assertFalse(user1.confirm(''))
def test_user_suspend(self): admin_role = Role.query.filter_by(name='Administrator').first() user_role = Role.query.filter_by(name='User').first() User.generate_fake(count=1, roles=[admin_role]) admin_user = User.query.filter_by(role=admin_role).first() self.assertEqual(admin_user.role, admin_role) is_suspended = admin_user.suspend() self.assertTrue(is_suspended) self.assertNotEqual(admin_user.role, admin_role) self.assertEqual(admin_user.role, user_role)
def test_invite_request_get(self): number_of_users = current_app.config['APP_ITEMS_PER_PAGE'] * 2 User.generate_fake(count=number_of_users) # will be first user in the list of users, since it's not confirmed yet user = self.create_user(email='*****@*****.**', username='******', role_name='User', confirmed=False) admin = self.create_user(confirmed=True) response = self.login(email='*****@*****.**', password='******', follow_redirects=True) response_page_1 = self.client.get(url_for('auth.invite_request'), follow_redirects=True) data_page_1 = response_page_1.get_data(as_text=True) self.assertIn( '*****@*****.**', data_page_1, 'Failed to show unconfirmed invited user on the first page ' 'of user invitation form page.') response_page_2 = self.client.get(url_for('auth.invite_request', page=2), follow_redirects=True) data_page_2 = response_page_2.get_data(as_text=True) self.assertNotIn( '*****@*****.**', data_page_2, 'Failed to show invited user on the first page ' 'of user invitation form page.') response_page_3 = self.client.get(url_for('auth.invite_request', page=3), follow_redirects=True) data_page_3 = response_page_3.get_data(as_text=True) self.assertNotIn('*****@*****.**', data_page_3) response_page_4 = self.client.get(url_for('auth.invite_request', page=4), follow_redirects=True) data_page_4 = response_page_4.get_data(as_text=True) self.assertNotIn( '@', data_page_4, 'Failed to show empty list of users when pagination ' 'is exhausted as error_out=False prevents ' 'throwing error 404.')
def test_sentiment_json(self): User.generate_fake(count=2) Sentiment.generate_fake(count=1) s = Sentiment.query.first() json_data = s.to_json() self.assertEqual(json_data['body'], s.body) self.assertEqual(json_data['body_html'], s.body_html) self.assertEqual(json_data['timestamp'], s.timestamp) sentiment_from_json = Sentiment.from_json(json_data) self.assertEqual(sentiment_from_json.body, s.body) with self.assertRaises(ValidationError) as err: Sentiment.from_json({}) self.assertIn("sentiment does not have a body", str(err.exception)) self.assertEqual(repr(s), "<Sentiment '{}'>".format(s.body))
def test_sentiment_generate_fake(self): User.generate_fake(count=10) self.assertEqual(Sentiment.query.all(), []) levels = [i / 10.0 for i in range(100)] Sentiment.generate_fake(count=100, subsequent_scores=False, levels=levels) sentiments = Sentiment.query.order_by(Sentiment.id.asc()).all() self.assertNotEqual(levels, [s.score for s in sentiments]) Sentiment.query.filter().delete() db.session.commit() Sentiment.generate_fake(count=100, subsequent_scores=True, levels=levels) sentiments = Sentiment.query.order_by(Sentiment.id.asc()).all() self.assertEqual(levels, [s.score for s in sentiments])
def setUp(self): """Method called before each unit-test""" # create app, set TESTING flag to disable error catching self.app = create_app('testing') # push app context self.app_context = self.app.app_context() self.app_context.push() # create databases, see config.py for testing db settings db.create_all() # pre-fill db with minimal needed things Role.insert_roles() Language.insert_basic_languages() User.generate_fake(10) Sentiment.generate_fake(25) # Werkzeug Client to make requests self.client = self.app.test_client(use_cookies=True)
def setUp(self): """Method called before each unit-test""" # create app, set TESTING flag to disable error catching self.app = create_app('testing') # push app context self.app_context = self.app.app_context() self.app_context.push() # make celery tasks eager # celery emulates the API and behavior of AsyncResult, # except the result is already evaluated. celery.conf.update(CELERY_ALWAYS_EAGER=True) # create databases, see config.py for testing db settings db.create_all() # pre-fill db with minimal needed things Role.insert_roles() Language.insert_basic_languages() User.generate_fake(5)
def test_user_change_email(self): User.generate_fake(count=2) user1 = User.query.first() user2 = User.query.filter(User.id != user1.id).first() old_email_user1 = user1.email new_email_user2 = forgery_py.internet.email_address() valid_token_user1 = user1.generate_email_change_token( new_email=new_email_user2) invalid_token_user1 = user1.generate_confirmation_token() blank_new_email_user2 = user2.generate_email_change_token( new_email=None) email_already_in_use = user2.generate_email_change_token( new_email=user2.email) self.assertTrue(user1.change_email(valid_token_user1)) self.assertEqual(user1.email, new_email_user2) self.assertNotEqual(user1.email, old_email_user1) self.assertFalse(user1.change_email('')) self.assertFalse(user1.change_email(invalid_token_user1)) self.assertFalse(user2.change_email(blank_new_email_user2)) self.assertFalse(user2.change_email(email_already_in_use))
def test_user_invite(self): user_role = Role.query.filter_by(name='User').first() User.generate_fake(count=1) user1 = User.query.first() user2 = User(email=forgery_py.internet.email_address(), role=user_role) db.session.add(user2) db.session.commit() valid_token = user2.generate_invite_token(expiration=3600) invalid_token = user2.generate_confirmation_token(expiration=3600) valid_invite = user2.accept_invite(token=valid_token, username='******', new_password='******') invalid_invite = user2.accept_invite(invalid_token, 'username2', 'new_password') invalid_invite2 = user2.accept_invite('', 'username2', 'new_password') self.assertTrue(valid_invite) self.assertTrue(user2.username, 'username2') self.assertTrue(user2.verify_password('new_password')) self.assertFalse(invalid_invite) self.assertFalse(invalid_invite2)