From 155cbd43e8ef4be25dbfc2972da2ca9c48c1a1cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=20=E6=9F=8F?= Date: Fri, 6 Dec 2024 17:21:47 +0900 Subject: [PATCH] add pytest case --- backend/app/tests/conftest.py | 85 +++++++++++++++++++--------------- backend/app/tests/test_auth.py | 8 ++++ backend/app/tests/test_user.py | 22 +++++++++ 3 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 backend/app/tests/test_auth.py create mode 100644 backend/app/tests/test_user.py diff --git a/backend/app/tests/conftest.py b/backend/app/tests/conftest.py index ab33ff6..9563086 100644 --- a/backend/app/tests/conftest.py +++ b/backend/app/tests/conftest.py @@ -6,63 +6,74 @@ import typing as t from app.core import config, security from app.db.session import Base, get_db -from app.db import models +from app.db import models,schemas from app.main import app +SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://kabAdmin:P%40ssw0rd!@kintonetooldb.postgres.database.azure.com/test" -def get_test_db_url() -> str: - return f"{config.SQLALCHEMY_DATABASE_URI}" +engine = create_engine(SQLALCHEMY_DATABASE_URI,echo=True) +test_session_maker = sessionmaker(autocommit=False, autoflush=False, bind=engine) -@pytest.fixture +@pytest.fixture(scope="function") def test_db(): - """ - Modify the db session to automatically roll back after each test. - This is to avoid tests affecting the database state of other tests. - """ - # Connect to the test database - engine = create_engine( - get_test_db_url(), - ) - connection = engine.connect() - trans = connection.begin() - - # Run a parent transaction that can roll back all changes - test_session_maker = sessionmaker( - autocommit=False, autoflush=False, bind=engine - ) - test_session = test_session_maker() - #test_session.begin_nested() - - # @event.listens_for(test_session, "after_transaction_end") - # def restart_savepoint(s, transaction): - # if transaction.nested and not transaction._parent.nested: - # s.expire_all() - # s.begin_nested() - + transaction = connection.begin() + test_session = test_session_maker(bind=connection) yield test_session - - # Roll back the parent transaction after the test is complete test_session.close() - trans.rollback() + transaction.rollback() connection.close() - @pytest.fixture(scope="function") def test_client(test_db): - """ - Get a TestClient instance that reads/write to the test database. - """ - def get_test_db(): - yield test_db + try: + yield test_db + finally: + test_db.close() app.dependency_overrides[get_db] = get_test_db with TestClient(app) as test_client: yield test_client +@pytest.fixture(scope="function") +def test_user(test_db): + password ="test" + user = models.User( + email = "test@test.com", + first_name = "test", + last_name = "abc", + hashed_password = security.get_password_hash(password), + is_active = True, + is_superuser = False + ) + test_db.add(user) + test_db.commit() + test_db.refresh(user) + dicUser = user.__dict__ + dicUser["password"] = password + return dicUser + +@pytest.fixture(scope="function") +def test_admin(test_db): + password ="admin" + user = models.User( + email = "admin@test.com", + first_name = "admin", + last_name = "abc", + hashed_password = security.get_password_hash(password), + is_active = True, + is_superuser = True + ) + test_db.add(user) + test_db.commit() + test_db.refresh(user) + dicUser = user.__dict__ + dicUser["password"] = password + return dicUser + # @pytest.fixture # def test_password() -> str: # return "securepassword" diff --git a/backend/app/tests/test_auth.py b/backend/app/tests/test_auth.py new file mode 100644 index 0000000..87baee5 --- /dev/null +++ b/backend/app/tests/test_auth.py @@ -0,0 +1,8 @@ + +def test_usr_login(test_client,test_user): + response = test_client.post("/api/token", data={"username": test_user["email"], "password": test_user["password"]}) + assert response.status_code == 200 + assert "access_token" in response.json() + assert "token_type" in response.json() + assert response.json()["user_name"] == test_user["first_name"]+ " " + test_user["last_name"] + diff --git a/backend/app/tests/test_user.py b/backend/app/tests/test_user.py new file mode 100644 index 0000000..04cdc24 --- /dev/null +++ b/backend/app/tests/test_user.py @@ -0,0 +1,22 @@ + +def test_users_list(test_client,test_user,test_admin): + response = test_client.post("/api/token", data={"username": test_user["email"], "password": test_user["password"]}) + assert "access_token" in response.json() + user_token =response.json()["access_token"] + + response = test_client.post("/api/token", data={"username": test_admin["email"], "password": test_admin["password"]}) + assert "access_token" in response.json() + admin_token =response.json()["access_token"] + + response = test_client.get("/api/v1/users", headers={"Authorization": "Bearer " + user_token}) + assert response.status_code == 200 + data = response.json() + assert len(data["data"]) == 1 + + response = test_client.get("/api/v1/users", headers={"Authorization": "Bearer " + admin_token}) + assert response.status_code == 200 + data = response.json() + assert len(data["data"]) == 2 + + + \ No newline at end of file