blob: c6e7a8b0b752f51df7455618258b18a291fe6caa (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#!/usr/bin/env bash
#
# record-api-responses.sh
# Records TT-RSS API v2 responses as JSON fixtures for unit tests.
#
# Usage:
# ./scripts/record-api-responses.sh -u <username> -p '<password>' -h https://example.com/tt-rss
# # or via environment:
# export TTRSS_URL_ENV=https://example.com/tt-rss
# export TTRSS_USER_ENV=<username>
# export TTRSS_PASS_ENV='<password>'
# ./scripts/record-api-responses.sh
#
# Individual endpoints can be recorded by passing endpoint names:
# ./scripts/record-api-responses.sh -u <user> -p '<pass>' -h https://example.com/tt-rss -- feeds headlines
#
# Available endpoints: feeds headlines article_detail categories feed_tree unread counters config version labels login login_error not_logged_in
set -euo pipefail
# ── Defaults ──────────────────────────────────────────────────────────────
OUTPUT_DIR=""
ARTICLE_ID=""
ALL_ENDPOINTS="feeds headlines article_detail categories feed_tree unread counters config version labels login login_error not_logged_in"
ENDPOINTS_TO_RECORD=()
# ── Parse arguments ───────────────────────────────────────────────────────
while [[ $# -gt 0 ]]; do
case "$1" in
-u|--user) TTRSS_USER="$2"; shift 2 ;;
-p|--pass) TTRSS_PASS="$2"; shift 2 ;;
-h|--host) TTRSS_URL="$2"; shift 2 ;;
-o|--output) OUTPUT_DIR="$2"; shift 2 ;;
--) ENDPOINTS_TO_RECORD=("${@:2}"); break ;;
*) ENDPOINTS_TO_RECORD+=("$1"); shift ;;
esac
done
# Fall back to environment variables (only if not set via args)
if [[ -z "${TTRSS_URL:-}" ]]; then TTRSS_URL="$TTRSS_URL_ENV"; fi
if [[ -z "${TTRSS_USER:-}" ]]; then TTRSS_USER="$TTRSS_USER_ENV"; fi
if [[ -z "${TTRSS_PASS:-}" ]]; then TTRSS_PASS="$TTRSS_PASS_ENV"; fi
# Resolve output directory (default: src/test/resources/responses/)
if [[ -z "$OUTPUT_DIR" ]]; then
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
OUTPUT_DIR="$PROJECT_DIR/org.fox.ttrss/src/test/resources/responses"
fi
# ── Validation ────────────────────────────────────────────────────────────
if [[ -z "$TTRSS_URL" || -z "$TTRSS_USER" || -z "$TTRSS_PASS" ]]; then
echo "Error: TTRSS_URL, TTRSS_USER, and TTRSS_PASS must be set (via args or env)."
echo " Example: ./scripts/record-api-responses.sh -u <user> -p '<pass>' -h https://example.com/tt-rss"
exit 1
fi
mkdir -p "$OUTPUT_DIR"
# Strip trailing slash
TTRSS_URL="${TTRSS_URL%/}"
BASE_URL="$TTRSS_URL/api/v2"
# ── Determine which endpoints to record ───────────────────────────────────
if [[ ${#ENDPOINTS_TO_RECORD[@]} -eq 0 ]]; then
ENDPOINTS_TO_RECORD=($ALL_ENDPOINTS)
fi
# Validate endpoint names
for ep in "${ENDPOINTS_TO_RECORD[@]}"; do
if [[ ! " $ALL_ENDPOINTS " =~ " $ep " ]]; then
echo "Warning: Unknown endpoint '$ep', skipping."
continue
fi
done
# ── Login ─────────────────────────────────────────────────────────────────
echo "Logging in to $TTRSS_URL ..."
LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/auth/login" \
-H "Content-Type: text/json" \
-d "{\"user\":\"$TTRSS_USER\", \"password\": \"$TTRSS_PASS\"}")
SESSION_ID=$(echo "$LOGIN_RESPONSE" | grep -o '"session_id":"[^"]*"' | cut -d'"' -f4)
if [[ -z "$SESSION_ID" ]]; then
echo "Error: Login failed. Response: $LOGIN_RESPONSE"
exit 1
fi
echo " Session ID: $SESSION_ID"
# Save login response (replace real session_id with placeholder)
echo "$LOGIN_RESPONSE" | sed "s/$SESSION_ID/SESSION_ID_PLACEHOLDER/g" > "$OUTPUT_DIR/login.json"
echo " Saved: login.json"
# ── Record endpoints ─────────────────────────────────────────────────────
record_endpoint() {
local name="$1"
local method="${2:-GET}"
local path="$3"
local body="${4:-}"
local headers="${5:-}"
local outfile="$OUTPUT_DIR/${name}.json"
if [[ "$method" == "GET" ]]; then
local response
response=$(curl -s "$BASE_URL/$path" \
-H "X-Session-ID: $SESSION_ID" \
$headers)
elif [[ "$method" == "POST" ]]; then
local response
response=$(curl -s -X POST "$BASE_URL/$path" \
-H "Content-Type: text/json" \
-H "X-Session-ID: $SESSION_ID" \
$headers \
-d "$body")
fi
echo "$response" > "$outfile"
local size
size=$(wc -c < "$outfile")
echo " Saved: ${name}.json ($size bytes)"
}
# Record each requested endpoint
for ep in "${ENDPOINTS_TO_RECORD[@]}"; do
case "$ep" in
feeds)
record_endpoint "feeds" "GET" "feeds"
;;
headlines)
record_endpoint "headlines" "GET" "articles?unread=true&limit=20&sort=updated&order=descending"
;;
article_detail)
# Need an article ID from headlines first
if [[ -z "$ARTICLE_ID" && -f "$OUTPUT_DIR/headlines.json" ]]; then
ARTICLE_ID=$(grep -o '"id":[0-9]*' "$OUTPUT_DIR/headlines.json" | head -1 | cut -d: -f2)
fi
if [[ -z "$ARTICLE_ID" ]]; then
echo " Skipping: article_detail (no article_id available, record headlines first)"
continue
fi
record_endpoint "article_detail" "GET" "articles/$ARTICLE_ID"
;;
categories)
record_endpoint "categories" "GET" "categories"
;;
feed_tree)
record_endpoint "feed_tree" "GET" "feed-tree"
;;
unread)
record_endpoint "unread" "GET" "unread"
;;
counters)
record_endpoint "counters" "GET" "counters"
;;
config)
record_endpoint "config" "GET" "config"
;;
version)
record_endpoint "version" "GET" "version"
;;
labels)
if [[ -z "$ARTICLE_ID" && -f "$OUTPUT_DIR/headlines.json" ]]; then
ARTICLE_ID=$(grep -o '"id":[0-9]*' "$OUTPUT_DIR/headlines.json" | head -1 | cut -d: -f2)
fi
if [[ -z "$ARTICLE_ID" ]]; then
echo " Skipping: labels (no article_id available, record headlines first)"
continue
fi
record_endpoint "labels" "GET" "labels?article_id=$ARTICLE_ID"
;;
login_error)
record_endpoint "login_error" "POST" "auth/login" '{"user":"unknown","password":"wrong"}'
;;
not_logged_in)
outfile="$OUTPUT_DIR/not_logged_in.json"
response=$(curl -s "$BASE_URL/feeds" -H "X-Session-ID: invalid-session")
echo "$response" > "$outfile"
size=$(wc -c < "$outfile")
echo " Saved: not_logged_in.json ($size bytes)"
;;
esac
done
echo ""
echo "Done. All fixtures saved to: $OUTPUT_DIR"
|