Skip to content

Commit 7d87818

Browse files
authored
IaC - Server Settings (#129)
* Fix for settings and test * Update test content api_call
1 parent ab162c3 commit 7d87818

File tree

6 files changed

+58
-20
lines changed

6 files changed

+58
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sbin/**
1414
!opentofu/templates/cloudinit-oke.sh
1515
!src/entrypoint.sh
1616
!src/client/spring_ai/templates/env.sh
17+
tests/db_startup_temp/**
1718

1819
##############################################################################
1920
# Environment (PyVen, IDE, etc.)

src/client/content/api_server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ def copy_user_settings(new_client: ClientIdType) -> None:
4040
api_call.patch(
4141
endpoint="v1/settings",
4242
payload={"json": state.user_settings},
43+
params={"client": new_client},
4344
)
44-
st.success(f"Settings for {new_client} - Updated", icon="✅")
4545
st_common.clear_state_key(f"{new_client}_settings")
4646
except api_call.ApiError as ex:
47-
st.success(f"Settings for {new_client} - Update Failed", icon="❌")
47+
st.error(f"Settings for {new_client} - Update Failed", icon="❌")
4848
logger.error("%s Settings Update failed: %s", new_client, ex)
4949

5050

@@ -131,3 +131,4 @@ async def main() -> None:
131131

132132
if __name__ == "__main__" or "page.py" in inspect.stack()[1].filename:
133133
asyncio.run(main())
134+

src/client/utils/client.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,21 @@ def __init__(
4040
"timeout": timeout,
4141
}
4242

43-
def settings_request(method, extra_params=None):
43+
def settings_request(method):
4444
"""Send Settings to Server"""
45-
request_options = self.request_defaults.copy() # Copy defaults to avoid modifying the original
46-
if extra_params:
47-
request_options.update(extra_params)
48-
4945
with httpx.Client() as client:
5046
return client.request(
5147
method=method,
5248
url=f"{self.server_url}/v1/settings",
5349
json=self.settings,
54-
**request_options,
50+
**self.request_defaults,
5551
)
5652

5753
response = settings_request("PATCH")
5854
if response.status_code != 200:
5955
logger.error("Error updating settings with PATCH: %i - %s", response.status_code, response.text)
6056
# Retry with POST if PATCH fails
61-
response = settings_request("POST", {"params": {"client": self.settings["client"]}})
57+
response = settings_request("POST")
6258
if response.status_code != 200:
6359
logger.error("Error updating settings with POST: %i - %s", response.status_code, response.text)
6460
logger.info("Established Client")
@@ -99,3 +95,4 @@ async def get_history(self) -> list[ChatMessage]:
9995
return f"Error: {response.status_code} - {error_msg}"
10096
except httpx.ConnectError:
10197
logger.error("Unable to contact the API Server; will try again later.")
98+

src/server/endpoints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ async def settings_get(client: schema.ClientIdType) -> schema.Settings:
557557
return get_client_settings(client)
558558

559559
@auth.patch("/v1/settings", description="Update client settings")
560-
async def settings_update(payload: schema.Settings, client: schema.ClientIdType = Header(...)) -> schema.Settings:
560+
async def settings_update(payload: schema.Settings, client: schema.ClientIdType) -> schema.Settings:
561561
"""Update a single client settings"""
562562
logger.debug("Received %s Client Payload: %s", client, payload)
563563
client_settings = get_client_settings(client)

tests/client/content/test_api_server.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ def test_copy_user_settings_success(self, monkeypatch, app_test):
5656
mock_api_call.patch.assert_called_once_with(
5757
endpoint="v1/settings",
5858
payload={"json": {"key": "value"}},
59+
params={"client": "test_client"}
5960
)
6061

61-
# Verify success message was shown
62-
mock_st.success.assert_called_once_with("Settings for test_client - Updated", icon="✅")
6362
# Verify state was cleared
6463
mock_st_common.clear_state_key.assert_called_once_with("test_client_settings")
6564

@@ -92,7 +91,7 @@ def test_copy_user_settings_error(self, monkeypatch, app_test):
9291
copy_user_settings("test_client")
9392

9493
# Verify error message was shown
95-
mock_st.success.assert_called_once_with("Settings for test_client - Update Failed", icon="❌")
94+
mock_st.error.assert_called_once_with("Settings for test_client - Update Failed", icon="❌")
9695
# Verify error was logged
9796
mock_logger.error.assert_called_once()
9897

@@ -116,11 +115,14 @@ def test_copy_user_settings_real_api(self, monkeypatch, app_test):
116115
monkeypatch.setattr("client.content.api_server.state", at.session_state)
117116
monkeypatch.setattr("client.utils.api_call.state", at.session_state)
118117

119-
# Call the function
120-
copy_user_settings(TEST_CONFIG["test_client"])
121-
122-
# Assert that st.success was called (don't check the exact message)
123-
assert mock_st.success.called
118+
try:
119+
# Call the function
120+
copy_user_settings(TEST_CONFIG["test_client"])
121+
# If no exception was raised, the test passes
122+
assert True
123+
except Exception as e:
124+
# If an exception was raised, the test fails
125+
assert False, f"copy_user_settings raised an exception: {str(e)}"
124126

125127
@patch("launch_server.stop_server")
126128
@patch("launch_server.start_server")

tests/server/test_endpoints_settings.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ def test_settings_update(self, client: TestClient) -> None:
117117
)
118118

119119
# Update the settings
120-
response = client.patch("/v1/settings", headers=TEST_HEADERS, json=updated_settings.model_dump())
120+
response = client.patch(
121+
"/v1/settings",
122+
headers=TEST_HEADERS,
123+
json=updated_settings.model_dump(),
124+
params={"client": TEST_CONFIG["test_client"]},
125+
)
121126
assert response.status_code == 200
122127
updated = response.json()
123128

@@ -131,13 +136,45 @@ def test_settings_update(self, client: TestClient) -> None:
131136
assert updated["rag"]["top_k"] == 5
132137
assert updated["oci"]["auth_profile"] == "UPDATED"
133138

139+
def test_settings_copy(self, client: TestClient) -> None:
140+
"""Test copying settings for a client"""
141+
# First get the current settings for the test_client
142+
response = client.get("/v1/settings", params={"client": TEST_CONFIG["test_client"]}, headers=TEST_HEADERS)
143+
assert response.status_code == 200
144+
client_settings = response.json()
145+
146+
response = client.get("/v1/settings", params={"client": "server"}, headers=TEST_HEADERS)
147+
assert response.status_code == 200
148+
old_server_settings = response.json()
149+
150+
# Copy the client settings to the server settings
151+
response = client.patch(
152+
"/v1/settings",
153+
headers=TEST_HEADERS,
154+
json=client_settings,
155+
params={"client": "server"},
156+
)
157+
assert response.status_code == 200
158+
response = client.get("/v1/settings", params={"client": "server"}, headers=TEST_HEADERS)
159+
new_server_settings = response.json()
160+
assert old_server_settings != new_server_settings
161+
162+
del new_server_settings['client']
163+
del client_settings['client']
164+
assert new_server_settings == client_settings
165+
134166
def test_settings_update_nonexistent_client(self, client: TestClient) -> None:
135167
"""Test updating settings for a non-existent client"""
136168
headers = TEST_HEADERS.copy()
137169
headers["client"] = "nonexistent_client"
138170

139171
updated_settings = Settings(client="nonexistent_client", ll_model=LargeLanguageSettings(model="test-model"))
140172

141-
response = client.patch("/v1/settings", headers=headers, json=updated_settings.model_dump())
173+
response = client.patch(
174+
"/v1/settings",
175+
headers=headers,
176+
json=updated_settings.model_dump(),
177+
params={"client": "nonexistent_client"},
178+
)
142179
assert response.status_code == 404
143180
assert response.json() == {"detail": "Client: nonexistent_client not found."}

0 commit comments

Comments
 (0)