def handle_add_product(args): init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client = setup_product_client(protocol, host, port) # Put together the database connection's descriptor. if 'postgresql' in args: db_engine = 'postgresql' db_host = args.dbaddress db_port = args.dbport db_user = args.dbusername db_pass = args.dbpassword db_name = args.dbname else: db_engine = 'sqlite' db_host = "" db_port = 0 db_user = "" db_pass = "" db_name = args.sqlite dbc = DatabaseConnection( engine=db_engine, host=db_host, port=db_port, username_b64=convert.to_b64(db_user), password_b64=convert.to_b64(db_pass), database=db_name) # Put together the product configuration. name = convert.to_b64(args.display_name) \ if 'display_name' in args else None desc = convert.to_b64(args.description) \ if 'description' in args else None prod = ProductConfiguration( endpoint=args.endpoint, displayedName_b64=name, description_b64=desc, connection=dbc) LOG.debug("Sending request to add product...") success = client.addProduct(prod) if success: LOG.info("Product added successfully.") else: LOG.error("Adding the product has failed.") sys.exit(1)
def create_test_product(product_name, product_endpoint): # Create a new product on the secondary server. name = convert.to_b64(product_name) return ProductConfiguration(endpoint=product_endpoint, displayedName_b64=name, description_b64=name, connection=DatabaseConnection( engine='sqlite', host='', port=0, username_b64='', password_b64='', database=os.path.join( self.test_workspace_secondary, 'data.sqlite')))
def test_add_invalid_product(self): """ Test the server prohibiting the addition of bogus product configs. """ error = convert.to_b64("bogus") product_cfg = ProductConfiguration(displayedName_b64=error, description_b64=error) # Test setting up product with valid endpoint but no database # connection. with self.assertRaises(RequestFailed): cfg = deepcopy(product_cfg) cfg.endpoint = "valid" self._root_client.addProduct(cfg) # Test some invalid strings based on pattern. dbc = DatabaseConnection(engine='sqlite', host='', port=0, username_b64='', password_b64='', database="valid.sqlite") product_cfg.connection = dbc with self.assertRaises(RequestFailed): product_cfg.endpoint = "$$$$$$$" self._root_client.addProduct(product_cfg) # Test some forbidden URI parts. with self.assertRaises(RequestFailed): product_cfg.endpoint = "index.html" self._root_client.addProduct(product_cfg) with self.assertRaises(RequestFailed): product_cfg.endpoint = "CodeCheckerService" self._root_client.addProduct(product_cfg)