def test_invalid_password(self): with app.app_context(): with self.app: response = self.login('test', 'wrongpassword') assert response.status_code == 403 assert response.get_json()['status'] == 'failed' assert flask.session['logged_in'] == False
def write_coupons_to_db(self, id): # self.process_wordai() with app.app_context(): for row in self.coupons: coupon = Coupon( original_title=row.get('original_title'), title=row.get('original_title'), original_description=row.get('original_description'), description=row.get('original_description'), price=row.get('price'), expiry_date=row.get('expiry_date'), url=row.get('url'), code=row.get('code'), store_id=id) session.add(coupon) try: session.commit() except Exception as e: print(str(e)) session.rollback() store = Store.query.get(id) store.status = StoreStatus.done store.job_uid = None try: session.commit() return True except Exception as e: print(str(e)) session.rollback() return False
def test_can_login(self): with app.app_context(): with self.app: response = self.login('test', 'password') assert response.status_code == 200 assert response.get_json()['status'] == 'success' assert flask.session['logged_in'] == True
def init_db(): #Creates the database tables. with app.app_context(): db = get_db() with app.open_resource('db.sql') as f: db.cursor().executescript(f.read()) db.commit()
def get_new_credential(self): with app.app_context(): self.proxy = Proxy.last() if self.proxy: return self.proxy.name else: return None
def verificarEmail(correo, datosSesion): with app.app_context(): msg = Message(subject="Confirm Registration to URL Shortener", sender=app.config.get("MAIL_USERNAME"), recipients=[correo]) msg.html = render_template('usuario/correo.html', session=datosSesion) mail.send(msg)
def auto_check_avg_cpu_utilization(): """ Only Get The Instances SERVING THE APP, NOT JUST RUNNNING """ with app.app_context(): autoScalingConfig = AutoScalingConfig.query.first() print("auto config: " + str(autoScalingConfig)) if not autoScalingConfig: return if autoScalingConfig.isOn and not has_pending_instances(): print("auto scaling on") # only getting the instances that are serving the app _, num_workers = get_serving_instances() _, num_running_instances = get_running_instances() if num_workers != num_running_instances: return print('all the created instances in service now!') _, num_non_terminated_instances = get_non_terminated_instances() # avg util > expand_threshold all_has_cpu_util, avg_cpu_util = all_instance_has_valid_cpu_util() if not all_has_cpu_util: print('newly created worker has no cpu util yet, wait!') return if avg_cpu_util > autoScalingConfig.expand_threshold: if num_non_terminated_instances >= 8: print('number of instances created reaches limit !') return to_create = int( math.ceil((autoScalingConfig.expand_ratio - 1) * num_workers)) if to_create + num_non_terminated_instances >= 8: to_create = max(8 - num_non_terminated_instances, 0) print( "max number of workers reached! only creating {} additional workers" .format(to_create)) print( "CPU expand threshold: {} reached ---- creating {} new instances --- expand ratio: {}" .format(autoScalingConfig.expand_threshold, to_create, autoScalingConfig.expand_ratio)) for i in range(to_create): celery_create_worker() elif avg_cpu_util < autoScalingConfig.shrink_threshold: to_destroy = int(autoScalingConfig.shrink_ratio * num_workers) if to_destroy > 0: print( "CPU shrink threshold: {} reached ---- destorying {} instances --- shrink ratio: {}" .format(autoScalingConfig.shrink_threshold, to_destroy, autoScalingConfig.shrink_ratio)) random_destroy_worker(to_destroy) else: print("CPU utilization within range") elif has_pending_instances(): print('there are pending instances') else: print('auto config is off')
def test(): with app.app_context(): db.reflect() db.drop_all() db.create_all() os.system("pytest -x")
def test_client(): test_client = app.test_client() ctx = app.app_context() ctx.push() yield test_client ctx.pop()
def setUp(self): db.create_all() self.app = app.test_client() self.app_context = app.app_context() self.app_context.push() db_manage = DataManager() folder_path = os.path.dirname(os.path.abspath(__file__)) db_manage.load_data(folder_path)
def test_logout(self): with app.app_context(): with self.app: self.login('test', 'password') response = self.logout() assert response.status_code == 200 assert flask.session['logged_in'] == False
def clearTables(): with app.app_context(): try: meta = db.metadata for table in reversed(meta.sorted_tables): db.session.execute(table.delete()) db.session.commit() except Exception as e: logger.error("clearTables: error = {0}".format(e))
def testConnection(self): with app.app_context(): cur = mysql.connection.cursor() if cur.execute("select * from INFORMATION_SCHEMA.TABLES where 'TABLE_SCHEMA'='ThreatDetectorDB' and 'TABLE_NAME'='tdusers'") is False: value = "Connection Unsuccessfull" else: value = "Connection Successfull" assert value == "Connection Successfull"
def setUp(self): self.db_fd, app.config['DATABASE'] = tempfile.mkstemp() app.testing = True self.app = app.test_client() with app.app_context(): init_db() self.user = User('test', 'password') db_session.add(self.user) db_session.commit()
def create_app(config_obj): " Factory for creating app " load_config(app, config_obj) initialize_app(app) register_blueprints(app) register_errorhandlers(app) with app.app_context(): db.init_app(app) db.create_all() db.session.commit() return app
def start_browser(self): """ cross platform browser initiation :param proxy: proxy binded to specific account :return: chrome webdriver """ print( "Preparing selenium browser instance...\nGetting proxy instance.") print(self.get_new_credential()) with app.app_context(): if self.driver: self.close() if platform == 'darwin': chromedriver = os.path.join(app.config.get('BASE_DIR'), 'storage/chromedriver_mac') elif platform == 'win32': chromedriver = os.path.join(app.config.get('BASE_DIR'), 'storage/chromedriver.exe') else: chromedriver = os.path.join(app.config.get('BASE_DIR'), 'storage/chromedriver_linux') options = webdriver.ChromeOptions() # options.add_argument("--headless") options.add_argument("--window-size=1200,900") options.add_argument('--dns-prefetch-disable') options.add_argument('--js-flags="--max_old_space_size=4096"') options.add_argument('--no-sandbox') options.add_argument('--disable-gpu') if not self.proxy: self.get_new_credential() # if self.proxy and self.proxy.name: # options.add_argument("--proxy-server=" + self.proxy.name) prefs = {"profile.managed_default_content_settings.images": 2} options.add_experimental_option("prefs", prefs) try: self.driver = webdriver.Chrome(chromedriver, chrome_options=options) return True except Exception as e: print("Got an error in starting browser.") print(str(e)) if self.driver: print("Quiting the driver...") self.close() return False
def testUsername(self): username = "******" with app.app_context(): cur = mysql.connection.cursor() cur.execute("select username from tdusers where username =(%s)",(username,)) username_result = cur.fetchall() if username_result: value = "Username exists" else: value = "Username does not exist" assert value == "Username does not exist" cur.execute("delete from tdusers where username =(%s)",(username,))
def client(): # setup app with test config app.config.from_object("src.config.TestingConfig") # create test client client = app.test_client() # preparing app context for the testing phase ctx = app.app_context() ctx.push() # tests happen here yield client ctx.pop()
def app(request): """ Session-wide testable Flask app """ _app.config.from_mapping( TESTING=True, SQLALCHEMY_DATABASE_URI=os.getenv('TEST_DATABASE_URL'), SQLALCHEMY_TRACK_MODIFICATIONS=False, WTF_CSRF_ENABLED=False) ctx = _app.app_context() ctx.push() def teardown(): ctx.pop() request.addfinalizer(teardown) return _app
def send_email(token, email, name): with app.app_context(): try: msg = Message( subject="Reset your password from The Cancer Voice", sender=app.config.get("MAIL_USERNAME"), #sender email recipients=[email], body= f"Hi {name}! Thanks for comeback with us! To reset your email please enter the link : http://localhost:3000/new-password/?token=${token}" ) mail.send(msg) except Exception as err: print(f'{err}') else: print("success!")
def app(request): _app.config.from_mapping( TESTING=True, SECRET_KEY=os.environ.get('SECRET_KEY'), SQLALCHEMY_DATABASE_URI=os.getenv('TEST_DATABASE_URL'), SQLALCHEMY_TRACK_MODIFICATIONS=False ) ctx = _app.app_context() ctx.push() def teardown(): ctx.pop() request.addfinalizer(teardown) return _app
def setUp(self): app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['DEBUG'] = False db.init_app(app) with app.app_context(): db.create_all() example_user = User(id=1, email="*****@*****.**", username="******") example_user.set_password("111") db.session.merge(example_user) db.session.commit() self.app = app.test_client()
def app(request): """Testable flask application""" _app.config.from_mapping( TESTING=True, SECRET_KEY=os.environ.get('SECRET_KEY'), SQLALCHEMY_DATABASE_URI=os.getenv('TEST_DATABASE_URL'), SQLALCHEMY_TRACK_MODIFICATIONS=False, WTF_CSRF_ENABLED=False) ctx = _app.app_context() ctx.push() def teardown(): """Cleans up and closes out test session.""" ctx.pop() request.addfinalizer(teardown) return _app
def check_store(self, id): with app.app_context(): store = Store.query.get(id) store.status = StoreStatus.working try: session.commit() except Exception as e: print(str(e)) session.rollback() source = store.source spider = CouponSpiders[source.name.lower()]( self, base_url=source.base_url, start_point=store.start_point) result = spider.run(id) return { 'current': 50, 'total': 50, 'status': 'Job is Completed!', 'result': result }
def grab_coupons(self): with app.app_context(): # Removing all of the current coupons Coupon.query.delete() try: session.commit() except Exception as e: print(str(e)) session.rollback() print("===================================================") print("+++++++++ Starting the scheduler +++++++++++") print("---------------------------------------------------") count = 0 for store in Store.query.all(): if not store.source: # or store.source.name != 'offers': #!= 'retailmenot': #'groupon': continue # if count > 3: # break # else: # count += 1 task = check_store.apply_async([store.id]) store.job_uid = task.id store.status = StoreStatus.pending # Should be removed in production mode. # break try: session.commit() except Exception as e: session.rollback() print(str(e))
def instant_search(): keyword = request.args.get('keyword') with app.app_context(): # return json.dumps(list(map(lambda b: b.decode('utf-8'), instant.search(keyword)))[:20]) return jsonify(response=InstantSearch.search(keyword))
def fetch(): with app.app_context(): return SearchController.fetch(request.args)
def mark_credential_as_dead(self, **kwargs): with app.app_context(): return Proxy.mark_as(self.proxy.id, **kwargs)
def test_app(): app.config.from_object('src.config.TestingConfig') with app.app_context(): yield app # testing happens here
def send_async_email(app, msg): with app.app_context(): mail.send(msg)
from src import app, db, api from src.models.user import User from src.resources.register import Register from src.resources.verify import Verify from src.resources.login import Login from src.resources.refresh import Refresh from src.resources.me import Me with app.app_context(): db.drop_all() db.create_all() api.add_resource(Register, "/user/register") api.add_resource(Verify, "/user/verify") api.add_resource(Login, "/user/login") api.add_resource(Refresh, "/user/refresh") api.add_resource(Me, "/user/me") if __name__ == "__main__": app.run(host="0.0.0.0", port=80, debug=True)
def tearDown(self): with app.app_context(): db.session.remove() db.drop_all()
def jump(): with app.app_context(): return SearchController.jump(request.args)