def get_token_data_for_access_key(access_key): """ Fetches a token for an access key, if it has one. """ try: return TripitAccessToken.as_dict(access_key) except Exception as access_token_error: logger.warning("Failed to get an access token: %s", access_token_error) return None
def delete_existing_access_tokens(access_key): """ Delete access tokens associated with an access key, if any found. """ logger.debug("Deleting existing access tokens for key %s", access_key) try: TripitAccessToken.delete_tokens_by_access_key(access_key) except (GetError, TripitAccessToken.DoesNotExist): logger.warning("No request tokens found for key: %s", access_key)
def get_query_parameter(event, parameter): """ Finds a query parameter from an event. """ try: return event["queryStringParameters"][parameter] except (KeyError, TypeError): logger.warning("Couldn't find parameter %s in event %s", parameter, json.dumps(event)) return None
def get_access_key(event): """ Gets an access key from an event. """ try: return event["requestContext"]["identity"]["apiKey"] except (KeyError, TypeError): logger.warning("Failed to get access key from event, %s", json.dumps(event["requestContext"])) return None
def get_endpoint(event): """ Get an endpoint path from an event. """ try: return event["requestContext"]["path"] except (KeyError, TypeError): logger.warning( "Failed to get endpoint path from requestContext, %s", json.dumps(event["requestContext"]), ) return None
def as_dict(access_key, **attributes): """ Returns the token data mapped to this access key as a hash. """ try: data = TripitAccessToken.get(access_key, **attributes) return { "access_key": access_key, "token": data.token, "token_secret": data.token_secret, } except (TableDoesNotExist, GetError): logger.warning("Access token not created yet for key %s", access_key) return None
def delete_tokens_by_access_key(access_key): """ Deletes a token associated with an access key. """ try: existing_request_token_mapping = TripitAccessToken.get(access_key) existing_request_token_mapping.delete() existing_request_token_mapping.save() existing_request_token_mapping.refresh() return None except TransactWriteError as failed_write_error: logger.error("Failed to write new data for ak %s: %s", access_key, failed_write_error) return None except TableDoesNotExist: logger.warning("Access token not created yet for key %s", access_key) return None
def _find_element(self, element_id, element_type="*"): """ Attempts to find a matching element, first by ID, then by (slower) XPath. """ try: return self.driver.find_element_by_id(element_id) except NoSuchElementException: """ Note that finding elements by XPath will return the first element that matches the value being sought after. This might be the wrong element! """ try: xpath_query = f"//{element_type}[contains(text(), '{element_id}')]" return self.driver.find_element(By.XPATH, xpath_query) except NoSuchElementException: logger.warning("Element not found: %s", element_id) return None
def wait_for_persistence(): """ Ensure that the endpoint that we are using for persisting tokens is available before running any tests. """ if not os.environ.get("AWS_DYNAMODB_ENDPOINT_URL"): return True db_parts = urllib.parse.urlparse(os.environ.get("AWS_DYNAMODB_ENDPOINT_URL")) db_host = db_parts.netloc.split(":")[0] db_port = db_parts.netloc.split(":")[1] while True: db_socket = socket.socket() try: logger.info("Connected to persistence at %s", db_parts.netloc) db_socket.connect((db_host, int(db_port))) db_socket.close() break except socket.error: logger.warning("Failed to connect to persistence at %s.", db_parts.netloc) time.sleep(1) continue
def read_secret(secret_name): """ Reads a secret. """ if path.exists(_secret_path(secret_name)): return Path(_secret_path(secret_name)).read_text() logger.warning("Unable to retrieve secret during integration test: %s", secret_name) return None