Spaces:
Runtime error
Runtime error
Commit ·
bb4b25f
1
Parent(s): d454ab0
Added Test to test Login Route
Browse filesFiles changed (2)
- tests/database.py+2-3
- tests/test_users.py+22-7
11 unmodified lines12131415151617183 unmodified lines222324252526272829303132323311 unmodified linesengine = create_engine(SQLALCHEMY_DATABASE_URL)TestingSessionMaker = sessionmaker(autoflush=False, autocommit=False, bind=engine)
@pytest.fixture(scope="module")@pytest.fixture()def session(): Base.metadata.drop_all(bind=engine) Base.metadata.create_all(bind=engine)3 unmodified lines finally: db.close()
@pytest.fixture(scope="module")@pytest.fixture()def client(session): def override_get_db(): try: yield session finally: session.close()
app.dependency_overrides[get_db] = override_get_db yield TestClient(app)1213456456778910111213141516171819201 unmodified line22232415162526272829303132import pytestfrom app.config import settingfrom app.schemas import userfrom app.schemas import user, tokenfrom jose import jwtfrom tests.database import client, session
def test_root(client): response = client.get("/") assert response.status_code == 200 assert response.json().get('message') == "FastAPI Project"@pytest.fixturedef create_test_user(client): user_data={ "email": "abdullah@gmail.com", "password": "abdullah1234" } response = client.post("/users/",json=user_data) assert response.status_code == 201 new_user=response.json() new_user["password"]="abdullah1234" return new_user
def test_create_user(client): response = client.post("/users/", json={"email": "abdullah@gmail.com", "password": "abdullah1234"})1 unmodified line assert response.status_code == 201 assert new_user.email == "abdullah@gmail.com"
def test_login_user(client): response = client.post("/login", data={"username": "abdullah@gmail.com", "password": "abdullah1234"})def test_login_user(client,create_test_user): response = client.post("/login", data={"username": create_test_user["email"], "password": create_test_user["password"]}) login_response=token.Token(**response.json()) payload = jwt.decode(login_response.access_token, setting.secret_key, algorithms=[setting.algorithm]) id=payload.get("id") assert id == create_test_user["id"] assert login_response.token_type == "bearer" assert response.status_code==200tests/database.py+2-3
tests/test_users.py+22-7