def test_filtering(self): """ Tests filtering with an empty query, so just displaying all the recipes """ with app.test_request_context('/filter', method='GET'): resp = app.dispatch_request() self.assertIn('displaying', str(resp))
def test_check_history_table(self): with app.test_request_context(): tester=app.test_client(self) db.drop_all() db.create_all() tester.post('/registration',data=dict(username='******', email='*****@*****.**', password='******', confirm_password='******')) tester.post('/home',data=dict(username="******",password='******'), follow_redirects=True) login_user(User.query.first()) tester.post('/profile',data=dict(name="bob",address1='123456123456',address2="789456789456",city="Denver",state="CO",zipcode='80228'), follow_redirects=True) gallons = 975 day = 1 rates = [] totals = [] for quote in range(31): suggestRate, total = calculateRateAndTotal(gallons) rates.append(bytes(str(suggestRate),"ascii")) totals.append(bytes(str(total),"ascii")) tester.post('/quote',data=dict(gallonsRequested=str(gallons),deliveryDate='2020-07-'+str(day),deliveryAddress="123456123456 789456789456",rate=str(suggestRate),total=str(total),submit="True"), follow_redirects=True) gallons += 1 day += 1 response = tester.get('/history',content_type='html/text') for quote in range(31): self.assertIn(rates[quote],response.data) self.assertIn(totals[quote],response.data)
def test_login_post(self): """ Testing if loging in works properly """ ### Unfortunetely does not give proper result due to issue with session testing. Template should contain "Login", it does not with app.test_request_context('/login', method='POST', data=dict(author_name="Kaja", password="******")): resp = app.dispatch_request() self.assertNotIn('Logout', str(resp)) # Checking if works and displays expected message with app.test_request_context('/login', method='POST', data=dict(author_name="Kittykat", password="******")): resp = app.dispatch_request() self.assertIn('User does not exist', str(resp))
def test_logout_redirect(self): with app.test_request_context(): tester=app.test_client(self) db.drop_all() db.create_all() tester.post('/registration',data=dict(username='******', email='*****@*****.**', password='******', confirm_password='******')) tester.post('/home',data=dict(username="******",password='******'), follow_redirects=True) login_user(User.query.first()) response = tester.get('/logout',content_type='html/text', follow_redirects=True) self.assertIn(b"Login Here", response.data)
def test_get_random_animal(self): ''' Test if function returns any value ''' with app.test_request_context(): self.assertIsNotNone(get_random_animal()) '''Test if session variable is set (not empty)''' with app.test_client() as client: with client.session_transaction(): get_random_animal() random_animal = session.get('random_animal') self.assertIsNotNone(random_animal)
def test_logout_redirect(self): with app.test_request_context(): tester = app.test_client(self) tester.post('/home', data=dict(username="******", password='******'), follow_redirects=True) login_user(User.query.first()) response = tester.get('/logout', content_type='html/text', follow_redirects=True) self.assertIn(b"Login Here", response.data)
def test_rate_for_new_non_texans_with_more_than_1000_gallons(self): with app.test_request_context(): tester=app.test_client(self) db.drop_all() db.create_all() tester.post('/registration',data=dict(username='******', email='*****@*****.**', password='******', confirm_password='******')) tester.post('/home',data=dict(username="******",password='******'), follow_redirects=True) login_user(User.query.first()) tester.post('/profile',data=dict(name="bob",address1='123456123456',address2="789456789456",city="Denver",state="CO",zipcode='80228'), follow_redirects=True) suggestRate, total = calculateRateAndTotal(1234) self.assertEqual(suggestRate,Decimal('1.74')) self.assertEqual(total,Decimal('2147.16'))
def test_rate_for_old_texans_with_more_than_1000_gallons(self): with app.test_request_context(): tester=app.test_client(self) db.drop_all() db.create_all() tester.post('/registration',data=dict(username='******', email='*****@*****.**', password='******', confirm_password='******')) tester.post('/home',data=dict(username="******",password='******'), follow_redirects=True) login_user(User.query.first()) tester.post('/profile',data=dict(name="bob",address1='123456123456',address2="789456789456",city="Houston",state="TX",zipcode='77777'), follow_redirects=True) tester.post('/quote',data=dict(gallonsRequested="546",deliveryDate='2020-07-20',deliveryAddress="123456123456 Sample Drive",rate="2.00",total="1092",submit="True"), follow_redirects=True) suggestRate, total = calculateRateAndTotal(2002) self.assertEqual(suggestRate,Decimal('1.7')) self.assertEqual(total,Decimal('3403.4'))
def test_rate_for_old_non_texans_with_more_than_1000_gallons(self): with app.test_request_context(): tester=app.test_client(self) db.drop_all() db.create_all() tester.post('/registration',data=dict(username='******', email='*****@*****.**', password='******', confirm_password='******')) tester.post('/home',data=dict(username="******",password='******'), follow_redirects=True) login_user(User.query.first()) tester.post('/profile',data=dict(name="bob",address1='123456123456',address2="789456789456",city="Denver",state="CO",zipcode='80228'), follow_redirects=True) tester.post('/quote',data=dict(gallonsRequested="899",deliveryDate='2020-07-20',deliveryAddress="123456123456 789456789456",rate="1.73",total="1555.27",submit="True"), follow_redirects=True) suggestRate, total = calculateRateAndTotal(2255) self.assertEqual(suggestRate,Decimal('1.72')) self.assertEqual(total,Decimal('3878.6'))
def test_check_links_ADMIN(self): with app.test_request_context(): tester = app.test_client(self) tester.post('/home', data=dict(username="******", password='******'), follow_redirects=True) login_user(User.query.first()) response = tester.get('/links', content_type='html/text') userType = User.query.first().type if (userType == "ADMIN"): self.assertIn(b'Manage User Accounts', response.data) self.assertIn(b'Assign Roles', response.data) self.assertIn(b'Help Desk', response.data)
def test_redirection_to_profile_from_registration(self): with app.test_request_context(): tester=app.test_client(self) db.drop_all() db.create_all() hashed_password = bcrypt.generate_password_hash("123456")#.decode('utf-8') user = User(username="******", email="*****@*****.**", password=hashed_password) db.session.add(user) db.session.commit() login_user(user) tester.post('/home',data=dict(username="******",password='******'), follow_redirects=True) response = tester.get('/registration',content_type='html/text', follow_redirects=True) self.assertIn(b'Change Profile', response.data)
def test_check_links_SALES_ADMIN(self): with app.test_request_context(): tester = app.test_client(self) tester.post('/home', data=dict(username="******", password='******'), follow_redirects=True) login_user(User.query.first()) response = tester.get('/links', content_type='html/text') userType = User.query.first().type if (userType == "SALES_ADMIN"): self.assertIn(b'Sales Reports', response.data) self.assertIn(b'Sales Leads', response.data) self.assertIn(b'Sales Demo', response.data)
def test_check_links_FINANCE_ADMIN(self): with app.test_request_context(): tester = app.test_client(self) tester.post('/home', data=dict(username="******", password='******'), follow_redirects=True) login_user(User.query.first()) response = tester.get('/links', content_type='html/text') userType = User.query.first().type if (userType == "FINANCE_ADMIN"): self.assertIn(b'Finance Reports', response.data) self.assertIn(b'Accounts Payable', response.data) self.assertIn(b'Accounts Receivale', response.data) self.assertIn(b'Tax', response.data)
def test_redirection_to_quote_if_profile_exists(self): with app.test_request_context(): tester=app.test_client(self) db.drop_all() db.create_all() hashed_password = bcrypt.generate_password_hash("123456")#.decode('utf-8') user = User(username="******", email="*****@*****.**", password=hashed_password) db.session.add(user) db.session.commit() login_user(user) profile = Profile(name="bob", address1="Sample Drive", address2="", city="Houston",state="TX", zipcode="77777", user_id=current_user.id) db.session.add(profile) db.session.commit() response = tester.post('/home',data=dict(username="******",password='******'), follow_redirects=True) self.assertIn(b'Get Your Quote', response.data)
def test_check_links_HR_ADMIN(self): with app.test_request_context(): tester = app.test_client(self) tester.post('/home', data=dict(username="******", password='******'), follow_redirects=True) login_user(User.query.first()) response = tester.get('/links', content_type='html/text') userType = User.query.first().type if (userType == "HR_ADMIN"): self.assertIn(b'New Hire On-boarding', response.data) self.assertIn(b'Benefits', response.data) self.assertIn(b'Payroll', response.data) self.assertIn(b'Off-boarding', response.data) self.assertIn(b'HR Reports', response.data)
def test_check_links_TECH_ADMIN(self): with app.test_request_context(): tester = app.test_client(self) tester.post('/home', data=dict(username="******", password='******'), follow_redirects=True) login_user(User.query.first()) response = tester.get('/links', content_type='html/text') userType = User.query.first().type if (userType == "TECH_ADMIN"): self.assertIn(b'Application Monitoring', response.data) self.assertIn(b'Tech Support', response.data) self.assertIn(b'App Development', response.data) self.assertIn(b'App Admin', response.data) self.assertIn(b'Release Management', response.data)
def authorization(): '''用户验证''' code = request.args.get('code') api = WeixinAPI(appid=APP_ID, app_secret=APP_SECRET, redirect_uri=REDIRECT_URI) auth_info = api.exchange_code_for_access_token(code=code) api = WeixinAPI(access_token=auth_info['access_token']) resp = api.user(openid=auth_info['openid']) with app.test_request_context(): if check_user(auth_info['openid']): requests.post(data=resp) else: requests.put(data=resp) return jsonify(code=0)
def test_add_to_user_data_file(self): ''' Test if functions inserts entry in file is added to user_data file based on arguments passed''' with app.test_request_context(): clear_user_data_json() add_new_user('test_username') with app.test_client() as client: with client.session_transaction(): session['random_animal'] = {"title": "test_animal_title"} add_to_user_data_file('test_username', 'animals') add_to_user_data_file('test_username', 'correctlyGuessed') add_to_user_data_file('test_username', 'passed') self.assertIn("test_animal_title", open_user_data_json()[0]["animals"]) self.assertIn("test_animal_title", open_user_data_json()[0]["correctlyGuessed"]) self.assertIn("test_animal_title", open_user_data_json()[0]["passed"])
def test_create_player(self): """ Test that the a session is created and populated with a value for username and appended to player_info [] list as dictionary """ with app.test_client() as c: with c.session_transaction() as sess: sess['username'] = '******' with app.test_request_context(): self.assertEqual(run.create_player(sess['username']), [{ 'username': '******', 'score': 0, 'attempt': 0, "wrong": 0, "riddle_number": 0, "attempt_total": 0, "restart": False, "resume": False }])
def test_check_username(self): with self.app as c: with c.session_transaction() as sess: #creates session sess['username'] = '******' with app.test_request_context(): username = '******' usernames = [] if not usernames and username == 'bob' and sess['username']: self.assertTrue(run.check_username(username)) username = '******' usernames = [] if usernames and username == 'bob' and not sess['username']: self.assertFalse(run.check_username(username)) username = '******' usernames = ['bob'] if usernames and username == 'bob' and sess['username']: self.assertTrue(run.check_username(username)) username = '******' usernames = ['bob'] if not usernames and username == 'bob': self.assertFalse(run.check_username(username))
#!/usr/bin/python3 from run import app from datetime import datetime import traceback import models.merge as merge import models.delete as delete import models.link as link with app.test_request_context(): # grab all pending tasks ordered by creation time tasks = sorted( merge.Merge.getPending(app.data.driver.db) + delete.Delete.getPending(app.data.driver.db) + link.Link.getPending(app.data.driver.db) ) # now, perform the first one if it exists if tasks: try: tasks[0].do().commit() print( 'Task %s (%s) run successfully at %s for %s' % ( tasks[0].getId(), tasks[0].collection, datetime.now(), tasks[0].getOwner().get('name') ) ) except Exception as e:
def test_rendering_add_page(self): with app.test_request_context('add_recipe', method='GET'): resp = app.dispatch_request() self.assertEqual(resp._status_code, 302)
from run import app from flask import url_for # -------------------------------------------------------- # Testing # -------------------------------------------------------- with app.test_request_context(): print(url_for('join_room', room_id=123))