Skip to content

Commit 5c98368

Browse files
committed
feat(ci): environments and bruno tests
1 parent c8a6f0f commit 5c98368

File tree

2 files changed

+110
-40
lines changed

2 files changed

+110
-40
lines changed

scripts/run-tests.sh

Lines changed: 106 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@ setup_wordpress_env() {
9999

100100
print_header "Setting up WordPress Environment"
101101

102+
# Install npm dependencies in host environment (needed for build and wp-env)
103+
print_info "Installing npm dependencies..."
104+
if [[ ! -d "node_modules" ]] || [[ "package.json" -nt "node_modules" ]]; then
105+
# Remove lock file and node_modules to fix potential issues
106+
rm -rf package-lock.json node_modules
107+
npm install --legacy-peer-deps
108+
else
109+
print_success "npm dependencies are up to date"
110+
fi
111+
112+
print_info "Building the project..."
113+
npm run build
114+
115+
# Install Bruno CLI if needed for API tests
116+
if [[ "$RUN_API" == true ]]; then
117+
print_info "Checking Bruno CLI installation..."
118+
if ! command -v bru &> /dev/null; then
119+
print_info "Installing Bruno CLI..."
120+
npm install -g @usebruno/cli
121+
else
122+
print_success "Bruno CLI is already installed"
123+
fi
124+
fi
125+
102126
print_info "Starting WordPress environment with wp-env..."
103127
if ! npx @wordpress/env start; then
104128
print_error "Failed to start WordPress environment"
@@ -120,10 +144,21 @@ setup_wordpress_env() {
120144
sleep 5
121145
done
122146

147+
# List all plugins before activation
148+
print_info "Listing all available plugins:"
149+
npx @wordpress/env run cli wp plugin list --format=table || true
150+
123151
# Install and activate the plugin
124-
print_info "Installing and activating jwt-authentication-for-wp-rest-api plugin..."
125-
if ! npx @wordpress/env run cli wp plugin activate jwt-authentication-for-wp-rest-api 2>/dev/null; then
126-
print_error "Failed to activate jwt-authentication-for-wp-rest-api plugin"
152+
print_info "Installing and activating wp-api-jwt-auth plugin..."
153+
if ! npx @wordpress/env run cli wp plugin activate wp-api-jwt-auth 2>/dev/null; then
154+
print_error "Failed to activate wp-api-jwt-auth plugin"
155+
exit 1
156+
fi
157+
158+
# Install composer dependencies inside the container
159+
print_info "Installing composer dependencies inside wp-env container..."
160+
if ! npx @wordpress/env run cli bash -c "cd wp-content/plugins/wp-api-jwt-auth && composer install --no-interaction --prefer-dist"; then
161+
print_error "Failed to install composer dependencies"
127162
exit 1
128163
fi
129164

@@ -181,6 +216,41 @@ EOF" || true
181216
print_info "Checking REST API availability:"
182217
npx @wordpress/env run cli wp eval "echo 'REST API enabled: ' . (rest_get_server() ? 'Yes' : 'No') . PHP_EOL;" || true
183218

219+
# List all registered REST routes to debug
220+
print_info "Listing JWT Auth REST routes:"
221+
npx @wordpress/env run cli wp eval "
222+
\$server = rest_get_server();
223+
\$routes = \$server->get_routes();
224+
foreach (\$routes as \$route => \$endpoints) {
225+
if (strpos(\$route, 'jwt-auth') !== false) {
226+
echo \$route . PHP_EOL;
227+
}
228+
}
229+
" || true
230+
231+
# Create test user if it doesn't exist
232+
print_info "Ensuring test user exists..."
233+
npx @wordpress/env run cli wp user create admin admin@example.com --user_pass=password --role=administrator 2>/dev/null || true
234+
235+
# Give the server a moment to fully initialize
236+
print_info "Waiting for server to stabilize..."
237+
sleep 5
238+
239+
# Final check - try to authenticate with the API
240+
print_info "Testing authentication endpoint with credentials..."
241+
response=$(curl -s -X POST http://localhost:8888/wp-json/jwt-auth/v1/token \
242+
-H "Content-Type: application/json" \
243+
-d '{"username":"admin","password":"password"}' \
244+
-w "\nHTTP_CODE:%{http_code}")
245+
246+
http_code=$(echo "$response" | grep "HTTP_CODE:" | cut -d: -f2)
247+
if [ "$http_code" = "200" ]; then
248+
print_success "Authentication endpoint is working correctly"
249+
else
250+
print_warning "Authentication endpoint returned status: $http_code"
251+
echo "Response: $(echo "$response" | grep -v "HTTP_CODE:")"
252+
fi
253+
184254
print_success "Plugin setup completed"
185255
}
186256

@@ -192,15 +262,9 @@ run_php_tests() {
192262

193263
print_header "Running PHP Unit Tests"
194264

195-
# Check if composer dependencies are installed
196-
if [[ ! -d "includes/vendor" ]]; then
197-
print_info "Installing PHP dependencies..."
198-
composer install --no-interaction --prefer-dist
199-
fi
200-
201-
# Run PHPUnit tests using wp-env's tests-cli container
265+
# Run PHPUnit tests using wp-env's cli container
202266
print_info "Running PHPUnit tests in wp-env container..."
203-
if npx @wordpress/env run tests-cli --env-cwd="wp-content/plugins/$(basename "$(pwd)")" ./includes/vendor/bin/phpunit; then
267+
if npx @wordpress/env run cli bash -c "cd wp-content/plugins/wp-api-jwt-auth && includes/vendor/bin/phpunit --testdox"; then
204268
print_success "PHP Unit Tests passed"
205269
PHP_TESTS_PASSED=true
206270
else
@@ -217,12 +281,6 @@ run_frontend_tests() {
217281

218282
print_header "Running Frontend Tests"
219283

220-
# Check if node_modules exists
221-
if [[ ! -d "node_modules" ]]; then
222-
print_info "Installing Node.js dependencies..."
223-
npm install
224-
fi
225-
226284
# Run frontend tests
227285
if npm run test; then
228286
print_success "Frontend Tests passed"
@@ -239,28 +297,42 @@ run_api_tests() {
239297
return
240298
fi
241299

242-
print_header "Running API Tests with Bruno"
300+
print_header "Running API Tests (Bruno)"
301+
302+
# Add a delay after PHP tests to ensure server is ready
303+
print_info "Waiting for server to be ready after PHP tests..."
304+
sleep 5
243305

244-
# Check if Bruno CLI is available
245-
if ! command -v bru &> /dev/null; then
246-
print_info "Installing Bruno CLI..."
247-
npm install -g @usebruno/cli
306+
print_info "Executing REST API tests..."
307+
308+
# Debug: Verify wp-env is still running
309+
print_info "Verifying WordPress environment is still running..."
310+
if ! curl -f -s http://localhost:8888 > /dev/null; then
311+
print_error "WordPress environment is not accessible!"
312+
print_info "Restarting WordPress environment..."
313+
npx @wordpress/env start
314+
sleep 10
248315
fi
249316

250-
# Run Bruno API tests
251-
cd tests/bruno/wp-api-jwt-auth
252-
if bru run . --env local --output results.json; then
253-
print_success "API Tests passed"
317+
# Test the endpoint one more time before Bruno
318+
print_info "Testing endpoint before Bruno:"
319+
response=$(curl -s -X POST http://localhost:8888/wp-json/jwt-auth/v1/token \
320+
-H "Content-Type: application/json" \
321+
-d '{"username":"admin","password":"password"}' \
322+
-w "\nHTTP_CODE:%{http_code}")
323+
324+
http_code=$(echo "$response" | grep "HTTP_CODE:" | cut -d: -f2)
325+
echo "Response status: $http_code"
326+
echo "$response" | grep -v "HTTP_CODE:" | head -n 5
327+
328+
# Run Bruno tests
329+
print_info "Running Bruno tests..."
330+
if (cd tests/bruno/wp-api-jwt-auth && bru run --env local); then
254331
API_TESTS_PASSED=true
255-
cd - > /dev/null
332+
print_success "API tests passed"
256333
else
257-
print_error "API Tests failed"
258-
API_TESTS_PASSED=false
259-
cd - > /dev/null
334+
print_error "API tests failed"
260335
fi
261-
262-
# Clean up results file (from the Bruno collection directory)
263-
rm -f tests/bruno/wp-api-jwt-auth/results.json
264336
}
265337

266338
# Print final results
@@ -321,9 +393,8 @@ main() {
321393
print_header "WP API JWT Auth - Test Runner"
322394

323395
setup_wordpress_env
324-
run_php_tests
325-
# run_frontend_tests
326396
run_api_tests
397+
run_php_tests
327398
print_results
328399
}
329400

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
vars {
2-
baseURL: http://localhost:8888/wp-json
3-
username: admin
4-
password: password
5-
token:
6-
}
2+
baseURL: http://jwt.test/wp-json
3+
username: tmeister
4+
password: poipoipoi
5+
}

0 commit comments

Comments
 (0)