diff --git a/apihub/subscription/models.py b/apihub/subscription/models.py index e97867c..23b3619 100644 --- a/apihub/subscription/models.py +++ b/apihub/subscription/models.py @@ -13,7 +13,7 @@ from sqlalchemy.orm import relationship from ..common.db_session import Base -from .schemas import SubscriptionTier +from .schemas import SubscriptionTier, ApplicationCreate, SubscriptionPricingCreate class Application(Base): @@ -34,6 +34,19 @@ class Application(Base): def __str__(self): return f"{self.name} || {self.url}" + def to_schema(self, with_pricing=False) -> ApplicationCreate: + return ApplicationCreate( + name=self.name, + url=self.url, + description=self.description, + pricing=[ + SubscriptionPricingCreate( + tier=pricing.tier, price=pricing.price, credit=pricing.credit, application=self.name, + ) + for pricing in self.subscriptions_pricing + ] if with_pricing else [], + ) + class SubscriptionPricing(Base): """ diff --git a/apihub/subscription/queries.py b/apihub/subscription/queries.py index 117afc1..2f3656d 100644 --- a/apihub/subscription/queries.py +++ b/apihub/subscription/queries.py @@ -76,34 +76,17 @@ def get_application(self, name: str) -> ApplicationCreate: """ try: application = self.get_query().filter(Application.name == name).one() - application_create = ApplicationCreate( - name=application.name, - url=application.url, - description=application.description, - pricing=[], - ) - for pricing in application.subscriptions_pricing: - application_create.pricing.append( - SubscriptionPricingBase( - tier=pricing.tier, - price=pricing.price, - credit=pricing.credit, - ) - ) - return application_create + return application.to_schema(with_pricing=True) except NoResultFound: raise ApplicationException(f"Application {name} not found.") - def get_applications(self) -> List[ApplicationCreate]: + def get_applications(self, username=None) -> List[ApplicationCreate]: """ List applications. :return: List of applications. """ - - return [ - self.get_application(application.name) - for application in self.get_query().all() - ] + applications = map(lambda x: x.to_schema(), self.get_query().all()) + return list(applications) class SubscriptionPricingQuery(BaseQuery): @@ -285,6 +268,8 @@ def get_active_subscriptions(self, username: str) -> List[SubscriptionDetails]: balance=subscription.balance, expires_at=subscription.expires_at, recurring=subscription.recurring, + created_by=subscription.created_by, + created_at=subscription.created_at, ) for subscription in subscriptions ] diff --git a/apihub/subscription/router.py b/apihub/subscription/router.py index 6c85710..68d153a 100644 --- a/apihub/subscription/router.py +++ b/apihub/subscription/router.py @@ -54,15 +54,17 @@ def create_application( @router.get("/application", response_model=List[ApplicationCreate]) def get_applications( session: Session = Depends(create_session), - username: str = Depends(require_admin), + user: UserBase = Depends(require_token), ): + """ + List all applications. + """ + try: - """ - List all applications. - """ - return ApplicationQuery(session).get_applications() - except ApplicationException: - raise HTTPException(400, "Error while retrieving applications") + applications = ApplicationQuery(session).get_applications() + return applications + except ApplicationException as e: + raise HTTPException(400, detail=str(e)) @router.get("/application/{application}", response_model=ApplicationCreate) @@ -163,10 +165,12 @@ def get_active_subscriptions( return [] return [ - SubscriptionTokenResponse( + SubscriptionIn( username=subscription.username, application=subscription.application, - expires_time=subscription.expires_at, + tier=subscription.tier, + expires_at=subscription.expires_at, + recurring=subscription.recurring, ) for subscription in subscriptions ] diff --git a/tests/test_subscription.py b/tests/test_subscription.py index f0b6faf..723ac92 100644 --- a/tests/test_subscription.py +++ b/tests/test_subscription.py @@ -124,6 +124,24 @@ def api_function_2( UserFactory._meta.sqlalchemy_session_persistence = "commit" UserFactory(username="tester", role=UserType.USER) + ApplicationFactory._meta.sqlalchemy_session = db_session + ApplicationFactory._meta.sqlalchemy_session_persistence = "commit" + application = ApplicationFactory(name="test", url="/test") + + SubscriptionPricingFactory._meta.sqlalchemy_session = db_session + SubscriptionPricingFactory._meta.sqlalchemy_session_persistence = "commit" + pricing = SubscriptionPricingFactory( + tier=SubscriptionTier.TRIAL, + price=100, + credit=100, + application="test", + ) + + SubscriptionFactory._meta.sqlalchemy_session = db_session + SubscriptionFactory._meta.sqlalchemy_session_persistence = "commit" + + SubscriptionFactory(username="tester", application="test", credit=100) + yield TestClient(app) @@ -138,7 +156,7 @@ def _require_user_token(): class TestApplication: def test_create_application(self, client): new_application = ApplicationCreate( - name="test", + name="app", url="/test", description="test", pricing=[ @@ -160,53 +178,21 @@ def test_create_application(self, client): assert response.status_code == 200 response = client.get( - "/application/test", + "/application/app", ) response_json = response.json() assert len(response_json["pricing"]) == 3 def test_list_application(self, client, db_session): - ApplicationFactory._meta.sqlalchemy_session = db_session - ApplicationFactory._meta.sqlalchemy_session_persistence = "commit" - ApplicationFactory(name="application", url="/test") - ApplicationFactory(name="application2", url="/test2") - - SubscriptionPricingFactory._meta.sqlalchemy_session = db_session - SubscriptionPricingFactory._meta.sqlalchemy_session_persistence = "commit" - SubscriptionPricingFactory( - tier=SubscriptionTier.TRIAL, - price=100, - credit=100, - application="application", - ) - SubscriptionPricingFactory( - tier=SubscriptionTier.TRIAL, - price=200, - credit=200, - application="application2", - ) response = client.get("/application") assert response.status_code == 200 response_json = response.json() - assert len(response_json) == 2 + assert len(response_json) == 1 def test_get_application(self, client, db_session): - ApplicationFactory._meta.sqlalchemy_session = db_session - ApplicationFactory._meta.sqlalchemy_session_persistence = "commit" - ApplicationFactory(name="application", url="/test") - - SubscriptionPricingFactory._meta.sqlalchemy_session = db_session - SubscriptionPricingFactory._meta.sqlalchemy_session_persistence = "commit" - SubscriptionPricingFactory( - tier=SubscriptionTier.TRIAL, - price=100, - credit=100, - application="application", - ) - response = client.get( - "/application/application", + "/application/test", ) assert response.status_code == 200 response_json = response.json() @@ -257,6 +243,8 @@ def test_get_all_subscriptions(self, client): "/subscription", ) assert response.status_code == 200 + response_json = response.json() + assert len(response_json) == 1 def test_create_subscription_not_existing_user(self, client): new_subscription = SubscriptionIn(