tftsr-devops_investigation/src-tauri/src/audit/log.rs
Shaun Arman 7bc23d22a2
Some checks failed
Test / rust-fmt-check (push) Successful in 1m25s
Test / frontend-typecheck (pull_request) Successful in 1m35s
Test / rust-clippy (push) Failing after 12m2s
Test / frontend-tests (pull_request) Successful in 1m23s
Test / wiki-sync (pull_request) Has been skipped
Test / rust-tests (push) Successful in 13m2s
Test / frontend-typecheck (push) Successful in 1m35s
Test / frontend-tests (push) Successful in 1m30s
Test / wiki-sync (push) Has been skipped
Test / rust-fmt-check (pull_request) Successful in 1m32s
Test / rust-clippy (pull_request) Failing after 12m10s
Test / rust-tests (pull_request) Successful in 13m22s
fix: resolve clippy uninlined_format_args in integrations and related modules
Replace format!("msg: {}", var) with format!("msg: {var}") across 8 files
to satisfy the uninlined_format_args lint (-D warnings) in CI run 178.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 12:27:26 -05:00

132 lines
3.7 KiB
Rust

use crate::db::models::AuditEntry;
/// Write an audit event to the audit_log table.
pub fn write_audit_event(
conn: &rusqlite::Connection,
action: &str,
entity_type: &str,
entity_id: &str,
details: &str,
) -> anyhow::Result<()> {
let entry = AuditEntry::new(
action.to_string(),
entity_type.to_string(),
entity_id.to_string(),
details.to_string(),
);
conn.execute(
"INSERT INTO audit_log (id, timestamp, action, entity_type, entity_id, user_id, details) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
rusqlite::params![
entry.id,
entry.timestamp,
entry.action,
entry.entity_type,
entry.entity_id,
entry.user_id,
entry.details,
],
)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn setup_test_db() -> rusqlite::Connection {
let conn = rusqlite::Connection::open_in_memory().unwrap();
conn.execute_batch(
"CREATE TABLE audit_log (
id TEXT PRIMARY KEY,
timestamp TEXT NOT NULL,
action TEXT NOT NULL,
entity_type TEXT NOT NULL DEFAULT '',
entity_id TEXT NOT NULL DEFAULT '',
user_id TEXT NOT NULL DEFAULT 'local',
details TEXT NOT NULL DEFAULT '{}'
);",
)
.unwrap();
conn
}
#[test]
fn test_write_audit_event_inserts_row() {
let conn = setup_test_db();
write_audit_event(
&conn,
"test_action",
"issue",
"issue-123",
r#"{"key":"val"}"#,
)
.expect("should insert");
let count: i64 = conn
.prepare("SELECT COUNT(*) FROM audit_log")
.unwrap()
.query_row([], |row| row.get(0))
.unwrap();
assert_eq!(count, 1);
}
#[test]
fn test_write_audit_event_correct_fields() {
let conn = setup_test_db();
write_audit_event(&conn, "create_issue", "issue", "abc-999", "details here")
.expect("should insert");
let (action, entity_type, entity_id, user_id): (String, String, String, String) = conn
.prepare("SELECT action, entity_type, entity_id, user_id FROM audit_log LIMIT 1")
.unwrap()
.query_row([], |row| {
Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?))
})
.unwrap();
assert_eq!(action, "create_issue");
assert_eq!(entity_type, "issue");
assert_eq!(entity_id, "abc-999");
assert_eq!(user_id, "local");
}
#[test]
fn test_write_multiple_events() {
let conn = setup_test_db();
for i in 0..5 {
write_audit_event(
&conn,
&format!("action_{i}"),
"test",
&format!("id_{i}"),
"{}",
)
.unwrap();
}
let count: i64 = conn
.prepare("SELECT COUNT(*) FROM audit_log")
.unwrap()
.query_row([], |row| row.get(0))
.unwrap();
assert_eq!(count, 5);
}
#[test]
fn test_write_audit_event_generates_unique_ids() {
let conn = setup_test_db();
write_audit_event(&conn, "a", "t", "1", "{}").unwrap();
write_audit_event(&conn, "b", "t", "2", "{}").unwrap();
let mut stmt = conn.prepare("SELECT id FROM audit_log").unwrap();
let ids: Vec<String> = stmt
.query_map([], |row| row.get(0))
.unwrap()
.filter_map(|r| r.ok())
.collect();
assert_eq!(ids.len(), 2);
assert_ne!(ids[0], ids[1]);
}
}