From 7fc256ddb07ba998b33580d428beb3d84bb18b75 Mon Sep 17 00:00:00 2001 From: agostina Date: Mon, 24 Jul 2023 17:21:45 +0200 Subject: [PATCH] LITE-28077: Getting hubs by product --- connect_ext_ppr/webapp.py | 24 ++++++++++++++++++++++ tests/api/test_products.py | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/connect_ext_ppr/webapp.py b/connect_ext_ppr/webapp.py index 236e2d1..3785830 100644 --- a/connect_ext_ppr/webapp.py +++ b/connect_ext_ppr/webapp.py @@ -35,6 +35,7 @@ ConfigurationSchema, DeploymentRequestSchema, DeploymentSchema, + HubSchema, ProductSchema, ) from connect_ext_ppr.utils import ( @@ -330,6 +331,29 @@ def list_products( response_list.append(ProductSchema(id=product.id, name=product.name, icon=product.logo)) return response_list + @router.get( + '/products/{product_id}/hubs', + summary="List all product's hub", + response_model=List[HubSchema], + ) + def list_hubs_by_product( + self, + product_id: str, + client: ConnectClient = Depends(get_installation_client), + db: VerboseBaseSession = Depends(get_db), + installation: dict = Depends(get_installation), + ): + hubs_ids = [ + h[0] for h in db.query(Deployment.hub_id).filter_by( + account_id=installation['owner']['id'], + product_id=product_id, + ).distinct() + ] + reponse_list = [] + for hub in get_hubs(client, hubs_ids): + reponse_list.append(HubSchema(id=hub['id'], name=hub['name'])) + return reponse_list + @classmethod def on_startup(cls, logger, config): # When database schema is completely defined diff --git a/tests/api/test_products.py b/tests/api/test_products.py index 5371c67..af95c04 100644 --- a/tests/api/test_products.py +++ b/tests/api/test_products.py @@ -42,3 +42,45 @@ def test_products( 'icon': product2.logo, }, ] + + +def test_hubs_by_product( + deployment_factory, + installation, + api_client, + mocker, + product_factory, +): + product1 = product_factory(id='PRD-000-001', name='Product 1') + product2 = product_factory(id='PRD-000-002', name='Product 2') + + deployment_factory(account_id='PA-159-159') + deployment_factory( + account_id=installation['owner']['id'], + product_id=product1.id, + ) + dep1 = deployment_factory( + account_id=installation['owner']['id'], + product_id=product2.id, + ) + dep2 = deployment_factory( + account_id=installation['owner']['id'], + product_id=product2.id, + hub_id='HB-123-12', + ) + + hubs = [ + {'id': dep1.hub_id, 'name': 'An awesome hub'}, + {'id': dep2.hub_id, 'name': 'Another awesome hub'}, + ] + mocker.patch('connect_ext_ppr.webapp.get_hubs', side_effect=[hubs]) + + response = api_client.get( + f'/api/products/{product2.id}/hubs', + installation=installation, + ) + + assert response.status_code == 200 + assert len(response.json()) == 2 + + assert response.json() == hubs