def test_existing(db: Session, client: TestClient, user: User) -> None: server = crud.server.create(db, obj_in=TEST_SERVER_CREATE) response = client.get(f"/server/{server.id}/", headers=user.auth_header) assert response.status_code == 200, response.content response_data = schemas.ServerReturn(**response.json()) assert TEST_SERVER_CREATE == schemas.ServerCreate(**response_data.dict()) assert response_data.id == server.id assert response_data.status == schemas.ServerStatus.PENDING
def create_test_server_schema(number: int) -> schemas.ServerCreate: return schemas.ServerCreate( name=f"Test server {number}", hostname=f"127.0.0.{number}", username="******", password="******", pool_key="0x0", farmer_key="0x0", )
def test_name_collision(db: Session, client: TestClient, user: User) -> None: server = crud.server.create(db, obj_in=TEST_SERVER_CREATE) servers_count_start, _ = crud.server.get_multi(db) server_different_host = schemas.ServerCreate( **(TEST_SERVER_CREATE.dict() | dict(hostname="192.168.1.1"))) response = client.post("/server/", json=server_different_host, headers=user.auth_header) assert response.status_code == 403 servers_count_end, _ = crud.server.get_multi(db) assert servers_count_start == servers_count_end
def test_normal(db: Session, client: TestClient, user: User) -> None: servers_count_start, _ = crud.server.get_multi(db) response = client.post("/server/", json=TEST_SERVER_CREATE, headers=user.auth_header) assert response.status_code == 201, response.content response_data = schemas.ServerReturn(**response.json()) assert schemas.ServerCreate(**response_data.dict()) == TEST_SERVER_CREATE server = crud.server.get(db, id=response_data.id) assert server is not None assert response_data.status == schemas.ServerStatus.PENDING servers_count_end, _ = crud.server.get_multi(db) assert servers_count_start + 1 == servers_count_end
def add_item(self, data: schemas.ServerCreateExtended) -> schemas.ServerReturn: same_name_server = crud.server.get_by_name(self.db, name=data.name) if same_name_server is not None: raise HTTPException(403, detail="Server with such name already exists") same_hostname_server = crud.server.get_by_hostname( self.db, hostname=data.hostname) if same_hostname_server is not None: raise HTTPException( 403, detail="Server with such hostname already exists") server = crud.server.create(self.db, obj_in=schemas.ServerCreate(**data.dict())) server_id = server.id for directory in data.directories: crud.directory.create( self.db, obj_in=schemas.DirectoryCreateExtended(location=directory, server_id=server_id), ) return schemas.ServerReturn.from_orm(server)
GET /server/{server_id}/ - Test existing - Test not existing - Test no auth """ from pydantic.schema import schema from tests import Session, TestClient, User from app import crud, schemas, models import uuid TEST_SERVER_CREATE = schemas.ServerCreate( name="Test server", hostname="127.0.0.1", username="******", password="******", pool_key="0x0", farmer_key="0x0", ) def test_existing(db: Session, client: TestClient, user: User) -> None: server = crud.server.create(db, obj_in=TEST_SERVER_CREATE) response = client.get(f"/server/{server.id}/", headers=user.auth_header) assert response.status_code == 200, response.content response_data = schemas.ServerReturn(**response.json()) assert TEST_SERVER_CREATE == schemas.ServerCreate(**response_data.dict()) assert response_data.id == server.id assert response_data.status == schemas.ServerStatus.PENDING