Lines
100 %
Functions
Branches
//! The `ssh-key-add` form and `remove-ssh-key` end to end: nomiscript form ->
//! rpc::Session -> server::command::* -> real Postgres. Gated on the `db` feature.
#![cfg(feature = "db")]
use forms::{FormValues, SSH_KEY_ADD};
use rpc::{ScriptCtx, Session};
use server::db::install_test_pool;
use server::ssh_keys::parse_authorized_keys_line;
use sqlx::PgPool;
use supp_macro::local_db_sqlx_test;
use tests_integration::session_support::{insert_test_user, setup};
use uuid::Uuid;
const LAPTOP: &str =
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID4KsQxV3UhkA3wbnZ4C5n4kg6xdInR3sydCk+Hfg6ZF laptop";
async fn user_session(pool: &PgPool) -> anyhow::Result<Session> {
let user_id = Uuid::new_v4();
insert_test_user(pool, user_id).await?;
Ok(Session::new(ScriptCtx::new(user_id)).expect("Session::new"))
}
async fn call(session: &mut Session, id: u32, form: &str) -> String {
session
.handle_form(&format!("(:id {id} :form {form})"))
.await
fn add_form(key: &str) -> String {
let mut values = FormValues::default();
values.set("key", key);
cli_core::transaction::submit_call(&SSH_KEY_ADD, &values)
fn fingerprint_of(key: &str) -> String {
parse_authorized_keys_line(key)
.expect("a valid key")
.fingerprint
#[local_db_sqlx_test]
async fn an_added_key_is_listed_with_its_fingerprint(pool: PgPool) -> anyhow::Result<()> {
let mut session = user_session(&pool).await?;
let added = call(&mut session, 1, &add_form(LAPTOP)).await;
assert!(added.contains(":value \""), "got: {added}");
let listed = call(&mut session, 2, "(list-ssh-keys)").await;
assert!(listed.contains("ssh-ed25519"), "got: {listed}");
assert!(listed.contains("laptop"), "got: {listed}");
assert!(listed.contains(&fingerprint_of(LAPTOP)), "got: {listed}");
async fn adding_a_key_twice_is_refused_at_the_key(pool: PgPool) -> anyhow::Result<()> {
call(&mut session, 1, &add_form(LAPTOP)).await;
let again = call(&mut session, 2, &add_form(LAPTOP)).await;
assert!(again.contains(":code form-invalid"), "got: {again}");
assert!(again.contains("already registered"), "got: {again}");
async fn a_key_held_by_another_account_is_refused(pool: PgPool) -> anyhow::Result<()> {
let mut owner = user_session(&pool).await?;
let mut other = user_session(&pool).await?;
call(&mut owner, 1, &add_form(LAPTOP)).await;
let stolen = call(&mut other, 2, &add_form(LAPTOP)).await;
assert!(stolen.contains(":code form-invalid"), "got: {stolen}");
let listed = call(&mut other, 3, "(list-ssh-keys)").await;
assert!(listed.contains(":value nil"), "got: {listed}");
async fn invalid_keys_are_refused_and_nothing_is_stored(pool: PgPool) -> anyhow::Result<()> {
let two_lines = format!("{LAPTOP}\n{LAPTOP}");
let rejected = [
"not-a-key",
"-----BEGIN OPENSSH PRIVATE KEY-----",
"ssh-unknown AAAAC3NzaC1lZDI1NTE5AAAAID4KsQxV3UhkA3wbnZ4C5n4kg6xdInR3sydCk+Hfg6ZF x",
"ssh-rsa AAAAC3NzaC1lZDI1NTE5AAAAID4KsQxV3UhkA3wbnZ4C5n4kg6xdInR3sydCk+Hfg6ZF x",
&two_lines,
"",
];
for (n, key) in (1..).zip(rejected) {
let response = call(&mut session, n, &add_form(key)).await;
assert!(response.contains(":code form-invalid"), "{key}: {response}");
let listed = call(&mut session, 11, "(list-ssh-keys)").await;
async fn a_key_is_removed_by_its_owner_and_not_by_another_user(pool: PgPool) -> anyhow::Result<()> {
let fingerprint = fingerprint_of(LAPTOP);
let remove = format!("(remove-ssh-key \"{fingerprint}\")");
let foreign = call(&mut other, 2, &remove).await;
assert!(foreign.contains(":code not-found"), "got: {foreign}");
let still_listed = call(&mut owner, 3, "(list-ssh-keys)").await;
assert!(still_listed.contains(&fingerprint), "got: {still_listed}");
let removed = call(&mut owner, 4, &remove).await;
assert!(removed.contains(":value t"), "got: {removed}");
let listed = call(&mut owner, 5, "(list-ssh-keys)").await;
async fn the_same_key_is_removed_once(pool: PgPool) -> anyhow::Result<()> {
let remove = format!("(remove-ssh-key \"{}\")", fingerprint_of(LAPTOP));
call(&mut session, 2, &remove).await;
let twice = call(&mut session, 3, &remove).await;
assert!(twice.contains(":code not-found"), "got: {twice}");
async fn add_is_per_user(pool: PgPool) -> anyhow::Result<()> {
let mut first = user_session(&pool).await?;
let mut second = user_session(&pool).await?;
call(&mut first, 1, &add_form(LAPTOP)).await;
let listed = call(&mut second, 2, "(list-ssh-keys)").await;