Skip to content

Commit 304ee2a

Browse files
authored
test: integration tests for ic_env cookie (#4394)
Follow-up of #4387 and #4393.
1 parent 12b1492 commit 304ee2a

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

e2e/tests-dfx/assetscanister.bash

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,3 +2108,139 @@ EOF
21082108
assert_command dfx canister call e2e_project_frontend list_permitted '(record { permission = variant { ManagePermissions }; })'
21092109
assert_match "$(dfx identity get-principal)"
21102110
}
2111+
2112+
@test "ic_env cookie is set for html files" {
2113+
install_asset assetscanister
2114+
dfx_start
2115+
2116+
touch src/e2e_project_frontend/assets/index.html
2117+
echo "<html><body>Test</body></html>" > src/e2e_project_frontend/assets/index.html
2118+
2119+
dfx deploy
2120+
2121+
ID=$(dfx canister id e2e_project_frontend)
2122+
PORT=$(get_webserver_port)
2123+
2124+
IC_ENV_COOKIE_REGEX="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+; SameSite=Lax"
2125+
2126+
# Request HTML file and verify ic_env cookie is set
2127+
assert_command curl -v "http://$ID.localhost:$PORT/index.html"
2128+
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX"
2129+
}
2130+
2131+
@test "ic_env cookie contains PUBLIC_ environment variables" {
2132+
install_asset assetscanister
2133+
dfx_start
2134+
2135+
touch src/e2e_project_frontend/assets/index.html
2136+
echo "<html><body>Test</body></html>" > src/e2e_project_frontend/assets/index.html
2137+
2138+
dfx canister create --all
2139+
dfx build --all
2140+
dfx canister install e2e_project_frontend
2141+
2142+
ID=$(dfx canister id e2e_project_frontend)
2143+
2144+
set_canister_environment_variables "$ID" PUBLIC_TEST1=value1 PUBLIC_TEST2=value2
2145+
2146+
dfx deploy
2147+
2148+
PORT=$(get_webserver_port)
2149+
2150+
IC_ENV_COOKIE_REGEX="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+%26PUBLIC%5FTEST1%3Dvalue1%26PUBLIC%5FTEST2%3Dvalue2; SameSite=Lax"
2151+
2152+
# Request HTML file and verify cookie contains PUBLIC_ variables
2153+
assert_command curl -v "http://$ID.localhost:$PORT/index.html"
2154+
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX"
2155+
}
2156+
2157+
@test "ic_env cookie is not set for non-html files" {
2158+
install_asset assetscanister
2159+
dfx_start
2160+
2161+
touch src/e2e_project_frontend/assets/test.txt
2162+
echo "plain text file" > src/e2e_project_frontend/assets/test.txt
2163+
touch src/e2e_project_frontend/assets/script.js
2164+
echo "console.log('test');" > src/e2e_project_frontend/assets/script.js
2165+
2166+
dfx deploy
2167+
2168+
ID=$(dfx canister id e2e_project_frontend)
2169+
PORT=$(get_webserver_port)
2170+
2171+
# Request text file and verify no ic_env cookie is set
2172+
assert_command curl -v "http://$ID.localhost:$PORT/test.txt"
2173+
assert_not_match "set-cookie: ic_env="
2174+
2175+
# Request JS file and verify no ic_env cookie is set
2176+
assert_command curl -v "http://$ID.localhost:$PORT/script.js"
2177+
assert_not_match "set-cookie: ic_env="
2178+
}
2179+
2180+
@test "ic_env cookie is set only for PUBLIC_ prefixed environment variables" {
2181+
install_asset assetscanister
2182+
dfx_start
2183+
2184+
touch src/e2e_project_frontend/assets/app.html
2185+
echo "<html><body>App</body></html>" > src/e2e_project_frontend/assets/app.html
2186+
2187+
dfx canister create --all
2188+
dfx build --all
2189+
dfx canister install e2e_project_frontend
2190+
2191+
ID=$(dfx canister id e2e_project_frontend)
2192+
2193+
set_canister_environment_variables "$ID" PUBLIC_TEST1=value1 TEST2=value2
2194+
2195+
dfx deploy
2196+
2197+
PORT=$(get_webserver_port)
2198+
2199+
IC_ENV_COOKIE_REGEX_1="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+%26PUBLIC%5FTEST1%3Dvalue1; SameSite=Lax"
2200+
2201+
assert_command curl -v "http://$ID.localhost:$PORT/app.html"
2202+
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX_1"
2203+
2204+
# Redeploy with no PUBLIC_ prefixed (case sensitive!) environment variables
2205+
set_canister_environment_variables "$ID" public_TEST1=value1 TEST2=value2
2206+
dfx deploy
2207+
2208+
IC_ENV_COOKIE_REGEX_2="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+; SameSite=Lax"
2209+
2210+
assert_command curl -v "http://$ID.localhost:$PORT/app.html"
2211+
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX_2"
2212+
}
2213+
2214+
@test "ic_env cookie updates on redeploy with new environment variables" {
2215+
install_asset assetscanister
2216+
dfx_start
2217+
2218+
touch src/e2e_project_frontend/assets/app.html
2219+
echo "<html><body>App</body></html>" > src/e2e_project_frontend/assets/app.html
2220+
2221+
dfx canister create --all
2222+
dfx build --all
2223+
dfx canister install e2e_project_frontend
2224+
2225+
ID=$(dfx canister id e2e_project_frontend)
2226+
2227+
set_canister_environment_variables "$ID" PUBLIC_TEST1=value1
2228+
2229+
dfx deploy
2230+
2231+
PORT=$(get_webserver_port)
2232+
2233+
IC_ENV_COOKIE_REGEX_1="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+%26PUBLIC%5FTEST1%3Dvalue1; SameSite=Lax"
2234+
2235+
assert_command curl -v "http://$ID.localhost:$PORT/app.html"
2236+
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX_1"
2237+
2238+
# Redeploy with additional PUBLIC_ variable
2239+
set_canister_environment_variables "$ID" PUBLIC_TEST1=value1 PUBLIC_TEST2=value2
2240+
dfx deploy
2241+
2242+
IC_ENV_COOKIE_REGEX_2="ic_env=ic%5Froot%5Fkey%3D[0-9a-fA-F]+%26PUBLIC%5FTEST1%3Dvalue1%26PUBLIC%5FTEST2%3Dvalue2; SameSite=Lax"
2243+
2244+
assert_command curl -v "http://$ID.localhost:$PORT/app.html"
2245+
assert_match "set-cookie: $IC_ENV_COOKIE_REGEX_2"
2246+
}

e2e/utils/_.bash

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,24 @@ stop_and_delete() {
310310
assert_command dfx canister delete -y --no-withdrawal "$1"
311311
echo "Canister $1 deleted"
312312
}
313+
314+
set_canister_environment_variables() {
315+
local canister_id=$1
316+
shift
317+
318+
# Build the Candid vec of records from the remaining arguments
319+
# Each argument should be in format "NAME=VALUE"
320+
local env_records=""
321+
for env_var in "$@"; do
322+
local name="${env_var%%=*}"
323+
local value="${env_var#*=}"
324+
if [ -n "$env_records" ]; then
325+
env_records="$env_records; "
326+
fi
327+
env_records="${env_records}record { name = \"$name\"; value = \"$value\" }"
328+
done
329+
330+
management_canister_id=aaaaa-aa
331+
332+
dfx canister call $management_canister_id update_settings "(record { canister_id = principal \"$canister_id\"; settings = record { environment_variables = opt vec { $env_records } } })"
333+
}

0 commit comments

Comments
 (0)