agentmux_srv/server/app_api/
client_terms.rs1use super::*;
9
10pub(crate) const TERMS_ARE_THE_PERSONS: &str = "only the person accepts the terms: ask the person to accept them in the AgentMux window";
12
13pub fn register(engine: &Arc<WshRpcEngine>, state: &AppState) {
14 let state = state.clone();
15 engine.register_typed(COMMAND_CLIENT_AGREE_TOS, move |_req: Option<NoArgsReq>, ctx| {
16 let state = state.clone();
17 async move {
18 ctx.operator_only(TERMS_ARE_THE_PERSONS)?;
19 let mut client = crate::backend::wcore::get_client(&state.mstore).map_err(|e| e.to_string())?;
20 client.tosagreed = chrono::Utc::now().timestamp_millis();
21 state.mstore.update(&mut client).map_err(|e| e.to_string())?;
22 Ok(ClientAgreeTosResult { tosagreed: client.tosagreed })
23 }
24 });
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30 use crate::backend::service::{WebCallType, WebReturnType};
31
32 fn tosagreed(state: &AppState) -> i64 {
33 crate::backend::wcore::get_client(&state.mstore).unwrap().tosagreed
34 }
35
36 async fn agree(state: &AppState, ctx: RpcContext) -> (serde_json::Value, String) {
37 let (engine, mut rx) = WshRpcEngine::new();
38 register(&engine, state);
39 engine.set_rpc_context(ctx);
40 engine.handle_message(RpcMessage {
41 command: COMMAND_CLIENT_AGREE_TOS.to_string(),
42 reqid: "req-1".to_string(),
43 data: Some(json!({})),
44 ..Default::default()
45 });
46 let resp = tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv()).await.unwrap().unwrap();
47 (resp.data.unwrap_or_default(), resp.error)
48 }
49
50 #[tokio::test]
51 async fn an_agent_connection_even_one_carrying_the_proof_cannot_accept_the_terms() {
52 let state = crate::server::tests::test_state();
53 let before = tosagreed(&state);
54 let agent = RpcContext { agent_id: "korp".into(), operator_proven: true, ..Default::default() };
55 let (_, error) = agree(&state, agent).await;
56 assert!(error.starts_with("FORBIDDEN: only the person accepts the terms"), "{error}");
57 assert!(error.contains("ask the person to accept them in the AgentMux window"), "{error}");
58 assert_eq!(tosagreed(&state), before);
59 }
60
61 #[tokio::test]
62 async fn a_connection_that_has_not_proved_it_is_the_window_cannot_accept_the_terms() {
63 let state = crate::server::tests::test_state();
64 let before = tosagreed(&state);
65 let (_, error) = agree(&state, RpcContext::default()).await;
66 assert!(error.contains("isn't the AgentMux window"), "{error}");
67 assert_eq!(tosagreed(&state), before);
68 }
69
70 #[tokio::test]
71 async fn the_window_accepts_the_terms() {
72 let state = crate::server::tests::test_state();
73 let (data, error) = agree(&state, RpcContext::operator_for_tests()).await;
74 assert_eq!(error, "");
75 let stored = tosagreed(&state);
76 assert!(stored > 0);
77 assert_eq!(data["tosagreed"], json!(stored));
78 }
79
80 #[tokio::test]
81 async fn the_service_route_refuses_the_terms_and_says_where_the_person_accepts_them() {
82 let state = crate::server::tests::test_state();
83 let before = tosagreed(&state);
84 let call = WebCallType { service: "client".into(), method: "AgreeTos".into(), uicontext: None, args: vec![] };
85 let reply: WebReturnType = crate::server::service::run_service_call(&state, &call).await;
86 assert!(!reply.success);
87 assert_eq!(reply.error.as_deref(), Some(TERMS_ARE_THE_PERSONS));
88 assert_eq!(tosagreed(&state), before);
89 }
90}