예제 #1
0
 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
예제 #2
0
    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
예제 #3
0
 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
예제 #4
0
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()
예제 #5
0
 def get_new_credential(self):
     with app.app_context():
         self.proxy = Proxy.last()
         if self.proxy:
             return self.proxy.name
         else:
             return None
예제 #6
0
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)
예제 #7
0
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')
예제 #8
0
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()
예제 #10
0
 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)
예제 #11
0
    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
예제 #12
0
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))
예제 #13
0
    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"
예제 #14
0
    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()
예제 #15
0
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
예제 #16
0
    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
예제 #17
0
    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,))
예제 #18
0
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()
예제 #19
0
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
예제 #20
0
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!")
예제 #21
0
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
예제 #22
0
    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()
예제 #23
0
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
예제 #24
0
파일: units.py 프로젝트: mikeservice/luis
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
        }
예제 #25
0
파일: units.py 프로젝트: mikeservice/luis
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))
예제 #26
0
파일: web_app.py 프로젝트: 764664/BioSE
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))
예제 #27
0
파일: web_app.py 프로젝트: 764664/BioSE
def fetch():
    with app.app_context():
        return SearchController.fetch(request.args)
예제 #28
0
 def mark_credential_as_dead(self, **kwargs):
     with app.app_context():
         return Proxy.mark_as(self.proxy.id, **kwargs)
예제 #29
0
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)
예제 #31
0
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)
예제 #32
0
 def tearDown(self):
     with app.app_context():
         db.session.remove()
         db.drop_all()
예제 #33
0
파일: web_app.py 프로젝트: 764664/BioSE
def jump():
    with app.app_context():
        return SearchController.jump(request.args)