prole/tests/etc/test_init_1password.sh

144 lines
4.7 KiB
Bash

#!/usr/bin/env bash
# Tests for etc/init_1password.sh using a stub 'op' command.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
INIT_SCRIPT="${REPO_ROOT}/etc/init_1password.sh"
pass_count=0
fail_count=0
_pass() { echo " [PASS] $*"; pass_count=$((pass_count + 1)); }
_fail() { echo " [FAIL] $*"; fail_count=$((fail_count + 1)); }
# ---------------------------------------------------------------------------
# Test 1: --min flag skips preflight
# ---------------------------------------------------------------------------
echo "Test 1: --min mode skips preflight"
output=$(bash "$INIT_SCRIPT" --min 2>&1)
if echo "$output" | grep -q "Skipping"; then
_pass "--min skips 1Password preflight"
else
_fail "--min did not produce skip message; got: $output"
fi
# ---------------------------------------------------------------------------
# Test 2: missing op CLI exits with error
# ---------------------------------------------------------------------------
echo "Test 2: missing op CLI fails"
output=$(PATH=/nonexistent bash "$INIT_SCRIPT" 2>&1 || true)
if echo "$output" | grep -qi "not found\|install"; then
_pass "missing op CLI produces helpful error"
else
_fail "missing op CLI did not produce helpful error; got: $output"
fi
# ---------------------------------------------------------------------------
# Test 3: signed-in stub — vault already exists + administrator item exists
# ---------------------------------------------------------------------------
echo "Test 3: vault exists, administrator item exists — no create calls"
STUB_DIR="$(mktemp -d)"
trap 'rm -rf "$STUB_DIR"' EXIT
cat > "${STUB_DIR}/op" << 'STUB'
#!/usr/bin/env bash
case "${1:-} ${2:-}" in
"whoami "*) exit 0 ;;
"vault get") exit 0 ;; # knoey vault exists
"item get") exit 0 ;; # administrator item exists
"vault list") printf '[{"name":"knoey"}]'; exit 0 ;;
*) exit 0 ;;
esac
STUB
chmod +x "${STUB_DIR}/op"
op_calls_file="$(mktemp)"
cat > "${STUB_DIR}/op" << STUB
#!/usr/bin/env bash
echo "\$*" >> "${op_calls_file}"
case "\${1:-} \${2:-}" in
"whoami "*) exit 0 ;;
"vault get") exit 0 ;;
"item get") exit 0 ;;
"vault list") printf '[{"name":"knoey"}]'; exit 0 ;;
*) exit 0 ;;
esac
STUB
chmod +x "${STUB_DIR}/op"
PATH="${STUB_DIR}:$PATH" bash "$INIT_SCRIPT" >/dev/null 2>&1 || true
if ! grep -q "vault create" "${op_calls_file}" 2>/dev/null; then
_pass "vault create not called when vault already exists"
else
_fail "vault create was called unexpectedly"
fi
if ! grep -q "item create" "${op_calls_file}" 2>/dev/null; then
_pass "item create not called when administrator item exists"
else
_fail "item create was called unexpectedly"
fi
# ---------------------------------------------------------------------------
# Test 4: vault missing → vault create is called
# ---------------------------------------------------------------------------
echo "Test 4: vault missing — vault create is called"
op_calls_file2="$(mktemp)"
cat > "${STUB_DIR}/op" << STUB
#!/usr/bin/env bash
echo "\$*" >> "${op_calls_file2}"
case "\${1:-} \${2:-}" in
"whoami "*) exit 0 ;;
"vault get") exit 1 ;; # vault does NOT exist
"vault list") printf '[]'; exit 0 ;;
"vault create") exit 0 ;;
"item get") exit 0 ;;
*) exit 0 ;;
esac
STUB
chmod +x "${STUB_DIR}/op"
PATH="${STUB_DIR}:$PATH" bash "$INIT_SCRIPT" >/dev/null 2>&1 || true
if grep -q "vault create" "${op_calls_file2}" 2>/dev/null; then
_pass "vault create called when vault is absent"
else
_fail "vault create was NOT called when vault is absent"
fi
# ---------------------------------------------------------------------------
# Test 5: administrator item missing → item create is called
# ---------------------------------------------------------------------------
echo "Test 5: administrator item missing — item create is called"
op_calls_file3="$(mktemp)"
cat > "${STUB_DIR}/op" << STUB
#!/usr/bin/env bash
echo "\$*" >> "${op_calls_file3}"
case "\${1:-} \${2:-}" in
"whoami "*) exit 0 ;;
"vault get") exit 0 ;;
"item get") exit 1 ;; # administrator does NOT exist
"item create") exit 0 ;;
*) exit 0 ;;
esac
STUB
chmod +x "${STUB_DIR}/op"
PATH="${STUB_DIR}:$PATH" bash "$INIT_SCRIPT" >/dev/null 2>&1 || true
if grep -q "item create" "${op_calls_file3}" 2>/dev/null; then
_pass "item create called when administrator item is absent"
else
_fail "item create was NOT called when administrator item is absent"
fi
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
echo ""
echo "Results: ${pass_count} passed, ${fail_count} failed"
[[ "$fail_count" -eq 0 ]]