1+ import pytest
2+ from fastapi .testclient import TestClient
3+ from sqlmodel import Session , select
4+
5+ from app import crud
6+ from app .core .config import settings
7+ from app .core .security import verify_password
8+ from app .models import User , UserCreate
9+ from app .tests .utils .utils import random_email , random_lower_string
10+ from app .models import Organization , OrganizationCreate , OrganizationUpdate
11+ from app .api .deps import get_db
12+ from app .main import app
13+ from app .crud .organization import create_organization , get_organization_by_id
14+
15+ client = TestClient (app )
16+
17+ @pytest .fixture
18+ def test_organization (db : Session , superuser_token_headers : dict [str , str ]):
19+ unique_name = f"TestOrg-{ random_lower_string ()} "
20+ org_data = OrganizationCreate (name = unique_name , is_active = True )
21+ organization = create_organization (session = db , org_create = org_data )
22+ db .commit ()
23+ return organization
24+
25+ # Test retrieving organizations
26+ def test_read_organizations (db : Session , superuser_token_headers : dict [str , str ]):
27+ response = client .get (f"{ settings .API_V1_STR } /organizations/" , headers = superuser_token_headers )
28+ assert response .status_code == 200
29+ response_data = response .json ()
30+ assert "data" in response_data
31+ assert isinstance (response_data ["data" ], list )
32+
33+ # Test creating an organization
34+ def test_create_organization (db : Session , superuser_token_headers : dict [str , str ]):
35+ unique_name = f"Org-{ random_lower_string ()} "
36+ org_data = {"name" : unique_name , "is_active" : True }
37+ response = client .post (
38+ f"{ settings .API_V1_STR } /organizations/" , json = org_data , headers = superuser_token_headers
39+ )
40+
41+ assert 200 <= response .status_code < 300
42+ created_org = response .json ()
43+ assert "data" in created_org # Make sure there's a 'data' field
44+ created_org_data = created_org ["data" ]
45+ org = get_organization_by_id (session = db , org_id = created_org_data ["id" ])
46+ assert org is not None # The organization should be found in the DB
47+ assert org .name == created_org_data ["name" ]
48+ assert org .is_active == created_org_data ["is_active" ]
49+
50+
51+ def test_update_organization (db : Session , test_organization : Organization , superuser_token_headers : dict [str , str ]):
52+ unique_name = f"UpdatedOrg-{ random_lower_string ()} " # Ensure a unique name
53+ update_data = {"name" : unique_name , "is_active" : False }
54+
55+ response = client .patch (
56+ f"{ settings .API_V1_STR } /organizations/{ test_organization .id } " ,
57+ json = update_data ,
58+ headers = superuser_token_headers ,
59+ )
60+
61+ assert response .status_code == 200
62+ updated_org = response .json ()["data" ]
63+ assert "name" in updated_org
64+ assert updated_org ["name" ] == update_data ["name" ]
65+ assert "is_active" in updated_org
66+ assert updated_org ["is_active" ] == update_data ["is_active" ]
67+
68+
69+ # Test deleting an organization
70+ def test_delete_organization (db : Session , test_organization : Organization , superuser_token_headers : dict [str , str ]):
71+ response = client .delete (
72+ f"{ settings .API_V1_STR } /organizations/{ test_organization .id } " , headers = superuser_token_headers
73+ )
74+ assert response .status_code == 200
75+ response = client .get (f"{ settings .API_V1_STR } /organizations/{ test_organization .id } " , headers = superuser_token_headers )
76+ assert response .status_code == 404
77+
0 commit comments