| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# Matrix E2E Test Environment Setup Script |
| 4 |
# |
| 5 |
# This script performs all the steps required to set up the Matrix Synapse server |
| 6 |
# for end-to-end testing of Shoutrrr's Matrix service. |
| 7 |
# |
| 8 |
# Usage: |
| 9 |
# ./setup.sh [OPTIONS] [COMMAND] |
| 10 |
# |
| 11 |
# Commands: |
| 12 |
# generate-config Generate the Synapse server configuration |
| 13 |
# start-server Start the Matrix Synapse server using docker compose |
| 14 |
# create-user Create the admin test user |
| 15 |
# create-room Create the test room with alias #test:localhost |
| 16 |
# setup-all Run all setup steps in order (default) |
| 17 |
# |
| 18 |
# Options: |
| 19 |
# --help, -h Show this help message |
| 20 |
# --verbose, -v Enable verbose output |
| 21 |
# |
| 22 |
# Environment: |
| 23 |
# The script automatically loads .env file from its directory if present. |
| 24 |
# Required variables for create-user: |
| 25 |
# None (uses defaults: admin/admin) |
| 26 |
# Required variables for create-room: |
| 27 |
# SHOUTRRR_MATRIX_HOST |
| 28 |
# SHOUTRRR_MATRIX_USER |
| 29 |
# SHOUTRRR_MATRIX_PASSWORD |
| 30 |
# SHOUTRRR_MATRIX_ROOM (optional, defaults to #test:localhost) |
| 31 |
# SHOUTRRR_MATRIX_DISABLE_TLS (optional) |
| 32 |
# |
| 33 |
|
| 34 |
set -euo pipefail |
| 35 |
|
| 36 |
# ============================================================================= |
| 37 |
# Configuration and Constants |
| 38 |
# ============================================================================= |
| 39 |
|
| 40 |
# Color codes for output |
| 41 |
readonly RED='\033[0;31m' |
| 42 |
readonly GREEN='\033[0;32m' |
| 43 |
readonly YELLOW='\033[1;33m' |
| 44 |
readonly BLUE='\033[0;34m' |
| 45 |
readonly NC='\033[0m' # No Color |
| 46 |
|
| 47 |
# Script configuration |
| 48 |
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 49 |
DATA_DIR="${SCRIPT_DIR}/data" |
| 50 |
ENV_FILE="${SCRIPT_DIR}/.env" |
| 51 |
ELEMENT_ENV_FILE="${SCRIPT_DIR}/element.env" |
| 52 |
COMPOSE_FILE="${SCRIPT_DIR}/docker-compose.yaml" |
| 53 |
|
| 54 |
# Default values |
| 55 |
DEFAULT_MATRIX_USER="admin" |
| 56 |
DEFAULT_MATRIX_PASSWORD="admin" |
| 57 |
DEFAULT_MATRIX_HOST="localhost:8008" |
| 58 |
DEFAULT_MATRIX_ROOM="#test:localhost" |
| 59 |
|
| 60 |
# Rate limiting configuration for E2E testing |
| 61 |
RATE_LIMIT_CONFIG='# Rate limiting configuration - effectively disabled for E2E testing |
| 62 |
# Using very high values to allow rapid test execution without rate limits. |
| 63 |
# See: https://element-hq.github.io/synapse/latest/usage/configuration/config_documentation.html#ratelimiting |
| 64 |
rc_message: |
| 65 |
per_second: 1000 |
| 66 |
burst_count: 10000 |
| 67 |
|
| 68 |
rc_registration: |
| 69 |
per_second: 1000 |
| 70 |
burst_count: 10000 |
| 71 |
|
| 72 |
rc_login: |
| 73 |
address: |
| 74 |
per_second: 1000 |
| 75 |
burst_count: 10000 |
| 76 |
account: |
| 77 |
per_second: 1000 |
| 78 |
burst_count: 10000 |
| 79 |
failed_attempts: |
| 80 |
per_second: 1000 |
| 81 |
burst_count: 10000 |
| 82 |
|
| 83 |
rc_joins: |
| 84 |
local: |
| 85 |
per_second: 1000 |
| 86 |
burst_count: 10000 |
| 87 |
remote: |
| 88 |
per_second: 1000 |
| 89 |
burst_count: 10000' |
| 90 |
|
| 91 |
# Verbose mode |
| 92 |
VERBOSE=false |
| 93 |
|
| 94 |
# Docker compose command (detected in check_requirements) |
| 95 |
COMPOSE_CMD="" |
| 96 |
|
| 97 |
# ============================================================================= |
| 98 |
# Helper Functions |
| 99 |
# ============================================================================= |
| 100 |
|
| 101 |
# Print an error message and exit |
| 102 |
error() { |
| 103 |
echo -e "${RED}[ERROR]${NC} $1" >&2 |
| 104 |
exit 1 |
| 105 |
} |
| 106 |
|
| 107 |
# Print an info message |
| 108 |
info() { |
| 109 |
echo -e "${GREEN}[INFO]${NC} $1" |
| 110 |
} |
| 111 |
|
| 112 |
# Print a warning message |
| 113 |
warn() { |
| 114 |
echo -e "${YELLOW}[WARN]${NC} $1" |
| 115 |
} |
| 116 |
|
| 117 |
# Print a debug message (only in verbose mode) |
| 118 |
debug() { |
| 119 |
if [[ "$VERBOSE" == "true" ]]; then |
| 120 |
echo -e "${BLUE}[DEBUG]${NC} $1" |
| 121 |
fi |
| 122 |
} |
| 123 |
|
| 124 |
# Print usage information |
| 125 |
usage() { |
| 126 |
echo "Matrix E2E Test Environment Setup Script" |
| 127 |
echo "" |
| 128 |
echo "This script performs all the steps required to set up the Matrix Synapse server" |
| 129 |
echo "for end-to-end testing of Shoutrrr's Matrix service." |
| 130 |
echo "" |
| 131 |
echo "Usage:" |
| 132 |
echo " ./setup.sh [OPTIONS] [COMMAND]" |
| 133 |
echo "" |
| 134 |
echo "Commands:" |
| 135 |
echo " generate-config Generate the Synapse server configuration" |
| 136 |
echo " start-server Start the Matrix Synapse server using docker compose" |
| 137 |
echo " create-user Create the admin test user" |
| 138 |
echo " create-room Create the test room with alias #test:localhost" |
| 139 |
echo " setup-all Run all setup steps in order (default)" |
| 140 |
echo "" |
| 141 |
echo "Options:" |
| 142 |
echo " --help, -h Show this help message" |
| 143 |
echo " --verbose, -v Enable verbose output" |
| 144 |
echo "" |
| 145 |
echo "Environment:" |
| 146 |
echo " The script automatically loads .env file from its directory if present." |
| 147 |
echo " Required variables for create-user:" |
| 148 |
echo " None (uses defaults: admin/admin)" |
| 149 |
echo " Required variables for create-room:" |
| 150 |
echo " SHOUTRRR_MATRIX_HOST" |
| 151 |
echo " SHOUTRRR_MATRIX_USER" |
| 152 |
echo " SHOUTRRR_MATRIX_PASSWORD" |
| 153 |
echo " SHOUTRRR_MATRIX_ROOM (optional, defaults to #test:localhost)" |
| 154 |
echo " SHOUTRRR_MATRIX_DISABLE_TLS (optional)" |
| 155 |
echo "" |
| 156 |
echo "Examples:" |
| 157 |
echo " ./setup.sh # Run all setup steps" |
| 158 |
echo " ./setup.sh generate-config # Generate configuration only" |
| 159 |
echo " ./setup.sh start-server # Start the server only" |
| 160 |
echo " ./setup.sh --verbose setup-all # Run with verbose output" |
| 161 |
} |
| 162 |
|
| 163 |
# Load environment variables from .env file |
| 164 |
load_env_file() { |
| 165 |
if [[ -f "$ENV_FILE" ]]; then |
| 166 |
debug "Loading environment variables from ${ENV_FILE}" |
| 167 |
set -a |
| 168 |
source "$ENV_FILE" |
| 169 |
set +a |
| 170 |
info "Loaded environment variables from ${ENV_FILE}" |
| 171 |
else |
| 172 |
debug "No .env file found at ${ENV_FILE}" |
| 173 |
fi |
| 174 |
} |
| 175 |
|
| 176 |
# Check if required commands are available |
| 177 |
check_requirements() { |
| 178 |
local missing_cmds=() |
| 179 |
|
| 180 |
# Check for docker |
| 181 |
if ! command -v docker &> /dev/null; then |
| 182 |
missing_cmds+=("docker") |
| 183 |
fi |
| 184 |
|
| 185 |
# Check for docker compose (plugin or standalone) |
| 186 |
if docker compose version &> /dev/null 2>&1; then |
| 187 |
COMPOSE_CMD="docker compose" |
| 188 |
elif command -v docker-compose &> /dev/null; then |
| 189 |
COMPOSE_CMD="docker-compose" |
| 190 |
else |
| 191 |
missing_cmds+=("docker-compose") |
| 192 |
fi |
| 193 |
|
| 194 |
# Check for curl |
| 195 |
if ! command -v curl &> /dev/null; then |
| 196 |
missing_cmds+=("curl") |
| 197 |
fi |
| 198 |
|
| 199 |
# Check for jq |
| 200 |
if ! command -v jq &> /dev/null; then |
| 201 |
missing_cmds+=("jq") |
| 202 |
fi |
| 203 |
|
| 204 |
if [[ ${#missing_cmds[@]} -gt 0 ]]; then |
| 205 |
error "Missing required commands: ${missing_cmds[*]}" |
| 206 |
fi |
| 207 |
|
| 208 |
debug "All required commands are available" |
| 209 |
debug "Using compose command: ${COMPOSE_CMD}" |
| 210 |
} |
| 211 |
|
| 212 |
# Wait for the Matrix server to be ready |
| 213 |
wait_for_server() { |
| 214 |
local base="$1" |
| 215 |
local max_attempts="${2:-30}" |
| 216 |
local attempt=1 |
| 217 |
|
| 218 |
info "Waiting for Matrix server at ${base} to be ready..." |
| 219 |
|
| 220 |
while [[ $attempt -le $max_attempts ]]; do |
| 221 |
if curl -s -o /dev/null -w "%{http_code}" "${base}/_matrix/client/versions" 2>/dev/null | grep -q "200"; then |
| 222 |
info "Matrix server is ready!" |
| 223 |
return 0 |
| 224 |
fi |
| 225 |
|
| 226 |
debug "Attempt ${attempt}/${max_attempts}: Server not ready yet..." |
| 227 |
sleep 2 |
| 228 |
((attempt++)) |
| 229 |
done |
| 230 |
|
| 231 |
error "Matrix server did not become ready after ${max_attempts} attempts" |
| 232 |
} |
| 233 |
|
| 234 |
# ============================================================================= |
| 235 |
# Setup Functions |
| 236 |
# ============================================================================= |
| 237 |
|
| 238 |
# Generate the Synapse server configuration |
| 239 |
# This creates the necessary configuration files in the data directory |
| 240 |
generate_config() { |
| 241 |
info "Generating Matrix Synapse configuration..." |
| 242 |
|
| 243 |
# Check for element.env file |
| 244 |
if [[ ! -f "$ELEMENT_ENV_FILE" ]]; then |
| 245 |
warn "element.env not found at ${ELEMENT_ENV_FILE}" |
| 246 |
info "Creating default element.env file..." |
| 247 |
|
| 248 |
cat > "$ELEMENT_ENV_FILE" << 'EOF' |
| 249 |
# Matrix Synapse configuration |
| 250 |
# Server name |
| 251 |
SYNAPSE_SERVER_NAME=localhost |
| 252 |
# Enable registration |
| 253 |
SYNAPSE_ALLOW_GUEST_ACCESS=true |
| 254 |
# Enable registration without verification |
| 255 |
SYNAPSE_ALLOW_INSECURE_REGISTRATION_TOKEN=true |
| 256 |
EOF |
| 257 |
fi |
| 258 |
|
| 259 |
# Create data directory if it doesn't exist |
| 260 |
if [[ ! -d "$DATA_DIR" ]]; then |
| 261 |
mkdir -p "$DATA_DIR" |
| 262 |
debug "Created data directory at ${DATA_DIR}" |
| 263 |
fi |
| 264 |
|
| 265 |
# Run the Synapse generate command |
| 266 |
info "Running Synapse configuration generator..." |
| 267 |
docker run --rm \ |
| 268 |
-u "$(id -u):$(id -g)" \ |
| 269 |
-v "${DATA_DIR}:/data" \ |
| 270 |
--env-file "$ELEMENT_ENV_FILE" \ |
| 271 |
matrixdotorg/synapse:latest generate |
| 272 |
|
| 273 |
if [[ $? -eq 0 ]]; then |
| 274 |
info "Configuration generated successfully in ${DATA_DIR}" |
| 275 |
else |
| 276 |
error "Failed to generate configuration" |
| 277 |
fi |
| 278 |
|
| 279 |
# Add rate limiting configuration to homeserver.yaml |
| 280 |
# This must be done before starting the server to ensure the configuration takes effect |
| 281 |
local homeserver_yaml="${DATA_DIR}/homeserver.yaml" |
| 282 |
if [[ -f "$homeserver_yaml" ]]; then |
| 283 |
if ! grep -q "^rc_message:" "$homeserver_yaml" 2>/dev/null; then |
| 284 |
info "Adding rate limiting config to homeserver.yaml..." |
| 285 |
echo "$RATE_LIMIT_CONFIG" >> "$homeserver_yaml" |
| 286 |
info "Rate limiting configuration added" |
| 287 |
else |
| 288 |
debug "Rate limiting config already present in homeserver.yaml" |
| 289 |
fi |
| 290 |
fi |
| 291 |
} |
| 292 |
|
| 293 |
# Disable rate limiting in the Synapse configuration via Admin API |
| 294 |
# This uses the Synapse Admin API to override rate limiting per user |
| 295 |
disable_rate_limiting() { |
| 296 |
# Load environment variables |
| 297 |
load_env_file |
| 298 |
|
| 299 |
# Get configuration from environment variables |
| 300 |
local host="${SHOUTRRR_MATRIX_HOST:-${DEFAULT_MATRIX_HOST}}" |
| 301 |
local user="${SHOUTRRR_MATRIX_USER:-${DEFAULT_MATRIX_USER}}" |
| 302 |
local password="${SHOUTRRR_MATRIX_PASSWORD:-${DEFAULT_MATRIX_PASSWORD}}" |
| 303 |
|
| 304 |
# Validate required variables |
| 305 |
if [[ -z "$host" ]]; then |
| 306 |
error "SHOUTRRR_MATRIX_HOST is not set" |
| 307 |
fi |
| 308 |
if [[ -z "$user" ]]; then |
| 309 |
error "SHOUTRRR_MATRIX_USER is not set" |
| 310 |
fi |
| 311 |
if [[ -z "$password" ]]; then |
| 312 |
error "SHOUTRRR_MATRIX_PASSWORD is not set" |
| 313 |
fi |
| 314 |
|
| 315 |
local homeserver_yaml="${DATA_DIR}/homeserver.yaml" |
| 316 |
|
| 317 |
info "Disabling rate limiting for user '${user}' at ${host}..." |
| 318 |
|
| 319 |
# First, disable rate limiting in the global config (if not already done) |
| 320 |
if [[ -f "$homeserver_yaml" ]]; then |
| 321 |
if ! grep -q "^rc_message:" "$homeserver_yaml" 2>/dev/null; then |
| 322 |
info "Adding rate limiting config to homeserver.yaml..." |
| 323 |
echo "$RATE_LIMIT_CONFIG" >> "$homeserver_yaml" |
| 324 |
else |
| 325 |
debug "Rate limiting config already present in homeserver.yaml" |
| 326 |
fi |
| 327 |
fi |
| 328 |
|
| 329 |
# Get base URL using the helper function (handles TLS setting) |
| 330 |
local base_url |
| 331 |
base_url=$(get_base_url "$host") |
| 332 |
|
| 333 |
# Wait for server to be ready |
| 334 |
wait_for_server "$base_url" 30 |
| 335 |
|
| 336 |
info "Using base URL: ${base_url}" |
| 337 |
|
| 338 |
# Login to get access token using the existing login function |
| 339 |
local admin_token |
| 340 |
admin_token=$(login "$base_url" "$user" "$password") |
| 341 |
|
| 342 |
# Now disable rate limiting for the user via Admin API |
| 343 |
info "Disabling rate limit for user via Admin API..." |
| 344 |
local response |
| 345 |
response=$( |
| 346 |
curl -s -X POST "${base_url}/_synapse/admin/v1/users/@${user}:localhost/override_ratelimit" \ |
| 347 |
-H "Content-Type: application/json" \ |
| 348 |
-H "Authorization: Bearer ${admin_token}" \ |
| 349 |
-d '{ |
| 350 |
"messages_per_second": 0, |
| 351 |
"burst_count": 0 |
| 352 |
}' 2>&1 |
| 353 |
) || error "Failed to disable rate limiting for user" |
| 354 |
|
| 355 |
# Check for errors in response |
| 356 |
local error_msg |
| 357 |
error_msg=$(echo "$response" | jq -r '.errcode // empty' 2>/dev/null || true) |
| 358 |
|
| 359 |
if [[ -n "$error_msg" && "$error_msg" != "null" ]]; then |
| 360 |
local error_desc |
| 361 |
error_desc=$(echo "$response" | jq -r '.error // "Unknown error"' 2>/dev/null || true) |
| 362 |
error "Failed to disable rate limiting: ${error_msg} - ${error_desc}" |
| 363 |
fi |
| 364 |
|
| 365 |
info "Rate limit override response for ${user}: $response" |
| 366 |
|
| 367 |
# Also disable for testuser if it exists |
| 368 |
info "Disabling rate limit for testuser via Admin API..." |
| 369 |
response=$( |
| 370 |
curl -s -X POST "${base_url}/_synapse/admin/v1/users/@testuser:localhost/override_ratelimit" \ |
| 371 |
-H "Content-Type: application/json" \ |
| 372 |
-H "Authorization: Bearer ${admin_token}" \ |
| 373 |
-d '{ |
| 374 |
"messages_per_second": 0, |
| 375 |
"burst_count": 0 |
| 376 |
}' 2>&1 |
| 377 |
) || warn "Failed to disable rate limiting for testuser (user may not exist)" |
| 378 |
|
| 379 |
# Check for errors - but don't fail if testuser doesn't exist |
| 380 |
error_msg=$(echo "$response" | jq -r '.errcode // empty' 2>/dev/null || true) |
| 381 |
|
| 382 |
if [[ -n "$error_msg" && "$error_msg" != "null" ]]; then |
| 383 |
# Only warn if it's not a "not found" error |
| 384 |
local error_desc |
| 385 |
error_desc=$(echo "$response" | jq -r '.error // "Unknown error"' 2>/dev/null || true) |
| 386 |
if [[ "$error_msg" == "M_NOT_FOUND" ]]; then |
| 387 |
info "testuser does not exist, skipping rate limit override" |
| 388 |
else |
| 389 |
warn "Failed to disable rate limiting for testuser: ${error_msg} - ${error_desc}" |
| 390 |
fi |
| 391 |
else |
| 392 |
info "Rate limit override response for testuser: $response" |
| 393 |
fi |
| 394 |
|
| 395 |
info "Rate limiting disabled successfully via Admin API" |
| 396 |
} |
| 397 |
|
| 398 |
# Start the Matrix Synapse server using docker compose |
| 399 |
start_server() { |
| 400 |
info "Starting Matrix Synapse server..." |
| 401 |
|
| 402 |
# Check if docker compose file exists |
| 403 |
if [[ ! -f "$COMPOSE_FILE" ]]; then |
| 404 |
error "Docker compose file not found at ${COMPOSE_FILE}" |
| 405 |
fi |
| 406 |
|
| 407 |
# Ensure COMPOSE_CMD is set |
| 408 |
if [[ -z "$COMPOSE_CMD" ]]; then |
| 409 |
check_requirements |
| 410 |
fi |
| 411 |
|
| 412 |
# Change to the script directory |
| 413 |
cd "$SCRIPT_DIR" |
| 414 |
|
| 415 |
# Export UID and GID for docker-compose to use |
| 416 |
# This ensures the container runs as the current host user |
| 417 |
# Use _UID/_GID to avoid conflict with shell built-in readonly UID variable |
| 418 |
local _user_uid _user_gid |
| 419 |
_user_uid="${MATRIX_UID:-$(id -u)}" |
| 420 |
_user_gid="${MATRIX_GID:-$(id -g)}" |
| 421 |
export MATRIX_UID="$_user_uid" |
| 422 |
export MATRIX_GID="$_user_gid" |
| 423 |
|
| 424 |
debug "Using MATRIX_UID=${MATRIX_UID} MATRIX_GID=${MATRIX_GID} for container" |
| 425 |
|
| 426 |
# Stop any existing container |
| 427 |
if $COMPOSE_CMD ps --status running 2>/dev/null | grep -q synapse; then |
| 428 |
info "Stopping existing Synapse container..." |
| 429 |
$COMPOSE_CMD down || true |
| 430 |
fi |
| 431 |
|
| 432 |
# Start the server |
| 433 |
$COMPOSE_CMD up -d |
| 434 |
|
| 435 |
if [[ $? -eq 0 ]]; then |
| 436 |
info "Matrix Synapse server started successfully" |
| 437 |
info "Server is available at http://localhost:8008" |
| 438 |
info "Use 'docker logs synapse -f' to watch the logs" |
| 439 |
else |
| 440 |
error "Failed to start Matrix Synapse server" |
| 441 |
fi |
| 442 |
} |
| 443 |
|
| 444 |
# Create the admin test user |
| 445 |
create_user() { |
| 446 |
info "Creating admin test user..." |
| 447 |
|
| 448 |
# Load environment file before reading variables to ensure |
| 449 |
# values from .env take precedence over hardcoded defaults. |
| 450 |
load_env_file |
| 451 |
|
| 452 |
# Default credentials |
| 453 |
local user="${SHOUTRRR_MATRIX_USER:-${DEFAULT_MATRIX_USER}}" |
| 454 |
local password="${SHOUTRRR_MATRIX_PASSWORD:-${DEFAULT_MATRIX_PASSWORD}}" |
| 455 |
local server_url="${SHOUTRRR_MATRIX_HOST:-${DEFAULT_MATRIX_HOST}}" |
| 456 |
|
| 457 |
# Get base URL for server readiness check |
| 458 |
local base_url |
| 459 |
base_url=$(get_base_url "$server_url") |
| 460 |
|
| 461 |
# Wait for server to be ready |
| 462 |
wait_for_server "$base_url" 60 |
| 463 |
|
| 464 |
# Register the user using docker exec |
| 465 |
# Wrap in if to prevent set -e from exiting before fallback check |
| 466 |
info "Registering user '${user}'..." |
| 467 |
|
| 468 |
if docker exec synapse \ |
| 469 |
register_new_matrix_user \ |
| 470 |
-u "$user" \ |
| 471 |
-p "$password" \ |
| 472 |
-a \ |
| 473 |
-c /data/homeserver.yaml \ |
| 474 |
"${base_url}"; then |
| 475 |
info "User '${user}' created successfully" |
| 476 |
else |
| 477 |
# Check if user already exists |
| 478 |
if docker exec synapse \ |
| 479 |
register_new_matrix_user \ |
| 480 |
-u "$user" \ |
| 481 |
-p "$password" \ |
| 482 |
-a \ |
| 483 |
-c /data/homeserver.yaml \ |
| 484 |
"${base_url}" 2>&1 | grep -q "already exists"; then |
| 485 |
warn "User '${user}' already exists, continuing..." |
| 486 |
else |
| 487 |
error "Failed to create user '${user}'" |
| 488 |
fi |
| 489 |
fi |
| 490 |
|
| 491 |
# Persist resolved settings to ENV_FILE so downstream consumers |
| 492 |
# (e.g., testing/e2e/matrix/matrix_suite_test.go) see the same values. |
| 493 |
info "Persisting resolved settings to ${ENV_FILE}..." |
| 494 |
{ |
| 495 |
echo "SHOUTRRR_MATRIX_USER=${user}" |
| 496 |
echo "SHOUTRRR_MATRIX_PASSWORD=${password}" |
| 497 |
echo "SHOUTRRR_MATRIX_HOST=${server_url}" |
| 498 |
} >> "$ENV_FILE" |
| 499 |
|
| 500 |
# Always persist the TLS setting - default to true (HTTP) for local development |
| 501 |
# since Synapse runs on HTTP by default |
| 502 |
local disable_tls="${SHOUTRRR_MATRIX_DISABLE_TLS:-true}" |
| 503 |
echo "SHOUTRRR_MATRIX_DISABLE_TLS=${disable_tls}" >> "$ENV_FILE" |
| 504 |
|
| 505 |
info "Resolved settings persisted to ${ENV_FILE}" |
| 506 |
} |
| 507 |
|
| 508 |
# Get the base URL for Matrix API |
| 509 |
# Defaults to http unless TLS is explicitly enabled |
| 510 |
get_base_url() { |
| 511 |
local host="${1:-${DEFAULT_MATRIX_HOST}}" |
| 512 |
local disable_tls="${SHOUTRRR_MATRIX_DISABLE_TLS:-}" |
| 513 |
|
| 514 |
if [[ "$disable_tls" == "false" ]]; then |
| 515 |
echo "https://${host}" |
| 516 |
else |
| 517 |
echo "http://${host}" |
| 518 |
fi |
| 519 |
} |
| 520 |
|
| 521 |
# Login to Matrix and get access token |
| 522 |
login() { |
| 523 |
local base_url="$1" |
| 524 |
local user="$2" |
| 525 |
local password="$3" |
| 526 |
|
| 527 |
debug "Logging in as ${user} to ${base_url}..." |
| 528 |
|
| 529 |
local response |
| 530 |
response=$( |
| 531 |
curl -s -X POST "${base_url}/_matrix/client/v3/login" \ |
| 532 |
-H "Content-Type: application/json" \ |
| 533 |
--data "$(jq -n --arg user "$user" --arg password "$password" \ |
| 534 |
'{type: "m.login.password", identifier: {type: "m.id.user", user: $user}, password: $password}')" \ |
| 535 |
2>&1 |
| 536 |
) || error "Failed to connect to Matrix server at ${base_url}" |
| 537 |
|
| 538 |
# Check for errors in response |
| 539 |
local error_msg |
| 540 |
error_msg=$(echo "$response" | jq -r '.errcode // empty' 2>/dev/null || true) |
| 541 |
|
| 542 |
if [[ -n "$error_msg" && "$error_msg" != "null" ]]; then |
| 543 |
local error_desc |
| 544 |
error_desc=$(echo "$response" | jq -r '.error // "Unknown error"' 2>/dev/null || true) |
| 545 |
error "Login failed: ${error_msg} - ${error_desc}" |
| 546 |
fi |
| 547 |
|
| 548 |
local access_token |
| 549 |
access_token=$(echo "$response" | jq -r '.access_token' 2>/dev/null) || error "Failed to parse login response" |
| 550 |
|
| 551 |
if [[ -z "$access_token" || "$access_token" == "null" ]]; then |
| 552 |
error "No access token received from login" |
| 553 |
fi |
| 554 |
|
| 555 |
debug "Login successful, token length: ${#access_token}" |
| 556 |
echo "$access_token" |
| 557 |
} |
| 558 |
|
| 559 |
# Create the test room with alias #test:localhost |
| 560 |
create_room() { |
| 561 |
info "Creating test room..." |
| 562 |
|
| 563 |
# Load environment variables |
| 564 |
load_env_file |
| 565 |
|
| 566 |
# Get configuration |
| 567 |
local host="${SHOUTRRR_MATRIX_HOST:-${DEFAULT_MATRIX_HOST}}" |
| 568 |
local user="${SHOUTRRR_MATRIX_USER:-${DEFAULT_MATRIX_USER}}" |
| 569 |
local password="${SHOUTRRR_MATRIX_PASSWORD:-${DEFAULT_MATRIX_PASSWORD}}" |
| 570 |
local room_alias="${SHOUTRRR_MATRIX_ROOM:-${DEFAULT_MATRIX_ROOM}}" |
| 571 |
|
| 572 |
# Validate required variables |
| 573 |
if [[ -z "$host" ]]; then |
| 574 |
error "SHOUTRRR_MATRIX_HOST is not set" |
| 575 |
fi |
| 576 |
if [[ -z "$user" ]]; then |
| 577 |
error "SHOUTRRR_MATRIX_USER is not set" |
| 578 |
fi |
| 579 |
if [[ -z "$password" ]]; then |
| 580 |
error "SHOUTRRR_MATRIX_PASSWORD is not set" |
| 581 |
fi |
| 582 |
|
| 583 |
info "Using host: ${host}" |
| 584 |
info "Using user: ${user}" |
| 585 |
info "Using room: ${room_alias}" |
| 586 |
|
| 587 |
# Get base URL |
| 588 |
local base_url |
| 589 |
base_url=$(get_base_url "$host") |
| 590 |
|
| 591 |
# Wait for server to be ready |
| 592 |
wait_for_server "$base_url" 30 |
| 593 |
|
| 594 |
# Login to get access token |
| 595 |
local access_token |
| 596 |
access_token=$(login "$base_url" "$user" "$password") |
| 597 |
|
| 598 |
# Extract local part of the alias (e.g., "test" from "#test:localhost") |
| 599 |
local local_alias |
| 600 |
local_alias=$(echo "$room_alias" | sed 's/^#//' | cut -d':' -f1) |
| 601 |
|
| 602 |
debug "Creating room with alias: ${local_alias}" |
| 603 |
|
| 604 |
# Create the room |
| 605 |
local response |
| 606 |
response=$( |
| 607 |
curl -s -X POST "${base_url}/_matrix/client/v3/createRoom" \ |
| 608 |
-H "Content-Type: application/json" \ |
| 609 |
-H "Authorization: Bearer ${access_token}" \ |
| 610 |
-d "{ |
| 611 |
\"room_alias_name\": \"${local_alias}\", |
| 612 |
\"name\": \"Test Room\", |
| 613 |
\"topic\": \"Test room for shoutrrr e2e tests\", |
| 614 |
\"visibility\": \"public\" |
| 615 |
}" 2>&1 |
| 616 |
) || error "Failed to create room" |
| 617 |
|
| 618 |
# Check for errors in response |
| 619 |
local error_msg |
| 620 |
error_msg=$(echo "$response" | jq -r '.errcode // empty' 2>/dev/null || true) |
| 621 |
|
| 622 |
if [[ -n "$error_msg" && "$error_msg" != "null" ]]; then |
| 623 |
local error_desc |
| 624 |
error_desc=$(echo "$response" | jq -r '.error // "Unknown error"' 2>/dev/null || true) |
| 625 |
|
| 626 |
# Room might already exist - that's okay |
| 627 |
if [[ "$error_msg" == "ROOM_ALIAS_EXISTS" ]]; then |
| 628 |
warn "Room alias already exists: ${room_alias}" |
| 629 |
return 0 |
| 630 |
fi |
| 631 |
|
| 632 |
error "Failed to create room: ${error_msg} - ${error_desc}" |
| 633 |
fi |
| 634 |
|
| 635 |
local room_id |
| 636 |
room_id=$(echo "$response" | jq -r '.room_id' 2>/dev/null) || error "Failed to parse room creation response" |
| 637 |
|
| 638 |
if [[ -z "$room_id" || "$room_id" == "null" ]]; then |
| 639 |
error "No room_id received from room creation" |
| 640 |
fi |
| 641 |
|
| 642 |
info "Successfully created room: ${room_alias} (${room_id})" |
| 643 |
|
| 644 |
# Persist resolved room to ENV_FILE so downstream consumers see the same value. |
| 645 |
echo "SHOUTRRR_MATRIX_ROOM=${room_alias}" >> "$ENV_FILE" |
| 646 |
info "Resolved room persisted to ${ENV_FILE}" |
| 647 |
} |
| 648 |
|
| 649 |
# Run all setup steps in order |
| 650 |
setup_all() { |
| 651 |
info "Starting complete Matrix E2E test environment setup..." |
| 652 |
echo "" |
| 653 |
|
| 654 |
# Check requirements |
| 655 |
check_requirements |
| 656 |
|
| 657 |
# Step 1: Generate config |
| 658 |
echo "" |
| 659 |
info "=== Step 1/5: Generating configuration ===" |
| 660 |
generate_config |
| 661 |
|
| 662 |
# Step 2: Start server |
| 663 |
echo "" |
| 664 |
info "=== Step 2/5: Starting server ===" |
| 665 |
start_server |
| 666 |
|
| 667 |
# Step 3: Create user |
| 668 |
echo "" |
| 669 |
info "=== Step 3/5: Creating test user ===" |
| 670 |
create_user |
| 671 |
|
| 672 |
# Step 4: Disable rate limiting (requires server and user to be ready) |
| 673 |
echo "" |
| 674 |
info "=== Step 4/5: Disabling rate limiting ===" |
| 675 |
disable_rate_limiting |
| 676 |
|
| 677 |
# Step 5: Create room |
| 678 |
echo "" |
| 679 |
info "=== Step 5/5: Creating test room ===" |
| 680 |
create_room |
| 681 |
|
| 682 |
echo "" |
| 683 |
info "==========================================" |
| 684 |
info "Matrix E2E test environment setup complete!" |
| 685 |
info "==========================================" |
| 686 |
info "" |
| 687 |
info "You can now run the E2E tests with:" |
| 688 |
info " go test -v ./testing/e2e/matrix/..." |
| 689 |
info "" |
| 690 |
info "To stop the server, run:" |
| 691 |
info " docker compose -f ${COMPOSE_FILE} down" |
| 692 |
} |
| 693 |
|
| 694 |
# ============================================================================= |
| 695 |
# Main Entry Point |
| 696 |
# ============================================================================= |
| 697 |
|
| 698 |
main() { |
| 699 |
# Parse command-line arguments |
| 700 |
local command="setup-all" |
| 701 |
|
| 702 |
while [[ $# -gt 0 ]]; do |
| 703 |
case "$1" in |
| 704 |
--help|-h) |
| 705 |
usage |
| 706 |
exit 0 |
| 707 |
;; |
| 708 |
--verbose|-v) |
| 709 |
VERBOSE=true |
| 710 |
shift |
| 711 |
;; |
| 712 |
generate-config|start-server|create-user|create-room|setup-all|disable_rate_limiting) |
| 713 |
command="$1" |
| 714 |
shift |
| 715 |
;; |
| 716 |
*) |
| 717 |
error "Unknown option or command: $1" |
| 718 |
;; |
| 719 |
esac |
| 720 |
done |
| 721 |
|
| 722 |
# Execute the requested command |
| 723 |
case "$command" in |
| 724 |
generate-config) |
| 725 |
generate_config |
| 726 |
;; |
| 727 |
start-server) |
| 728 |
start_server |
| 729 |
;; |
| 730 |
create-user) |
| 731 |
create_user |
| 732 |
;; |
| 733 |
create-room) |
| 734 |
create_room |
| 735 |
;; |
| 736 |
setup-all) |
| 737 |
setup_all |
| 738 |
;; |
| 739 |
disable_rate_limiting) |
| 740 |
disable_rate_limiting |
| 741 |
;; |
| 742 |
esac |
| 743 |
} |
| 744 |
|
| 745 |
# Run main function with all arguments |
| 746 |
main "$@" |