示例#1
0
 def expire_device_setting_etag_by_account_id(self, account_id: str):
     """Expire the settings' etags for all devices from a given account. Used when the settings are updated
     at account level"""
     db = connect_to_db(self.db_connection_config)
     devices = DeviceRepository(db).get_devices_by_account_id(account_id)
     for device in devices:
         self.expire_device_setting_etag_by_device_id(device.id)
示例#2
0
    def db(self):
        if 'db' not in global_context:
            global_context.db = connect_to_db(
                current_app.config['DB_CONNECTION_CONFIG']
            )

        return global_context.db
示例#3
0
 def expire_device_location_etag_by_account_id(self, account_id: str):
     """Expire the locations' etag fpr açç device for a given acccount
     :param account_id: account uuid"""
     db = connect_to_db(self.db_connection_config)
     devices = DeviceRepository(db).get_devices_by_account_id(account_id)
     for device in devices:
         self.expire_device_location_etag_by_device_id(device.id)
示例#4
0
def add_api_metric(http_status):
    """Add a row to the table tracking metric for API calls"""
    api = None
    # We are not logging metric for the public API until after the socket
    # implementation to avoid putting millions of rows a day on the table
    for api_name in ('account', 'sso', 'market', 'public'):
        if api_name in current_app.name:
            api = api_name

    if api is not None and int(http_status) != 304:
        if 'db' not in global_context:
            global_context.db = connect_to_db(
                current_app.config['DB_CONNECTION_CONFIG'])
        if 'account_id' in global_context:
            account_id = global_context.account_id
        else:
            account_id = None

        if 'device_id' in global_context:
            device_id = global_context.device_id
        else:
            device_id = None

        duration = (datetime.utcnow() - global_context.start_ts)
        api_metric = ApiMetric(access_ts=datetime.utcnow(),
                               account_id=account_id,
                               api=api,
                               device_id=device_id,
                               duration=Decimal(str(duration.total_seconds())),
                               http_method=request.method,
                               http_status=int(http_status),
                               url=global_context.url)
        metric_repository = ApiMetricsRepository(global_context.db)
        metric_repository.add(api_metric)
示例#5
0
def before_all(context):
    use_fixture(acct_api_client, context)
    context.db = connect_to_db(context.client_config['DB_CONNECTION_CONFIG'])
    agreements = add_agreements(context.db)
    context.terms_of_use = agreements[0]
    context.privacy_policy = agreements[1]
    context.open_dataset = agreements[2]
示例#6
0
def before_all(context):
    """Setup static test data before any tests run.

    This is data that does not change from test to test so it only needs to be setup
    and torn down once.
    """
    use_fixture(acct_api_client, context)
    context.db = connect_to_db(context.client_config["DB_CONNECTION_CONFIG"])
    add_agreements(context)
def validate_response(context):
    device_id = context.response.data.decode()
    account = context.accounts["foo"]
    db = connect_to_db(context.client_config["DB_CONNECTION_CONFIG"])
    device_repository = DeviceRepository(db)
    device = device_repository.get_device_by_id(device_id)

    assert_that(device, not_none())
    assert_that(device.name, equal_to("Selene Test Device"))
    assert_that(device.placement, equal_to("Mycroft Offices"))
    assert_that(device.account_id, equal_to(account.id))
示例#8
0
    def get(self, agreement_type):
        """Process HTTP GET request for an agreement."""
        db = connect_to_db(self.config['DB_CONNECTION_CONFIG'])
        agreement_repository = AgreementRepository(db)
        agreement = agreement_repository.get_active_for_type(
            self.agreement_types[agreement_type])
        if agreement is not None:
            agreement = asdict(agreement)
            del (agreement['effective_date'])
        self.response = agreement, HTTPStatus.OK

        return self.response
示例#9
0
    def db(self):
        """Connect to the mycroft database"""
        if self._db is None:
            db_connection_config = DatabaseConnectionConfig(
                host=environ['DB_HOST'],
                db_name=environ['DB_NAME'],
                password=environ['DB_PASSWORD'],
                port=environ.get('DB_PORT', 5432),
                user=environ['DB_USER'],
                sslmode=environ.get('DB_SSLMODE'),
                use_namedtuple_cursor=True)
            self._db = connect_to_db(db_connection_config)

        return self._db
