-
Notifications
You must be signed in to change notification settings - Fork 19
186 lines (155 loc) · 6.39 KB
/
Copy pathe2e-api.yml
File metadata and controls
186 lines (155 loc) · 6.39 KB
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
name: E2E API Tests
on:
pull_request:
branches:
- main
- next
paths:
- "includes/API/**"
- "tests/e2e/api/**"
- ".github/workflows/e2e-api.yml"
workflow_dispatch:
permissions:
contents: read
checks: write
pull-requests: write
# Cancel in-progress runs for the same branch
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
e2e-api:
name: E2E API Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Setup PHP
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # v2
with:
php-version: "8.1"
tools: composer
- name: Setup pnpm
uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v4
- name: Setup Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: '22'
- name: Get Composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache Composer dependencies
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies
run: |
pnpm install --lockfile-only --ignore-scripts
pnpm install --frozen-lockfile
composer prefix-dependencies
composer install --prefer-dist --no-progress
- name: Start WordPress Environment
run: pnpm run wp-env start
- name: Wait for WordPress to be ready
run: |
ready=0
for i in {1..60}; do
# Check if Docker containers are running
if docker ps | grep -q wordpress; then
# Try to access WP CLI first (more reliable)
if pnpm run wp-env run cli -- wp core is-installed 2>/dev/null; then
echo "WordPress is ready"
ready=1
break
fi
fi
echo "Waiting for WordPress... ($i/60)"
sleep 2
done
if [ "$ready" -ne 1 ]; then
echo "WordPress did not become ready in time"
docker ps
pnpm run wp-env logs || true
exit 1
fi
- name: Configure WordPress for API tests
run: |
# Ensure plugins are active
pnpm run wp-env run cli -- wp plugin activate woocommerce woocommerce-pos
# Set permalinks structure
pnpm run wp-env run cli -- wp rewrite structure '/%postname%/' --hard
pnpm run wp-env run cli -- wp rewrite flush --hard
# Add Authorization header passthrough to .htaccess AFTER rewrite flush
# Apache strips the Authorization header by default; SetEnvIf passes it to PHP
# We use a PHP script to avoid shell escaping issues with $1 backreference
pnpm run wp-env run cli -- wp eval-file wp-content/plugins/woocommerce-pos/tests/e2e/api/fix-htaccess.php
# Verify REST API is accessible with X-WCPOS header
curl -sf -H "X-WCPOS: 1" "http://localhost:8888/?rest_route=/" | jq '.namespaces | map(select(. == "wcpos/v1"))' || echo "Checking alternative..."
curl -sf -H "X-WCPOS: 1" http://localhost:8888/wp-json/wcpos/v1/ | head -c 200 || true
- name: Create test product
run: |
pnpm run wp-env run cli -- wp wc product create \
--name="Test Product" \
--regular_price=10 \
--sku="TEST-001" \
--user=admin
- name: Generate JWT token for API tests
run: |
# Generate JWT tokens using PHP file and write to a shared file
TOKEN_FILE="tests/e2e/api/jwt-tokens.json"
pnpm run wp-env run cli -- bash -c "wp eval-file wp-content/plugins/woocommerce-pos/tests/e2e/api/generate-jwt.php > wp-content/plugins/woocommerce-pos/${TOKEN_FILE}"
if [ ! -f "$TOKEN_FILE" ]; then
echo "Token file not created: $TOKEN_FILE"
exit 1
fi
if jq -e '.error' "$TOKEN_FILE" >/dev/null; then
echo "Token generation error:"
cat "$TOKEN_FILE"
exit 1
fi
JWT_TOKEN=$(jq -r '.access_token // empty' "$TOKEN_FILE")
REFRESH_TOKEN=$(jq -r '.refresh_token // empty' "$TOKEN_FILE")
if [ -z "$JWT_TOKEN" ] || [ -z "$REFRESH_TOKEN" ]; then
echo "Failed to parse tokens from $TOKEN_FILE"
cat "$TOKEN_FILE"
exit 1
fi
echo "JWT_TOKEN=$JWT_TOKEN" >> $GITHUB_ENV
echo "REFRESH_TOKEN=$REFRESH_TOKEN" >> $GITHUB_ENV
rm -f "$TOKEN_FILE"
- name: Verify API setup
run: |
echo "=== .htaccess content ==="
pnpm run wp-env run wordpress -- cat /var/www/html/.htaccess
echo ""
echo "=== Verify pretty permalinks ==="
curl -s -o /dev/null -w "HTTP %{http_code}" -H "X-WCPOS: 1" http://localhost:8888/wp-json/wcpos/v1/auth/test
echo ""
echo ""
echo "=== Verify query string auth ==="
curl -s -o /dev/null -w "HTTP %{http_code}" -H "Authorization: Bearer $JWT_TOKEN" -H "X-WCPOS: 1" \
"http://localhost:8888/?rest_route=/wcpos/v1/products"
echo ""
- name: Run Newman API tests
run: |
pnpm exec newman run tests/e2e/api/wcpos-api.postman_collection.json \
--environment tests/e2e/api/environment.json \
--env-var "jwt_token=$JWT_TOKEN" \
--env-var "refresh_token=$REFRESH_TOKEN" \
--reporters cli,junit \
--reporter-junit-export newman-results.xml
- name: Upload test results
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
if: always()
with:
name: newman-results
path: newman-results.xml
retention-days: 7
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@d0a4676d0e0b938bc201470d88276b7c74c712b3 # v2
if: always()
with:
files: newman-results.xml
check_name: E2E API Test Results