-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #830 from JoshuaOloton/feat/get-single-billing-plan
feat: get single billing plan
- Loading branch information
Showing
3 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from datetime import datetime, timezone | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from fastapi.testclient import TestClient | ||
from sqlalchemy.orm import Session | ||
from uuid_extensions import uuid7 | ||
|
||
from api.db.database import get_db | ||
from api.v1.models.organization import Organization | ||
from api.v1.services.user import user_service | ||
from api.v1.services.billing_plan import billing_plan_service | ||
from api.v1.models.user import User | ||
from api.v1.models.billing_plan import BillingPlan | ||
from main import app | ||
|
||
|
||
|
||
@pytest.fixture | ||
def db_session_mock(): | ||
db_session = MagicMock(spec=Session) | ||
return db_session | ||
|
||
@pytest.fixture | ||
def client(db_session_mock): | ||
app.dependency_overrides[get_db] = lambda: db_session_mock | ||
client = TestClient(app) | ||
yield client | ||
app.dependency_overrides = {} | ||
|
||
|
||
def mock_get_current_user(): | ||
return User( | ||
id=str(uuid7()), | ||
email="[email protected]", | ||
password=user_service.hash_password("Testuser@123"), | ||
first_name='Test', | ||
last_name='User', | ||
is_active=True, | ||
is_super_admin=True, | ||
created_at=datetime.now(timezone.utc), | ||
updated_at=datetime.now(timezone.utc) | ||
) | ||
|
||
def mock_org(): | ||
return Organization( | ||
id=str(uuid7()), | ||
name="Test Organization", | ||
created_at=datetime.now(timezone.utc), | ||
updated_at=datetime.now(timezone.utc) | ||
) | ||
|
||
def mock_billing_plan(): | ||
return BillingPlan( | ||
id=str(uuid7()), | ||
organization_id=mock_org().id, | ||
name="Premium Plan", | ||
price=49.99, | ||
currency="NGN", # Currency code | ||
duration="1 year", # Duration of the plan | ||
description="This is a premium billing plan with extra features.", | ||
features=["Feature 1", "Feature 2", "Feature 3"], | ||
updated_at=datetime.now(timezone.utc) | ||
) | ||
|
||
|
||
|
||
def test_get_billing_plan(client, db_session_mock): | ||
'''Test to successfully create a new product category''' | ||
|
||
# Mock the user service to return the current user | ||
app.dependency_overrides[user_service.get_current_user] = lambda: mock_get_current_user | ||
app.dependency_overrides[billing_plan_service.fetch] = lambda: mock_billing_plan | ||
|
||
db_session_mock.add.return_value = None | ||
db_session_mock.commit.return_value = None | ||
db_session_mock.refresh.return_value = None | ||
|
||
mock_plan_instance = mock_billing_plan() | ||
|
||
|
||
with patch("api.v1.services.billing_plan.BillingPlanService.fetch", return_value=mock_plan_instance) as mock_fetch: | ||
|
||
response = client.get(f'/api/v1/organizations/billing-plans/{mock_plan_instance.id}') | ||
|
||
assert response.status_code == 200 | ||
|
||
|
||
def test_get_plan_unauthorized(client, db_session_mock): | ||
'''Test for unauthorized user''' | ||
|
||
mock_plan_instance = mock_billing_plan() | ||
|
||
|
||
with patch("api.v1.services.billing_plan.BillingPlanService.fetch", return_value=mock_plan_instance) as mock_fetch: | ||
|
||
response = client.get(f'/api/v1/organizations/billing-plans/{mock_plan_instance.id}') | ||
|
||
assert response.status_code == 401 |