Пример #1
0
    def _build_response(self):
        """
        Build the response data to return to the device.

        The pairing code generated may already exist for another device. So,
        continue to generate pairing codes until one that does not already
        exist is created.
        """
        response_data = dict(
            state=self.request.args['state'],
            token=self._generate_token(),
            expiration=ONE_DAY
        )
        pairing_code_added = False
        while not pairing_code_added:
            pairing_code = self._generate_pairing_code()
            _log.debug('Generated pairing code ' + pairing_code)
            response_data.update(code=pairing_code)
            pairing_code_added = self.cache.set_if_not_exists_with_expiration(
                DEVICE_PAIRING_CODE_KEY.format(pairing_code=pairing_code),
                value=json.dumps(response_data),
                expiration=ONE_DAY
            )
            log_msg = 'Pairing code {pairing_code} exists, generating new code'
            _log.debug(log_msg.format(pairing_code=pairing_code))

        return response_data
Пример #2
0
def validate_pairing_code_removal(context):
    """Ensure that the endpoint removed the pairing code entry from the cache."""
    cache = SeleneCache()
    pairing_data = cache.get(
        DEVICE_PAIRING_CODE_KEY.format(pairing_code=context.pairing_code)
    )
    assert_that(pairing_data, none())
Пример #3
0
def validate_pairing_code(pairing_code):
    """Ensure the pairing code exists in the cache of valid pairing codes."""
    cache_key = DEVICE_PAIRING_CODE_KEY.format(pairing_code=pairing_code)
    cache = SeleneCache()
    pairing_cache = cache.get(cache_key)

    if pairing_cache is None:
        raise ValidationError("pairing code not found")
Пример #4
0
 def _pair_device(self):
     """Add the paired device to the database."""
     cache_key = DEVICE_PAIRING_CODE_KEY.format(
         pairing_code=self.validated_request["pairing_code"])
     pairing_data = self._get_pairing_data(cache_key)
     device_id = self._add_device()
     pairing_data["uuid"] = device_id
     self.cache.delete(cache_key)
     self._build_pairing_token(pairing_data)
Пример #5
0
    def _get_pairing_data(self, pairing_code: str) -> dict:
        """Checking if there's one pairing session for the pairing code.

        :param pairing_code: the six character pairing code
        :return: the pairing code information from the Redis database
        """
        cache_key = DEVICE_PAIRING_CODE_KEY.format(pairing_code=pairing_code)
        pairing_cache = self.cache.get(cache_key)
        pairing_data = json.loads(pairing_cache)

        return pairing_data
Пример #6
0
def check_cached_pairing_data(context):
    """Confirm that the pairing data stored in Redis is as expected."""
    pairing_code_key = DEVICE_PAIRING_CODE_KEY.format(
        pairing_code=context.response.json["code"])
    pairing_data = context.cache.get(pairing_code_key)
    pairing_data = json.loads(pairing_data)
    context.cache.delete(pairing_code_key)
    assert_that(pairing_data, has_key("token"))
    assert_that(pairing_data["code"], equal_to(context.response.json["code"]))
    assert_that(pairing_data["expiration"], equal_to(ONE_DAY))
    assert_that(pairing_data["state"], equal_to(context.state))
    assert_that(pairing_data["packaging_type"], equal_to("pantacor"))
Пример #7
0
def add_device(context):
    """Imitate the logic in the account API to pair a device"""
    pairing_code_key = DEVICE_PAIRING_CODE_KEY.format(
        pairing_code=context.pairing_response.json['code'])
    pairing_data = context.cache.get(pairing_code_key)
    pairing_data = json.loads(pairing_data)
    pairing_data.update(uuid=context.device_id)
    context.cache.set_with_expiration(key=DEVICE_PAIRING_TOKEN_KEY.format(
        pairing_token=pairing_data['token']),
                                      value=json.dumps(pairing_data),
                                      expiration=ONE_MINUTE)
    context.cache.delete(pairing_code_key)
Пример #8
0
    def _add_pairing_code_to_cache(self, response_data):
        """Add data necessary to activate the device to cache for retrieval."""
        cache_key = DEVICE_PAIRING_CODE_KEY.format(pairing_code=response_data["code"])
        cache_value = dict(**response_data)
        core_packaging_type = self.request.args.get("packaging")
        if core_packaging_type is not None:
            cache_value.update(packaging_type=core_packaging_type)
        added_to_cache = self.cache.set_if_not_exists_with_expiration(
            cache_key, value=json.dumps(cache_value), expiration=ONE_DAY
        )
        if not added_to_cache:
            log_msg = "Pairing code {pairing_code} exists, generating new code"
            _log.debug(log_msg.format(pairing_code=response_data["pairing_code"]))

        return added_to_cache
Пример #9
0
 def _pair_device(self, device: dict):
     """Add the paired device to the database."""
     self.db.autocommit = False
     try:
         pairing_data = self._get_pairing_data(device["pairing_code"])
         device_id = self._add_device(device)
         pairing_data["uuid"] = device_id
         self.cache.delete(
             DEVICE_PAIRING_CODE_KEY.format(
                 pairing_code=device["pairing_code"]))
         self._build_pairing_token(pairing_data)
     except Exception:
         self.db.rollback()
         raise
     else:
         self.db.commit()