示例#10
0
    def db(self):
        """Connect to the mycroft database"""
        if self._db is None:
            db_connection_config = DatabaseConnectionConfig(
                host=environ["DB_HOST"],
                db_name=environ["DB_NAME"],
                password=environ["DB_PASSWORD"],
                port=environ.get("DB_PORT", 5432),
                user=environ["DB_USER"],
                sslmode=environ.get("DB_SSLMODE"),
            )
            self._db = connect_to_db(db_connection_config)

        return self._db
示例#11
0
def get_cursor():
    """Get a cursor object for executing SQL against the DB"""
    db_connection_config = DatabaseConnectionConfig(
        host=environ["DB_HOST"],
        db_name=environ["DB_NAME"],
        password=environ["DB_PASSWORD"],
        port=environ.get("DB_PORT", 5432),
        user=environ["DB_USER"],
        sslmode=environ.get("DB_SSLMODE"),
    )
    db = connect_to_db(db_connection_config)
    cursor = Cursor(db)

    return cursor
示例#12
0
def validate_response(context):
    """Ensure that the database was updated as expected."""
    account = context.accounts["foo"]
    db = connect_to_db(context.client_config["DB_CONNECTION_CONFIG"])
    device_repository = DeviceRepository(db)
    devices = device_repository.get_devices_by_account_id(account.id)
    device = None
    for device in devices:
        if device.name == "Selene Test Device":
            break
    assert_that(device, not_none())
    assert_that(device.name, equal_to("Selene Test Device"))
    assert_that(device.placement, equal_to("Mycroft Offices"))
    assert_that(device.account_id, equal_to(account.id))
    context.device_id = device.id
示例#13
0
def before_all(context):
    """Setup static test data before any tests run.

    This is data that does not change from test to test so it only needs to be setup
    and torn down once.
    """
    _log.info("setting up test suite...")
    use_fixture(public_api_client, context)
    context.cache = SeleneCache()
    context.db = connect_to_db(context.client_config["DB_CONNECTION_CONFIG"])
    add_agreements(context)
    context.wake_words = {"hey selene": add_wake_word(context.db)}
    data_dir = mkdtemp()
    context.wake_word_dir = Path(data_dir).joinpath("wake-word")
    os.environ["SELENE_DATA_DIR"] = data_dir
示例#14
0
def get_device_subscription(context):
    membership = AccountMembership(
        start_date=date.today(),
        type='Monthly Membership',
        payment_method='Stripe',
        payment_account_id='test_monthly',
        payment_id='stripe_id'
    )
    login = context.device_login
    device_id = login['uuid']
    access_token = login['accessToken']
    headers = dict(Authorization='Bearer {token}'.format(token=access_token))
    db = connect_to_db(context.client_config['DB_CONNECTION_CONFIG'])
    AccountRepository(db).add_membership(context.account.id, membership)
    context.subscription_response = context.client.get(
        '/v1/device/{uuid}/subscription'.format(uuid=device_id),
        headers=headers
    )
示例#15
0
def get_account(context) -> Account:
    db = connect_to_db(context.client['DB_CONNECTION_CONFIG'])
    acct_repository = AccountRepository(db)
    account = acct_repository.get_account_by_id(context.account.id)

    return account
    def db(self):
        if "db" not in global_context:
            global_context.db = connect_to_db(
                current_app.config["DB_CONNECTION_CONFIG"])

        return global_context.db
def before_all(context):
    """Global setup to run before any tests."""
    use_fixture(sso_client, context)
    os.environ["SALT"] = "testsalt"
    context.db = connect_to_db(context.client_config["DB_CONNECTION_CONFIG"])
    add_agreements(context)
示例#18
0
 def expire_skill_etag_by_account_id(self, account_id):
     db = connect_to_db(self.db_connection_config)
     devices = DeviceRepository(db).get_devices_by_account_id(account_id)
     for device in devices:
         self.expire_skill_etag_by_device_id(device.id)
示例#19
0
def before_scenario(context, _):
    db = connect_to_db(context.client_config['DB_CONNECTION_CONFIG'])
    _add_agreement(context, db)
    _add_account(context, db)
示例#20
0
def after_scenario(context, _):
    db = connect_to_db(context.client_config['DB_CONNECTION_CONFIG'])
    acct_repository = AccountRepository(db)
    acct_repository.remove(context.account)
    agreement_repository = AgreementRepository(db)
    agreement_repository.remove(context.agreement, testing=True)