Use Pydantic to validate the server config file.

This commit is contained in:
grossmj committed 2021-04-12 17:02:23 +09:30
1 parent 478119b40d
commit 30ebae207f
61 files changed
+871 -695

No files matched your search

+1 -2
View File
@@ -41,9 +41,8 @@ async def test_interfaces(app: FastAPI, client: AsyncClient) -> None:
assert isinstance(response.json(), list)
async def test_version_output(app: FastAPI, client: AsyncClient, config) -> None:
async def test_version_output(app: FastAPI, client: AsyncClient) -> None:
config.set("Server", "local", "true")
response = await client.get(app.url_path_for("compute_version"))
assert response.status_code == status.HTTP_200_OK
assert response.json() == {'local': True, 'version': __version__}
+6 -6
View File
@@ -158,10 +158,10 @@ async def test_close_project_invalid_uuid(app: FastAPI, client: AsyncClient) ->
assert response.status_code == status.HTTP_404_NOT_FOUND
async def test_get_file(app: FastAPI, client: AsyncClient, tmpdir) -> None:
async def test_get_file(app: FastAPI, client: AsyncClient, config, tmpdir) -> None:
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
config.settings.Server.projects_path = str(tmpdir)
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
with open(os.path.join(project.path, "hello"), "w+") as f:
f.write("world")
@@ -179,10 +179,10 @@ async def test_get_file(app: FastAPI, client: AsyncClient, tmpdir) -> None:
assert response.status_code == status.HTTP_404_NOT_FOUND
async def test_write_file(app: FastAPI, client: AsyncClient, tmpdir) -> None:
async def test_write_file(app: FastAPI, client: AsyncClient, config, tmpdir) -> None:
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
config.settings.Server.projects_path = str(tmpdir)
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
response = await client.post(app.url_path_for("write_compute_project_file",
project_id=project.id,
+4 -4
View File
@@ -425,9 +425,9 @@ async def test_create_img_relative(app: FastAPI, client: AsyncClient):
assert response.status_code == status.HTTP_204_NO_CONTENT
async def test_create_img_absolute_non_local(app: FastAPI, client: AsyncClient, config: dict) -> None:
async def test_create_img_absolute_non_local(app: FastAPI, client: AsyncClient, config) -> None:
config.set("Server", "local", "false")
config.settings.Server.local = False
params = {
"qemu_img": "/tmp/qemu-img",
"path": "/tmp/hda.qcow2",
@@ -443,9 +443,9 @@ async def test_create_img_absolute_non_local(app: FastAPI, client: AsyncClient,
assert response.status_code == 403
async def test_create_img_absolute_local(app: FastAPI, client: AsyncClient, config: dict) -> None:
async def test_create_img_absolute_local(app: FastAPI, client: AsyncClient, config) -> None:
config.set("Server", "local", "true")
config.settings.Server.local = True
params = {
"qemu_img": "/tmp/qemu-img",
"path": "/tmp/hda.qcow2",
@@ -30,7 +30,7 @@ pytestmark = pytest.mark.asyncio
async def test_shutdown_local(app: FastAPI, client: AsyncClient, config: Config) -> None:
os.kill = MagicMock()
config.set("Server", "local", True)
config.settings.Server.local = True
response = await client.post(app.url_path_for("shutdown"))
assert response.status_code == status.HTTP_204_NO_CONTENT
assert os.kill.called
@@ -38,7 +38,7 @@ async def test_shutdown_local(app: FastAPI, client: AsyncClient, config: Config)
async def test_shutdown_non_local(app: FastAPI, client: AsyncClient, config: Config) -> None:
config.set("Server", "local", False)
config.settings.Server.local = False
response = await client.post(app.url_path_for("shutdown"))
assert response.status_code == status.HTTP_403_FORBIDDEN
+1 -1
View File
@@ -178,7 +178,7 @@ async def test_open_project(app: FastAPI, client: AsyncClient, project: Project)
async def test_load_project(app: FastAPI, client: AsyncClient, project: Project, config) -> None:
config.set("Server", "local", "true")
config.settings.Server.local = True
with asyncio_patch("gns3server.controller.Controller.load_project", return_value=project) as mock:
response = await client.post(app.url_path_for("load_project"), json={"path": "/tmp/test.gns3"})
assert response.status_code == status.HTTP_201_CREATED
+5 -4
View File
@@ -131,7 +131,7 @@ class TestAuthTokens:
config: Config
) -> None:
jwt_secret = config.get_section_config("Server").get("jwt_secret_key", DEFAULT_JWT_SECRET_KEY)
jwt_secret = config.settings.Controller.jwt_secret_key
token = auth_service.create_access_token(test_user.username)
payload = jwt.decode(token, jwt_secret, algorithms=["HS256"])
username = payload.get("sub")
@@ -139,7 +139,7 @@ class TestAuthTokens:
async def test_token_missing_user_is_invalid(self, app: FastAPI, client: AsyncClient, config: Config) -> None:
jwt_secret = config.get_section_config("Server").get("jwt_secret_key", DEFAULT_JWT_SECRET_KEY)
jwt_secret = config.settings.Controller.jwt_secret_key
token = auth_service.create_access_token(None)
with pytest.raises(jwt.JWTError):
jwt.decode(token, jwt_secret, algorithms=["HS256"])
@@ -171,11 +171,12 @@ class TestAuthTokens:
test_user: User,
wrong_secret: str,
wrong_token: Optional[str],
config,
) -> None:
token = auth_service.create_access_token(test_user.username)
if wrong_secret == "use correct secret":
wrong_secret = auth_service._server_config.get("jwt_secret_key", DEFAULT_JWT_SECRET_KEY)
wrong_secret = config.settings.Controller.jwt_secret_key
if wrong_token == "use correct token":
wrong_token = token
with pytest.raises(HTTPException):
@@ -192,7 +193,7 @@ class TestUserLogin:
config: Config
) -> None:
jwt_secret = config.get_section_config("Server").get("jwt_secret_key", DEFAULT_JWT_SECRET_KEY)
jwt_secret = config.settings.Controller.jwt_secret_key
client.headers["content-type"] = "application/x-www-form-urlencoded"
login_data = {
"username": test_user.username,
+1 -2
View File
@@ -25,9 +25,8 @@ from gns3server.version import __version__
pytestmark = pytest.mark.asyncio
async def test_version_output(app: FastAPI, client: AsyncClient, config) -> None:
async def test_version_output(app: FastAPI, client: AsyncClient) -> None:
config.set("Server", "local", "true")
response = await client.get(app.url_path_for("get_version"))
assert response.status_code == status.HTTP_200_OK
assert response.json() == {'local': True, 'version': __version__}