Add site selection and config override

This commit is contained in:
Dejvino
2026-07-30 17:55:58 +02:00
parent 160388194a
commit 8d1488f354
5 changed files with 78 additions and 5 deletions
+28
View File
@@ -34,6 +34,13 @@ SERVER="http://127.0.0.1:$PORT"
CATEGORIES="3,7"
TAGS="10"
EOF
# Deliberately sparse: only the fields that differ from config.sh, to prove
# --site layers on top of the default config rather than replacing it.
cat > "$HOME/.config/wordpress-rest-curl/othersite.sh" <<EOF
USER="other-tester"
PASSWORD="other-pass"
CATEGORIES="1"
EOF
POST_FILE="$WORKDIR/post.md"
cat > "$POST_FILE" <<'EOF2'
@@ -77,6 +84,27 @@ REQ=$(cat "$MOCK_WP_REQUEST_FILE")
assert_eq "edit: path includes post ID" "/wp-json/wp/v2/posts/99" "$(jq -r '.path' <<<"$REQ")"
assert_eq "edit: status defaults to draft" "draft" "$(jq -r '.payload.status' <<<"$REQ")"
# --- --site layers on top of config.sh: overrides what it sets, inherits the rest ---
rm -f "$MOCK_WP_REQUEST_FILE" "$MOCK_WP_CONTROL_FILE"
run_post_sh --site othersite --file "$POST_FILE" >/dev/null
assert_eq "site: exit code" "0" "$?"
REQ=$(cat "$MOCK_WP_REQUEST_FILE")
assert_eq "site: categories overridden by site config" "[1]" "$(jq -c '.payload.categories' <<<"$REQ")"
assert_eq "site: tags inherited from default config" "[10]" "$(jq -c '.payload.tags' <<<"$REQ")"
assert_eq "site: title transform inherited from default config" "My Test Title" "$(jq -r '.payload.title' <<<"$REQ")"
rm -f "$MOCK_WP_REQUEST_FILE" "$MOCK_WP_CONTROL_FILE"
OUT=$(run_post_sh --site does-not-exist --file "$POST_FILE" 2>&1)
assert_eq "site: unknown site exits non-zero" "1" "$?"
assert_eq "site: unknown site error message" "true" "$(grep -q "Config file not found" <<<"$OUT" && echo true || echo false)"
# --- no --site and no default config.sh: clean error, not a crash ---
mv "$HOME/.config/wordpress-rest-curl/config.sh" "$WORKDIR/config.sh.bak"
OUT=$(run_post_sh --file "$POST_FILE" 2>&1)
assert_eq "no config: exits non-zero" "1" "$?"
assert_eq "no config: error message" "true" "$(grep -q "No site specified and no default config found" <<<"$OUT" && echo true || echo false)"
mv "$WORKDIR/config.sh.bak" "$HOME/.config/wordpress-rest-curl/config.sh"
# --- non-2xx response is surfaced and script exits non-zero ---
rm -f "$MOCK_WP_REQUEST_FILE"
echo '{"status": 401, "body": {"code": "rest_forbidden", "message": "Sorry, you are not allowed to do that."}}' > "$MOCK_WP_CONTROL_FILE"