# shellcheck shell=bash DESCRIPTION="JSON through JavaScript for Automation, and secrets in the login keychain" TIER=quick source "$CONNECTOR/configure.d/lib/json.sh" source "$CONNECTOR/configure.d/lib/keychain.sh" scenario() { local file="$TMPDIR/smoke.json" pass check_eq "json_get reads a value" abc123 \ "$(echo '{"ocs":{"data":{"apppassword":"abc123"}}}' | json_get 'd.ocs.data.apppassword')" check_not "json_get fails on false" bash -c "source '$CONNECTOR/configure.d/lib/json.sh'; echo '{\"m\":false}' | json_get d.m" check_not "json_get fails on bad JSON" bash -c "source '$CONNECTOR/configure.d/lib/json.sh'; echo nope | json_get d.m" check_eq "json_get passes arguments as a[]" true \ "$(echo '{"x":"a b"}' | json_get 'd.x === a[0]' 'a b')" rm -f "$file" check "json_edit makes a file" json_edit "$file" 'd.k = {"m.x": a[0]};' 'v"1' check_eq "json_edit wrote it" 'v"1' "$(json_get 'd.k["m.x"]' < "$file")" check "json_edit changes it" json_edit "$file" 'd.n = 2;' check_eq "json_edit kept what was there" 'v"1' "$(json_get 'd.k["m.x"]' < "$file")" echo 'not json' > "$file" check_not "json_edit leaves bad JSON alone" json_edit "$file" 'd.n = 3;' check_eq "...and doesn't change it" 'not json' "$(cat "$file")" pass="$(openssl rand -base64 24)" check "keychain_save" keychain_save federated-smoke test "$pass" check_eq "keychain_load" "$pass" "$(keychain_load federated-smoke test)" check "keychain_save replaces" keychain_save federated-smoke test abc check_eq "keychain_load gets the new one" abc "$(keychain_load federated-smoke test)" check_not "keychain_save refuses a secret with spaces or quotes" keychain_save federated-smoke bad 'a b"c' check "keychain_delete" keychain_delete federated-smoke test check_eq "it's gone" "" "$(keychain_load federated-smoke test)" }